This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pong.py
382 lines (330 loc) · 11.6 KB
/
pong.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
import pygame, sys, random, os
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Pygame init
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
clock = pygame.time.Clock()
pygame.key.set_repeat(20,1)
pygame.display.set_caption('Pong - nullpointer.at')
# Farben
BLACK = (0,0,0)
WHITE = (255,255,255)
# Schriftart
basicFont = pygame.font.SysFont(None, 48)
startFont = pygame.font.SysFont(None, 28)
playerFont = pygame.font.SysFont(None, 20)
punktFont = pygame.font.SysFont(None, 72)
#Win Loose Text
winText = basicFont.render('WIN', True, WHITE, BLACK)
looseText = basicFont.render('LOOSE', True, WHITE, BLACK)
winTextRect = winText.get_rect()
looseTextRect = looseText.get_rect()
# Starttext
restartBoolean = True
startText = startFont.render('press SPACE to start a new game', True, WHITE, BLACK)
startTextRect = startText.get_rect()
centerx = screen.get_rect().centerx
y = 240
startTextRect.centerx = centerx
startTextRect.y = y
musicText = startFont.render('m to mute music', True, WHITE, BLACK)
musicTextRect = musicText.get_rect()
musicTextRect.centerx = centerx
musicTextRect.y = y + 30
soundText = startFont.render('s to mute sounds', True, WHITE, BLACK)
soundTextRect = soundText.get_rect()
soundTextRect.centerx = centerx
soundTextRect.y = y + 60
backText = startFont.render('1-3 to switch soundtrack', True, WHITE, BLACK)
backTextRect = backText.get_rect()
backTextRect.centerx = centerx
backTextRect.y = y + 90
#Background
bif = "background.jpg"
background = pygame.image.load(bif).convert()
# Flugrichtungen
speed = 0
startspeed = 100
speed_plus = 20
winkel = 0
# -1 up 1 down
ball_dir_updown = 1
# -1 left 1 down
ball_dir_leftright = 1
#Ball
ball_x = 302
ball_y = 162
ball_gr = (36,36)
ball_position_root = (ball_x,ball_y)
ball_position = (ball_x,ball_y)
ballif = "ball.png"
ball = pygame.image.load(ballif).convert_alpha()
#Balken
playerspeed = 250
#temporaere startpositionen
a_x = 30
a_y = 162
b_x = 595
b_y = 30
balken_gr = (16,95)
balkenif = "balken.png"
balken = pygame.image.load(balkenif).convert()
# Punkte
player_a = 0
player_a_pkt_pos = (60, 55)
player_b = 0
player_b_pkt_pos = (540, 55)
#Spielernamen
playeraText = playerFont.render('Computer', True, WHITE, BLACK)
playeraTextRect = playeraText.get_rect()
playeraTextRect.x = 50
playeraTextRect.y = 35
playerbText = playerFont.render('Spieler', True, WHITE, BLACK)
playerbTextRect = playerbText.get_rect()
playerbTextRect.x = 530
playerbTextRect.y = 35
#Punkteanzeige
punkt0Text = punktFont.render('0', True, WHITE, BLACK)
punkt1Text = punktFont.render('1', True, WHITE, BLACK)
punkt2Text = punktFont.render('2', True, WHITE, BLACK)
punkt3Text = punktFont.render('3', True, WHITE, BLACK)
punkt4Text = punktFont.render('4', True, WHITE, BLACK)
punkt5Text = punktFont.render('5', True, WHITE, BLACK)
# set up music
soundBoolean = True
balkenSound = pygame.mixer.Sound('balken.wav')
punktSound = pygame.mixer.Sound('punkt.wav')
wandSound = pygame.mixer.Sound('wand.wav')
def set_musik(music):
if music == 1:
pygame.mixer.music.load('back1.ogg')
elif music == 2:
pygame.mixer.music.load('back2.ogg')
else:
pygame.mixer.music.load('back3.ogg')
def draw_punkte(punkt, pos):
punktText = punkt0Text
if punkt == 1:
punktText = punkt1Text
elif punkt == 2:
punktText = punkt2Text
elif punkt == 3:
punktText = punkt3Text
elif punkt == 4:
punktText = punkt4Text
elif punkt == 5:
punktText = punkt5Text
screen.blit(punktText, pos)
def ball_hit_wall(ball_pos, ball_gr):
global ball_dir_updown
global ball_dir_leftright
global ball_x
global ball_y
global player_a
global player_b
global speed
global winkel
# Ball trifft oberen Bereich
if ball_pos[1] < 0:
ball_dir_updown = 1
winkel *= -1
if soundBoolean:
wandSound.play()
# Ball trifft unteren Bereich
elif ball_pos[1] + ball_gr[1] > 360:
ball_dir_updown = -1
winkel *= -1
if soundBoolean:
wandSound.play()
# Punkt Spieler B
if ball_pos[0] < 0:
player_b += 1
ball_dir_leftright = 1
ball_x = 302
ball_y = 162
speed = startspeed
winkel = 0
if soundBoolean:
punktSound.play()
# Punkt Spieler A
elif ball_pos[0] + ball_gr[0] > 640:
player_a += 1
ball_dir_leftright = -1
ball_x = 302
ball_y = 162
speed = startspeed
winkel = 0
if soundBoolean:
punktSound.play()
def move_player_a(ball_y):
global a_y
if a_y + 31 > ball_y and a_y > 0:
#hoch
a_y += seconds * playerspeed * -1
elif a_y + 31 < ball_y and a_y + 95 < 360:
#runter
a_y += seconds * playerspeed * 1
def object_hit(a_pos, a_gr, b_pos, b_gr, seconds):
global ball_dir_leftright
global speed
global winkel
# errechne die 4 ecken von a
aa = a_pos
ab = tuple([a_pos[0] + a_gr[0], a_pos[1]])
ac = tuple([a_pos[0] + a_gr[0], a_pos[1] + a_gr[1]])
ad = tuple([a_pos[0], a_pos[1] + a_gr[1]])
# errechne die 4 ecken von b
ba = b_pos
bb = tuple([b_pos[0] + b_gr[0], b_pos[1]])
bc = tuple([b_pos[0] + b_gr[0], b_pos[1] + b_gr[1]])
bd = tuple([b_pos[0], b_pos[1] + b_gr[1]])
winkelmin = 1
winkelmax = 10
maxErreichterWinkel = 45
#computerseite
if aa[0] < 320:
if punkt_innerhalb(aa, ba, bb, bc, bd) and punkt_innerhalb(ad, ba, bb, bc, bd):
ball_dir_leftright = 1
speed += speed_plus
if soundBoolean:
balkenSound.play()
elif punkt_innerhalb(aa, ba, bb, bc, bd):
ball_dir_leftright = 1
speed += speed_plus
if winkel > maxErreichterWinkel * -1:
winkel += random.randint(winkelmax * -1, winkelmin * -1) * seconds * 5
if soundBoolean:
balkenSound.play()
elif punkt_innerhalb(ad, ba, bb, bc, bd):
ball_dir_leftright = 1
speed += speed_plus
if winkel < maxErreichterWinkel:
winkel += random.randint(winkelmin, winkelmax) * seconds * 5
if soundBoolean:
balkenSound.play()
#spielerseite
else:
if punkt_innerhalb(ab, ba, bb, bc, bd) and punkt_innerhalb(ac, ba, bb, bc, bd):
ball_dir_leftright = -1
speed += speed_plus
if soundBoolean:
balkenSound.play()
elif punkt_innerhalb(ab, ba, bb, bc, bd):
ball_dir_leftright = -1
speed += speed_plus
if winkel > maxErreichterWinkel * -1:
winkel += random.randint(winkelmax * -1, winkelmin * -1) * seconds * 5
if soundBoolean:
balkenSound.play()
elif punkt_innerhalb(ac, ba, bb, bc, bd):
ball_dir_leftright = -1
speed += speed_plus
if winkel < maxErreichterWinkel:
winkel += random.randint(winkelmin, winkelmax) * seconds * 5
if soundBoolean:
balkenSound.play()
def punkt_innerhalb(aa, ba, bb, bc, bd):
if ba[0] < aa[0] < bb[0] and ba[1] < aa[1] < bd[1]:
return True
else:
return False
def change_angle():
pass
def change_direction():
pass
#start musik
music = random.randint(1,3)
set_musik(music)
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == ord('m'):
if musicPlaying:
pygame.mixer.music.stop()
else:
pygame.mixer.music.play(-1, 0.0)
musicPlaying = not musicPlaying
if event.key == ord('s'):
soundBoolean = not soundBoolean
if event.key == K_1:
pygame.mixer.music.stop()
set_musik(1)
pygame.mixer.music.play(-1, 0.0)
if event.key == K_2:
pygame.mixer.music.stop()
set_musik(2)
pygame.mixer.music.play(-1, 0.0)
if event.key == K_3:
pygame.mixer.music.stop()
set_musik(3)
pygame.mixer.music.play(-1, 0.0)
if restartBoolean:
if event.key == K_SPACE:
player_a = 0
player_b = 0
speed = startspeed
ball_x = 302
ball_y = 162
restartBoolean = False
## if event.type == KEYDOWN:
## if event.key == K_DOWN and b_y + 95 < 360:
## b_y += seconds * playerspeed * 1
## if event.key == K_UP and b_y > 0:
## b_y += seconds * playerspeed * -1
key_pressed = pygame.key.get_pressed()
if key_pressed[K_DOWN] and b_y + 95 < 360:
b_y += seconds * playerspeed * 1
if key_pressed[K_UP] and b_y > 0:
b_y += seconds * playerspeed * -1
screen.blit(background, (0,0))
screen.blit(playeraText, playeraTextRect)
screen.blit(playerbText, playerbTextRect)
balken_a_pos = (a_x, a_y)
balken_b_pos = (b_x, b_y)
draw_punkte(player_a, player_a_pkt_pos)
draw_punkte(player_b, player_b_pkt_pos)
screen.blit(balken, balken_a_pos)
screen.blit(balken, balken_b_pos)
screen.blit(ball, ball_position)
milli = clock.tick()
seconds = milli/1000.
dmx = seconds * speed * ball_dir_leftright
dmy = seconds * speed * ball_dir_updown
ball_x += dmx
ball_y += dmy + winkel
ball_position = (ball_x,ball_y)
# ball hits player a
if object_hit(ball_position, ball_gr, balken_a_pos, balken_gr, seconds):
change_direction()
change_angle()
# ball hits player b
elif object_hit(ball_position, ball_gr, balken_b_pos, balken_gr, seconds):
change_direction()
change_angle()
ball_hit_wall(ball_position, ball_gr)
move_player_a(ball_y)
if player_a == 5 or player_b == 5:
speed = 0
if player_a == 5:
winTextRect.x = 80
looseTextRect.x = 450
else:
winTextRect.x = 450
looseTextRect.x = 80
looseTextRect.y = 160
winTextRect.y = 160
screen.blit(winText, winTextRect)
screen.blit(looseText, looseTextRect)
restartBoolean = True
if restartBoolean:
screen.blit(startText, startTextRect)
screen.blit(musicText, musicTextRect)
screen.blit(soundText, soundTextRect)
screen.blit(backText, backTextRect)
pygame.display.flip()