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
 12from __future__ import annotations
 13
 14import random
 15import math
 16import arcade
 17
 18SCREEN_WIDTH = 800
 19SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Snow"
 21
 22
 23class Snowflake:
 24    """
 25    Each instance of this class represents a single snowflake.
 26    Based on drawing filled-circles.
 27    """
 28
 29    def __init__(self):
 30        self.x = 0
 31        self.y = 0
 32
 33    def reset_pos(self):
 34        # Reset flake to random position above screen
 35        self.y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT + 100)
 36        self.x = random.randrange(SCREEN_WIDTH)
 37
 38
 39class MyGame(arcade.Window):
 40    """ Main application class. """
 41
 42    def __init__(self, width, height, title):
 43        """ Initializer """
 44        # Calls "__init__" of parent class (arcade.Window) to setup screen
 45        super().__init__(width, height, title)
 46
 47        # Sprite lists
 48        self.snowflake_list = None
 49
 50    def start_snowfall(self):
 51        """ Set up snowfall and initialize variables. """
 52        self.snowflake_list = []
 53
 54        for i in range(50):
 55            # Create snowflake instance
 56            snowflake = Snowflake()
 57
 58            # Randomly position snowflake
 59            snowflake.x = random.randrange(SCREEN_WIDTH)
 60            snowflake.y = random.randrange(SCREEN_HEIGHT + 200)
 61
 62            # Set other variables for the snowflake
 63            snowflake.size = random.randrange(4)
 64            snowflake.speed = random.randrange(20, 40)
 65            snowflake.angle = random.uniform(math.pi, math.pi * 2)
 66
 67            # Add snowflake to snowflake list
 68            self.snowflake_list.append(snowflake)
 69
 70        # Don't show the mouse pointer
 71        self.set_mouse_visible(False)
 72
 73        # Set the background color
 74        self.background_color = arcade.color.BLACK
 75
 76    def on_draw(self):
 77        """ Render the screen. """
 78
 79        # This command is necessary before drawing
 80        self.clear()
 81
 82        # Draw the current position of each snowflake
 83        for snowflake in self.snowflake_list:
 84            arcade.draw_circle_filled(snowflake.x, snowflake.y,
 85                                      snowflake.size, arcade.color.WHITE)
 86
 87    def on_update(self, delta_time):
 88        """ All the logic to move, and the game logic goes here. """
 89
 90        # Animate all the snowflakes falling
 91        for snowflake in self.snowflake_list:
 92            snowflake.y -= snowflake.speed * delta_time
 93
 94            # Check if snowflake has fallen below screen
 95            if snowflake.y < 0:
 96                snowflake.reset_pos()
 97
 98            # Some math to make the snowflakes move side to side
 99            snowflake.x += snowflake.speed * math.cos(snowflake.angle) * delta_time
100            snowflake.angle += 1 * delta_time
101
102
103def main():
104    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
105    window.start_snowfall()
106    arcade.run()
107
108
109if __name__ == "__main__":
110    main()