TypeError: 对于 ‘pygame.time.Clock’ 对象,’tick’ 描述符不适用于 ‘int’ 对象

huangapple go评论194阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 01:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定