pygame-KidsCanCode系列jumpy-part7-游戏启动/结束画面

通常一个游戏启动(start)或结束(game over)时,都会显示一个画面,来引导用户。这节,我们学习如何处理这块逻辑。

其实之前,我们已经预留了2个函数,只要把它实现即可:

    def show_start_screen(self):
        self.screen.fill(BG_COLOR)
        self.draw_text(TITLE, 48, WHITE, WIDTH / 2, HEIGHT * 0.4)
        self.draw_text("Arrows to move, Space to jump", 22, WHITE, WIDTH / 2, HEIGHT * 0.55)
        self.draw_text("Press a key to play", 20, WHITE, WIDTH / 2, HEIGHT * 0.7)
        pg.display.update()
        self.wait_for_key()

    def show_go_screen(self):
        self.screen.fill(BG_COLOR)
        self.draw_text("GAME OVER", 48, WHITE, WIDTH / 2, HEIGHT * 0.4)
        self.draw_text("Score:  " + str(self.score), 22, WHITE, WIDTH / 2, HEIGHT * 0.55)
        self.draw_text("Press a key to play again", 20, WHITE, WIDTH / 2, HEIGHT * 0.7)
        pg.display.update()
        self.wait_for_key()

其中BG_COLOR,自然还是在settings.py中定义:

YELLOW = 255, 255, 0
LIGHT_BLUE = 0, 155, 155
BG_COLOR = LIGHT_BLUE

另外有一个细节要注意,通常在启动或结束画面上,需要等待用户下一步操作(比如:点击"开始"或"再来"),所以还需要加一个新的函数:

    def wait_for_key(self):
        waiting = True
        while waiting:
            self.clock.tick(FPS)
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    waiting = False
                    self.running = False
                if event.type == pg.KEYUP:
                    waiting = False

大意是先来一个循环,让画面卡住,然后等待用户的键盘事件,随便按个键就跳出循环。

posted @ 2019-01-27 23:28 菩提树下的杨过 阅读(...) 评论(...) 编辑 收藏