tuple对象非可调用 - Pygam

2024-09-30 18:15:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用pygame编写游戏时又遇到了一个问题。在

在处理允许玩家射击的函数时,我一直收到这样的错误:“tuple”object is not callable'

在我的主程序中,“循环”子例程:

play=True

    sprites,platforms,player1=GameClass.Game.run('')  # Player 1 is defined here.

    display.fill(WHITE)
    GameClass.Game.restart(player1)
    display.fill(WHITE)


    while play:
        display.fill(WHITE)
        GameClass.Game.getevent(player1)
        platforms.draw(display)
        sprites.draw(display)
        pygame.display.update()

        collision=pygame.sprite.spritecollide(player1,platforms, False)
        if collision:
            player1.updateposition(True,False)

        else:
            player1.updateposition(False,False)

如您所见,player1已定义。然后我打电话来GameClass.Game.getevent,以self作为参数。在

我把代码传给玩家1,然后转到下面这行:

^{pr2}$

由于self仍然被分配给player1,所以它将转到最后一个部分,即bullet类。在

class Bullet(pygame.sprite.Sprite):

    def __init__(self,speed,Game):

        self.speed=speed
        self.image=pygame.Surface((10,10))
        self.rect=self.image.get_rect()
        #self.x,self.y=Player.exportlocation(player1)

    def movebullet(self):
        self.rect.center((self.x+self.speed),self.y)

我得到了一个错误:

(如果需要的话这里还有更多)

 File "C:\Users\Luke\Desktop\Year13CA\Base.py", line 293, in loop
    GameClass.Game.getevent(player1)
  File "C:\Users\Luke\Desktop\Year13CA\GameClass.py", line 64, in getevent
    PlayerClasses.Player.shoot(self)
  File "C:\Users\Luke\Desktop\Year13CA\PlayerClasses.py", line 77, in shoot
    Bullet.movebullet(self)
  File "C:\Users\Luke\Desktop\Year13CA\PlayerClasses.py", line 96, in movebullet
    self.rect.center((self.x+self.speed),self.y)
TypeError: 'tuple' object is not callable

我不知道为什么会出现这个问题。在

非常感谢, 卢克


Tags: rectselfgamefalsedisplaypygameusersfile
2条回答

来自the FineManual(重点是我的):

The Rect object has several virtual attributes which can be used to move and align the Rect:

x,y top, left, bottom, right topleft, bottomleft, topright, bottomright midtop, midleft, midbottom, midright center, centerx, centery size, width, height w,h

All of these attributes can be assigned to:

rect1.right = 10

rect2.center = (20,30)

这是因为self.rect.center(...)self.rect.center大概是一个元组。您可能想用self.rect.center = ...来指定它。在

相关问题 更多 >