-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
435 lines (351 loc) · 14.3 KB
/
entity.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- encoding: UTF-8 -*-
import gameEngine
import math
import time
import random
from pyglet.gl import * # parce les pyglet.gl.GLMACHIN non merci
import types
try:
range = xrange
except NameError:
pass
class Platform(object):
def __init__(self, x, y, width=20, height=100, blinking=False):
self.x = x
self.y = y
self.width = width
self.height = height
self.blinking = blinking
self.isMoving = False
self.isFalling = False
self.shown = True
self.type = "Normal"
self.tick = 0
self.blinkTick = 0
def jump(self, player):
if player.dy > 0:
return
y = player.y - player.dy
while y > player.y + player.dy:
if self.x < player.getX() < self.x + self.width and self.y < y < self.y + self.height or \
self.x < player.getX() + player.WIDTH < self.x + self.width and self.y < y < self.y + self.height:
player.startJumpY = self.y + self.height
player.timeJumping = 0
return True
y += player.dy / 10.0
return False
def _render(self):
if self.blinkTick < 50 and self.blinking:
self.shown = True
elif 50 <= self.blinkTick < 100 and self.blinking:
self.shown = False
else:
self.blinkTick = 0
if self.shown:
if self.type == "Normal":
glColor4f(0.3, 0.8, 0.3, 1)
elif self.type == "Moving":
glColor4f(0.2, 0.5, 1, 1)
elif self.type == "Booming":
glColor4f(0.5, 0.7, 0.5, 1)
elif self.type == "Spikes":
glColor4f(0.2, 0.3, 0.4, 1)
else:
glColor4f(1, 0.6, 0.3, 1)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.width, self.y)
glVertex2f(self.x + self.width, self.y + self.height)
glVertex2f(self.x, self.y + self.height)
glEnd()
self.render()
def _simulate(self, dt):
self.blinkTick += 1
self.tick += 1
self.simulate(dt)
def simulate(self, dt):
pass
def render(self):
pass
class EnlargingPlatform(Platform):
def __init__(self, x, y, width=20, height=100, dir=0, blinking=False):
super(EnlargingPlatform, self).__init__(x, y, width, height, blinking)
self.enlargissement = "+"
def simulate(self, dt):
if self.enlargissement == "+":
self.width += 1
self.x -= 0.5
if self.width > 125:
self.enlargissement = "-"
else:
self.width -= 1
self.x += 0.5
if self.width < 25:
self.enlargissement = "+"
class BoomingPlatform(Platform):
def __init__(self, x, y, width=20, height=100, dir=0, blinking=False):
super(BoomingPlatform, self).__init__(x, y, width, height, blinking)
self.type = "Booming"
self.particles = []
self.exploded = False
def jump(self, player):
if super(BoomingPlatform, self).jump(player) and not player.isDead:
player.isDead = True
self.exploded = True
self.shown = False
self.blinking = False
for j in range(1, 4):
for i in range(25):
angle = 6.28 * random.random()
self.particles.append(Particle(self.x + self.width / 2, self.y + self.height / 2, 10, 10, (math.cos(angle), math.sin(angle)), j))
def render(self):
if self.exploded:
for i in self.particles:
i.render()
def simulate(self, dt):
for i in self.particles:
i.simulate(dt)
class Particle:
def __init__(self, x, y, width, height, vec, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.vec = vec
self.speed = speed * 100
if not random.randint(0, 1):
self.color = (0.5, 0.7, 0.5, 1)
else:
self.color = (1, 1, 1, 1)
def render(self):
glColor4f(self.color[0], self.color[1], self.color[2], self.color[3])
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.width, self.y)
glVertex2f(self.x + self.width, self.y + self.height)
glVertex2f(self.x, self.y + self.height)
glEnd()
def simulate(self, dt):
self.x += self.vec[0] * dt * self.speed
self.y += self.vec[1] * dt * self.speed
class MovingPlatform(Platform):
# Dir: 0 => Left
# : 1 => Right
def __init__(self, x, y, width=20, height=100, dir=0, blinking=False):
super(MovingPlatform, self).__init__(x, y, width, height, blinking)
self.movementDirection = "Right" if dir else "Left"
self.speed = max(100, min(self.y * 0.01, 500))
self.type = "Moving"
def simulate(self, dt):
if self.x < gameEngine.GameEngine.W_WIDTH - self.width and self.movementDirection == "Right":
self.x += self.speed * dt
elif self.x > gameEngine.GameEngine.W_WIDTH - self.width and self.movementDirection == "Right":
self.movementDirection = "Left"
elif 0 < self.x and self.movementDirection == "Left":
self.x -= self.speed * dt
else:
self.movementDirection = "Right"
class SpikesPlatform(MovingPlatform):
def __init__(self, x, y, width=20, height=100, dir=0, blinking=False):
super(SpikesPlatform, self).__init__(x, y, width, height, dir, blinking)
self.type = "Spikes"
def jump(self, player):
if player.dy > 0:
y = player.y - player.dy
while y < player.y + player.dy:
if self.x < player.getX() < self.x + self.width and self.y < y < self.y + self.height or \
self.x < player.getX() + player.WIDTH < self.x + self.width and self.y < y < self.y + self.height:
if self.x < player.getX() < self.x + self.width and self.y < player.y < self.y + self.height or \
self.x < player.getX() + player.WIDTH < self.x + self.width and self.y < player.y < self.y + self.height:
player.isDead = True
return True
y += player.dy / 10.0
y = player.y - player.dy
while y > player.y + player.dy:
if self.x < player.getX() < self.x + self.width and self.y < y < self.y + self.height or \
self.x < player.getX() + player.WIDTH < self.x + self.width and self.y < y < self.y + self.height:
player.startJumpY = self.y + self.height
player.timeJumping = 0
return True
y += player.dy / 10.0
return False
class FallingPlatform(Platform):
def __init__(self, x, y, width=20, height=100, blinking=False):
super(FallingPlatform, self).__init__(x, y, width, height, blinking)
self.speed = 250
self.type = "Falling"
def jump(self, player):
if super(FallingPlatform, self).jump(player):
player.velY = 100
self.isFalling = True
return True
return False
def simulate(self, dt):
if self.isFalling:
self.y -= self.speed * dt
class BlinkingPlatform(BoomingPlatform, FallingPlatform, MovingPlatform, Platform):
def __init__(self, x, y, width=20, height=100, dir=0, blinking=False):
super(BlinkingPlatform, self).__init__(x, y, width, height, blinking)
self.movementDirection = "Right" if dir else "Left"
self.speed = max(100, min(self.y * 0.01, 500))
self.order = [i for i in range(3)]
random.shuffle(self.order)
self.blinking = blinking
self.type = "Moving"
def _simulate(self, dt):
if not self.isFalling:
if self.tick < 100:
self.changeType(self.order[0])
elif 100 <= self.tick < 200:
self.changeType(self.order[1])
elif 200 <= self.tick < 300:
self.changeType(self.order[2])
else:
self.tick = 0
super(BlinkingPlatform, self)._simulate(dt)
def changeType(self, n):
if not n:
self.simulate = types.MethodType(MovingPlatform.simulate, self)
self.jump = types.MethodType(MovingPlatform.jump, self)
self.speed = max(100, min(self.y * 0.01, 500))
self.type = "Moving"
elif n == 1:
self.simulate = types.MethodType(FallingPlatform.simulate, self)
self.jump = types.MethodType(FallingPlatform.jump, self)
self.speed = 250
self.type = "Falling"
else:
self.simulate = types.MethodType(BoomingPlatform.simulate, self)
self.jump = types.MethodType(BoomingPlatform.jump, self)
self.type = "Booming"
class Ennemy(object):
def __init__(self, x, y, dir=0):
self.x = x
self.y = y
self.width = 20
self.height = 20
self.movementDirection = "Right" if dir else "Left"
self.speed = max(25, min(self.x * 0.005, 100))
self.health = 1
def collide(self, ent):
if self.x <= ent.getX() <= self.x + self.width or self.x <= ent.getX() + ent.WIDTH <= self.x + self.width:
if self.y <= ent.y <= self.y + self.height or self.y <= ent.y + ent.HEIGHT <= self.y + self.height \
or ent.y <= self.y <= ent.y + ent.HEIGHT or ent.y <= self.y + self.height <= ent.y + ent.HEIGHT:
return True
elif ent.getX() <= self.x <= ent.getX() + ent.WIDTH or ent.getX() <= self.x + self.width <= ent.getX() + ent.WIDTH:
if self.y <= ent.y <= self.y + self.height or self.y <= ent.y + ent.HEIGHT <= self.y + self.height or \
ent.y <= self.y <= ent.y + ent.HEIGHT or ent.y <= self.y + self.height <= ent.y + ent.HEIGHT:
return True
return False
def simulate(self, dt):
if self.x < gameEngine.GameEngine.W_WIDTH - self.width and self.movementDirection == "Right":
self.x += self.speed * dt
elif self.x > gameEngine.GameEngine.W_WIDTH - self.width and self.movementDirection == "Right":
self.movementDirection = "Left"
elif 0 < self.x and self.movementDirection == "Left":
self.x -= self.speed * dt
else:
self.movementDirection = "Right"
def render(self):
glColor4f(1, 0.3, 0.3, 1)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.width, self.y)
glVertex2f(self.x + self.width, self.y + self.height)
glVertex2f(self.x, self.y + self.height)
glEnd()
class Player(object):
WIDTH = 32
HEIGHT = 32
def __init__(self):
self.x = gameEngine.GameEngine.W_WIDTH / 2
self.y = 0
self.dy = 0
self.velY = 100
self.timeJumping = 0
self.startJumpY = 150
self.cursorPosX = self.x
self.isDead = False
self.fireRate = 2.0
self.lastShoot = time.time()
self.isShooting = False
self.item = None
def move(self, dt):
xBefore, yBefore = self.x, self.y
# Deplacement en x
dx = self.cursorPosX - self.x
if dx > 400:
dx = 400
self.x = (self.x + dx * dt * math.log(math.sqrt(dx ** 2) / 50 + 2))
if self.item is None:
# deplacement en y
self.timeJumping += dt * 7
self.y = (- 9.81 * self.timeJumping ** 2 + self.velY * self.timeJumping + self.startJumpY)
else:
self.y += self.item.effectYVel * dt
self.item.effectTime -= dt
if self.item.effectTime <= 0:
self.item = None
self.timeJumping = 0
self.startJumpY = self.y
self.dy = self.y - yBefore
def shoot(self, bullets):
if time.time() - self.lastShoot > 1 / self.fireRate and self.isShooting:
bullets.append(Bullet(self.getX() + self.WIDTH / 2, self.y + self.HEIGHT / 2, 0, 1000))
self.lastShoot = time.time()
def getX(self):
return self.x % gameEngine.GameEngine.W_WIDTH
def render(self):
glColor4f(1, 1, 1, 1)
glBegin(GL_QUADS)
glVertex2f(self.getX(), self.y)
glVertex2f(self.getX() + self.WIDTH, self.y)
glVertex2f(self.getX() + self.WIDTH, self.y + self.HEIGHT)
glVertex2f(self.getX(), self.y + self.HEIGHT)
glEnd()
class Bullet(object):
WIDTH = 10
HEIGHT = 10
def __init__(self, x, y, xVel, yVel):
self.x = x
self.y = y
self.xVel = xVel
self.yVel = yVel
def simulate(self, dt):
self.x += self.xVel * dt
self.y += self.yVel * dt
def getX(self):
return self.x
def render(self):
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.WIDTH, self.y)
glVertex2f(self.x + self.WIDTH, self.y + self.HEIGHT)
glVertex2f(self.x, self.y + self.HEIGHT)
glEnd()
class JetPack(object):
WIDTH = 20
HEIGHT = 20
def __init__(self, x, y):
self.x = x
self.y = y
self.effectYVel = 725
self.effectTime = 5
def render(self):
glBegin(GL_QUADS)
glColor4f(1, 0.6, 1, 1)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.WIDTH, self.y)
glVertex2f(self.x + self.WIDTH, self.y + self.HEIGHT)
glVertex2f(self.x, self.y + self.HEIGHT)
glEnd()
def collide(self, ent):
if self.x <= ent.getX() <= self.x + self.WIDTH or self.x <= ent.getX() + ent.WIDTH <= self.x + self.WIDTH:
if self.y <= ent.y <= self.y + self.HEIGHT or self.y <= ent.y + ent.HEIGHT <= self.y + self.HEIGHT \
or ent.y <= self.y <= ent.y + ent.HEIGHT or ent.y <= self.y + self.HEIGHT <= ent.y + ent.HEIGHT:
return True
elif ent.getX() <= self.x <= ent.getX() + ent.WIDTH or ent.getX() <= self.x + self.WIDTH <= ent.getX() + ent.WIDTH:
if self.y <= ent.y <= self.y + self.HEIGHT or self.y <= ent.y + ent.HEIGHT <= self.y + self.HEIGHT or \
ent.y <= self.y <= ent.y + ent.HEIGHT or ent.y <= self.y + self.HEIGHT <= ent.y + ent.HEIGHT:
return True
return False