Randomly Place Coins, But Away From Walls And Other Coins

sprite_no_coins_on_walls.py
1"""
2No coins on the walls
3
4Simple program to show basic sprite usage. Specifically, create coin sprites that
5aren't on top of any walls, and don't have coins on top of each other.
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_no_coins_on_walls
11"""
12import arcade
13import random
14
15SPRITE_SCALING = 0.5
16SPRITE_SCALING_COIN = 0.2
17
18WINDOW_WIDTH = 1280
19WINDOW_HEIGHT = 720
20WINDOW_TITLE = "Sprite No Coins on Walls Example"
21
22NUMBER_OF_COINS = 50
23
24MOVEMENT_SPEED = 5
25
26
27class GameView(arcade.View):
28 """ Main application class. """
29
30 def __init__(self):
31 """
32 Initializer
33 """
34 super().__init__()
35
36 # Sprite lists
37 self.player_list = None
38 self.coin_list = None
39
40 # Set up the player
41 self.player_sprite = None
42 self.wall_list = None
43 self.physics_engine = None
44
45 def setup(self):
46 """ Set up the game and initialize the variables. """
47
48 # Sprite lists
49 self.player_list = arcade.SpriteList()
50 self.wall_list = arcade.SpriteList()
51 self.coin_list = arcade.SpriteList()
52
53 # Set up the player
54 self.player_sprite = arcade.Sprite(
55 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
56 scale=SPRITE_SCALING,
57 )
58 self.player_sprite.center_x = 50
59 self.player_sprite.center_y = 64
60 self.player_list.append(self.player_sprite)
61
62 # -- Set up the walls
63 # Create a series of horizontal walls
64 for y in range(0, 720, 200):
65 for x in range(100, 1000, 64):
66 wall = arcade.Sprite(
67 ":resources:images/tiles/boxCrate_double.png",
68 scale=SPRITE_SCALING,
69 )
70 wall.center_x = x
71 wall.center_y = y
72 self.wall_list.append(wall)
73
74 # -- Randomly place coins where there are no walls
75 # Create the coins
76 for i in range(NUMBER_OF_COINS):
77
78 # Create the coin instance
79 # Coin image from kenney.nl
80 coin = arcade.Sprite(
81 ":resources:images/items/coinGold.png",
82 scale=SPRITE_SCALING_COIN,
83 )
84
85 # --- IMPORTANT PART ---
86
87 # Boolean variable if we successfully placed the coin
88 coin_placed_successfully = False
89
90 # Keep trying until success
91 while not coin_placed_successfully:
92 # Position the coin
93 coin.center_x = random.randrange(WINDOW_WIDTH)
94 coin.center_y = random.randrange(WINDOW_HEIGHT)
95
96 # See if the coin is hitting a wall
97 wall_hit_list = arcade.check_for_collision_with_list(coin, self.wall_list)
98
99 # See if the coin is hitting another coin
100 coin_hit_list = arcade.check_for_collision_with_list(coin, self.coin_list)
101
102 if len(wall_hit_list) == 0 and len(coin_hit_list) == 0:
103 # It is!
104 coin_placed_successfully = True
105
106 # Add the coin to the lists
107 self.coin_list.append(coin)
108
109 # --- END OF IMPORTANT PART ---
110
111 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
112
113 # Set the background color
114 self.background_color = arcade.color.AMAZON
115
116 def on_draw(self):
117 """ Render the screen. """
118
119 # This command has to happen before we start drawing
120 self.clear()
121
122 # Draw all the sprites.
123 self.wall_list.draw()
124 self.coin_list.draw()
125 self.player_list.draw()
126
127 def on_key_press(self, key, modifiers):
128 """ Called whenever a key is pressed. """
129
130 if key == arcade.key.UP:
131 self.player_sprite.change_y = MOVEMENT_SPEED
132 elif key == arcade.key.DOWN:
133 self.player_sprite.change_y = -MOVEMENT_SPEED
134 elif key == arcade.key.LEFT:
135 self.player_sprite.change_x = -MOVEMENT_SPEED
136 elif key == arcade.key.RIGHT:
137 self.player_sprite.change_x = MOVEMENT_SPEED
138
139 def on_key_release(self, key, modifiers):
140 """ Called when the user releases a key. """
141
142 if key == arcade.key.UP or key == arcade.key.DOWN:
143 self.player_sprite.change_y = 0
144 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
145 self.player_sprite.change_x = 0
146
147 def on_update(self, delta_time):
148 """ Movement and game logic """
149
150 # Call update on all sprites (The sprites don't do much in this
151 # example though.)
152 self.physics_engine.update()
153
154
155def main():
156 """ Main function """
157 # Create a window class. This is what actually shows up on screen
158 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
159
160 # Create and setup the GameView
161 game = GameView()
162 game.setup()
163
164 # Show GameView on screen
165 window.show_view(game)
166
167 # Start the arcade game loop
168 arcade.run()
169
170
171if __name__ == "__main__":
172 main()