Python 游戏中对象属性的持续更新与及时提示

56 阅读2分钟
  1. 你使用 Pygame 和 Livewires 创建了一个简单的游戏,玩家需控制一个精灵躲避下落的蘑菇。你希望随着游戏分数的增加,同时下落的蘑菇数量也能随之增加。但是,在你的代码中,蘑菇的数量和速度无法正确地递增,并且游戏会在第一个蘑菇触底时显示通关提示,而不是在达到指定分数后。

2、解决方案: 为了解决这个问题,你需要在 Mushroom 类中修改一些方法,以确保对象属性能够持续更新并及时显示提示:

  • next_level() 方法中,你需要增加 start 属性,以便在下一关中蘑菇开始下落的位置更高。同时,你需要更新 start 属性的初始值,因为在第一关开始时,蘑菇应该从顶部开始下落。

  • check() 方法中,你需要首先检查 Mushroom.score 是否达到 Mushroom.score_required。如果达到,则调用 next_level() 方法以提高难度。如果你想在通关后继续游戏,你可以将 next_level() 方法修改为重置 Mushroom.scoreMushroom.total_score 属性,并保持其他属性不变。

class Mushroom(games.Sprite):
    score = 0
    start = 400  # Initial position for the first mushroom
    score_required = 100
    level = 1
    total_score = 0
    speed = 1
    mushroom = games.load_image("mushroom.jpg")
    x_position = random.randrange(640)

    @staticmethod
    def next_level():
        indicate = 'Level  ', + Mushroom.level, ' cleared'
        message = games.Message(value=indicate, size=50, color=color.red,
                                x=games.screen.width / 2, y=games.screen.height / 2,
                                lifetime=150)
        games.screen.add(message)
        Mushroom.score_required += 50
        Mushroom.score -= Mushroom.score_required
        Mushroom.start -= 150
        Mushroom.speed += 5
        Mushroom.level += 1

        # Increase the start position for the next level
        Mushroom.start += 10

    def __init__(self):
        super(Mushroom, self).__init__(image=Mushroom.mushroom, x=games.mouse.x, y=0)

    def update(self):
        self.dy = Mushroom.speed
        self.check()
        self.check2()

    def check(self):
        if self.bottom == games.screen.height:
            self.destroy()
            Mushroom.score += 50
            Mushroom.total_score += Mushroom.score

        # Check if the score has reached the required amount for the next level
        if Mushroom.score >= Mushroom.score_required:
            self.next_level()

    def check2(self):
        # Create a new mushroom when the current one reaches the start position
        if self.top == Mushroom.start:
            self.duplicate()

    def duplicate(self):
        new_mush = Mushroom()
        games.screen.add(new_mush)
  • check() 方法中,你需要添加一个条件语句来检查 Mushroom.score 是否达到 Mushroom.score_required。如果达到,则调用 next_level() 方法以提高难度。如果你想在通关后继续游戏,你可以将 next_level() 方法修改为重置 Mushroom.scoreMushroom.total_score 属性,并保持其他属性不变。

修改后的代码如下:

class Mushroom(games.Sprite):
    score = 0
    start = 400  # Initial position for the first mushroom
    score_required = 100
    level = 1
    total_score = 0
    speed = 1
    mushroom = games.load_image("mushroom.jpg")
    x_position = random.randrange(640)

    @staticmethod
    def next_level():
        indicate = 'Level  ', + Mushroom.level, ' cleared'
        message = games.Message(value=indicate, size=50, color=color.red,
                                x=games.screen.width / 2, y=games.screen.height / 2,
                                lifetime=150)
        games.screen.add(message)
        Mushroom.score_required += 50
        Mushroom.score -= Mushroom.score_required
        Mushroom.start -= 150
        Mushroom.speed += 5
        Mushroom.level += 1

        # Increase the start position for the next level
        Mushroom.start += 10

    def __init__(self):
        super(Mushroom, self).__init__(image=Mushroom.mushroom, x=games.mouse.x, y=0)

    def update(self):
        self.dy = Mushroom.speed
        self.check()
        self.check2()

    def check(self):
        if self.bottom == games.screen.height:
            self.destroy()
            Mushroom.score += 50
            Mushroom.total_score += Mushroom.score

        # Check if the score has reached the required amount for the next level
        if Mushroom.score >= Mushroom.score_required:
            self.next_level()

    def check2(self):
        # Create a new mushroom when the current one reaches the start position
        if self.top == Mushroom.start:
            self.duplicate()

    def duplicate(self):
        new_mush = Mushroom()
        games.screen.add(new_mush)

在以上解决方案中,你可以通过修改 next_level() 方法来控制何时显示通关提示,并可以通过调整 start 的初始值来控制第一个蘑菇开始下落的位置。

希望这可以帮助你解决问题,并在游戏中实现想要的效果。