Sprite Bouncing Coins

Screen shot of simple bouncing coins
sprite_bouncing_coins.py
  1"""
  2Sprite Simple Bouncing
  3
  4Simple program to show how to bounce items.
  5This only works for straight vertical and horizontal angles.
  6
  7Artwork from http://kenney.nl
  8
  9If Python and Arcade are installed, this example can be run from the command line with:
 10python -m arcade.examples.sprite_bouncing_coins
 11"""
 12
 13import arcade
 14import os
 15import random
 16
 17SPRITE_SCALING = 0.5
 18
 19SCREEN_WIDTH = 832
 20SCREEN_HEIGHT = 632
 21SCREEN_TITLE = "Sprite Bouncing Coins"
 22
 23MOVEMENT_SPEED = 5
 24
 25
 26class MyGame(arcade.Window):
 27    """ Main application class. """
 28
 29    def __init__(self, width, height, title):
 30        """
 31        Initializer
 32        """
 33        super().__init__(width, height, title)
 34
 35        # Set the working directory (where we expect to find files) to the same
 36        # directory this .py file is in. You can leave this out of your own
 37        # code, but it is needed to easily run the examples using "python -m"
 38        # as mentioned at the top of this program.
 39        file_path = os.path.dirname(os.path.abspath(__file__))
 40        os.chdir(file_path)
 41
 42        # Sprite lists
 43        self.coin_list = None
 44        self.wall_list = None
 45
 46    def setup(self):
 47        """ Set up the game and initialize the variables. """
 48
 49        # Sprite lists
 50        self.wall_list = arcade.SpriteList()
 51        self.coin_list = arcade.SpriteList()
 52
 53        # -- Set up the walls
 54
 55        # Create horizontal rows of boxes
 56        for x in range(32, SCREEN_WIDTH, 64):
 57            # Bottom edge
 58            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 59            wall.center_x = x
 60            wall.center_y = 32
 61            self.wall_list.append(wall)
 62
 63            # Top edge
 64            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 65            wall.center_x = x
 66            wall.center_y = SCREEN_HEIGHT - 32
 67            self.wall_list.append(wall)
 68
 69        # Create vertical columns of boxes
 70        for y in range(96, SCREEN_HEIGHT, 64):
 71            # Left
 72            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 73            wall.center_x = 32
 74            wall.center_y = y
 75            self.wall_list.append(wall)
 76
 77            # Right
 78            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 79            wall.center_x = SCREEN_WIDTH - 32
 80            wall.center_y = y
 81            self.wall_list.append(wall)
 82
 83        # Create boxes in the middle
 84        for x in range(128, SCREEN_WIDTH, 196):
 85            for y in range(128, SCREEN_HEIGHT, 196):
 86                wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 87                wall.center_x = x
 88                wall.center_y = y
 89                # wall.angle = 45
 90                self.wall_list.append(wall)
 91
 92        # Create coins
 93        for i in range(10):
 94            coin = arcade.Sprite(":resources:images/items/coinGold.png", 0.25)
 95            coin.center_x = random.randrange(100, 700)
 96            coin.center_y = random.randrange(100, 500)
 97            while coin.change_x == 0 and coin.change_y == 0:
 98                coin.change_x = random.randrange(-4, 5)
 99                coin.change_y = random.randrange(-4, 5)
100
101            self.coin_list.append(coin)
102
103        # Set the background color
104        arcade.set_background_color(arcade.color.AMAZON)
105
106    def on_draw(self):
107        """
108        Render the screen.
109        """
110
111        # This command has to happen before we start drawing
112        arcade.start_render()
113
114        # Draw all the sprites.
115        self.wall_list.draw()
116        self.coin_list.draw()
117
118    def on_update(self, delta_time):
119        """ Movement and game logic """
120
121        for coin in self.coin_list:
122
123            coin.center_x += coin.change_x
124            walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
125            for wall in walls_hit:
126                if coin.change_x > 0:
127                    coin.right = wall.left
128                elif coin.change_x < 0:
129                    coin.left = wall.right
130            if len(walls_hit) > 0:
131                coin.change_x *= -1
132
133            coin.center_y += coin.change_y
134            walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
135            for wall in walls_hit:
136                if coin.change_y > 0:
137                    coin.top = wall.bottom
138                elif coin.change_y < 0:
139                    coin.bottom = wall.top
140            if len(walls_hit) > 0:
141                coin.change_y *= -1
142
143
144def main():
145    """ Main method """
146    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
147    window.setup()
148    arcade.run()
149
150
151if __name__ == "__main__":
152    main()