|
| 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() |
0 commit comments