Falling Snow

Screen shot of using using an array for snow
snow.py
  1"""
  2Simple Snow
  3Based primarily on:
  4https://api.arcade.academy/en/latest/examples/sprite_collect_coins_move_down.html
  5
  6Contributed to Python Arcade Library by Nicholas Hartunian
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.snow
 10"""
 11
 12import random
 13import math
 14import arcade
 15
 16WINDOW_WIDTH = 1280
 17WINDOW_HEIGHT = 720
 18WINDOW_TITLE = "Snow"
 19SNOWFLAKE_COUNT = 800
 20
 21
 22class Snowflake(arcade.SpriteCircle):
 23    """
 24    Each instance of this class represents a single snowflake.
 25    Based on drawing filled-circles.
 26    """
 27
 28    def __init__(self, size, speed, drift, center_x: float = 0, center_y: float = 0):
 29        super().__init__(size, arcade.color.WHITE, center_x=center_x, center_y=center_y)
 30        self.speed = speed
 31        self.drift = drift
 32
 33    def reset_pos(self):
 34        # Reset flake to random position above screen
 35        self.position = (
 36            random.randrange(WINDOW_WIDTH),
 37            random.randrange(WINDOW_HEIGHT, WINDOW_HEIGHT + 100),
 38        )
 39
 40    def update(self, delta_time: float = 1 / 60) -> None:
 41        self.center_y -= self.speed * delta_time
 42
 43        # Check if snowflake has fallen below screen
 44        if self.center_y < 0:
 45            self.reset_pos()
 46
 47        # Some math to make the snowflakes move side to side
 48        self.center_x += self.speed * math.cos(self.drift) * delta_time
 49        self.drift += 1 * delta_time
 50
 51
 52class GameView(arcade.View):
 53    """Main application class."""
 54
 55    def __init__(self):
 56        """Initializer"""
 57        # Calls "__init__" of parent class (arcade.Window) to setup screen
 58        super().__init__()
 59
 60        self.snowflake_list = arcade.SpriteList()
 61
 62        # Don't show the mouse pointer
 63        self.window.set_mouse_visible(False)
 64
 65        # Set the background color
 66        self.background_color = arcade.color.BLACK
 67
 68    def start_snowfall(self):
 69        """Set up snowfall and initialize variables."""
 70        for i in range(SNOWFLAKE_COUNT):
 71            # Create snowflake instance
 72            snowflake = Snowflake(
 73                size=random.randrange(1, 4),
 74                speed=random.randrange(20, 40),
 75                drift=random.uniform(math.pi, math.pi * 2),
 76                # Randomly position snowflake
 77                center_x=random.randrange(WINDOW_WIDTH),
 78                center_y=random.randrange(WINDOW_HEIGHT + 200),
 79            )
 80            # Add snowflake to snowflake list
 81            self.snowflake_list.append(snowflake)
 82
 83    def on_draw(self):
 84        """Render the screen."""
 85        # Clear the screen to the background color
 86        self.clear()
 87
 88        # Draw the current position of each snowflake
 89        self.snowflake_list.draw()
 90
 91    def on_update(self, delta_time):
 92        """All the logic to move, and the game logic goes here."""
 93        # Call update on all the snowflakes
 94        self.snowflake_list.update(delta_time)
 95
 96
 97def main():
 98    """Main function"""
 99    # Create a window class. This is what actually shows up on screen
100    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
101
102    # Create and setup the GameView
103    game = GameView()
104    game.start_snowfall()
105
106    # Show GameView on screen
107    window.show_view(game)
108
109    # Start the arcade game loop
110    arcade.run()
111
112
113if __name__ == "__main__":
114    main()