Have Enemies Randomly Shoot#

Having enemies randomly shoot is easier than periodically shooting enemies as shown in Have Enemies Periodically Shoot. This is because we don’t have to track how long it has been since we last fired.

See the highlighted lines for what is specific to this example.

Screenshot of using sprites to shoot things
sprite_bullets_random.py#
  1"""
  2Show how to have enemies shoot bullets at random intervals.
  3
  4If Python and Arcade are installed, this example can be run from the command line with:
  5python -m arcade.examples.sprite_bullets_random
  6"""
  7from __future__ import annotations
  8
  9import arcade
 10import random
 11
 12SCREEN_WIDTH = 800
 13SCREEN_HEIGHT = 600
 14SCREEN_TITLE = "Sprites and Random Bullets Example"
 15
 16
 17class MyGame(arcade.Window):
 18    """ Main application class """
 19
 20    def __init__(self, width, height, title):
 21        super().__init__(width, height, title)
 22
 23        self.background_color = arcade.color.BLACK
 24
 25        self.frame_count = 0
 26        self.player_list = None
 27        self.enemy_list = None
 28        self.bullet_list = None
 29
 30        self.player = None
 31
 32    def setup(self):
 33        """ Setup the variables for the game. """
 34        self.player_list = arcade.SpriteList()
 35        self.enemy_list = arcade.SpriteList()
 36        self.bullet_list = arcade.SpriteList()
 37
 38        # Add player ship
 39        self.player = arcade.Sprite(":resources:images/space_shooter/playerShip1_orange.png", scale=0.5)
 40        self.player_list.append(self.player)
 41
 42        # Add top-left enemy ship
 43        enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", scale=0.5)
 44        enemy.center_x = 120
 45        enemy.center_y = SCREEN_HEIGHT - enemy.height
 46        enemy.angle = 180
 47        self.enemy_list.append(enemy)
 48
 49        # Add top-right enemy ship
 50        enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", scale=0.5)
 51        enemy.center_x = SCREEN_WIDTH - 120
 52        enemy.center_y = SCREEN_HEIGHT - enemy.height
 53        enemy.angle = 180
 54        self.enemy_list.append(enemy)
 55
 56    def on_draw(self):
 57        """Render the screen. """
 58
 59        self.clear()
 60
 61        self.enemy_list.draw()
 62        self.bullet_list.draw()
 63        self.player_list.draw()
 64
 65    def on_update(self, delta_time):
 66        """All the logic to move, and the game logic goes here. """
 67
 68        # Loop through each enemy that we have
 69        for enemy in self.enemy_list:
 70
 71            # Have a random 1 in 200 change of shooting each 1/60th of a second
 72            odds = 200
 73
 74            # Adjust odds based on delta-time
 75            adj_odds = int(odds * (1 / 60) / delta_time)
 76
 77            if random.randrange(adj_odds) == 0:
 78                bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png")
 79                bullet.center_x = enemy.center_x
 80                bullet.angle = 90
 81                bullet.top = enemy.bottom
 82                bullet.change_y = -2
 83                self.bullet_list.append(bullet)
 84
 85        # Get rid of the bullet when it flies off-screen
 86        for bullet in self.bullet_list:
 87            if bullet.top < 0:
 88                bullet.remove_from_sprite_lists()
 89
 90        self.bullet_list.update()
 91
 92    def on_mouse_motion(self, x, y, delta_x, delta_y):
 93        """ Called whenever the mouse moves. """
 94        self.player.center_x = x
 95        self.player.center_y = 20
 96
 97
 98def main():
 99    """ Main function """
100    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
101    window.setup()
102    arcade.run()
103
104
105if __name__ == "__main__":
106    main()