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 = 1280
10HEIGHT = 720
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_rect_filled(
36 arcade.XYWH(
37 self.window.width / 2,
38 self.window.height / 2,
39 self.window.width,
40 self.window.height,
41 ),
42 color=(0, 0, 0, self.fade_out),
43 )
44
45 if self.fade_in is not None:
46 arcade.draw_rect_filled(
47 arcade.XYWH(
48 self.window.width / 2,
49 self.window.height / 2,
50 self.window.width,
51 self.window.height,
52 ),
53 color=(0, 0, 0, self.fade_in),
54 )
55
56
57class MenuView(FadingView):
58 """ Class that manages the 'menu' view. """
59
60 def on_update(self, dt):
61 self.update_fade(next_view=GameView)
62
63 def on_show_view(self):
64 """ Called when switching to this view"""
65 self.window.background_color = arcade.color.WHITE
66
67 def on_draw(self):
68 """ Draw the menu """
69 self.clear()
70 arcade.draw_text("Menu Screen - press space to advance", WIDTH / 2, HEIGHT / 2,
71 arcade.color.BLACK, font_size=30, anchor_x="center")
72 self.draw_fading()
73
74 def on_key_press(self, key, _modifiers):
75 """ Handle key presses. In this case, we'll just count a 'space' as
76 game over and advance to the game over view. """
77 if self.fade_out is None and key == arcade.key.SPACE:
78 self.fade_out = 0
79
80 def setup(self):
81 """ This should set up your game and get it ready to play """
82 # Replace 'pass' with the code to set up your game
83 pass
84
85
86class GameView(FadingView):
87 """ Manage the 'game' view for our program. """
88
89 def setup(self):
90 """ This should set up your game and get it ready to play """
91 # Replace 'pass' with the code to set up your game
92 pass
93
94 def on_update(self, dt):
95 self.update_fade(next_view=GameOverView)
96
97 def on_show_view(self):
98 """ Called when switching to this view"""
99 self.window.background_color = arcade.color.ORANGE_PEEL
100
101 def on_draw(self):
102 """ Draw everything for the game. """
103 self.clear()
104 arcade.draw_text("Game - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
105 arcade.color.BLACK, font_size=30, anchor_x="center")
106 self.draw_fading()
107
108 def on_key_press(self, key, _modifiers):
109 """ Handle key presses. In this case, we'll just count a 'space' as
110 game over and advance to the game overview. """
111 if key == arcade.key.SPACE:
112 self.fade_out = 0
113
114
115class GameOverView(FadingView):
116 """ Class to manage the game overview """
117 def on_update(self, dt):
118 self.update_fade(next_view=MenuView)
119
120 def on_show_view(self):
121 """ Called when switching to this view"""
122 self.background_color = arcade.color.BLACK
123
124 def on_draw(self):
125 """ Draw the game overview """
126 self.clear()
127 arcade.draw_text("Game Over - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
128 arcade.color.WHITE, 30, anchor_x="center")
129 self.draw_fading()
130
131 def on_key_press(self, key, _modifiers):
132 """ If user hits escape, go back to the main menu view """
133 if key == arcade.key.SPACE:
134 self.fade_out = 0
135
136 def setup(self):
137 """ This should set up your game and get it ready to play """
138 # Replace 'pass' with the code to set up your game
139 pass
140
141
142def main():
143 """ Startup """
144 window = arcade.Window(WIDTH, HEIGHT, "Different Views Minimal Example")
145 menu_view = MenuView()
146 window.show_view(menu_view)
147 arcade.run()
148
149
150if __name__ == "__main__":
151 main()