Falling Snow

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