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 https://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
 13from __future__ import annotations
 14
 15import arcade
 16import random
 17
 18SPRITE_SCALING = 0.5
 19
 20SCREEN_WIDTH = 832
 21SCREEN_HEIGHT = 632
 22SCREEN_TITLE = "Sprite Bouncing Coins"
 23
 24MOVEMENT_SPEED = 5
 25
 26
 27class MyGame(arcade.Window):
 28    """ Main application class. """
 29
 30    def __init__(self, width, height, title):
 31        """
 32        Initializer
 33        """
 34        super().__init__(width, height, title)
 35
 36        # Sprite lists
 37        self.coin_list = None
 38        self.wall_list = None
 39
 40    def setup(self):
 41        """ Set up the game and initialize the variables. """
 42
 43        # Sprite lists
 44        self.wall_list = arcade.SpriteList()
 45        self.coin_list = arcade.SpriteList()
 46
 47        # -- Set up the walls
 48
 49        # Create horizontal rows of boxes
 50        for x in range(32, SCREEN_WIDTH, 64):
 51            # Bottom edge
 52            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
 53            wall.center_x = x
 54            wall.center_y = 32
 55            self.wall_list.append(wall)
 56
 57            # Top edge
 58            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
 59            wall.center_x = x
 60            wall.center_y = SCREEN_HEIGHT - 32
 61            self.wall_list.append(wall)
 62
 63        # Create vertical columns of boxes
 64        for y in range(96, SCREEN_HEIGHT, 64):
 65            # Left
 66            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
 67            wall.center_x = 32
 68            wall.center_y = y
 69            self.wall_list.append(wall)
 70
 71            # Right
 72            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
 73            wall.center_x = SCREEN_WIDTH - 32
 74            wall.center_y = y
 75            self.wall_list.append(wall)
 76
 77        # Create boxes in the middle
 78        for x in range(128, SCREEN_WIDTH, 196):
 79            for y in range(128, SCREEN_HEIGHT, 196):
 80                wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
 81                wall.center_x = x
 82                wall.center_y = y
 83                # wall.angle = 45
 84                self.wall_list.append(wall)
 85
 86        # Create coins
 87        for i in range(10):
 88            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=0.25)
 89            coin.center_x = random.randrange(100, 700)
 90            coin.center_y = random.randrange(100, 500)
 91            while coin.change_x == 0 and coin.change_y == 0:
 92                coin.change_x = random.randrange(-4, 5)
 93                coin.change_y = random.randrange(-4, 5)
 94
 95            self.coin_list.append(coin)
 96
 97        # Set the background color
 98        self.background_color = arcade.color.AMAZON
 99
100    def on_draw(self):
101        """
102        Render the screen.
103        """
104
105        # This command has to happen before we start drawing
106        self.clear()
107
108        # Draw all the sprites.
109        self.wall_list.draw()
110        self.coin_list.draw()
111
112    def on_update(self, delta_time):
113        """ Movement and game logic """
114
115        for coin in self.coin_list:
116
117            coin.center_x += coin.change_x
118            walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
119            for wall in walls_hit:
120                if coin.change_x > 0:
121                    coin.right = wall.left
122                elif coin.change_x < 0:
123                    coin.left = wall.right
124            if len(walls_hit) > 0:
125                coin.change_x *= -1
126
127            coin.center_y += coin.change_y
128            walls_hit = arcade.check_for_collision_with_list(coin, self.wall_list)
129            for wall in walls_hit:
130                if coin.change_y > 0:
131                    coin.top = wall.bottom
132                elif coin.change_y < 0:
133                    coin.bottom = wall.top
134            if len(walls_hit) > 0:
135                coin.change_y *= -1
136
137
138def main():
139    """ Main function """
140    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
141    window.setup()
142    arcade.run()
143
144
145if __name__ == "__main__":
146    main()