“道奇”型游戏

发布于 2024-08-29 11:54:44 字数 2339 浏览 4 评论 0原文

我正在尝试使用 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()

I am attempting to write a game using livewires and pygame where I have a chef (only image I had, haha), avoid rocks that are falling from the sky. The rocks are supposed to fall in random places. I want it to be that 1 rock falls to begin with, then every time you successfully dodge a rock, 2 more rocks fall, until you lose. What I have so far is the chef and 1 rock falling. However, for some reason if the sprites collide, or if the rock touches the bottom of the screen, the game ends, without giving a game over message like I told it to. I'm very confused, and can't see what I did. I know that I did not code it correctly for the 2 rocks part, but I can't even get it to slightly run. Help! Here's what I have now:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-09-05 11:54:44

您已在 Rock class 中声明了 end_game 方法,但您是从 Chef classcheck_catch 方法调用它。

You have declared end_game method in Rock class but you are calling it from check_catch method of Chef class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文