-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPongFramework.py
267 lines (193 loc) · 9.31 KB
/
PongFramework.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
from GameFramework import *
class PongGM(GameManager):
def __init__(self, screenSize, screenCaption, allowedEvents):
super().__init__(screenSize, screenCaption, allowedEvents)
#controlls
self.wDown = False
self.sDown = False
self.upDown = False
self.downDown = False
self.currentScene = StartMenu(self)
"""
Scenes
"""
class StartMenu(Scene):
def __init__(self, gameManager):
super().__init__(gameManager)
playButtonSize = Vector2(176, 96)
playButton = Button(self, gameManager.screenSize / 2 - playButtonSize / 2, playButtonSize, Colour.black, Colour.grey, "PLAY", 60, Colour.white, pyg.font.get_default_font(), Vector2(10, 10))
playButton.scripts.append(OnButtonPressedChangeScene(playButton, TwoPlayer(self.gameManager)))
class TwoPlayer(Scene):
def __init__(self, gameManager):
super().__init__(gameManager)
usedColours = []
barrierColour = GetUniqueColour(usedColours)
barrierOffset = Vector2(0, 70)
barrierSize = Vector2(gameManager.screenSize.x, 30)
topBarrier = Box(self, barrierOffset, barrierSize, barrierColour)
topBarrier.collider = BoxCollider(topBarrier, barrierSize)
bottomBarrierPosition = Vector2(0, gameManager.screenSize.y - barrierSize.y - barrierOffset.y)
bottomBarrier = Box(self, bottomBarrierPosition, barrierSize, barrierColour)
bottomBarrier.collider = BoxCollider(bottomBarrier, barrierSize)
paddleSize = Vector2(16, 70)
paddleYPosition = gameManager.screenSize.y / 2 - paddleSize.y / 2
paddleXOffset = 30
paddleLeftColour = GetUniqueColour(usedColours)
paddleLeft = Box(self, Vector2(paddleXOffset, paddleYPosition), paddleSize, paddleLeftColour)
paddleLeft.collider = BoxCollider(paddleLeft, paddleSize)
paddleLeft.scripts.append(PlayerController(paddleLeft, 1))
paddleLeft.rigidBody = RigidBody(paddleLeft)
paddleRightColour = GetUniqueColour(usedColours)
paddleRight = Box(self, Vector2(gameManager.screenSize.x - paddleXOffset - paddleSize.x, paddleYPosition),
paddleSize, paddleRightColour)
paddleRight.collider = BoxCollider(paddleRight, paddleSize)
paddleRight.scripts.append(PlayerController(paddleRight, 2))
paddleRight.rigidBody = RigidBody(paddleRight)
goalSize = Vector2(100, gameManager.screenSize.y)
goalLeft = Box(self, Vector2(-goalSize.x, 0), goalSize, Colour.black)
goalLeft.collider = BoxCollider(goalLeft, goalSize)
goalRight = Box(self, Vector2(gameManager.screenSize.x, 0), goalSize, Colour.black)
goalRight.collider = BoxCollider(goalRight, goalSize)
scoreY = 8
scoreXSeparation = 140
scoreBoxSize = Vector2(50, 50)
scoreFontSize = 42
scoreTextOffset = Vector2(5, 5)
scoreFont = "Times New Roman"
scoreLeft = Counter(self, Vector2(gameManager.screenSize.x / 2 - scoreBoxSize.x / 2 - scoreXSeparation, scoreY), scoreBoxSize, Colour.black, 0, scoreFontSize, paddleLeftColour, scoreFont, scoreTextOffset)
scoreRight = Counter(self, Vector2(gameManager.screenSize.x / 2 - scoreBoxSize.x / 2 + scoreXSeparation, scoreY), scoreBoxSize, Colour.black, 0, scoreFontSize, paddleRightColour, scoreFont, scoreTextOffset)
puckSize = Vector2(10, 10)
puckStartPosition = gameManager.screenSize / 2 - puckSize / 2
puck = Box(self, puckStartPosition, puckSize, Colour.white)
puck.collider = BoxCollider(puck, puckSize)
puck.rigidBody = RigidBody(puck)
puck.initialSpeed = 9
puck.rigidBody.velocity = RandomVelocity(puck.initialSpeed)
puck.scripts.append(PuckController(puck, topBarrier, bottomBarrier, paddleLeft, paddleRight, goalLeft, goalRight, scoreLeft, scoreRight))
maxScore = 2
scoreKeeper = GameObject(self)
scoreKeeper.sprite.enabled = False
scoreKeeper.scripts.append(ScoreKeeper(scoreKeeper, maxScore, "end menu", scoreLeft, scoreRight))
class EndMenu(Scene):
def __init__(self, gameManager, winner):
super().__init__(gameManager)
boxOfftset = 150
playAgainButtonSize = Vector2(370, 96)
playAgainButton = Button(self, gameManager.screenSize / 2 - playAgainButtonSize / 2 + Vector2.Up() * boxOfftset, playAgainButtonSize , Colour.black, Colour.grey, "PLAY AGAIN", 60, Colour.white, "Times New Roman", Vector2(10, 10))
playAgainButton.scripts.append(OnButtonPressedChangeScene(playAgainButton, TwoPlayer(self.gameManager)))
winnerTextSize = Vector2(400, 96)
winnerText = TextBox(self, gameManager.screenSize / 2 - winnerTextSize / 2 + Vector2.Down() * boxOfftset, winnerTextSize, Colour.black, winner, 60, Colour.Random(), "Times New Roman", Vector2(10, 10))
"""
Custom Game Objects
"""
"""
Custom Scripts
"""
class PlayerController(Script):
#Script to be attached to paddles. Handles player controls.
speed = 10
def __init__(self, parent, playerNum):
super().__init__(parent)
self.playerNum = playerNum
gameManager = self.parent.gameManager
if playerNum == 1:
self.up = gameManager.wDown
self.down = gameManager.sDown
elif playerNum == 2:
self.up = gameManager.upDown
self.down = gameManager.downDown
def Update(self):
rigidBody = self.parent.rigidBody
gameManager = self.parent.gameManager
if self.playerNum == 1:
self.up = gameManager.wDown
self.down = gameManager.sDown
elif self.playerNum == 2:
self.up = gameManager.upDown
self.down = gameManager.downDown
if self.down:
rigidBody.velocity = Vector2.Up() * PlayerController.speed
elif self.up:
rigidBody.velocity = Vector2.Down() * PlayerController.speed
else:
rigidBody.velocity = Vector2.Zero()
class PuckController(Script):
#script to be attached to puck. Handles puck physics and scoring
def __init__(self, parent, top, bottom, paddleLeft, paddleRight, goalLeft, goalRight, scoreLeft, scoreRight):
super().__init__(parent)
self.top = top
self.bottom = bottom
self.paddleL = paddleLeft
self.paddleR = paddleRight
self.goalL = goalLeft
self.goalR = goalRight
self.scoreL = scoreLeft
self.scoreR = scoreRight
self.velocityIncrease = 1
def Update(self):
if self.parent.collider.collisions != []:
for collision in self.parent.collider.collisions:
if collision.parent == self.top or collision.parent == self.bottom:
self.parent.rigidBody.velocity.y *= -1
if collision.parent == self.paddleL or collision.parent == self.paddleR:
IncreaseSpeed(self.parent.rigidBody.velocity, self.velocityIncrease)
self.parent.rigidBody.velocity.x *= -1
if collision.parent == self.goalL:
ResetPuck(self.parent)
self.scoreR.count +=1
self.scoreR.UpdateCount()
elif collision.parent == self.goalR:
ResetPuck(self.parent)
self.scoreL.count += 1
self.scoreL.UpdateCount()
class ScoreKeeper(Script):
#Checks if the max score is exeeded, if true changes scene to end menu
def __init__(self, parent, maxScore, nextScene, scoreLeft, scoreRight):
super().__init__(parent)
self.maxScore = maxScore
self.nextScene = nextScene
self.scoreL = scoreLeft
self.scoreR = scoreRight
def Update(self):
if self.scoreL.count == self.maxScore:
self.parent.scene.gameManager.ChangeScene(EndMenu(self.parent.scene.gameManager, "PLAYER 1 WINS"))
elif self.scoreR.count == self.maxScore:
self.parent.scene.gameManager.ChangeScene(EndMenu(self.parent.scene.gameManager, "PLAYER 2 WINS"))
"""
Custom functions
"""
def RandomVelocity(speed):
#gives the puck a random diagonal velocity when a new point is started
rand = random.random()
if rand > 0.5:
x = speed
else:
x = -speed
rand = random.random()
if rand > 0.5:
y = speed
else:
y = -speed
return Vector2(x, y)
def IncreaseSpeed(velocity, increaseMod):
#increases the speed of the puck
if velocity.x > 0:
velocity.x += increaseMod
elif velocity.x < 0:
velocity.x -= increaseMod
if velocity.y > 0:
velocity.y += increaseMod
elif velocity.y < 0:
velocity.y -= increaseMod
def ResetPuck(puck):
#resets puck for a new point
screenSize = puck.scene.gameManager.screenSize
puck.rigidBody.velocity = RandomVelocity(puck.initialSpeed)
puck.transform.position = screenSize / 2
def GetUniqueColour(usedColours):
#returns a colour that has not been used
colour = Colour.Random()
while colour in usedColours:
colour = Colour.Random()
usedColours.append(colour)
return colour