-
Notifications
You must be signed in to change notification settings - Fork 0
/
boss_ship.py
268 lines (209 loc) · 10.4 KB
/
boss_ship.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from __future__ import annotations
import datetime
import os
import random
from datetime import time
from enum import Enum, auto
from pygame import transform, Surface
import boss_bullet
import ship
from boss_bullet import BossBullet
# this inspection is an error in PyCharm when dealing with auto
# noinspection PyArgumentList
from player_ship import PlayerShip
# this inspection is an error in PyCharm when dealing with auto
# noinspection PyArgumentList
class BossState(Enum):
IDLING = auto()
SHOOTING = auto()
SLAMMING = auto()
MOVING = auto()
RETURNING = auto()
@classmethod
def get_random_state(cls) -> BossState:
return random.choice([cls.IDLING, cls.SHOOTING, cls.SLAMMING, cls.MOVING])
# this inspection is an error in PyCharm when dealing with auto
# noinspection PyArgumentList
class BossDirection(Enum):
UP = auto()
DOWN = auto()
LEFT = auto()
RIGHT = auto()
@classmethod
def get_random_direction(cls) -> BossDirection:
return random.choice([cls.UP, cls.DOWN, cls.LEFT, cls.RIGHT])
class BossStateMachine:
IDLING_BASE_DURATION: int = 50
IDLING_BASE_VARIATION: int = 15
SHOOTING_BASE_COUNT: int = 3
SHOOTING_BASE_VARIATION: int = 1
SHOOTING_COOLDOWN: int = 50
MOVING_DISTANCE: int = 200
def __init__(self) -> None:
super().__init__()
self.current_state: BossState = BossState.IDLING
# in frames
self.idling_timer: int = 0
self.idling_current_duration: int = self.calculate_idling_duration()
self.shooting_current_bullets_to_shoot: int = self.calculate_shooting_bullet_count()
self.shooting_bullets_shot: int = 0
self.shooting_timer: int = 0
self.slamming_done: bool = False
self.moving_current_distance: float = 0
self.moving_current_direction: BossDirection = BossDirection.get_random_direction()
self.is_returning: bool = False
def update(self):
if self.current_state == BossState.IDLING:
self.idling_timer += 1
elif self.current_state == BossState.SHOOTING:
self.shooting_timer += 1
self.try_change_state()
def try_change_state(self):
match self.current_state:
case BossState.IDLING if self.idling_timer == self.idling_current_duration:
self.idling_timer = 0
self.idling_current_duration = self.calculate_idling_duration()
# can go back to idling (as per design)!
self.current_state = BossState.get_random_state()
case BossState.SHOOTING if self.shooting_bullets_shot == self.shooting_current_bullets_to_shoot:
self.shooting_bullets_shot = 0
self.shooting_current_bullets_to_shoot = self.calculate_shooting_bullet_count()
self.current_state = BossState.IDLING
case BossState.SLAMMING if self.slamming_done:
self.slamming_done = False
self.current_state = BossState.IDLING
case BossState.MOVING if self.moving_current_distance == self.MOVING_DISTANCE:
self.moving_current_distance = 0
self.current_state = BossState.RETURNING
case BossState.RETURNING if self.moving_current_distance == self.MOVING_DISTANCE:
self.moving_current_distance = 0
self.moving_current_direction = BossDirection.get_random_direction()
self.current_state = BossState.IDLING
@staticmethod
def calculate_idling_duration():
idling_variation = random.randint(
-BossStateMachine.IDLING_BASE_VARIATION, BossStateMachine.IDLING_BASE_VARIATION)
return BossStateMachine.IDLING_BASE_DURATION + idling_variation
@staticmethod
def calculate_shooting_bullet_count():
count_variation = random.randint(
-BossStateMachine.SHOOTING_BASE_VARIATION, BossStateMachine.SHOOTING_BASE_VARIATION)
return BossStateMachine.SHOOTING_BASE_COUNT + count_variation
# this inspection is an error in PyCharm when dealing with auto
# noinspection PyArgumentList
class SlammingDirection(Enum):
LEFT = auto()
MIDDLE = auto()
RIGHT = auto()
NONE = auto()
@classmethod
def get_random_slamming_direction(cls) -> SlammingDirection:
return random.choice([cls.LEFT, cls.MIDDLE, cls.RIGHT])
class BossShip(ship.Ship):
MOVING_VELOCITY: float = 4
SLAMMING_VELOCITY: float = 12
def __init__(self, given_x: float, given_y: float, given_width: int, boss_model: Surface, player: PlayerShip,
screen_x: int, screen_y: int):
super().__init__(given_x, given_y)
self.width: int = given_width
self.height: int = self.width
self.is_enemy: bool = True
self.enemy_model: Surface = transform.scale(boss_model, (self.width, self.height))
self.state_machine: BossStateMachine = BossStateMachine()
self.bullets = []
self.player = player
self.screen_x = screen_x
self.screen_y = screen_y
self.original_x = given_x
self.original_y = given_y
self.slamming_direction = SlammingDirection.NONE
self.health = 10
self.exists = True
def shoot(self):
self.bullets.append(boss_bullet.BossBullet(
self.s_x, self.s_y + self.width, -4545, self.width * 0.1, self.bullet_width, self.bullet_height,
self.player, self.screen_y))
def update(self) -> None:
# if self.state_machine.current_state == BossState.SLAMMING:
# print(self.state_machine.current_state, datetime.datetime.now())
self.state_machine.update()
for bullet in self.bullets:
bullet.update()
match self.state_machine.current_state:
case BossState.MOVING:
self.state_machine.moving_current_distance += BossShip.MOVING_VELOCITY
match self.state_machine.moving_current_direction:
case BossDirection.UP:
self.s_y -= BossShip.MOVING_VELOCITY
case BossDirection.DOWN:
self.s_y += BossShip.MOVING_VELOCITY
case BossDirection.LEFT:
self.s_x -= BossShip.MOVING_VELOCITY
case BossDirection.RIGHT:
self.s_x += BossShip.MOVING_VELOCITY
case BossState.RETURNING:
self.state_machine.moving_current_distance += BossShip.MOVING_VELOCITY
match self.state_machine.moving_current_direction:
case BossDirection.UP:
self.s_y += BossShip.MOVING_VELOCITY
case BossDirection.DOWN:
self.s_y -= BossShip.MOVING_VELOCITY
case BossDirection.LEFT:
self.s_x += BossShip.MOVING_VELOCITY
case BossDirection.RIGHT:
self.s_x -= BossShip.MOVING_VELOCITY
case BossState.SHOOTING if self.state_machine.shooting_timer == BossStateMachine.SHOOTING_COOLDOWN:
self.state_machine.shooting_timer = 0
self.state_machine.shooting_bullets_shot += 1
self.shoot()
case BossState.SLAMMING:
match self.slamming_direction:
case SlammingDirection.NONE:
self.slamming_direction = SlammingDirection.get_random_slamming_direction()
case SlammingDirection.LEFT:
if self.s_x == self.width / 2:
if self.s_y >= self.screen_y - self.width:
self.s_x = self.original_x
self.s_y = self.original_y
self.state_machine.slamming_done = True
self.slamming_direction = SlammingDirection.get_random_slamming_direction()
else:
self.s_y = min(float(self.screen_y - self.width), self.s_y + BossShip.SLAMMING_VELOCITY)
else:
self.s_x = max(self.width / 2, self.s_x - BossShip.SLAMMING_VELOCITY)
case SlammingDirection.RIGHT:
if self.s_x == self.screen_x - self.width / 2:
if self.s_y == self.screen_y - self.width:
self.s_x = self.original_x
self.s_y = self.original_y
self.state_machine.slamming_done = True
self.slamming_direction = SlammingDirection.get_random_slamming_direction()
else:
self.s_y = min(float(self.screen_y - self.width), self.s_y + BossShip.SLAMMING_VELOCITY)
else:
self.s_x = min(self.screen_x - self.width / 2, self.s_x + BossShip.SLAMMING_VELOCITY)
case SlammingDirection.MIDDLE:
if self.s_y == self.screen_y - self.width:
self.s_x = self.original_x
self.s_y = self.original_y
self.state_machine.slamming_done = True
self.slamming_direction = SlammingDirection.get_random_slamming_direction()
else:
self.s_y = min(float(self.screen_y - self.width), self.s_y + BossShip.SLAMMING_VELOCITY)
def damage(self):
self.health -= 1
if self.health <= 0:
self.exists = False
def draw_for_boss(self, game_display):
game_display.blit(self.enemy_model, [
self.s_x, self.s_y, self.width, self.width])
for player_bullet in self.bullets:
game_display.blit(player_bullet.bullet_model, [
player_bullet.x_pos, player_bullet.y_pos])
for i in reversed(range(len(self.bullets))):
if not self.bullets[i].exists:
del self.bullets[i]
def check_player(self, player): # wykraczy sie jesli kiedykolwiek gracz/enemy nie bedzie kwadratem
if ((self.s_x <= player.s_x <= self.s_x + self.width) or (self.s_x <= player.s_x + player.width <= self.s_x + self.width)) and \
((self.s_y <= player.s_y <= self.s_y + self.width) or (self.s_y <= player.s_y + player.width <= self.s_y + self.width)):
player.exists = False