Fade In/Out of Views

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

transitions.py
  1"""
  2Example showing how to do transitions between views.
  3
  4If Python and Arcade are installed, this example can be run from the command line with:
  5python -m arcade.examples.transitions
  6"""
  7import arcade
  8
  9WIDTH = 800
 10HEIGHT = 600
 11FADE_RATE = 5
 12
 13
 14class FadingView(arcade.View):
 15    def __init__(self):
 16        super().__init__()
 17        self.fade_out = None
 18        self.fade_in = 255
 19
 20    def update_fade(self, next_view=None):
 21        if self.fade_out is not None:
 22            self.fade_out += FADE_RATE
 23            if self.fade_out is not None and self.fade_out > 255 and next_view is not None:
 24                game_view = next_view()
 25                game_view.setup()
 26                self.window.show_view(game_view)
 27
 28        if self.fade_in is not None:
 29            self.fade_in -= FADE_RATE
 30            if self.fade_in <= 0:
 31                self.fade_in = None
 32
 33    def draw_fading(self):
 34        if self.fade_out is not None:
 35            arcade.draw_rectangle_filled(self.window.width / 2, self.window.height / 2,
 36                                         self.window.width, self.window.height,
 37                                         (0, 0, 0, self.fade_out))
 38
 39        if self.fade_in is not None:
 40            arcade.draw_rectangle_filled(self.window.width / 2, self.window.height / 2,
 41                                         self.window.width, self.window.height,
 42                                         (0, 0, 0, self.fade_in))
 43
 44
 45class MenuView(FadingView):
 46    """ Class that manages the 'menu' view. """
 47
 48    def on_update(self, dt):
 49        self.update_fade(next_view=GameView)
 50
 51    def on_show_view(self):
 52        """ Called when switching to this view"""
 53        self.window.background_color = arcade.color.WHITE
 54
 55    def on_draw(self):
 56        """ Draw the menu """
 57        self.clear()
 58        arcade.draw_text("Menu Screen - press space to advance", WIDTH / 2, HEIGHT / 2,
 59                         arcade.color.BLACK, font_size=30, anchor_x="center")
 60        self.draw_fading()
 61
 62    def on_key_press(self, key, _modifiers):
 63        """ Handle key presses. In this case, we'll just count a 'space' as
 64        game over and advance to the game over view. """
 65        if self.fade_out is None and key == arcade.key.SPACE:
 66            self.fade_out = 0
 67
 68    def setup(self):
 69        """ This should set up your game and get it ready to play """
 70        # Replace 'pass' with the code to set up your game
 71        pass
 72
 73
 74class GameView(FadingView):
 75    """ Manage the 'game' view for our program. """
 76
 77    def setup(self):
 78        """ This should set up your game and get it ready to play """
 79        # Replace 'pass' with the code to set up your game
 80        pass
 81
 82    def on_update(self, dt):
 83        self.update_fade(next_view=GameOverView)
 84
 85    def on_show_view(self):
 86        """ Called when switching to this view"""
 87        self.window.background_color = arcade.color.ORANGE_PEEL
 88
 89    def on_draw(self):
 90        """ Draw everything for the game. """
 91        self.clear()
 92        arcade.draw_text("Game - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
 93                         arcade.color.BLACK, font_size=30, anchor_x="center")
 94        self.draw_fading()
 95
 96    def on_key_press(self, key, _modifiers):
 97        """ Handle key presses. In this case, we'll just count a 'space' as
 98        game over and advance to the game overview. """
 99        if key == arcade.key.SPACE:
100            self.fade_out = 0
101
102
103class GameOverView(FadingView):
104    """ Class to manage the game overview """
105    def on_update(self, dt):
106        self.update_fade(next_view=MenuView)
107
108    def on_show_view(self):
109        """ Called when switching to this view"""
110        self.background_color = arcade.color.BLACK
111
112    def on_draw(self):
113        """ Draw the game overview """
114        self.clear()
115        arcade.draw_text("Game Over - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
116                         arcade.color.WHITE, 30, anchor_x="center")
117        self.draw_fading()
118
119    def on_key_press(self, key, _modifiers):
120        """ If user hits escape, go back to the main menu view """
121        if key == arcade.key.SPACE:
122            self.fade_out = 0
123
124    def setup(self):
125        """ This should set up your game and get it ready to play """
126        # Replace 'pass' with the code to set up your game
127        pass
128
129
130def main():
131    """ Startup """
132    window = arcade.Window(WIDTH, HEIGHT, "Different Views Minimal Example")
133    menu_view = MenuView()
134    window.show_view(menu_view)
135    arcade.run()
136
137
138if __name__ == "__main__":
139    main()