gpt4 book ai didi

python - 属性错误 : Class Instance has no __call__ method

转载 作者:太空狗 更新时间:2023-10-29 19:32:28 32 4
gpt4 key购买 nike

我对 python 有点陌生,但熟悉 OOP。我正在尝试使用 PyGame 编写游戏。基本上,我的目标是每隔几秒渲染一次树,并在屏幕上移动树矩形。

这是我的代码:

from collections import deque
import pygame,random,sys

pygame.init()
size = 800,600
screen = pygame.display.set_mode(size)

class tree:
def __init__(self):
self.img = pygame.image.load("tree.png")
self.rect = self.img.get_rect()
def render(self):
screen.blit(self.img,self.rect)
def move(self,x,y):
self.rect = self.rect.move(x,y)

#creating a queue of trees
trees = deque()

#appending the first tree on to the queue
trees.append(tree())


while 1:


for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

#appending tree() to trees queue every 300 ms
if pygame.time.get_ticks() % 300 == 0:
trees.append(tree())

#rendering and moving all the tree rects of trees in the queue
for tree in trees:
tree.render()
tree.move(20,2)
pygame.display.flip()

但是当我执行此操作时,前几棵树已成功生成,但随后 PyGame 窗口关闭并出现此错误:

Traceback (most recent call last):
File "error.py", line 25, in <module>
trees.append(tree())
AttributeError: tree instance has no __call__ method

最佳答案

我猜这是因为你有一个变量名 tree(在 tree.render() 中使用)与你的类名冲突。称它为 Tree 会更好(而且更像 pythonic ^^)。

关于python - 属性错误 : Class Instance has no __call__ method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11324589/

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