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