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

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

TypeError: descriptor 'tick' for 'pygame.time.Clock' objects doesn't apply to a 'int' object

问题

  1. 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:

  1. import pygame, sys
  2. from pygame.locals import QUIT
  3. pygame.init()
  4. DISPLAYSURF = pygame.display.set_mode((400, 300))
  5. pygame.display.set_caption('Warrior Pre-1.0')
  6. pos = 0
  7. while True:
  8. DISPLAYSURF.fill("red")
  9. #pygame.draw.rect(rect())
  10. for event in pygame.event.get():
  11. if event.type == QUIT:
  12. pygame.quit()
  13. sys.exit()
  14. pygame.display.update()
  15. 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:

  1. Traceback (most recent call last):
  2. File "D:\main.py", line 16, in <module>
  3. pygame.time.Clock.tick(40)
  4. TypeError: descriptor 'tick' for 'pygame.time.Clock' objects doesn't apply to a 'int' object

答案1

得分: 2

使用 pygame.time.Clock 的正确方式如下:

  1. clock = pygame.time.Clock() # 在主循环之前初始化时钟,通常是在创建显示表面之后
  2. while True:
  3. clock.tick(40) # 在初始化的 `Clock` 对象上调用 `tick`
  4. ...

另外,自从 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:

  1. clock = pygame.time.Clock() # initialize the clock before the main loop, usually after creating the display surface
  2. while True:
  3. clock.tick(40) # call `tick` on the initialized `Clock` object
  4. ...

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:

确定