Skip to content

Commit 41fa0b8

Browse files
authored
feat(gui): add arcade splash screen (#2207)
1 parent 5751f52 commit 41fa0b8

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

arcade/future/splash.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
An experimental splash screen for arcade.
3+
4+
This is a simple splash screen that shows the arcade logo
5+
for a few seconds before the actual game starts.
6+
7+
If arcade is properly installed, you can run this script with:
8+
python -m arcade.gui.experimental.splash
9+
"""
10+
11+
import arcade
12+
from arcade import Sprite, SpriteList, Text, View
13+
14+
15+
class ArcadeSplash(View):
16+
"""This view shows an arcade splash screen before the actual game starts.
17+
18+
Args:
19+
view: Next view to show after the splash screen.
20+
duration: The duration of the splash screen in seconds. (Default 3 seconds)
21+
dark_mode: If True, the splash screen will be shown in dark mode. (Default False)
22+
"""
23+
24+
def __init__(self, view: View, duration: int = 3, dark_mode: bool = False):
25+
super().__init__()
26+
self._next_view = view
27+
self._duration = duration
28+
self._time = 0.0
29+
self._dark_mode = dark_mode
30+
31+
_text_color = (255, 255, 255, 255) if dark_mode else (0, 0, 0, 255)
32+
self._bg_color = (0, 0, 0, 255) if dark_mode else arcade.color.WHITE_SMOKE
33+
34+
self._sprites: SpriteList[Sprite] = SpriteList()
35+
self._logo = Sprite(
36+
arcade.load_texture(":system:/logo.png"),
37+
center_x=self.window.center_x,
38+
center_y=self.window.center_y,
39+
)
40+
self._logo.size = 300, 300
41+
self._sprites.append(self._logo)
42+
43+
self._text = Text(
44+
"Python Arcade",
45+
anchor_x="center",
46+
x=self.window.center_x,
47+
y=self._logo.bottom - 20,
48+
color=_text_color,
49+
font_size=40,
50+
bold=True,
51+
)
52+
53+
def on_show_view(self):
54+
"""Set background color and reset time."""
55+
arcade.set_background_color(self._bg_color)
56+
self._time = 0.0
57+
self._logo.alpha = 0
58+
59+
def on_update(self, delta_time: float):
60+
"""Update the time and switch to the next view after the duration."""
61+
self._time += delta_time
62+
if self._time >= self._duration:
63+
self.window.show_view(self._next_view)
64+
65+
# fade in arcade logo
66+
self._logo.alpha = min(255, int(255 * self._time / self._duration))
67+
68+
def on_draw(self):
69+
"""Draw the sprites and text."""
70+
self.clear()
71+
self._sprites.draw()
72+
self._text.draw()
73+
74+
75+
if __name__ == "__main__":
76+
window = arcade.Window()
77+
window.show_view(ArcadeSplash(ArcadeSplash(dark_mode=True, view=arcade.View())))
78+
arcade.run()

arcade/gui/widgets/image.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class UIImage(UIWidget):
2424
"""
2525

2626
texture: Union[Texture, NinePatchTexture] = Property() # type: ignore
27+
"""Texture to show"""
28+
alpha: int = Property(255) # type: ignore
29+
"""Alpha value of the texture, value between 0 and 255.
30+
0 is fully transparent, 255 is fully visible."""
2731

2832
def __init__(
2933
self,
@@ -41,6 +45,7 @@ def __init__(
4145
**kwargs,
4246
)
4347
bind(self, "texture", self.trigger_render)
48+
bind(self, "alpha", self.trigger_full_render)
4449

4550
@override
4651
def do_render(self, surface: Surface):
@@ -53,4 +58,5 @@ def do_render(self, surface: Surface):
5358
width=self.content_width,
5459
height=self.content_height,
5560
tex=self.texture,
61+
alpha=self.alpha,
5662
)

arcade/resources/system/logo.png

53.2 KB
Loading

0 commit comments

Comments
 (0)