英文:
TypeError: descriptor 'tick' for 'pygame.time.Clock' objects doesn't apply to a 'int' object
问题
pygame.time.Clock().tick(40)
英文:
I'm trying to write a game with Pygame, but when I try to use the tick code, it stops working. Here is my code:
import pygame, sys
from pygame.locals import QUIT
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Warrior Pre-1.0')
pos = 0
while True:
DISPLAYSURF.fill("red")
#pygame.draw.rect(rect())
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
pygame.time.Clock.tick(40)
I tried checking the Pygame docs, but I copy the code across and it does not work. Here is my error:
Traceback (most recent call last):
File "D:\main.py", line 16, in <module>
pygame.time.Clock.tick(40)
TypeError: descriptor 'tick' for 'pygame.time.Clock' objects doesn't apply to a 'int' object
答案1
得分: 2
使用 pygame.time.Clock
的正确方式如下:
clock = pygame.time.Clock() # 在主循环之前初始化时钟,通常是在创建显示表面之后
while True:
clock.tick(40) # 在初始化的 `Clock` 对象上调用 `tick`
...
另外,自从 pygame-ce
版本 2.1.4
起,pygame.Clock
的别名也可用,因此在此版本中,你可以简单地使用 clock = pygame.Clock()
,请参阅文档 这里。
有关 pygame-ce
的更多信息,请阅读这里:Pygame: Community Edition Announcement
英文:
The proper way to utilize pygame.time.Clock
goes like this:
clock = pygame.time.Clock() # initialize the clock before the main loop, usually after creating the display surface
while True:
clock.tick(40) # call `tick` on the initialized `Clock` object
...
Also since pygame-ce
version 2.1.4
the pygame.Clock
alias is also available for use so in this version you could simply have clock = pygame.Clock()
, see the docs here
More about pygame-ce
can be read here: Pygame: Community Edition Announcement
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论