Pygame:玩家死亡后如何将背景重置到新位置

2024-10-03 11:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我用pygame制作了一个垂直平台的游戏。现在,我有一个垂直滚动的背景图像。我也有平台,敌人,电源也滚动,但他们是独立于背景。然而,一旦玩家死亡,背景图像将保持在玩家死亡时的位置。基本上,如果玩家死亡并再次尝试,背景将与玩家死亡时的高度相同,因此一切看起来都很奇怪和不合适。我想知道是否可以将背景重置为播放器位置,以便所有内容都能正确对齐。任何其他想法也会有所帮助 这是我的重要密码。如果您还需要,请给我留言:

#Enemies spawning
for ene in self.enemy_list:
    e = enemy(*ene)
    self.all_enemy.add(e)
    self.all_sprites.add(e)

#Platforms spawning
for plat in self.platform_list:
    p = Platform(*plat)
    self.all_sprites.add(p)
    self.platforms.add(p)

#Power ups spawning
for powerup in self.powerup_list:
    pu = powerUp(*powerup)
    self.all_sprites.add(pu)
    self.powerups.add(pu)
self.run()








# Scrolling Screen
            if self.player.rect.top <= screenLength / 4:
                self.player.pos.y += abs(self.player.vel.y)
                background.scroll(0,int(abs(self.player.vel.y)))
                #Enemy Scrolling
                for ene in self.all_enemy:
                    ene.rect.y += abs(self.player.vel.y)
                #Platform Scrolling
                for plat in self.platforms:
                    plat.rect.y += abs(self.player.vel.y)
                    if plat.rect.top >= screenLength:
                        plat.kill()


                #Projectile scrolling
                for bull in self.bullets:
                    bull.rect.y += abs(self.player.vel.y)
                #Powerup scrolling
                for powerup in self.powerups:
                    powerup.rect.y += abs(self.player.vel.y)

                #End object scrolling
                for End in self.endings:
                    End.rect.y += abs(self.player.vel.y)

     #Dying
        if self.health <= 0:
            self.playing = False

Tags: inrectselfaddfor玩家absall
1条回答
网友
1楼 · 发布于 2024-10-03 11:24:03

你可以有一个default_background作为图像并且不会改变,然后当你在new()中设置游戏时,使用background = default_background.copy()使background=default\u background。所以它从默认值开始。然后滚动background,当您需要重置它时,只需再次使background=default\u background

相关问题 更多 >