1
我想在pygame库的帮助下编写一个小型的滚动游戏。当我试图在运行时添加障碍时,我注意到pygame/python中有一些奇怪的行为。添加后列表的最后一项不会改变
class ObstaclesGroup(pygame.sprite.Group):
def update(self, offset):
lastSprite = self.sprites()[-1]
if lastSprite.rect.x < distance + 640:
# add obstacle with a distance of 300 px to the previous
self.add(Obstacle(distance + 940))
sprite = self.sprites()[-1]
# often the values are the same, although the last one
# should be 300px bigger
# update: they even seem to be identical
if (lastSprite == sprite):
print (lastSprite.rect.x, " ", sprite.rect.x)
后下部(后“如果”)被执行第二次,lastSprite和精灵的x坐标,似乎是同一不少。
下面是从控制台输出一些例子:
740 1043
1043 1043
1043 1043
1043 1043
1043 1344
1344 1344
1344 1648
1648 1648
1648 1648
1648 1648
1648 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 1953
1953 2326
2326 2326
2326 2326
2326 2326
2326 2326
2326 2326
2326 2326
2326 2326
2326 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
2288 2288
精灵(Obstacle
)似乎并没有被正确添加到spritegroup,虽然他们被吸引(我可以看到不同的偏移多重障碍,因为它在每个循环周期中增加)。
可能是什么问题?
UPDATE: 如果在它们结束时添加:两个精灵是相同的。