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