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):
 29        super().__init__(size, arcade.color.WHITE)
 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            )
 77            # Randomly position snowflake
 78            snowflake.position = (
 79                random.randrange(WINDOW_WIDTH),
 80                random.randrange(WINDOW_HEIGHT + 200),
 81            )
 82            # Add snowflake to snowflake list
 83            self.snowflake_list.append(snowflake)
 84
 85    def on_draw(self):
 86        """ Render the screen. """
 87        # Clear the screen to the background color
 88        self.clear()
 89
 90        # Draw the current position of each snowflake
 91        self.snowflake_list.draw()
 92
 93    def on_update(self, delta_time):
 94        """ All the logic to move, and the game logic goes here. """
 95        # Call update on all the snowflakes
 96        self.snowflake_list.update(delta_time)
 97
 98
 99def main():
100    """ Main function """
101    # Create a window class. This is what actually shows up on screen
102    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
103
104    # Create and setup the GameView
105    game = GameView()
106    game.start_snowfall()
107
108    # Show GameView on screen
109    window.show_view(game)
110
111    # Start the arcade game loop
112    arcade.run()
113
114
115if __name__ == "__main__":
116    main()