让敌人瞄准玩家#

sprite_bullets_enemy_aims.py#
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
10
11SCREEN_WIDTH = 800
12SCREEN_HEIGHT = 600
13SCREEN_TITLE = "Sprites and Bullets Enemy Aims Example"
14BULLET_SPEED = 4
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
27 self.enemy_list = None
28 self.bullet_list = None
29 self.player_list = None
30 self.player = None
31
32 def setup(self):
33 self.enemy_list = arcade.SpriteList()
34 self.bullet_list = arcade.SpriteList()
35 self.player_list = arcade.SpriteList()
36
37 # Add player ship
38 self.player = arcade.Sprite(":resources:images/space_shooter/playerShip1_orange.png", scale=0.5)
39 self.player_list.append(self.player)
40
41 # Add top-left enemy ship
42 enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", scale=0.5)
43 enemy.center_x = 120
44 enemy.center_y = SCREEN_HEIGHT - enemy.height
45 enemy.angle = 180
46 self.enemy_list.append(enemy)
47
48 # Add top-right enemy ship
49 enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", scale=0.5)
50 enemy.center_x = SCREEN_WIDTH - 120
51 enemy.center_y = SCREEN_HEIGHT - enemy.height
52 enemy.angle = 180
53 self.enemy_list.append(enemy)
54
55 def on_draw(self):
56 """Render the screen. """
57
58 self.clear()
59
60 self.enemy_list.draw()
61 self.bullet_list.draw()
62 self.player_list.draw()
63
64 def on_update(self, delta_time):
65 """All the logic to move, and the game logic goes here. """
66
67 self.frame_count += 1
68
69 # Loop through each enemy that we have
70 for enemy in self.enemy_list:
71
72 # First, calculate the angle to the player. We could do this
73 # only when the bullet fires, but in this case we will rotate
74 # the enemy to face the player each frame, so we'll do this
75 # each frame.
76
77 # Position the start at the enemy's current location
78 start_x = enemy.center_x
79 start_y = enemy.center_y
80
81 # Get the destination location for the bullet
82 dest_x = self.player.center_x
83 dest_y = self.player.center_y
84
85 # Do math to calculate how to get the bullet to the destination.
86 # Calculation the angle in radians between the start points
87 # and end points. This is the angle the bullet will travel.
88 x_diff = dest_x - start_x
89 y_diff = dest_y - start_y
90 angle = -math.atan2(y_diff, x_diff) + 3.14 / 2
91
92 # Set the enemy to face the player.
93 enemy.angle = math.degrees(angle)
94
95 # Shoot every 60 frames change of shooting each frame
96 if self.frame_count % 60 == 0:
97 bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png")
98 bullet.center_x = start_x
99 bullet.center_y = start_y
100
101 # Angle the bullet sprite
102 bullet.angle = math.degrees(angle) - 90
103
104 # Taking into account the angle, calculate our change_x
105 # and change_y. Velocity is how fast the bullet travels.
106 bullet.change_x = math.sin(angle) * BULLET_SPEED
107 bullet.change_y = math.cos(angle) * BULLET_SPEED
108
109 self.bullet_list.append(bullet)
110
111 # Get rid of the bullet when it flies off-screen
112 for bullet in self.bullet_list:
113 if bullet.top < 0:
114 bullet.remove_from_sprite_lists()
115
116 self.bullet_list.update()
117
118 def on_mouse_motion(self, x, y, delta_x, delta_y):
119 """Called whenever the mouse moves. """
120 self.player.center_x = x
121 self.player.center_y = y
122
123
124def main():
125 """ Main function """
126 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
127 window.setup()
128 arcade.run()
129
130
131if __name__ == "__main__":
132 main()