如何在pygame中从头开始滚动背景图片

时间:2019-07-03 12:45:00

标签: python pygame

我正在制作一个有3个关卡的游戏。我已经成功创建了2个级别,并且一切运行良好,但是当我尝试达到3级时,它会从中间加载背景图像并卡在其中,并且不会随着玩家的前进而滚动。对于1级和2级代码运行良好,当玩家进入3级时只会出现问题

我尝试过的:

                  if self.level == 3:
                    # Moving Coins
                    self.updateCoinPos(self.coin_forest)

                    # Moving Platform_sky
                    self.updatePlatformPos(self.platform_forest)

                    self.updateEnemyPos(self.enemy_forest)

                    # For moving enemies dead bodies in lvl 2
                    for e in self.enemy_forest:
                        if e.enemyDead:
                            e.x += self.hero.velocity

                    if self.hero.x >= startScrollingPos_forest:
                        self.noBorderCross = False

                    self.coordinates = 2200

                    # Updating rect of Platform_sky
                    self.updatePlatformRect(self.platform_forest)

                    self.updateCoinRect(self.coin_forest)

            self.CameraX += self.hero.velocity

        # 6500 is Camera Position Limit - If increased Camera will behave abnormally
        if self.CameraX >= self.coordinates:
            self.noCameraCross = False
            if self.hero.x < 890 - self.hero.velocity - self.hero.width:
                self.hero.x += self.hero.velocity
                #self.level = 2

用于绘制图像的方法:

bg_3是最后一级背景

    def redrawGameWindow3(self):

    # This statement continuously keep drawing background image according to the camera value
    screen.blit(bg_3, (0 - self.CameraX, 0 - self.CameraY))

    text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
    screen.blit(text, (390, 10))

    # Drawing hero and enemies on Screen
    self.hero.draw(screen)

    # Drawing Enemies
    for e in self.enemy_forest:
        e.draw(screen)

    # Drawing Platform_sky(Tiles)
    for p in self.platform_forest:
        p.draw(screen)

    # Drawing Coins for Platform
    for c in self.coin_forest:
        c.draw(screen)

    for bullet in self.bullets:
        bullet.draw(screen)

    pygame.display.flip()

我的游戏类别代码:https://pastebin.com/szT3mWuy

我想要的是,当玩家进入3级时,背景必须随着玩家的前进而滚动。

0 个答案:

没有答案