1"""
2Show how to have enemies shoot bullets aimed at the player.
3
4If Python and Arcade are installed, this example can be run from the command line with:
5python -m arcade.examples.sprite_bullets_enemy_aims
6"""
7
8import arcade
9import math
10import os
11
12SCREEN_WIDTH = 800
13SCREEN_HEIGHT = 600
14SCREEN_TITLE = "Sprites and Bullets Enemy Aims Example"
15BULLET_SPEED = 4
16
17
18class MyGame(arcade.Window):
19 """ Main application class """
20
21 def __init__(self, width, height, title):
22 super().__init__(width, height, title)
23
24 # Set the working directory (where we expect to find files) to the same
25 # directory this .py file is in. You can leave this out of your own
26 # code, but it is needed to easily run the examples using "python -m"
27 # as mentioned at the top of this program.
28 file_path = os.path.dirname(os.path.abspath(__file__))
29 os.chdir(file_path)
30
31 arcade.set_background_color(arcade.color.BLACK)
32
33 self.frame_count = 0
34
35 self.enemy_list = None
36 self.bullet_list = None
37 self.player_list = None
38 self.player = None
39
40 def setup(self):
41 self.enemy_list = arcade.SpriteList()
42 self.bullet_list = arcade.SpriteList()
43 self.player_list = arcade.SpriteList()
44
45 # Add player ship
46 self.player = arcade.Sprite(":resources:images/space_shooter/playerShip1_orange.png", 0.5)
47 self.player_list.append(self.player)
48
49 # Add top-left enemy ship
50 enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", 0.5)
51 enemy.center_x = 120
52 enemy.center_y = SCREEN_HEIGHT - enemy.height
53 enemy.angle = 180
54 self.enemy_list.append(enemy)
55
56 # Add top-right enemy ship
57 enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", 0.5)
58 enemy.center_x = SCREEN_WIDTH - 120
59 enemy.center_y = SCREEN_HEIGHT - enemy.height
60 enemy.angle = 180
61 self.enemy_list.append(enemy)
62
63 def on_draw(self):
64 """Render the screen. """
65
66 arcade.start_render()
67
68 self.enemy_list.draw()
69 self.bullet_list.draw()
70 self.player_list.draw()
71
72 def on_update(self, delta_time):
73 """All the logic to move, and the game logic goes here. """
74
75 self.frame_count += 1
76
77 # Loop through each enemy that we have
78 for enemy in self.enemy_list:
79
80 # First, calculate the angle to the player. We could do this
81 # only when the bullet fires, but in this case we will rotate
82 # the enemy to face the player each frame, so we'll do this
83 # each frame.
84
85 # Position the start at the enemy's current location
86 start_x = enemy.center_x
87 start_y = enemy.center_y
88
89 # Get the destination location for the bullet
90 dest_x = self.player.center_x
91 dest_y = self.player.center_y
92
93 # Do math to calculate how to get the bullet to the destination.
94 # Calculation the angle in radians between the start points
95 # and end points. This is the angle the bullet will travel.
96 x_diff = dest_x - start_x
97 y_diff = dest_y - start_y
98 angle = math.atan2(y_diff, x_diff)
99
100 # Set the enemy to face the player.
101 enemy.angle = math.degrees(angle)-90
102
103 # Shoot every 60 frames change of shooting each frame
104 if self.frame_count % 60 == 0:
105 bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png")
106 bullet.center_x = start_x
107 bullet.center_y = start_y
108
109 # Angle the bullet sprite
110 bullet.angle = math.degrees(angle)
111
112 # Taking into account the angle, calculate our change_x
113 # and change_y. Velocity is how fast the bullet travels.
114 bullet.change_x = math.cos(angle) * BULLET_SPEED
115 bullet.change_y = math.sin(angle) * BULLET_SPEED
116
117 self.bullet_list.append(bullet)
118
119 # Get rid of the bullet when it flies off-screen
120 for bullet in self.bullet_list:
121 if bullet.top < 0:
122 bullet.remove_from_sprite_lists()
123
124 self.bullet_list.update()
125
126 def on_mouse_motion(self, x, y, delta_x, delta_y):
127 """Called whenever the mouse moves. """
128 self.player.center_x = x
129 self.player.center_y = y
130
131
132def main():
133 """ Main method """
134 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
135 window.setup()
136 arcade.run()
137
138
139if __name__ == "__main__":
140 main()