generated from StRobertCHSCS/python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminion.py
223 lines (197 loc) · 8.41 KB
/
minion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
-------------------------------------------------------------------------------
Name: minion.py
Purpose: Code for the minion enemy type.
Author: Wang.D
Created: 23/01/2020
-------------------------------------------------------------------------------
"""
import arcade, math, random
from player import Player
from arcade.draw_commands import rotate_point
from typing import Tuple
class Minion(arcade.AnimatedTimeSprite):
def __init__(self, center_x: int, center_y: int, direction="DOWN", enemy_width=32,
enemy_height=48):
"""Constructor of the Player class, that is the entity that the user will be moving controlling.
:param direction: default direction of player
:param player_speed: speed of player
:param window_width: width of game window
:param window_heigth: height of game window
"""
super().__init__()
# setting direction based on creation of Player object
self.direction = direction
self.previous_direction = None
# change animation rate
self.texture_change_frames = 30
# setting position of Player
self.center_x = center_x
self.center_y = center_y
# defining size of player for later use
self.enemy_width = enemy_width
self.enemy_height = enemy_height
self.movement = True
self.count = 0
self.hit = False
self.time = None
# create textures for animations
self.textures_left = []
self.textures_right = []
self.textures_dead = []
self.create_textures()
# spawn facing forward
self.face_direction(direction)
self.is_player_hit_already = False
self.is_player_hit = False
self.stop = False
self.health = 30
# create textures
def create_textures(self) -> None:
"""
Creates textures for enemy. By default, all entities except player will face right.
:return: none
"""
# add textures to respective locations
self.textures_right.append(arcade.load_texture("images/boss_sprite.png", mirrored=True, scale=0.1))
self.textures_right.append(arcade.load_texture("images/boss_sprite_2.png", mirrored=True, scale=0.1))
self.textures_left.append(arcade.load_texture("images/boss_sprite.png", scale=0.1))
self.textures_left.append(arcade.load_texture("images/boss_sprite_2.png", scale=0.1))
self.textures_dead.append(arcade.load_texture("images/boss_dead.png", scale=0.1))
self.textures_dead.append(arcade.load_texture("images/boss_dead.png", scale=0.1))
# animation for the player to face when it is not moving
def face_direction(self, direction) -> None:
"""
Faces the character animation based on the direction.
:param direction: direction for the character to face
:return: None
"""
if direction == "LEFT":
self.textures = self.textures_left
elif direction == "RIGHT":
self.textures = self.textures_right
elif direction == "UP" or direction == "DOWN":
self.textures = self.textures_right
else:
print("Invalid direction to face")
# animation for moving
def move_direction(self, direction) -> None:
"""
Sets animation to the direction of the player movement
:param direction: direction of player movement
:return: None
"""
if direction == "LEFT":
self.textures = self.textures_left
elif direction == "RIGHT":
self.textures = self.textures_right
elif direction == "UP" or direction == "DOWN":
self.textures = self.textures_right
else:
print("Direction not valid to move")
def follow(self, player: Player, delta_time=1 / 60) -> None:
"""
Makes enemy follow the player, engine that will run all moving sprites
Method that is called in the main.py file on_update()
:param delta_time: time of rate of execution
:param player: the player to follow
:return: none
"""
self.texture_change_frames = 30
wait = random.randint(10, 30)
if self.movement:
self.count += 1
if abs(self.center_x - player.center_x) > 5:
if self.center_x < player.center_x:
self.direction = "RIGHT"
if self.center_x > player.center_x:
self.direction = "LEFT"
else:
self.count = wait
else:
self.count += 1
if abs(self.center_y - player.center_y) > 5:
if self.center_y < player.center_y:
self.direction = "UP"
if self.center_y > player.center_y:
self.direction = "DOWN"
else:
self.count = wait
# changing the direction (up/down to left/right) every 25 loops
if self.count == wait:
self.movement = not self.movement
self.count = 0
if player.health < 1:
self.direction = None
self.move_direction("RIGHT")
if self.health < 1:
self.direction = None
self.textures = self.textures_dead
if self.direction is not None:
if self.direction == "RIGHT":
self.change_x = 2
if self.direction == "LEFT":
self.change_x = -2
if self.direction == "UP":
self.change_y = 2
if self.direction == "DOWN":
self.change_y = -2
# update direction of sprite
if self.is_player_hit:
self.is_player_hit_already = False
self.move_direction(self.direction)
else:
# update to standing animation
self.texture_change_frames = 30
def get_points(self) -> Tuple[Tuple[float, float]]:
"""
Get the corner points for the rect that makes up the sprite.
"""
if self._point_list_cache is not None:
return self._point_list_cache
if self._points is not None:
point_list = []
for point in range(len(self._points)):
point = (self._points[point][0] + self.center_x,
self._points[point][1] + self.center_y)
point_list.append(point)
self._point_list_cache = tuple(point_list)
else:
x1, y1 = rotate_point(self.center_x - self.enemy_width / 4,
self.center_y - self.enemy_height / 3,
self.center_x,
self.center_y,
self.angle)
x2, y2 = rotate_point(self.center_x + self.enemy_width / 4,
self.center_y - self.enemy_height / 3,
self.center_x,
self.center_y,
self.angle)
x3, y3 = rotate_point(self.center_x + self.enemy_width / 4,
self.center_y + self.enemy_height / 4,
self.center_x,
self.center_y,
self.angle)
x4, y4 = rotate_point(self.center_x - self.enemy_width / 4,
self.center_y + self.enemy_height / 4,
self.center_x,
self.center_y,
self.angle)
self._point_list_cache = ((x1, y1), (x2, y2), (x3, y3), (x4, y4))
return self._point_list_cache
points = property(get_points, arcade.Sprite.set_points)
def update_animation(self, player: Player):
"""
Logic for selecting the proper texture to use.
"""
if self.frame % self.texture_change_frames == 0:
if self.is_player_hit:
if not self.is_player_hit_already:
player.health -= 0.25
self.is_player_hit_already = True
self.is_player_hit = False
self.cur_texture_index += 1
if self.cur_texture_index >= len(self.textures):
self.cur_texture_index = 0
self.set_texture(self.cur_texture_index)
self.frame += 1