-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameObject.py
390 lines (274 loc) · 8.43 KB
/
GameObject.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
import time # NOTE : DEBUG
try:
import cfg
if cfg.DEBUG_MODE:
from master import pg
import defs as df
except ModuleNotFoundError:
import game.PingPongRebound.cfg as cfg
import game.PingPongRebound.defs as df
# ------------------------------------------ GAMEOBJECT CLASS ------------------------------------------ #
# object class
class GameObject:
( dx, fx, px, sx ) = ( 0, 0, 0, 0 )
( dy, fy, py, sy ) = ( 0, 0, 0, 0 )
def __init__( self, _id, _game, _x, _y, _w, _h, _maxSpeed = 160 ): #_type = df.OBJ_BASE ):
self.game = _game
self.id = _id
self.setSize( int( _w ), int ( _h ))
self.setPos( int( _x ), int( _y ))
self.maxSpeed = int( _maxSpeed )
self.setDirs( 0, 0 )
self.setSpeeds( 0, 0 )
#self.type = _type
if cfg.DEBUG_MODE:
self.box = pg.Rect( _game.width / 2, _game.height / 2, _w, _h )
def getCopy( self ):
copy = GameObject( self.id, self.game, self.px, self.py, self.sx, self.sy, self.maxSpeed )
copy.setSpeeds( self.dx, self.dy )
copy.setDirs( self.fx, self.fy )
return copy
def drawSelf( self ): # NOTE : DEBUG MODE ONLY
if cfg.DEBUG_MODE:
self.box.center = ( self.px, self.py )
pg.draw.rect( self.game.win, df.COL_OBJ, self.box )
else:
print( "GameObject.drawSelf() is a DEBUG_MODE function" )
# ---------------------------------------------- POSITION ---------------------------------------------- #
def setSize( self, _w, _h ): # NOTE : .sx and .sy are half lenghts
self.sx = int( _w / 2 )
self.sy = int( _h / 2 )
def getSize( self ):
return( int( 2 * self.sx ), int( 2 * self.sy ))
def setLeft( self, _x ):
self.px = int( _x + self.sx )
def setRight( self, _x ):
self.px = int( _x - self.sx )
def setTop( self, _y ):
self.py = int( _y + self.sy )
def setBottom( self, _y ):
self.py = int( _y - self.sy )
def getLeft( self ):
return int( self.px - self.sx )
def getRight( self ):
return int( self.px + self.sx )
def getTop( self ):
return int( self.py - self.sy )
def getBottom( self ):
return int( self.py + self.sy )
def setPos( self, _x, _y ):
self.px = int( _x )
self.py = int( _y )
def setPosX( self, _x ):
self.px = int( _x )
def setPosY( self, _y ):
self.py = int( _y )
def getPos( self ):
return( self.px, self.py )
def getPosX( self ):
return self.px
def getPosY( self ):
return self.py
def updatePos( self ):
self.clampSpeed()
self.px += self.dx * self.fx
self.py += self.dy * self.fy
self.px = int( self.px )
self.py = int( self.py )
def clampPos( self ):
# prevent balls from going off screen
if self.getLeft() < 0:
self.setLeft( 0 )
if cfg.PRINT_EXTRA:
print( "clamping pos to left" )
if self.getRight() > self.game.width:
self.setRight( self.game.width )
if cfg.PRINT_EXTRA:
print( "clamping pos to right" )
if self.getTop() < 0:
self.setTop( 0 )
if cfg.PRINT_EXTRA:
print( "clamping pos to top" )
if self.getBottom() > self.game.height:
self.setBottom( self.game.height )
if cfg.PRINT_EXTRA:
print( "clamping pos to bottom" )
def isOnScreen( self ):
if self.getLeft() < 0 or self.getRight() > self.game.width:
return False
if self.getTop() < 0 or self.getBottom() > self.game.height:
return False
return True
def isOnScreenX( self ):
if self.getLeft() < 0 or self.getRight() > self.game.width:
return False
return True
def isOnScreenY( self ):
if self.getTop() < 0 or self.getBottom() > self.game.height:
return False
return True
# ---------------------------------------------- COLLISION --------------------------------------------- #
def isOverlaping( self, other ):
if self.getRight() >= other.getLeft() and self.getLeft() <= other.getRight():
if self.getBottom() >= other.getTop() and self.getTop() <= other.getBottom():
return True
return False
def bounceOnWall( self, mode ):
if mode == "stop":
self.stopDirs()
return
# vertical surface bounces ( | )
elif mode == "x":
self.fx *= -1
self.dx *= self.game.factor_wall
if cfg.PRINT_COLLISIONS and cfg.PRINT_EXTRA:
print ( "bouncing on x" )
# horizontal surface bounces ( -- )
elif mode == "y":
self.fy *= -1
self.dy *= self.game.factor_wall
if cfg.PRINT_COLLISIONS and cfg.PRINT_EXTRA:
print ( "bouncing on y" )
self.clampSpeed()
if df.NO_STUCK_BALLS:
self.makeUnstuck()
if cfg.PRINT_COLLISIONS:
t = time.time() - self.game.start_time
print( f"{self.game.gameID} ) {self.game.type} \t: wall bounce at {'{:.1f}'.format( t )}s" )
# NOTE : IS ONLY FOR BALLS
def bounceOnRack( self, other, mode ):
# vertical surface bounces ( | )
if mode == "x":
self.fx *= -1
self.dx *= self.game.factor_rack
self.dy = int( self.dy + ( other.getMvY() * df.KICK_FACTOR * df.getSign( self.fy )))
if cfg.PRINT_COLLISIONS and cfg.PRINT_EXTRA:
print ( "bouncing on x" )
# horizontal surface bounces ( -- )
elif mode == "y":
self.fy *= -1
self.dy *= self.game.factor_rack
self.dx = int( self.dx + ( other.getMvX() * df.KICK_FACTOR * df.getSign( self.fx )))
if cfg.PRINT_COLLISIONS and cfg.PRINT_EXTRA:
print ( "bouncing on y" )
if cfg.PRINT_COLLISIONS:
t = time.time() - self.game.start_time
print( f"{self.game.gameID} ) {self.game.type} \t: racket bounce at {'{:.1f}'.format( t )}s" )
self.clampSpeed()
if df.NO_STUCK_BALLS:
self.makeUnstuck()
def makeUnstuck( self ):
if self.dy < 1:
self.dy = 1
if self.py < self.game.height / 2:
self.fy = 1
else:
self.fy = -1
if cfg.PRINT_EXTRA:
print( "unstuck on x" )
if self.dx < 1:
self.dx = 1
if self.px < self.game.width / 2:
self.fx = 1
else:
self.fx = -1
if cfg.PRINT_EXTRA:
print( "unstuck on y" )
# ---------------------------------------------- MOVEMENT ---------------------------------------------- #
def setSpeeds( self, _dx, _dy ):
self.dx = int( _dx )
self.dy = int ( _dy )
self.clampSpeed()
def getSpeeds( self ):
return( self.dx, self.dy )
def setDirs( self, _fx, _fy ):
self.fx = int( _fx )
self.fy = int( _fy )
def getDirs( self ):
return( self.fx, self.fy )
def getMove( self ):
return( self.fx * self.dx, self.fy * self.dy )
def getMvX( self ):
return( self.fx * self.dx )
def getMvY( self ):
return( self.fy * self.dy )
def stopSpeeds( self ):
self.dx = 0
self.dy = 0
def stopDirs( self ):
self.fx = 0
self.fy = 0
def clampSpeed( self ):
# making sure dx is positive
if self.dx < 0:
self.dx *= -1
self.fx *= -1
if cfg.PRINT_EXTRA:
print( "inverting speed on x" )
# making sure dy is positive
if self.dy < 0:
self.dy *= -1
self.fy *= -1
if cfg.PRINT_EXTRA:
print( "inverting speed on y" )
self.checkMaxSpeed()
def checkMaxSpeed( self ):
# checking on x
if abs( self.dx * self.fx ) > self.maxSpeed:
if self.dx > self.maxSpeed: # NOTE : handling for balls (variable dx/dy)
self.dx = self.maxSpeed
else: # NOTE : handling for rackets (variable fx/fy)
while abs( self.dx * self.fx ) > self.maxSpeed:
self.fx -= df.getSign( self.fx )
if cfg.PRINT_EXTRA:
print( "clamping speed on x" )
# checking on y
if abs( self.dy * self.fy ) > self.maxSpeed:
if self.dy > self.maxSpeed: # NOTE : handling for balls (variable dx/dy)
self.dy = self.maxSpeed
else: # NOTE : handling for rackets (variable fx/fy)
while abs( self.dy * self.fy ) > self.maxSpeed:
self.fy -= df.getSign( self.fy )
if cfg.PRINT_EXTRA:
print( "clamping speed on y" )
# ---------------------------------------------- DIRECTION --------------------------------------------- #
def isGoingLeft( self ):
if self.fx < 0:
return True
return False
def isGoingRight( self ):
if self.fx > 0:
return True
return False
def isGoingUp( self ):
if self.fy < 0:
return True
return False
def isGoingDown( self ):
if self.fy > 0:
return True
return False
def isLeftOfX( self, X ):
if self.getRight() < X:
return True
return False
def isRightOfX( self, X ):
if self.getLeft() > X:
return True
return False
def isAboveY( self, Y ):
if self.getBottom() < Y:
return True
return False
def isBelowY( self, Y ):
if self.getTop() > Y:
return True
return False
def isLeftOf( self, other ):
return self.isLeftOfX( other.getLeft() )
def isRightOf( self, other ):
return self.isRightOfX( other.getRight() )
def isAbove( self, other ):
return self.isAboveY( other.getTop() )
def isBelow( self, other ):
return self.isBelowY( other.getBottom() )