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 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_no_coins_on_walls
11"""
12import arcade
13import random
14import os
15
16SPRITE_SCALING = 0.5
17SPRITE_SCALING_COIN = 0.2
18
19SCREEN_WIDTH = 800
20SCREEN_HEIGHT = 600
21SCREEN_TITLE = "Sprite No Coins on Walls Example"
22
23NUMBER_OF_COINS = 50
24
25MOVEMENT_SPEED = 5
26
27
28class MyGame(arcade.Window):
29 """ Main application class. """
30
31 def __init__(self, width, height, title):
32 """
33 Initializer
34 """
35 super().__init__(width, height, title)
36
37 # Set the working directory (where we expect to find files) to the same
38 # directory this .py file is in. You can leave this out of your own
39 # code, but it is needed to easily run the examples using "python -m"
40 # as mentioned at the top of this program.
41 file_path = os.path.dirname(os.path.abspath(__file__))
42 os.chdir(file_path)
43
44 # Sprite lists
45 self.all_sprites_list = None
46 self.coin_list = None
47
48 # Set up the player
49 self.player_sprite = None
50 self.wall_list = None
51 self.physics_engine = None
52
53 def setup(self):
54 """ Set up the game and initialize the variables. """
55
56 # Sprite lists
57 self.all_sprites_list = arcade.SpriteList()
58 self.wall_list = arcade.SpriteList()
59 self.coin_list = arcade.SpriteList()
60
61 # Set up the player
62 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
63 self.player_sprite.center_x = 50
64 self.player_sprite.center_y = 64
65
66 # -- Set up the walls
67 # Create a series of horizontal walls
68 for y in range(0, 800, 200):
69 for x in range(100, 700, 64):
70 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
71 wall.center_x = x
72 wall.center_y = y
73 self.wall_list.append(wall)
74
75 # -- Randomly place coins where there are no walls
76 # Create the coins
77 for i in range(NUMBER_OF_COINS):
78
79 # Create the coin instance
80 # Coin image from kenney.nl
81 coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
82
83 # --- IMPORTANT PART ---
84
85 # Boolean variable if we successfully placed the coin
86 coin_placed_successfully = False
87
88 # Keep trying until success
89 while not coin_placed_successfully:
90 # Position the coin
91 coin.center_x = random.randrange(SCREEN_WIDTH)
92 coin.center_y = random.randrange(SCREEN_HEIGHT)
93
94 # See if the coin is hitting a wall
95 wall_hit_list = arcade.check_for_collision_with_list(coin, self.wall_list)
96
97 # See if the coin is hitting another coin
98 coin_hit_list = arcade.check_for_collision_with_list(coin, self.coin_list)
99
100 if len(wall_hit_list) == 0 and len(coin_hit_list) == 0:
101 # It is!
102 coin_placed_successfully = True
103
104 # Add the coin to the lists
105 self.coin_list.append(coin)
106
107 # --- END OF IMPORTANT PART ---
108
109 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
110
111 # Set the background color
112 arcade.set_background_color(arcade.color.AMAZON)
113
114 def on_draw(self):
115 """
116 Render the screen.
117 """
118
119 # This command has to happen before we start drawing
120 arcade.start_render()
121
122 # Draw all the sprites.
123 self.wall_list.draw()
124 self.coin_list.draw()
125 self.player_sprite.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 method """
157 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
158 window.setup()
159 arcade.run()
160
161
162if __name__ == "__main__":
163 main()