好吧。。我重写了它,但我还是有问题的随机选择 我明白它想做什么,我真的想要雷达天线选择当精灵离开屏幕时重置,但不是随机的。。。你知道吗
class Planets(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
imageFiles = ["planet_{}.gif".format(num) for num in range (1,4)]
for files in imageFiles:
self.image = pygame.image.load(files)
self.image.convert()
self.rect = self.image.get_rect()
self.x = 700
self.y = 50
self.dx = -5
def update(self):
self.rect.center = (self.x, self.y)
self.x += self.dx
if self.x <= -800:
self.reset()
def reset(self):
self.x = 800
self.image = random.choice(files)
self.y = random.randrange(0, screen.get_height())
此代码:
将一个图像加载到
self.image
,然后依次用其他图像替换它。我不知道你想要什么,但这个循环没有任何用处。你知道吗这个代码:
self.image = random.choice(files)
-应该完全失败,因为files
在这个函数中不存在。即使它引用了init函数中的files变量,它也只是一个字符串,所以自我形象最终会变成一个字符,而不是一个实际的图像。你知道吗我会尝试这样的方法(遗漏一些代码):
我会这么做:
当你有一张所有图片的列表时
相关问题 更多 >
编程相关推荐