我正在尝试随机选择要为屏幕上生成的Angryball对象加载的图像,但是使用我的代码,该程序将加载随机图像并在主循环的整个过程中坚持使用我产生的每个对象都具有相同的图像。是该类的更新定义中的错误,还是我在主循环中缺少某些内容?
课程:
mob_images = [pygame.image.load("image1.png").convert_alpha(),pygame.image.load("image2.png").convert_alpha(),pygame.image.load("image3.png").convert_alpha(),pygame.image.load("image4.png").convert_alpha(),pygame.image.load("image5.png").convert_alpha()]
mob_image = random.choice(mob_images)
class Angryball(pygame.sprite.Sprite):
def __init__(self, image, pos_x, pos_y):
super(Angryball, self).__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = pos_x
self.rect.y = pos_y
self.facing = 'LEFT'
def update(self, screen):
if self.rect.x <= 0:
self.rect.right = screen.get_rect().width
self.rect.top = random.randint(0, screen.get_rect().height)
else:
self.rect.move_ip(-5, 0)
精灵:
angryball = Angryball(mob_image, 700, random.randrange(400))
sprites = pygame.sprite.Group()
sprites.add(angryball)
在主循环中:
sprites.update(screen)
sprites.draw(screen)
答案 0 :(得分:2)
更改
(->> (filter (fn [[k v]] v) a) (map (fn [[k v]] k)))
到
angryball = Angryball(mob_image, 700, random.randrange(400))
您要在创建对象时随机选择图像。如果在创建对象之前选择一个随机图像(如您现在所做的那样),它将随机图像存储为全局变量(如果它不在我假设的函数或类中),并且仅在任何时候使用该图像您致电angryball = Angryball(random.choice(mob_images), 700, random.randrange(400))
(例如,当您按原样创建Angryball实例时)。如果您将mob_image
作为变量传递,它会强制Python每次 选择一个随机图像,然后创建一个Angryball实例,从而提供与随机选择的静态图像不同的随机图像就像您当前代码中的代码一样。
答案 1 :(得分:1)
每次重置Angryball的位置时,您都可以设置新图像:
...
class Angryball(pygame.sprite.Sprite):
def __init__(self, mob_images, pos_x, pos_y):
super(Angryball, self).__init__()
self.mob_images = mob_images
self.image = random.choice(self.mob_images)
self.rect = self.image.get_rect(x=pos_x, y=pos_y)
self.facing = 'LEFT'
def update(self, screen):
if self.rect.x <= 0:
self.rect.right = screen.get_rect().width
self.rect.top = random.randint(0, screen.get_rect().height)
self.image = random.choice(self.mob_images)
# we assume all images are the same size
# if not, we should call 'self.rect = self.image.get_rect()' again
else:
self.rect.move_ip(-5, 0)
...
mob_images = [pygame.image.load("image1.png").convert_alpha(),pygame.image.load("image2.png").convert_alpha(),pygame.image.load("image3.png").convert_alpha(),pygame.image.load("image4.png").convert_alpha(),pygame.image.load("image5.png").convert_alpha()]
angryball = Angryball(mob_images , 700, random.randrange(400))
...
另一个想法是创建一个新的Angryball
实例,无论如何这可能是一个更好的想法。像这样:
...
class Angryball(pygame.sprite.Sprite):
def __init__(self, mob_images, pos_x, pos_y):
super(Angryball, self).__init__()
self.mob_images = mob_images
self.image = random.choice(self.mob_images)
self.rect = self.image.get_rect(x=pos_x, y=pos_y)
self.facing = 'LEFT'
def update(self, screen):
if self.rect.x <= 0:
newBall = Angryball(self.mob_images, 0, 0)
for g in self.groups():
g.add(newBall)
self.kill()
newBall.rect.right = screen.get_rect().width
newBall.rect.top = random.randint(0, screen.get_rect().height)
else:
self.rect.move_ip(-5, 0)
...
mob_images = [pygame.image.load("image1.png").convert_alpha(),pygame.image.load("image2.png").convert_alpha(),pygame.image.load("image3.png").convert_alpha(),pygame.image.load("image4.png").convert_alpha(),pygame.image.load("image5.png").convert_alpha()]
angryball = Angryball(mob_images , 700, random.randrange(400))
...