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