|
| 1 | +# Basic arcade shooter |
| 2 | +# |
| 3 | + |
| 4 | +# Imports |
| 5 | +import arcade |
| 6 | +import random |
| 7 | + |
| 8 | +# Constants |
| 9 | +SCREEN_WIDTH = 800 |
| 10 | +SCREEN_HEIGHT = 600 |
| 11 | +SCREEN_TITLE = "Arcade Space Shooter" |
| 12 | +SCALING = 2.0 |
| 13 | + |
| 14 | +# Classes |
| 15 | + |
| 16 | + |
| 17 | +class FlyingSprite(arcade.Sprite): |
| 18 | + """Base class for all flying sprites |
| 19 | + Flying sprites include enemies and clouds |
| 20 | + """ |
| 21 | + |
| 22 | + def update(self): |
| 23 | + """Update the position of the sprite |
| 24 | + When it moves off screen to the left, remove it |
| 25 | + """ |
| 26 | + |
| 27 | + # Move the sprite |
| 28 | + super().update() |
| 29 | + |
| 30 | + # Remove us if we're off screen |
| 31 | + if self.right < 0: |
| 32 | + self.kill() |
| 33 | + |
| 34 | + |
| 35 | +class SpaceShooter(arcade.Window): |
| 36 | + """Space Shooter side scroller game |
| 37 | + Player starts on the left, enemies appear on the right |
| 38 | + Player can move anywhere, but not off screen |
| 39 | + Enemies fly to the left at variable speed |
| 40 | + Collisions end the game |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__(self, width, height, title): |
| 44 | + """Initialize the game |
| 45 | + """ |
| 46 | + super().__init__(width, height, title) |
| 47 | + |
| 48 | + # Setup the empty sprite lists |
| 49 | + self.enemies_list = arcade.SpriteList() |
| 50 | + self.clouds_list = arcade.SpriteList() |
| 51 | + self.all_sprites = arcade.SpriteList() |
| 52 | + |
| 53 | + def setup(self): |
| 54 | + """Get the game ready to play |
| 55 | + """ |
| 56 | + |
| 57 | + # Set the background color |
| 58 | + arcade.set_background_color(arcade.color.SKY_BLUE) |
| 59 | + |
| 60 | + # Setup the player |
| 61 | + self.player = arcade.Sprite("images/jet.png", SCALING) |
| 62 | + self.player.center_y = self.height / 2 |
| 63 | + self.player.left = 10 |
| 64 | + self.all_sprites.append(self.player) |
| 65 | + |
| 66 | + # Spawn a new enemy every second |
| 67 | + arcade.schedule(self.add_enemy, 1.0) |
| 68 | + |
| 69 | + # Spawn a new cloud every 3 seconds |
| 70 | + arcade.schedule(self.add_cloud, 3.0) |
| 71 | + |
| 72 | + # Load our background music |
| 73 | + # Sound source: http://ccmixter.org/files/Apoxode/59262 |
| 74 | + # License: https://creativecommons.org/licenses/by/3.0/ |
| 75 | + self.background_music = arcade.load_sound( |
| 76 | + "sounds/Apoxode_-_Electric_1.wav" |
| 77 | + ) |
| 78 | + |
| 79 | + # Load our other sounds |
| 80 | + # Sound sources: Jon Fincher |
| 81 | + self.collision_sound = arcade.load_sound("sounds/Collision.wav") |
| 82 | + self.move_up_sound = arcade.load_sound("sounds/Rising_putter.wav") |
| 83 | + self.move_down_sound = arcade.load_sound("sounds/Falling_putter.wav") |
| 84 | + |
| 85 | + # Start the background music |
| 86 | + arcade.play_sound(self.background_music) |
| 87 | + |
| 88 | + # Unpause everything and reset the collision timer |
| 89 | + self.paused = False |
| 90 | + self.collided = False |
| 91 | + self.collision_timer = 0.0 |
| 92 | + |
| 93 | + def add_enemy(self, delta_time: float): |
| 94 | + """Adds a new enemy to the screen |
| 95 | + |
| 96 | + Arguments: |
| 97 | + delta_time {float} -- How much time as passed since the last call |
| 98 | + """ |
| 99 | + |
| 100 | + # First, create the new enemy sprite |
| 101 | + enemy = FlyingSprite("images/missile.png", SCALING) |
| 102 | + |
| 103 | + # Set its position to a random height and off screen right |
| 104 | + enemy.left = random.randint(self.width, self.width + 10) |
| 105 | + enemy.top = random.randint(10, self.height - 10) |
| 106 | + |
| 107 | + # Set its speed to a random speed heading left |
| 108 | + enemy.velocity = (random.randint(-200, -50), 0) |
| 109 | + |
| 110 | + # Add it to the enemies list |
| 111 | + self.enemies_list.append(enemy) |
| 112 | + self.all_sprites.append(enemy) |
| 113 | + |
| 114 | + def add_cloud(self, delta_time: float): |
| 115 | + """Adds a new cloud to the screen |
| 116 | + |
| 117 | + Arguments: |
| 118 | + delta_time {float} -- How much time has passed since the last call |
| 119 | + """ |
| 120 | + # First, create the new cloud sprite |
| 121 | + cloud = FlyingSprite("images/cloud.png", SCALING) |
| 122 | + |
| 123 | + # Set its position to a random height and off screen right |
| 124 | + cloud.left = random.randint(self.width, self.width + 10) |
| 125 | + cloud.top = random.randint(10, self.height - 10) |
| 126 | + |
| 127 | + # Set its speed to a random speed heading left |
| 128 | + cloud.velocity = (random.randint(-50, -20), 0) |
| 129 | + |
| 130 | + # Add it to the enemies list |
| 131 | + self.clouds_list.append(cloud) |
| 132 | + self.all_sprites.append(cloud) |
| 133 | + |
| 134 | + def on_key_press(self, symbol, modifiers): |
| 135 | + """Handle user keyboard input |
| 136 | + Q: Quit the game |
| 137 | + P: Pause the game |
| 138 | + I/J/K/L: Move Up, Left, Down, Right |
| 139 | + Arrows: Move Up, Left, Down, Right |
| 140 | + |
| 141 | + Arguments: |
| 142 | + symbol {int} -- Which key was pressed |
| 143 | + modifiers {int} -- Which modifiers were pressed |
| 144 | + """ |
| 145 | + if symbol == arcade.key.Q: |
| 146 | + # Quit immediately |
| 147 | + arcade.close_window() |
| 148 | + |
| 149 | + if symbol == arcade.key.P: |
| 150 | + self.paused = not self.paused |
| 151 | + |
| 152 | + if symbol == arcade.key.I or symbol == arcade.key.UP: |
| 153 | + self.player.change_y = 250 |
| 154 | + arcade.play_sound(self.move_up_sound) |
| 155 | + |
| 156 | + if symbol == arcade.key.K or symbol == arcade.key.DOWN: |
| 157 | + self.player.change_y = -250 |
| 158 | + arcade.play_sound(self.move_down_sound) |
| 159 | + |
| 160 | + if symbol == arcade.key.J or symbol == arcade.key.LEFT: |
| 161 | + self.player.change_x = -250 |
| 162 | + |
| 163 | + if symbol == arcade.key.L or symbol == arcade.key.RIGHT: |
| 164 | + self.player.change_x = 250 |
| 165 | + |
| 166 | + def on_key_release(self, symbol: int, modifiers: int): |
| 167 | + """Undo movement vectors when movement keys are released |
| 168 | + |
| 169 | + Arguments: |
| 170 | + symbol {int} -- Which key was pressed |
| 171 | + modifiers {int} -- Which modifiers were pressed |
| 172 | + """ |
| 173 | + if ( |
| 174 | + symbol == arcade.key.I |
| 175 | + or symbol == arcade.key.K |
| 176 | + or symbol == arcade.key.UP |
| 177 | + or symbol == arcade.key.DOWN |
| 178 | + ): |
| 179 | + self.player.change_y = 0 |
| 180 | + |
| 181 | + if ( |
| 182 | + symbol == arcade.key.J |
| 183 | + or symbol == arcade.key.L |
| 184 | + or symbol == arcade.key.LEFT |
| 185 | + or symbol == arcade.key.RIGHT |
| 186 | + ): |
| 187 | + self.player.change_x = 0 |
| 188 | + |
| 189 | + def on_update(self, delta_time: float): |
| 190 | + """Update the positions and statuses of all game objects |
| 191 | + If we're paused, do nothing |
| 192 | + Once everything has moved, check for collisions between |
| 193 | + the player and the list of enemies |
| 194 | + |
| 195 | + Arguments: |
| 196 | + delta_time {float} -- Time since the last update |
| 197 | + """ |
| 198 | + |
| 199 | + # Did we collide with something earlier? If so, update our timer |
| 200 | + if self.collided: |
| 201 | + self.collision_timer += delta_time |
| 202 | + # If we've paused for two seconds, we can quit |
| 203 | + if self.collision_timer > 2.0: |
| 204 | + arcade.close_window() |
| 205 | + # Stop updating things as well |
| 206 | + return |
| 207 | + |
| 208 | + # If we're paused, don't update anything |
| 209 | + if self.paused: |
| 210 | + return |
| 211 | + |
| 212 | + # Did we hit anything? If so, end the game |
| 213 | + if len(self.player.collides_with_list(self.enemies_list)) > 0: |
| 214 | + self.collided = True |
| 215 | + self.collision_timer = 0.0 |
| 216 | + arcade.play_sound(self.collision_sound) |
| 217 | + |
| 218 | + # Update everything |
| 219 | + for sprite in self.all_sprites: |
| 220 | + sprite.center_x = int( |
| 221 | + sprite.center_x + sprite.change_x * delta_time |
| 222 | + ) |
| 223 | + sprite.center_y = int( |
| 224 | + sprite.center_y + sprite.change_y * delta_time |
| 225 | + ) |
| 226 | + # self.all_sprites.update() |
| 227 | + |
| 228 | + # Keep the player on screen |
| 229 | + if self.player.top > self.height: |
| 230 | + self.player.top = self.height |
| 231 | + if self.player.right > self.width: |
| 232 | + self.player.right = self.width |
| 233 | + if self.player.bottom < 0: |
| 234 | + self.player.bottom = 0 |
| 235 | + if self.player.left < 0: |
| 236 | + self.player.left = 0 |
| 237 | + |
| 238 | + def on_draw(self): |
| 239 | + """Draw all game objects |
| 240 | + """ |
| 241 | + |
| 242 | + arcade.start_render() |
| 243 | + self.all_sprites.draw() |
| 244 | + |
| 245 | + |
| 246 | +if __name__ == "__main__": |
| 247 | + # Create a new Space Shooter window |
| 248 | + space_game = SpaceShooter( |
| 249 | + int(SCREEN_WIDTH * SCALING), int(SCREEN_HEIGHT * SCALING), SCREEN_TITLE |
| 250 | + ) |
| 251 | + # Setup to play |
| 252 | + space_game.setup() |
| 253 | + # Run the game |
| 254 | + arcade.run() |
0 commit comments