PyGame错误:clock.tick(40)AttributeError:' function'对象没有属性' tick'

时间:2015-08-19 19:26:36

标签: python pygame

我正在制作一个"粉丝游戏"弗雷迪的五夜之夜。我的代码工作正常,直到我开始收到此错误:

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()

2 个答案:

答案 0 :(得分:2)

您有一个名为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名称时,它会访问您创建的函数。

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

答案 1 :(得分:0)

你用一个名为clock的函数覆盖了文件顶部的clock = pygame.time.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