Skip to main content
 首页 » 编程设计

python之pygame Sprite 和绘制方法无法按预期工作

2024年10月25日14zhwl

如有任何帮助,我们将不胜感激。 我正在用 Pygame 编写游戏,并在创建了我需要的所有类和方法之后。当我运行游戏时,我看到我的游戏中的五个外星人角色从屏幕左侧连在一起,然后我才真正看到我希望我的代码显示的内容(外星人在屏幕上随机移动)。

这是我的代码:

class Alien(): 
    def __init__(self, image): 
        self.x = random.randrange(20,width - 20) 
        self.y = random.randrange(-200, -20) 
        self.speed = random.randint(1, 5) 
        self.image = pygame.image.load(os.path.join("../images", image)).convert_alpha() 
        self.rect = self.image.get_rect() 
 
    def move_down(self): 
        self.rect.y += self.speed 
 
    def draw(self, screen): 
        screen.blit(self.image, self.rect) 

它的实现

for i in range (20): 
    aliens = Alien("alien.png") 
    enemies.append(aliens) 
 
done = False 
while done == False: 
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True 
 
    screen.fill(white) 
 
    for i in range(len(enemies)): 
        enemies[i].move_down() 
        enemies[i].draw(screen) 
        enemies[i].check_landed() 
 
    pygame.display.flip() 
    clock.tick(30) 

注意:为清楚起见,我删除了一些代码。 结果

请您参考如下方法:

您将外星人的位置存储在字段 self.x 和 self.y 中,但是要绘制它们,您实际上并没有使用 self .x 和 self.y,但是 self.rect。

您通过调用 self.image.get_rect() 创建 self.rect,当您在 上调用 get_rect() >Surface,Rect 的位置总是(0, 0)。

因此 x 坐标始终为 0,因此它们都在屏幕的左侧。

我建议将您的代码重写为:

class Alien(): 
    # pass the Surface to the instance instead of the filename 
    # this way, you only load the image once, not once for each alien 
    def __init__(self, image): 
        self.speed = random.randint(1, 5) 
        self.image = image 
        # since we're going to use a Rect of drawing, let's use the 
        # same Rect to store the correct position of the alien 
        self.rect = self.image.get_rect(top=random.randrange(-200, -20), left=random.randrange(20,width - 20)) 
 
    def move_down(self): 
        # the Rect class has a lot of handy functions like move_ip 
        self.rect.move_ip(0, self.speed) 
 
    def draw(self, screen): 
        screen.blit(self.image, self.rect) 
 
# load the image once. In more complex games, you usually 
# want to abstract the loading/storing of images          
alienimage = pygame.image.load(os.path.join("../images", 'alien.png')).convert_alpha() 
for _ in range (20): 
    aliens = Alien(alienimage) 
    enemies.append(aliens) 
 
done = False 
while done == False: 
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True 
 
    screen.fill(white) 
 
    # just use a "regular" for loop to loop through all enemies 
    for enemy in enemies: 
        enemy.move_down() 
        enemy.draw(screen) 
        enemy.check_landed() 
 
    pygame.display.flip() 
    clock.tick(30)         

您可以更进一步,使用 Sprite 和 Group 类来进一步概括您的代码,但这是另一个话题。