generated from StRobertCHSCS/python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage.py
222 lines (201 loc) · 8.12 KB
/
mage.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
"""
-------------------------------------------------------------------------------
Name: mage.py
Purpose: Code for the mage enemy type.
Author: Wang.D
Created: 01/01/2020
-------------------------------------------------------------------------------
"""
from typing import Tuple
import arcade
import math
from player import Player
class Mage(arcade.Sprite):
def __init__(self, center_x, center_y, width, height):
"""
Wizard tower that shoots towards player
:param center_x: center x of the wizard
:param center_y: center y of the wizard
:param width: width of the wizard
:param height: height of the wizard
"""
super().__init__(center_x=center_x, center_y=center_y)
# initiate width and height variables
self.width = width
self.height = height
# set direction facing for statue
self.direction = None
# load fireball
self.fireball = self.create_fireball()
self.can_shoot = True
def point_towards(self, player: Player) -> None:
"""
Points wizard towards the player
:return: none
"""
if self.center_x < player.center_x:
self.texture = arcade.load_texture("images/wizard_tower.png", scale=1.5)
self.direction = "RIGHT"
else:
self.texture = arcade.load_texture("images/wizard_tower.png", mirrored=True, scale=1.5)
self.direction = "LEFT"
if self.fireball.reset and player.health > 0:
self.reset_fireball()
elif player.health <= 0:
self.reset_fireball()
self.fireball.textures.append(arcade.load_texture("images/fireball_end.png"))
def create_fireball(self) -> arcade.Sprite:
"""
Creates a fireball
:return: a fireball
"""
return Fireball(self.center_x, self.center_y)
def shoot(self, player: Player) -> None:
"""
Shoots fireball, calls fireball.shoot_fireball()
:param player: player to shoot at
:return: none
"""
self.fireball.shoot_fireball(player)
self.can_shoot = False
def reset_fireball(self) -> None:
"""
resets fireball after hitting a wall
:return: none
"""
self.fireball.is_wall_hit = False
self.fireball.center_x = self.center_x
self.fireball.center_y = self.center_y
self.fireball.change_y = 0
self.fireball.change_x = 0
self.can_shoot = True
self.fireball.reset = False
class Fireball(arcade.AnimatedTimeSprite):
def __init__(self, center_x, center_y):
"""
Creates a fireball
:param center_x: center x of the fireball start point
:param center_y: center y of the fireball start point
"""
# call parent
super().__init__(center_x=center_x, center_y=center_y)
self.center_x = center_x
self.center_y = center_y
self.is_wall_hit = False
self.reset = False
self.take_damage = False
self.load_textures()
self.is_player_hit = False
def load_textures(self) -> None:
"""
loads textures
:return: none
"""
self.textures.append(arcade.load_texture("images/fireball.png", scale=0.3))
self.textures.append(arcade.load_texture("images/fireball_2.png", scale=0.3))
def shoot_fireball(self, player: Player) -> None:
"""
Draws a fireball
:param player: player to shoot at
:return: none
"""
# condition if the fireball has hit the wall to send a new one
self.is_wall_hit = False
self.take_damage = False
# determine end points of fireball
end_x, end_y = player.center_x, player.center_y
if player.direction == "UP":
end_y = player.center_y + 50
if player.direction == "DOWN":
end_y = player.center_y - 50
if player.direction == "LEFT":
end_x = player.center_x - 50
if player.direction == "RIGHT":
end_x = player.center_x + 50
# finding the side lengths of the triangles
delta_x = abs(self.center_x - end_x)
delta_y = abs(self.center_y - end_y)
# finding the related acute angle
if delta_x == 0 or delta_y == 0:
# find where the player is in relation to the fireball
if delta_x == 0:
if self.center_y < player.center_y:
self.angle = 90
else:
self.angle = 270
if delta_y == 0:
if self.center_x < player.center_x:
self.angle = 0
else:
self.angle = 180
else:
self.theta = math.atan(delta_y / delta_x) * 180 / math.pi
if self.center_x < player.center_x:
if self.center_y <= player.center_y:
self.angle = self.theta
else:
self.angle = 360 - self.theta
elif self.center_x > player.center_x:
if self.center_y <= player.center_y:
self.angle = 180 - self.theta
else:
self.angle = 180 + self.theta
# distance from fireball to player /5 is the movement speed
speed = math.sqrt(math.pow(delta_x, 2) + math.pow(delta_y, 2)) / 5
# move fireball
if speed is not 0:
self.change_x = (end_x - self.center_x) / speed
self.change_y = (end_y - self.center_y) / speed
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 = arcade.rotate_point(self.center_x - self.width / 4,
self.center_y - self.width / 4,
self.center_x,
self.center_y,
self.angle)
x2, y2 = arcade.rotate_point(self.center_x + self.width / 4,
self.center_y - self.height / 4,
self.center_x,
self.center_y,
self.angle)
x3, y3 = arcade.rotate_point(self.center_x + self.width / 4,
self.center_y + self.height / 4,
self.center_x,
self.center_y,
self.angle)
x4, y4 = arcade.rotate_point(self.center_x - self.width / 4,
self.center_y + self.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_wall_hit:
self.reset = True
if self.is_player_hit and not self.take_damage:
player.health -= 2.5
self.take_damage = 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