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