gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:21 25 4
gpt4 key购买 nike

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

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

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

关于python - PyGame 错误 : clock. tick(40) AttributeError: 'function' object has no attribute 'tick',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com