Using Views for Instruction and Game Over Screens

You might also want to check out Using Views for Start/End Screens.

view_instructions_and_game_over.py
  1"""
  2This program shows how to:
  3  * Have one or more instruction screens
  4  * Show a 'Game over' text and halt the game
  5  * Allow the user to restart the game
  6
  7Make a separate class for each view (screen) in your game.
  8The class will inherit from arcade.View. The structure will
  9look like an arcade.Window as each view will need to have its own draw,
 10update and window event methods. To switch a view, simply create a view
 11with `view = MyView()` and then use the view.show() method.
 12
 13This example shows how you can set data from one View on another View to pass data
 14around (see: time_taken), or you can store data on the Window object to share data between
 15all Views (see: total_score).
 16
 17If Python and Arcade are installed, this example can be run from the command line with:
 18python -m arcade.examples.view_instructions_and_game_over.py
 19"""
 20
 21import arcade
 22import random
 23import os
 24
 25
 26file_path = os.path.dirname(os.path.abspath(__file__))
 27os.chdir(file_path)
 28
 29
 30WIDTH = 800
 31HEIGHT = 600
 32SPRITE_SCALING = 0.5
 33
 34
 35class MenuView(arcade.View):
 36    def on_show(self):
 37        arcade.set_background_color(arcade.color.WHITE)
 38
 39    def on_draw(self):
 40        arcade.start_render()
 41        arcade.draw_text("Menu Screen", WIDTH/2, HEIGHT/2,
 42                         arcade.color.BLACK, font_size=50, anchor_x="center")
 43        arcade.draw_text("Click to advance", WIDTH/2, HEIGHT/2-75,
 44                         arcade.color.GRAY, font_size=20, anchor_x="center")
 45
 46    def on_mouse_press(self, _x, _y, _button, _modifiers):
 47        instructions_view = InstructionView()
 48        self.window.show_view(instructions_view)
 49
 50
 51class InstructionView(arcade.View):
 52    def on_show(self):
 53        arcade.set_background_color(arcade.color.ORANGE_PEEL)
 54
 55    def on_draw(self):
 56        arcade.start_render()
 57        arcade.draw_text("Instructions Screen", WIDTH/2, HEIGHT/2,
 58                         arcade.color.BLACK, font_size=50, anchor_x="center")
 59        arcade.draw_text("Click to advance", WIDTH/2, HEIGHT/2-75,
 60                         arcade.color.GRAY, font_size=20, anchor_x="center")
 61
 62    def on_mouse_press(self, _x, _y, _button, _modifiers):
 63        game_view = GameView()
 64        self.window.show_view(game_view)
 65
 66
 67class GameView(arcade.View):
 68    def __init__(self):
 69        super().__init__()
 70
 71        self.time_taken = 0
 72
 73        # Sprite lists
 74        self.player_list = arcade.SpriteList()
 75        self.coin_list = arcade.SpriteList()
 76
 77        # Set up the player
 78        self.score = 0
 79        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
 80        self.player_sprite.center_x = 50
 81        self.player_sprite.center_y = 50
 82        self.player_list.append(self.player_sprite)
 83
 84        for i in range(5):
 85
 86            # Create the coin instance
 87            coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING / 3)
 88
 89            # Position the coin
 90            coin.center_x = random.randrange(WIDTH)
 91            coin.center_y = random.randrange(HEIGHT)
 92
 93            # Add the coin to the lists
 94            self.coin_list.append(coin)
 95
 96    def on_show(self):
 97        arcade.set_background_color(arcade.color.AMAZON)
 98
 99        # Don't show the mouse cursor
100        self.window.set_mouse_visible(False)
101
102    def on_draw(self):
103        arcade.start_render()
104        # Draw all the sprites.
105        self.player_list.draw()
106        self.coin_list.draw()
107
108        # Put the text on the screen.
109        output = f"Score: {self.score}"
110        arcade.draw_text(output, 10, 30, arcade.color.WHITE, 14)
111        output_total = f"Total Score: {self.window.total_score}"
112        arcade.draw_text(output_total, 10, 10, arcade.color.WHITE, 14)
113
114    def on_update(self, delta_time):
115        self.time_taken += delta_time
116
117        # Call update on all sprites (The sprites don't do much in this
118        # example though.)
119        self.coin_list.update()
120        self.player_list.update()
121
122        # Generate a list of all sprites that collided with the player.
123        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
124
125        # Loop through each colliding sprite, remove it, and add to the
126        # score.
127        for coin in hit_list:
128            coin.kill()
129            self.score += 1
130            self.window.total_score += 1
131
132        # If we've collected all the games, then move to a "GAME_OVER"
133        # state.
134        if len(self.coin_list) == 0:
135            game_over_view = GameOverView()
136            game_over_view.time_taken = self.time_taken
137            self.window.set_mouse_visible(True)
138            self.window.show_view(game_over_view)
139
140    def on_mouse_motion(self, x, y, _dx, _dy):
141        """
142        Called whenever the mouse moves.
143        """
144        self.player_sprite.center_x = x
145        self.player_sprite.center_y = y
146
147
148class GameOverView(arcade.View):
149    def __init__(self):
150        super().__init__()
151        self.time_taken = 0
152
153    def on_show(self):
154        arcade.set_background_color(arcade.color.BLACK)
155
156    def on_draw(self):
157        arcade.start_render()
158        """
159        Draw "Game over" across the screen.
160        """
161        arcade.draw_text("Game Over", 240, 400, arcade.color.WHITE, 54)
162        arcade.draw_text("Click to restart", 310, 300, arcade.color.WHITE, 24)
163
164        time_taken_formatted = f"{round(self.time_taken, 2)} seconds"
165        arcade.draw_text(f"Time taken: {time_taken_formatted}",
166                         WIDTH/2,
167                         200,
168                         arcade.color.GRAY,
169                         font_size=15,
170                         anchor_x="center")
171
172        output_total = f"Total Score: {self.window.total_score}"
173        arcade.draw_text(output_total, 10, 10, arcade.color.WHITE, 14)
174
175    def on_mouse_press(self, _x, _y, _button, _modifiers):
176        game_view = GameView()
177        self.window.show_view(game_view)
178
179
180def main():
181    window = arcade.Window(WIDTH, HEIGHT, "Different Views Example")
182    window.total_score = 0
183    menu_view = MenuView()
184    window.show_view(menu_view)
185    arcade.run()
186
187
188if __name__ == "__main__":
189    main()