-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
230 lines (188 loc) · 5.51 KB
/
models.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
import arcade.key
import gc
from random import randint,random
from time import time
GAME_MENU = 1
GAME_RUNNING = 2
GAME_OVER = 3
GAME_PAUSE = 4
GAME_TIME = 30
class Model:
def __init__(self,world,x,y):
self.world = world
self.x = x
self.y = y
def hit(self, other, hit_sizeX, hit_sizeY):
return (abs(self.x - other.x) <= hit_sizeX) and (abs(self.y - other.y) <= hit_sizeY)
class Bullets():
NUM_BULLET = 8
def __init__(self, world):
self.world = world
self.bulletsList = []
self.setup()
def setup(self):
# del self.bulletsList [:]
self.bulletsList.clear()
for i in range(Bullets.NUM_BULLET):
bullet = Bullet(self.world, 0, self.world.height, 0, 0)
bullet.random()
self.bulletsList.append(bullet)
self.lastadd = 0
self.lastadd_toxic = 0
def animate(self, delta):
for bullet in self.bulletsList:
bullet.animate(delta)
self.update()
def shoot(self, x, y, vx, vy):
bullet = Bullet(self.world, x, y, vx, vy)
self.bulletsList.append(bullet)
def update(self):
for bullet in self.bulletsList:
if bullet.is_bullet_out_of_bound():
bullet.random()
self.addBulletByTime()
def addBulletByTime(self):
current_time = int(self.world.current_time)
if current_time > self.lastadd and current_time % 5 == 0:
self.bulletsList.append(Bullet(self.world, 0, self.world.height, 0, 0))
self.bulletsList.append(Bullet(self.world, 0, self.world.height, 0, 0))
self.lastadd = current_time
if current_time > 5 and current_time % 4 == 1 and current_time > self.lastadd_toxic:
tmp = Bullet(self.world, 0, self.world.height, 0, 0)
tmp.isToxic = True
self.bulletsList.append(tmp)
self.lastadd_toxic = int(current_time)
class Bullet(Model):
def __init__(self, world, x, y, vx, vy):
super(). __init__(world, x, y)
self.vx = vx
self.vy = vy
self.x = x
self.y = y
self.world = world
self.speed = 20
self.random()
self.isToxic = False
def animate(self, delta):
if (self.x < 0) or (self.x > self.world.width):
self.vx *= -1
if (self.y < 0) or (self.y > self.world.height):
self.vy -= self.vy
self.random()
self.x += self.vx
self.y -= self.vy
def is_bullet_out_of_bound(self):
if self.y > self.world.height or self.y < 0:
return True
return False
def random(self):
self.x = 400 * random()
self.y = self.world.height
self.vx = self.speed * (random() - 0.5)
self.vy = self.speed * random()
class World:
def __init__(self, width, height):
self.width = width
self.height = height
self.player = Player(self, width/2 , 56)
self.bullets = Bullets(self)
self.current_state = GAME_MENU
self.current_time = 0
self.setup()
def setup(self):
self.start_time = time()
self.current_time = (time() - self.start_time)
self.player.setup()
self.bullets.setup()
self.score = 0
self.jar = 0
def animate(self, delta):
#print(self.current_time)
if self.current_state == GAME_RUNNING:
self.player.animate(delta)
self.bullets.animate(delta)
self.current_time = (time() - self.start_time)
self.jar = (int)(self.score/4)
if(self.current_time >= GAME_TIME):
self.current_state = GAME_OVER
elif self.current_state == GAME_PAUSE:
self.start_time += delta
def on_key_press(self, key, key_modifiers):
self.player.on_key_press(key,key_modifiers)
self.on_key_press_state(key,key_modifiers)
def on_key_release(self, key, key_modifiers):
self.player.on_key_release(key,key_modifiers)
def on_key_press_state(self, key, key_modifiers):
if key == arcade.key.SPACE:
if self.current_state == GAME_RUNNING:
self.current_state = GAME_PAUSE
elif self.current_state == GAME_PAUSE:
self.current_state = GAME_RUNNING
elif self.current_state == GAME_MENU:
self.current_state = GAME_RUNNING
self.start_time = time()
elif self.current_state == GAME_OVER:
self.setup()
self.current_state = GAME_RUNNING
class Player(Model):
DIR_LEFT = -1
DIR_STILL = 0
DIR_RIGHT = 1
isPress = False
def __init__(self, world, x, y):
self.world = world
self.LIFE = 50
self.width = 64
self.height = 112
self.x = x
self.y = y
self.direction = Player.DIR_STILL
self.isPress = False
# self.bullets = Bullets(self.world);
self.speed = 20
def setup(self):
self.x = self.world.width/2
self.y = 56
def animate(self,delta):
self.move()
# self.bullets.animate(delta)
for bullet in self.world.bullets.bulletsList:
if self.hit(bullet, 20, 60):
if bullet.isToxic == False:
self.world.score += 1
elif bullet.isToxic == True:
if self.world.score % 4 == 0:
self.world.score -= 4
else:
self.world.score -= self.world.score % 4
bullet.random()
def move(self):
if self.direction == Player.DIR_RIGHT:
if self.x > self.world.width:
self.x = 0
self.x += self.speed
if self.direction == Player.DIR_LEFT:
if self.x < 0:
self.x = self.world.width
self.x -= self.speed
if self.direction == Player.DIR_STILL:
self.x += 0
def switch_direction(self, new_direction):
if new_direction == "left":
self.direction = Player.DIR_LEFT
elif new_direction == "right":
self.direction = Player.DIR_RIGHT
elif new_direction == "still":
self.direction = Player.DIR_STILL
def on_key_press(self, key, key_modifiers):
if key == arcade.key.LEFT:
self.switch_direction("left")
self.isPress = True
if key == arcade.key.RIGHT:
self.switch_direction("right")
self.isPress = True
def on_key_release(self, key, key_modifiers):
if key == arcade.key.LEFT or key == arcade.key.RIGHT:
if self.isPress == True:
self.isPress = False
self.switch_direction("still")