Skip to main content
 首页 » 编程设计

python之PyGame 错误 : clock. tick(40) AttributeError: 'function' object has no attribute 'tick'

2024年10月24日14lyj

我正在制作玩具熊的五夜后宫的“粉丝游戏”。在我开始收到此错误之前,我的代码工作正常:

File "C:\Users\Admin\Desktop\Python\Five Nights\Five Nights.py", line 116, in main 
clock.tick(40) 
AttributeError: 'function' object has no attribute 'tick' 

我将代码与其他工作的 pygame 游戏进行了比较,时钟代码完全相同,但我只在这个游戏中遇到这个错误。

我的代码如下:

import pygame, random 
pygame.init() 
 
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN, 32) 
pygame.display.toggle_fullscreen() 
pygame.display.set_caption("Five Nights") 
clock = pygame.time.Clock() 
 
office_pic = pygame.image.load('images/office.png') 
stage = pygame.image.load('images/stage.jpg') 
building_map = pygame.image.load('images/map.png') 
building_map.convert_alpha() 
 
breathing = pygame.mixer.Sound('sounds/breathing.wav') 
 
screen_width = 1280 
screen_height = 800 
 
x = 0 
x_move = 0 
 
v = 0 
 
camera = stage 
camera_up = False 
 
click = None 
 
fps_tick = 0 
seconds = 0 
hour = 10 
 
 
def clock(): 
    global fps_tick, seconds, hou 
    fps_tick += 1 
 
    if fps_tick == 40: 
        seconds += 1 
        fps_tick = 0 
 
    if seconds == 30: 
        hour += 1 
 
 
 
 
def cameras(mouse_x, mouse_y): 
    global click, camera 
 
    screen.blit(camera, (0,0)) 
 
    screen.blit(building_map, (screen_width-650, screen_height-426)) 
 
 
 
 
def main(): 
 
    global x, x_move, camera_up, v, click 
 
    while True:        
 
        for event in pygame.event.get(): 
            if event.type == pygame.KEYDOWN: 
               if event.key == pygame.K_ESCAPE: 
                   pygame.quit() 
                   quit() 
 
        mouse_xy = pygame.mouse.get_pos() 
        mouse_x = mouse_xy[0] 
        mouse_y = mouse_xy[1] 
        click = pygame.mouse.get_pressed() 
 
 
        if mouse_x >= (screen_width/2) + 200 and mouse_x <= (screen_width/2) + 400: 
            x_move = 5 
        elif mouse_x <= (screen_width/2) - 200 and mouse_x >= (screen_width/2) - 400: 
            x_move = -5 
        elif mouse_x > (screen_width/2) + 400: 
            x_move = 10 
        elif mouse_x < (screen_width/2) - 400: 
            x_move = -10 
        else: 
            x_move = 0 
 
        x -= x_move 
 
 
        if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False and v == 0: 
            camera_up = True 
 
        if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True: 
            v = 1 
 
        if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True and v == 1: 
            screen.fill((0,0,0)) 
            camera_up = False 
 
        if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False: 
            v = 0 
 
 
        if camera_up == False: 
            screen.blit(office_pic, (x,0)) 
 
 
        if camera_up == True: 
            cameras(mouse_x, mouse_y) 
 
 
 
 
 
        pygame.display.update() 
        clock.tick(40) 
 
 
 
main() 

请您参考如下方法:

你有一个名为 clock() 的函数 -

def clock(): 
    global fps_tick, seconds, hou 
    fps_tick += 1 
 
    if fps_tick == 40: 
        seconds += 1 
        fps_tick = 0 
 
    if seconds == 30: 
        hour += 1 

它屏蔽了 clock = pygame.time.Clock() 变量,因此当您访问 clock 名称时,它会访问您创建的函数。

我没有看到你在任何地方使用这个函数,如果它实际上在你没有发布的代码的某些部分中使用,你应该重命名它,这样名称就不会掩盖你创建的其他变量。如果您不使用该功能,请将其删除。