如何从 pygame 精灵组传递值

How to pass values from pygame sprite group

我正在构建一个生成一堆士兵精灵的模拟。他们 运行 穿过地图,走向一些枪支。我想要枪来射击士兵。我正在尝试将士兵 class 矩形 x 和 y 值传递给塔对象。

相关代码如下:

class soldier(pygame.sprite.Sprite):  
    def __init__(self):  
        pygame.sprite.Sprite.__init__(self)  
        self.rect.centerx = soldier_starting_positionx               
        self.rect.centery = soldier_starting_positiony    

    def x(self):
        return (self.rect.centerx)  

class gaurdtower(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.rect.centerx = tower_starting_positionx
        self.rect.centery = tower_starting_positiony

    def update(self):
        print (soldier.x)

这两个 classes 从 for 循环中生成多个实例并添加到 all_sprite pygame 组。

打印输出是这样的行列表:
函数 soldier.x 在 0x00000286C296F0D0

我想要的是一个简单的整数。

soldier.x引用了士兵class中的一个函数。 类 不包含任何实际士兵数据。你需要引用一个士兵的对象class.

例如:

soldier = soldier()
soldier.x() # should return the soldiers x value.

在尝试这样的事情之前我会先看教程:http://learnpythonthehardway.org/book/ex40.html