-
Notifications
You must be signed in to change notification settings - Fork 0
/
arena_actors.py
393 lines (296 loc) · 11.4 KB
/
arena_actors.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
import random
import g2d_pygame as g2d
class Actor():
'''Interface to be implemented by each game character
'''
def move(self):
'''Called by Arena, at the actor's turn
'''
raise NotImplementedError('Abstract method')
def collide(self, other: 'Actor'):
'''Called by Arena, whenever the `self` actor collides with some
`other` actor'''
raise NotImplementedError('Abstract method')
def position(self) -> (int, int, int, int):
'''Return the rectangle containing the actor, as a 4-tuple of ints:
(left, top, width, height)
'''
raise NotImplementedError('Abstract method')
def symbol(self) -> (int, int, int, int):
'''Return the position (x, y, w, h) of current sprite, if it is contained in
a larger image, with other sprites. Otherwise, simply return (0, 0, 0, 0)
'''
raise NotImplementedError('Abstract method')
class Jumper(Actor):
def move(self):
raise NotImplementedError("abstract method")
def position(self):
raise NotImplementedError("abstract method")
def symbol(self):
raise NotImplementedError("abstract method")
def collide(self,a):
raise NotImplementedError("abstract method")
class Stable(Actor):
def move(self):
raise NotImplementedError("abstract method")
def position(self):
raise NotImplementedError("abstract method")
def symbol(self):
raise NotImplementedError("abstract method")
def collide(self,a):
raise NotImplementedError("abstract method")
class Wall(Stable):
def __init__(self,arena,x,y):
arena.add(self)
self._x = x
self._y = y
self._w = 100
self._h = 25
def position(self):
return self._x,self._y,self._w,self._h
def move(self):
pass
def collide(self,other):
pass
def symbol(self):
pass
class Booble(Jumper):
def __init__(self,arena:object, pos: tuple , dx: int): #the dx of the ball is the same of the Dragon
arena.add(self)
self._dx = dx
self._x,self._y = pos
self._counter = 0
self._w, self._h = 20, 20
def move(self):
self._counter += 1
self._x += self._dx * 3
if self._counter >= 20:
self._y -= 3
def position(self):
return self._x, self._y, self._w, self._h
def symbol(self):
return 0, 1200, self._w, self._h
def collide(self,a):
pass
class Dragon(Jumper):
def __init__(self, arena:object, pos: tuple):
self._x, self._y = pos
self._w, self._h = 20, 18
self._SPEED = 5
self._dx, self._dy = 0, 0
self._lives = 3
self._arena = arena
self._called = 0 # This variable is used to avoid multiple jump in a single frame
arena.add(self)
self._landed = False
self._g = 0.5
def move(self):
self._landed = False
arena_w, arena_h = self._arena.size()
self._called -= 0.25
if self._called < 0:
self._called = 0
self._dy += self._g
self._y += self._dy
if self._y < 0:
self._y = 0
if self._y > arena_h - self._h:
self._y = arena_h - self._h + 2
self._landed = True
self._x += self._dx
if self._x < 0:
self._x = 0
elif self._x > arena_w - self._w:
self._x = arena_w - self._w
def go_left(self):
self._dx = -self._SPEED
def go_right(self):
self._dx = self._SPEED
def go_up(self):
arena_w, arena_h = self._arena.size()
if (self._landed and self._called == 0):
self._dy = -2*self._SPEED
self._called = 1
self._landed = False
def collide(self, other:object):
arena_w, arena_h = self._arena.size()
if isinstance(other, Wall) and self._dy > 0:
wx, wy, ww, wh = other.position()
px, py, pw, ph = self.position()
previous_x, previous_y = px - self._dx, py - self._dy
if previous_x + ww <= wx:
self._x = wx - pw
elif previous_x >= wx + ww:
self._x = wx + ww
elif previous_y + ph <= wy:
self._y, self._dy = wy - ph, 1
self._landed = True
elif previous_y >= wy + wh:
self._y, self._dy = wy + wh, 1
if isinstance(other, Opposite):
self._lives -= 1
self._y = 0
self._x = 0
self._landed = False
g2d.alert("You lost one life")
def position(self ) -> tuple :
return self._x, self._y, self._w, self._w
def state(self) -> str :
''' This function return the state of the actor to the function symbol in order to choose the right image'''
arena_w, arena_h = self._arena.size()
if not self._landed or self._dx == 0:
return "up"
else:
if self._dx < 0:
return "sx"
elif self._dx > 0 and self:
return "dx"
def symbol(self) -> tuple :
if self.state() == "sx":
print("sx")
return 0, 18, self._w, self._h
elif self.state() == "dx":
print("dx")
return 1225, 18, self._w, self._h
elif self.state() == "up":
print("up")
return 0, 90, self._w, self._h
def create_ball(self):
ball = Booble(self._arena,(self._x,self._y-10),self._dx/2)
def return_lives(self) -> int :
return self._lives
class Opposite(Jumper):
def __init__(self, arena:object, pos:tuple):
self._x, self._y = pos
self._w, self._h = 20, 18
self._SPEED = 5
self._dx, self._dy = 0, 0
self._g = 0.5
self._arena = arena
arena.add(self)
self._counter = 0 # this variable is used to control the random movements of the opponent
self._landed = False
def move(self):
arena_w, arena_h = self._arena.size()
self._counter += 1
self._landed = False
self._x += self._dx
if self._x < 0:
self._x = 0
self._dx = -self._dx
elif self._x > arena_w - self._w:
self._x = arena_w - self._w
self._dx = - self._dx
#in this point the funcion decides the random movement of the actor
rand_move = random.choice((-1,0,1))
print(rand_move)
if self._counter >= 75 and rand_move == -1:
self._dx = -self._SPEED
self._counter = 0
elif self._counter >= 75 and rand_move == 1:
self._dx = self._SPEED
self._counter = 0
if self._counter >= 75 and rand_move == 0:
self._dy = -4*self._SPEED #modificato -2 -> -4
self._landed = False
self._dy += self._g
self._y += self._dy
if self._y < 0:
self._y = 0
if self._y > arena_h - self._h:
self._y = arena_h - self._h +2
self._landed = True
def collide(self, other:object):
arena_w, arena_h = self._arena.size()
if isinstance(other, Wall) and self._dy > 0:
wx, wy, ww, wh = other.position()
px, py, pw, ph = self.position()
previous_x, previous_y = px - self._dx, py - self._dy
if previous_x + ww <= wx:
self._x = wx - pw
elif previous_x >= wx + ww:
self._x = wx + ww
elif previous_y + ph <= wy:
self._y, self._dy = wy - ph, 1
self._landed = True
elif previous_y >= wy + wh:
self._y, self._dy = wy + wh, 1
if isinstance(other, Booble):
self._arena.remove(self)
def position(self):
return self._x, self._y, self._w, self._w
def state(self) -> str :
''' This function return the state of the actor to the function symbol in order to choose the right image
'''
arena_w, arena_h = self._arena.size()
if not self._landed or self._dx == 0:
return "up"
else:
if self._dx < 0:
return "sx"
elif self._dx > 0 and self:
return "dx"
def symbol(self) -> tuple :
if self.state() == "sx":
print("sx")
return 5, 455, self._w, self._h
elif self.state() == "dx":
print("dx")
return 1265, 455, self._w, self._h
elif self.state() == "up":
print("up")
return 76, 455, self._w, self._h
class Arena():
'''A generic 2D game, with a given size in pixels and a list of actors
'''
def __init__(self, size: (int, int)):
'''Create an arena, with given dimensions in pixels
'''
self._w, self._h = size
self._count = 0
self._actors = []
def add(self, a: Actor):
'''Register an actor into this arena.
Actors are blitted in their order of registration
'''
if a not in self._actors:
self._actors.append(a)
def remove(self, a: Actor):
'''Cancel an actor from this arena
'''
if a in self._actors:
self._actors.remove(a)
def move_all(self):
'''Move all actors (through their own move method).
After each single move, collisions are checked and eventually
the `collide` methods of both colliding actors are called
'''
actors = list(reversed(self._actors))
for a in actors:
a.move()
for other in actors:
if other is not a and self.check_collision(a, other):
a.collide(other)
other.collide(a)
self._count += 1
def check_collision(self, a1: Actor, a2: Actor) -> bool:
'''Check the two actors (args) for mutual collision (bounding-box
collision detection). Return True if colliding, False otherwise
'''
x1, y1, w1, h1 = a1.position()
x2, y2, w2, h2 = a2.position()
return (y2 < y1 + h1 and y1 < y2 + h2
and x2 < x1 + w1 and x1 < x2 + w2
and a1 in self._actors and a2 in self._actors)
def actors(self) -> list:
'''Return a copy of the list of actors
'''
return list(self._actors)
def size(self) -> (int, int):
'''Return the size of the arena as a couple: (width, height)
'''
return (self._w, self._h)
def count(self) -> int:
'''Return the total count of ticks (or frames)
'''
return self._count