“躲避者”类型游戏

2 投票
1 回答
939 浏览
提问于 2025-04-15 21:49

我正在尝试用livewires和pygame写一个游戏,游戏里有一个厨师(这是我唯一找到的图片,哈哈),他需要躲避从天上掉下来的石头。这些石头会随机掉落。我想要的效果是,开始时掉下1块石头,然后每次成功躲过一块石头后,就再掉下2块石头,直到游戏失败为止。目前我已经有了厨师和1块石头掉落的部分。但是,有个问题是,如果这些角色碰到一起,或者石头碰到屏幕底部,游戏就会结束,而没有显示我设置的游戏结束信息。我感到很困惑,也不知道自己哪里出了问题。我知道我在处理2块石头的部分时没有写对,但我连基本的运行都搞不定。求助!这是我现在的代码:

from livewires import games, color
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)


class Chef(games.Sprite):

    image = games.load_image("chef.bmp")

    def __init__(self):
        super(Chef, self).__init__(image = Chef.image,
                                  x = games.mouse.x,
                                  bottom = games.screen.height)

    def update(self):
        self.x = games.mouse.x

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width

        self.check_catch()

    def check_catch(self):
        for pizza in self.overlapping_sprites:
            if not self.bottom>games.screen.height:
                self.end_game()


class Rock(games.Sprite):

    def update(self):
        if self.bottom > games.screen.height:
                new_rock=Rock(x=random.randrange(games.screen.width),
                              y=10,
                              dy=1)
                games.screen.add(new_rock)

    def end_game(self):
        end_message = games.Message(value = "Game Over",
                                    size = 90,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 5 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)


def main():

    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    the_chef = Chef()
    games.screen.add(the_chef)

    rock_image=games.load_image("rock.bmp")
    the_rock=Rock(image=rock_image,
                  x=random.randrange(games.screen.width),
                  y=10,
                  dy=1)
    games.screen.add(the_rock)

    games.mouse.is_visible = False

    games.screen.event_grab = True
    games.screen.mainloop()

main()

1 个回答

0

你在Rock类里面定义了一个叫end_game的方法,但你却是在Chef类check_catch方法里调用它。

撰写回答