-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgame.py
More file actions
493 lines (423 loc) · 19.8 KB
/
game.py
File metadata and controls
493 lines (423 loc) · 19.8 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -----imports-----
import pygame
import time
from pygame import mixer
from os.path import exists
import sys
# ---Global variables---
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-W", "--width", dest="width", type=int, metavar="WIDTH", help="Set window width", default=800)
parser.add_argument("-H", "--height", dest="height", type=int, metavar="HEIGHT", help="Set window height", default=400)
parser.add_argument("-fps", "--framerate", dest="framerate", type=int, metavar="FRAMERATE",
help="Set desired framerate", default=60)
parser.add_argument("-fc", "--flipcolors", dest="fc", action="store_true", help="Flip black and white for another look of the game")
parser.add_argument("-sc", "--setcolor", dest="setcolors", type=int, default=0, help="Set color scheme using predefined setups. Values go from 0 to 9.")
parser.add_argument("-sfc", "--setfirstcolor", dest="setfirstcolor", type=str, default="black", help="Set the first color using basic color names.")
parser.add_argument("-ssc", "--setsecondcolor", dest="setsecondcolor", type=str, default="white", help="Set the second color using basic color names.")
parser.set_defaults(fc=False)
args = parser.parse_args()
if args.setcolors and (args.setfirstcolor or args.setsecondcolor):
print("-sc and -sfc/-ssc are mutually exclusive ...")
sys.exit(2)
screenWidth = args.width
screenHeight = args.height
FPS = args.framerate
flipcolors = args.fc
setcolors = args.setcolors
setFirstColor = args.setfirstcolor
setSecondColor = args.setsecondcolor
goals = [0, 0]
# colors
ownYellow = (230, 255, 0)
ownDarkBlue = (0, 10, 130)
ownBlue = (0,0,255)
ownPurple = (119, 0, 255)
ownPink = (255, 0, 230)
ownGreen = (0,255,0)
ownOrange = (255, 94, 0)
ownBlack = (0,0,0)
ownWhite = (255,255,255)
ownRed = (255,0,0)
colorSchemes = {
0: {
"first": ownBlack,
"second": ownWhite
},
1: {
"first": ownWhite,
"second": ownBlack
},
2: {
"first": ownBlack,
"second": ownYellow
},
3: {
"first": ownBlack,
"second": ownRed
},
4: {
"first": ownBlack,
"second": ownBlue
},
5: {
"first": ownBlack,
"second": ownGreen
},
6: {
"first": ownWhite,
"second": ownYellow
},
7: {
"first": ownWhite,
"second": ownRed
},
8: {
"first": ownWhite,
"second": ownBlue
},
9: {
"first": ownWhite,
"second": ownGreen
}
}
colorDict = {
"yellow": ownYellow,
"orange": ownOrange,
"red": ownRed,
"purple": ownPurple,
"pink": ownPink,
"blue": ownBlue,
"green": ownGreen,
"white": ownWhite,
"black": ownBlack
}
if (args.setfirstcolor):
if setFirstColor in colorDict:
firstColor = colorDict[setFirstColor]
else:
print("A color you're trying to set does not exist here yet.")
sys.exit(2)
if (args.setsecondcolor):
if setSecondColor in colorDict:
secondColor = colorDict[setSecondColor]
else:
print("A color you're trying to set does not exist here yet.")
sys.exit(2)
if (args.setcolors):
# flip black and white if the flipcolors option is set
if(flipcolors):
setcolors = 1
firstColor = colorSchemes[setcolors]["first"]
secondColor = colorSchemes[setcolors]["second"]
# -----Initializing the game-------
def init():
pygame.init() # initialize pygame module
logo = pygame.image.load("resources/logo32x32.png") # Load logo
pygame.display.set_icon(logo) # Set logo
pygame.display.set_caption("Pong") # Set window Title
screen = pygame.display.set_mode(
(screenWidth, screenHeight)) # Surface on screen with size of screenWidth x screenHeight
screen.fill(firstColor)
settings(screen)
# -----Settings-----
def settings(screen):
font = pygame.font.SysFont("arialroundedmtbold", 24)
settingsText = font.render("Settings", True, secondColor)
screen.blit(settingsText, (screenWidth // 2 - settingsText.get_width() // 2, 10))
pygame.display.flip()
done = False
startgame = False
settingsList = [
"Initial speed of the ball",
"Number of players",
"Moving speed of the players",
"Ball size",
"Paddle Length",
"DONE"
]
settingsIterator = 0
settingsLength = len(settingsList)
playerSpeed = 10 # Pixels the player moves per frame (speed)
ballSpeed = 4 # "Speed" of the ball
ballSize = 1
playerNumber = 1
paddleLength = 1
lengthText = ["Short ", "Normal", "Long "] # Extra whitespace is to clear background when text changes
renderAndUpdate(screen, str(ballSpeed), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
time.sleep(0.1)
renderAndUpdate(screen, str(playerNumber), secondColor, firstColor,
(screenWidth // 2, 40 + 20 * settingsIterator + settingsText.get_height()))
time.sleep(0.1)
renderAndUpdate(screen, str(playerSpeed), secondColor, firstColor,
(screenWidth // 2, 60 + 20 * settingsIterator + settingsText.get_height()))
time.sleep(0.1)
renderAndUpdate(screen, str(ballSize), secondColor, firstColor,
(screenWidth // 2, 80 + 20 * settingsIterator + settingsText.get_height()))
time.sleep(0.1)
renderAndUpdate(screen, str(lengthText[paddleLength]), secondColor, firstColor,
(screenWidth // 2, 100 + 20 * settingsIterator + settingsText.get_height()))
while not done:
time.sleep(0.1)
for event in pygame.event.get():
if event.type == pygame.QUIT: # Event type = quit:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
settingsIterator += 1
# resetting settingsIterator to 0 if it would be higher than any index of a setting in the list
if settingsIterator == settingsLength:
settingsIterator = 0
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
settingsIterator -= 1
# resetting settingsIterator to last index of the settingsList if it would be lower than 0
if settingsIterator == -1:
settingsIterator = settingsLength - 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
# determining which setting is selected and increasing (or decreasing value of that setting)
if settingsIterator == 0:
ballSpeed += 1
renderAndUpdate(screen, str(ballSpeed), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 1:
if playerNumber == 1:
playerNumber = 2
else:
playerNumber = 1
renderAndUpdate(screen, str(playerNumber), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 2:
playerSpeed += 1
renderAndUpdate(screen, str(playerSpeed), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 3:
if (ballSize < 10):
ballSize += 1
renderAndUpdate(screen, str(ballSize), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 4:
paddleLength += 1
if (paddleLength > 2):
paddleLength = 0
renderAndUpdate(screen, str(lengthText[paddleLength]), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
if settingsIterator == 0:
ballSpeed -= 1
if ballSpeed == 0:
ballSpeed = 1
renderAndUpdate(screen, str(ballSpeed), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 1:
if playerNumber == 1:
playerNumber = 2
else:
playerNumber = 1
renderAndUpdate(screen, str(playerNumber), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 2:
playerSpeed -= 1
if playerSpeed == 0:
playerSpeed = 1
renderAndUpdate(screen, str(playerSpeed), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 3:
if (ballSize > 1):
ballSize -= 1
renderAndUpdate(screen, str(ballSize), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
elif settingsIterator == 4:
paddleLength -= 1
if (paddleLength < 0):
paddleLength = 2
renderAndUpdate(screen, str(lengthText[paddleLength]), secondColor, firstColor,
(screenWidth // 2, 20 + 20 * settingsIterator + settingsText.get_height()))
# if option "done" is selected and the return key is pressed the main function is called
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
if settingsIterator == settingsLength - 1: # True on the last setting in the list
done = True
startgame = True
# rendering the (selected) settings
for i in range(settingsLength):
if i == settingsIterator:
setText = font.render(settingsList[i], True, firstColor, secondColor)
screen.blit(setText, (10, 20 + 20 * i + setText.get_height()))
pygame.display.flip()
else:
setText = font.render(settingsList[i], True, secondColor, firstColor)
screen.blit(setText, (10, 20 + 20 * i + setText.get_height()))
pygame.display.flip()
if startgame:
if exists('resources/gamemusic.mp3'):
pygame.mixer.init()
playerMusic = pygame.mixer.music.load('resources/gamemusic.mp3')
pygame.mixer.music.play(-1)
screen.fill(firstColor)
main(screen, playerNumber, ballSpeed, playerSpeed, ballSize, paddleLength)
# -----Updating display with a text-----
def renderAndUpdate(screen, text, textColor, backgroundColor, pos):
font = pygame.font.SysFont("arialroundedmtbold", 24)
if len(text) == 2:
text += " "
elif len(text) == 1:
text += " "
valueText = font.render(text, True, textColor, backgroundColor)
screen.blit(valueText, pos)
pygame.display.flip()
# -----Update positions-----
def updatePos(xPos, yPos, oldRect, screen, image):
screen.fill(firstColor)
updatedRect = screen.blit(image, (xPos, yPos))
font = pygame.font.SysFont("arialroundedmtbold", 18)
goalText = font.render(str(goals[0]) + " : " + str(goals[1]), True, secondColor, firstColor)
pygame.display.update(updatedRect)
pygame.display.update(oldRect)
pygame.display.update(screen.blit(goalText, (screenWidth // 2 - goalText.get_width() // 2, 10)))
return updatedRect
# -----Main function--------------
def main(screen, playerCount, ballSpeed, playerSpeed, ballSize, paddleLength):
clock = pygame.time.Clock()
xstepB = ballSpeed
ystepB = ballSpeed
running = True # Variable for main loop control
playerImg = pygame.image.load("resources/player.png") # Load the player image
playerImg = pygame.transform.scale(playerImg, (playerImg.get_size()[0],
(int) (playerImg.get_size()[1] * [0.66, 1.0, 1.5][paddleLength]))) # Set the paddle length
ballImg = pygame.image.load("resources/ball.png")
ballImg = pygame.transform.scale(ballImg, [x * ballSize for x in ballImg.get_size()]) # Set the ball size
screen.fill(firstColor) # Fill the background with one colour (black)
playerWidth = playerImg.get_width() # Width of the player
playerHeight = playerImg.get_height() # Height of the player
xpos1 = 20 # X-Position of the player 1
xpos2 = screenWidth - 20 - playerWidth # X-Position of the player 2
ypos1 = screenHeight // 2 - playerHeight // 2 # Y-Position of the player 1
ypos2 = ypos1 # Y-Position of the player 2
ballWidth = ballImg.get_width() # Width of the ball
ballHeight = ballImg.get_height() # Height of the ball
xposB = xpos1 + playerWidth + 10 # X-Position of the ball
yposB = ypos1 + playerHeight // 2 - ballHeight // 2 # Y-Position of the ball
lastRects = [screen.blit(playerImg, (xpos1, ypos1)), # Which parts of the screen
screen.blit(playerImg, (xpos2, ypos2)), # needs to be updated
screen.blit(ballImg, (xposB, yposB))]
curRects = lastRects.copy()
pygame.display.flip() # Refresh the screen
firstStart = True
upPressed = False
downPressed = False
wPressed = False
sPressed = False
# -----main loop-----
while running:
# ---event handling, gets all events from event queue---
clock.tick(FPS)
if not firstStart:
# somebody scored
if (xposB >= screenWidth) or (xposB <= 0):
firstStart = True
if xposB >= screenWidth:
goals[0] += 1
else:
goals[1] += 1
# resetting ball position
xposB = screenWidth // 2 - ballWidth // 2 - xstepB
yposB = screenHeight // 2 - ballHeight // 2 - ystepB
# resetting speed of the ball
xstepB = ballSpeed
ystepB = ballSpeed
# reversing ball if its out of the window (top or bottom)
if (yposB + ballHeight >= screenHeight) or (yposB <= 0):
ystepB = -ystepB
# Collision check with player 1
# if ball with speed would be left of player 1
if xposB - abs(xstepB) <= (xpos1 + playerWidth):
# upDist = abs(yposB + ballHeight - ypos1)
# downDist = abs(yposB - ypos1 - playerHeight)
ystepBAbs = abs(ystepB)
if (xposB - xstepB - 3 <= (xpos1 + playerWidth) and (
yposB + ballHeight >= ypos1 and yposB <= ypos1 + playerHeight)):
# Ball entered through top or bottom and is still inside the paddle
ystepB = +ystepBAbs
elif (yposB + ballHeight > ypos1) and (yposB < ypos1 + playerHeight):
xAccelerator = 1 if xstepB > 1 else -1
xstepB = (-xstepB - xAccelerator)
yAccelerator = 1 if ystepB > 1 else -1
ystepB += yAccelerator
# Collision check with player 2
# if ball with speed is right to player 2
if (xposB + ballWidth) + abs(xstepB) >= xpos2:
# upDist = abs(yposB + ballHeight - ypos2)
# downDist = abs(yposB - ypos2 - playerHeight)
ystepBAbs = abs(ystepB)
if (xposB + ballWidth - xstepB + 3 >= xpos2) and (
yposB + ballHeight >= ypos2 and yposB <= ypos2 + playerHeight):
# Ball entered through top or bottom and is still inside the paddle
ystepB = +ystepBAbs
elif (yposB + ballHeight > ypos2) and (yposB < ypos2 + playerHeight):
xAccelerator = 1 if xstepB > 1 else -1
xstepB = (-xstepB - xAccelerator)
yAccelerator = 1 if ystepB > 1 else -1
ystepB += yAccelerator
xposB += xstepB
yposB += ystepB
curRects[2] = updatePos(xposB, yposB, lastRects[2], screen, ballImg)
lastRects[2] = curRects[2]
for event in pygame.event.get():
if event.type == pygame.QUIT: # Event type = quit:
running = False # Change running to False -> Main loop quits
# Switches firstStart variable if the game is started
if firstStart:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
firstStart = False
# Switches the variables for the key pressed when they are pressed or released
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
downPressed = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
upPressed = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
sPressed = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
wPressed = True
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
downPressed = False
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
upPressed = False
if event.type == pygame.KEYUP and event.key == pygame.K_s:
sPressed = False
if event.type == pygame.KEYUP and event.key == pygame.K_w:
wPressed = False
# --- Bot for one player gaming --- #
if playerCount == 1:
if yposB > ypos2 + playerSpeed and ypos2 <= screenHeight - playerSpeed - playerHeight:
# upPressed = False
# downPressed = True
ypos2 += playerSpeed
curRects[1] = updatePos(xpos2, ypos2, lastRects[1], screen, playerImg)
lastRects[1] = curRects[1]
elif yposB < ypos2 - playerSpeed and ypos2 >= playerSpeed - playerHeight:
# downPressed = False
# upPressed = True
ypos2 -= playerSpeed
curRects[1] = updatePos(xpos2, ypos2, lastRects[1], screen, playerImg)
lastRects[1] = curRects[1]
# Actually moves the players if a key is pressed
# First player
if sPressed and ypos1 <= screenHeight - playerSpeed - playerHeight:
ypos1 += playerSpeed
curRects[0] = updatePos(xpos1, ypos1, lastRects[0], screen, playerImg)
lastRects[0] = curRects[0]
if wPressed and ypos1 >= playerSpeed:
ypos1 -= playerSpeed
curRects[0] = updatePos(xpos1, ypos1, lastRects[0], screen, playerImg)
lastRects[0] = curRects[0]
# Second player
if playerCount == 2:
if downPressed and ypos2 <= screenHeight - playerSpeed - playerHeight:
ypos2 += playerSpeed
curRects[1] = updatePos(xpos2, ypos2, lastRects[1], screen, playerImg)
lastRects[1] = curRects[1]
if upPressed and ypos2 >= playerSpeed:
ypos2 -= playerSpeed
curRects[1] = updatePos(xpos2, ypos2, lastRects[1], screen, playerImg)
lastRects[1] = curRects[1]
if __name__ == "__main__": # Only if the script is called as main script not if its imported as a module
init() # Call init function