Minimal Views Example#

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

view_screens_minimal.py#
  1"""
  2This program shows how to:
  3  * Display a sequence of screens in your game.  The "arcade.View"
  4    class makes it easy to separate the code for each screen into
  5    its own class.
  6  * This example shows the absolute basics of using "arcade.View".
  7    See the "different_screens_example.py" for how to handle
  8    screen-specific data.
  9
 10Make a separate class for each view (screen) in your game.
 11The class will inherit from arcade.View. The structure will
 12look like an arcade.Window as each View will need to have its own draw,
 13update and window event methods. To switch a View, simply create a View
 14with `view = MyView()` and then use the "self.window.set_view(view)" method.
 15
 16If Python and Arcade are installed, this example can be run from the command line with:
 17python -m arcade.examples.view_screens_minimal
 18"""
 19
 20from __future__ import annotations
 21
 22import arcade
 23
 24
 25WIDTH = 800
 26HEIGHT = 600
 27
 28
 29class MenuView(arcade.View):
 30    """ Class that manages the 'menu' view. """
 31
 32    def on_show_view(self):
 33        """ Called when switching to this view"""
 34        self.window.background_color = arcade.color.WHITE
 35
 36    def on_draw(self):
 37        """ Draw the menu """
 38        self.clear()
 39        arcade.draw_text("Menu Screen - click to advance", WIDTH / 2, HEIGHT / 2,
 40                         arcade.color.BLACK, font_size=30, anchor_x="center")
 41
 42    def on_mouse_press(self, _x, _y, _button, _modifiers):
 43        """ Use a mouse press to advance to the 'game' view. """
 44        game_view = GameView()
 45        game_view.setup()
 46        self.window.show_view(game_view)
 47
 48
 49class GameView(arcade.View):
 50    """ Manage the 'game' view for our program. """
 51
 52    def __init__(self):
 53        super().__init__()
 54        # Create variables here
 55
 56    def setup(self):
 57        """ This should set up your game and get it ready to play """
 58        # Replace 'pass' with the code to set up your game
 59        pass
 60
 61    def on_show_view(self):
 62        """ Called when switching to this view"""
 63        self.background_color = arcade.color.ORANGE_PEEL
 64
 65    def on_draw(self):
 66        """ Draw everything for the game. """
 67        self.clear()
 68        arcade.draw_text("Game - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
 69                         arcade.color.BLACK, font_size=30, anchor_x="center")
 70
 71    def on_key_press(self, key, _modifiers):
 72        """ Handle key presses. In this case, we'll just count a 'space' as
 73        game over and advance to the game over view. """
 74        if key == arcade.key.SPACE:
 75            game_over_view = GameOverView()
 76            self.window.show_view(game_over_view)
 77
 78
 79class GameOverView(arcade.View):
 80    """ Class to manage the game over view """
 81    def on_show_view(self):
 82        """ Called when switching to this view"""
 83        self.window.background_color = arcade.color.BLACK
 84
 85    def on_draw(self):
 86        """ Draw the game over view """
 87        self.clear()
 88        arcade.draw_text("Game Over - press ESCAPE to advance", WIDTH / 2, HEIGHT / 2,
 89                         arcade.color.WHITE, 30, anchor_x="center")
 90
 91    def on_key_press(self, key, _modifiers):
 92        """ If user hits escape, go back to the main menu view """
 93        if key == arcade.key.ESCAPE:
 94            menu_view = MenuView()
 95            self.window.show_view(menu_view)
 96
 97
 98def main():
 99    """ Startup """
100    window = arcade.Window(WIDTH, HEIGHT, "Different Views Minimal Example")
101    menu_view = MenuView()
102    window.show_view(menu_view)
103    arcade.run()
104
105
106if __name__ == "__main__":
107    main()