-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathth_model.py
208 lines (177 loc) · 6 KB
/
th_model.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
import pygame
import random
from character import Reim, Enemy
from bullet import ReimBullet, EnemyBullet
from math import sqrt, cos, sin
class ThGame:
"""
Game state
"""
def __init__(self):
"""
Attributes:
running: a boolean representing the game status
reim: a Reim instance
enemies: a list of Enemy instances
enemy_bullets: a list of EnemyBullets
bullets: a list of ReimBullets
ENEMY_BULLETS_MAX: a int representing a constant max of bullets
"""
self._running = True
self._reim = Reim()
self._bullets = []
self._enemy_bullets = []
self._enemies = []
for enemy_count in range(6):
self._enemies.append(Enemy())
self._enemy_bullets.append(EnemyBullet(
self._enemies[enemy_count]._pos_x, self._enemies[enemy_count]._pos_y))
self.ENEMY_BULLETS_MAX = 100
def set_running(self, running):
"""
Setter for running
Args:
running: a boolean representing the new status
"""
self._running = running
@property
def running(self):
"""
Returns running
Returns:
a boolean representing
"""
return self._running
@property
def reim(self):
"""
Returns the reim instance
Returns:
a Reim instance
"""
return self._reim
@property
def enemies(self):
"""
Returns the enemies list
Returns:
a list of Enemy instances
"""
return self._enemies
@property
def bullets(self):
"""
Returns the bullets list
Returns:
a list of ReimBullet instances
"""
return self._bullets
@property
def enemy_bullets(self):
"""
Returns the enemy_bullets list
Returns:
a list of EnemyBullet instances
"""
return self._enemy_bullets
def check_boundary(self):
"""
Check if character hits the boundary
"""
if self._reim.pos_x > 468:
self._reim.set_pos_x(468)
if self._reim.pos_x < 50:
self._reim.set_pos_x(50)
if self._reim.pos_y > 528:
self._reim.set_pos_y(528)
if self._reim.pos_y < 20:
self._reim.set_pos_y(20)
for enemy in self._enemies:
# Allow enemies to go back and forth through edges
if enemy.pos_x > 468:
enemy.set_pos_x(50)
if enemy.pos_x < 50:
enemy.set_pos_x(468)
if enemy.pos_y > 528:
enemy.set_pos_y(528)
if enemy.pos_y < 20:
enemy.set_pos_y(20)
# Catch occasional ValueError where a bullet has already been deleted
try:
# Remove bullets off screen
for enemy_bullets in self._enemy_bullets:
if enemy_bullets.pos_y > 534:
self._enemy_bullets.remove(enemy_bullets)
if enemy_bullets.pos_x > 484:
self._enemy_bullets.remove(enemy_bullets)
if enemy_bullets.pos_x < 50:
self._enemy_bullets.remove(enemy_bullets)
except ValueError:
print("bullet not in list, ignored")
except:
print("what else could happen?")
def enemy_move(self):
"""
Make the Enemy move in random directions
"""
for enemy in self._enemies:
angle = enemy.angle + 1
speed_y = enemy.speed * sin(angle) * random.randint(30, 40)
speed_x = enemy.speed * cos(angle) * random.randint(-30, 40)
enemy.set_pos_y(enemy.pos_y + speed_y)
enemy.set_pos_x(enemy.pos_x + speed_x)
enemy.set_angle(angle)
def enemy_keep_firing(self):
"""
Make the Enemy keep firing if the game has less than 100 bullets
"""
if self.running and len(self.enemy_bullets) < self.ENEMY_BULLETS_MAX:
for enemy in self._enemies:
temp_bullet = EnemyBullet(enemy._pos_x, enemy._pos_y)
temp_bullet.update()
self._enemy_bullets.append(temp_bullet)
def reim_keep_firing(self):
"""
Make Reim keep firing
"""
if self.running and self.reim.z_pressed:
temp_bullet = ReimBullet(
# Center bullets
self._reim._pos_x + 12, self._reim._pos_y + 10)
temp_bullet.update()
self._bullets.append(temp_bullet)
def check_win_condition(self):
"""
Check if the game ends with a win or loss
Checks the amount of enemies left in the enemies list and if the player
has been hit by a bullet
"""
for bullet in self.bullets:
for enemy in self._enemies:
if(find_distance(enemy._pos_x, enemy._pos_y,
bullet._pos_x, bullet._pos_y) < 30):
self._enemies.remove(enemy)
# Win condition
if len(self._enemies) == 0:
print("You Won!")
self.set_running(False)
# Lose condition
for enemy_bullet in self._enemy_bullets:
if(find_distance(self._reim._pos_x, self._reim._pos_y,
enemy_bullet._pos_x, enemy_bullet._pos_y) < 8):
print("You Lost!")
self.set_running(False)
def find_distance(pos_x_1, pos_y_1, pos_x_2, pos_y_2):
"""
Find distance between two coordinate sets with euler's
Args:
pos_x_1: an int representing the set 1 x coordinate
pos_y_1: an int representing the set 1 y coordinate
pos_x_2: an int representing the set 2 x coordinate
pos_y_2: an int representing the set 2 y coordinate
Returns:
a float representing the distance
"""
delta_x = pos_x_1 - pos_x_2
delta_y = pos_y_1 - pos_y_2
return sqrt(delta_x ** 2 + delta_y ** 2)