Skip to main content
 首页 » 编程设计

python之“道奇”式游戏

2025年05月04日9langtianya

我正在尝试使用 livewires 和 pygame 编写一个游戏,我有一个厨师(我只有图像,哈哈),避免从天上掉下来的石头。岩石应该随机掉落。我希望一开始有 1 block 石头掉落,然后每次你成功躲避一 block 石头时,又有 2 block 石头掉落,直到你输了。到目前为止,我所拥有的是厨师和 1 block 落石。然而,出于某种原因,如果 Sprite 发生碰撞,或者如果岩石接触到屏幕底部,游戏就会结束,而不会像我告诉的那样给出游戏结束的消息。我很困惑,看不到我做了什么。我知道我没有为 2 rocks 部分正确编码,但我什至无法让它稍微运行。帮助!这是我现在拥有的:

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

请您参考如下方法:

您已在 Rock 类 中声明了 end_game 方法,但您正在从 Chef 类 的 check_catch 方法中调用它.