-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameControler.py
115 lines (80 loc) · 2.34 KB
/
GameControler.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
try:
import defs as df
except ModuleNotFoundError:
import game.PingPongRebound.defs as df
# controler class
class GameControler:
name = "unnamed"
game = None
racket = None
racketDir = None
playerID = 0
mode = df.CONTROLER
def __init__( self, _game ):
self.game = _game
self.defaultX = _game.width / 2
self.defaultY = _game.height / 2
def setRacket( self, racketID ):
self.racket = self.game.getRacket( racketID )
if self.racket != None:
self.recordDefaultPos()
else:
print( "Error : racket not found" )
def recordDefaultPos( self ):
self.defaultX = self.racket.getPosX()
self.defaultY = self.racket.getPosY()
if self.racket.dx != 0:
self.racketDir = 'x'
elif self.racket.dy != 0:
self.racketDir = 'y'
self.goal = self.findOwnGoal()
def findOwnGoal( self ):
if self.racketDir == 'x':
if( self.defaultY < self.game.height / 2 ): # goal is on the top
return df.UP
else:
return df.DOWN
elif self.racketDir == 'y':
if( self.defaultX < ( self.game.width / 2 )): # goal is on the left
return df.LEFT
else:
return df.RIGHT
def playMove( self, move ):
if self.racket == 0:
print( "Error: no racket selected" )
elif self.game.state == df.STARTING:
print( "The game has not started yet" )
elif self.game.state == df.ENDING:
print( "The game is over" )
elif move != df.NULL:
self.game.makeMove( self.racket.id, move )
def stopHere( self ):
if self.racket.fx != 0 or self.racket.fy != 0:
self.playMove( df.STOP )
def goUp( self, maxFactor ):
if abs( self.racket.fy ) <= maxFactor:
self.playMove( df.UP )
def goRight( self, maxFactor ):
if abs( self.racket.fx ) <= maxFactor:
self.playMove( df.RIGHT )
def goDown( self, maxFactor ):
if abs( self.racket.fy ) <= maxFactor:
self.playMove( df.DOWN )
def goLeft( self, maxFactor ):
if abs( self.racket.fx ) <= maxFactor:
self.playMove( df.LEFT )
def goToCenter( self, maxFactor ):
self.goTo( maxFactor, self.game.width / 2, self.game.height / 2 )
def goToDefaultPos( self, maxFactor ):
self.goTo( maxFactor, self.defaultX, self.defaultY )
def getInfo( self ):
if self.mode == df.PLAYER:
isBot = False
else:
isBot = True
return {
"isBot": isBot,
"name": self.name,
"playerID": self.playerID,
"teamID": self.racket.id # this won't work on team games (ponger / pinger )
}