-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
823 lines (594 loc) · 27.1 KB
/
pong.py
File metadata and controls
823 lines (594 loc) · 27.1 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import pygame
import math
import random
import pathlib
from noiseengine import NoiseEngine1D
from vector import Vector2
# ======================================================================
# constants to help code readability
# ======================================================================
GAME_STATE_INTRO = 0
GAME_STATE_IN_PROGRESS = 1
GAME_STATE_SCORED = 2
GAME_STATE_OVER = 3
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 600
ORIGINX = SCREEN_WIDTH // 2
ORIGINY = SCREEN_HEIGHT // 2
COLOUR_BLACK = [0,0,0]
COLOUR_WHITE = [255,255,255]
COLOUR_STARS = [100,50,255]
COLOUR_YELLOW = [255,255,0]
COLOUR_RED = [255,0,0]
particles_SPAWN_FROM_OPPONENT = 180
particles_SPAWN_FROM_PLAYER = 0
# ======================================================================
# setup pygame
# ======================================================================
# set mixer to 512 value to stop buffering causing sound delay
# this must be called before anything else using mixer.pre_init()
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Pong 2020")
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
clock = pygame.time.Clock()
# ======================================================================
# load images and sounds
# ======================================================================
# pngs are saved with a black background layer in gimp with no transparency
# note - use convert() and not convert_alpha()
# instead I use set_colorkey to make black pixels transparent
# and now I can set the alpha value of each image
# using pathlib functions to make this cross platform (hopefully)
# get the path that this script is running from (current working dir)
FILEPATH = pathlib.Path().cwd()
sound_blip = pygame.mixer.Sound(str(FILEPATH.joinpath('sounds' ,'blip.ogg')))
sound_blip2 = pygame.mixer.Sound(str(FILEPATH.joinpath('sounds' ,'blip2.ogg')))
sound_score = pygame.mixer.Sound(str(FILEPATH.joinpath('sounds' ,'score.ogg')))
sound_boom = pygame.mixer.Sound(str(FILEPATH.joinpath('sounds' ,'boom.ogg')))
image_pong_title = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_title.png'))).convert()
image_pong_numbers = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_numbers.png'))).convert()
image_pong_game = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_game.png'))).convert()
image_pong_over = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_over.png'))).convert()
image_pong_you = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_you.png'))).convert()
image_pong_won = pygame.image.load(str(FILEPATH.joinpath('png' ,'pong_won.png'))).convert()
# set the transparent colour, in my case black
image_pong_title.set_colorkey(COLOUR_BLACK)
image_pong_numbers.set_colorkey(COLOUR_BLACK)
image_pong_game.set_colorkey(COLOUR_BLACK)
image_pong_over.set_colorkey(COLOUR_BLACK)
image_pong_you.set_colorkey(COLOUR_BLACK)
image_pong_won.set_colorkey(COLOUR_BLACK)
# ======================================================================
# chop the scoreboard numbers into individual surfaces
# ======================================================================
# set the alpha value
image_pong_numbers.set_alpha(100)
# the numbers are all stored as a single image.
# I use subsurface() to create a new image for each number
# and store them in a list for later use
image_offsets = [(0 ,0 ,110, 96),
(130 ,0 ,90 , 96),
(215 ,0 ,110, 96),
(340 ,0 ,110, 96),
(460 ,0 ,110, 96),
(580 ,0 ,110, 96),
(705 ,0 ,110, 96),
(825 ,0 ,110, 96),
(938 ,0 ,110, 96),
(1063,0 ,110, 96)]
image_numbers = []
for loc in image_offsets:
img = image_pong_numbers.subsurface(loc)
image_numbers.append(img)
#=======================================================================
# some utility functions
#=======================================================================
def maprange( a, b, val):
# map val from range a to range b
(a1, a2), (b1, b2) = a, b
return b1 + ((val - a1) * (b2 - b1) / (a2 - a1))
def clamp(n, minn, maxn):
if n < minn:
return minn
elif n > maxn:
return maxn
else:
return n
#=======================================================================
# Partical class
#=======================================================================
class Partical():
def __init__(self, pos, angle, speed, size, colour):
self.pos = Vector2(pos.x, pos.y)
self.vel = Vector2(0, 0)
self.acc = Vector2(0,0)
self.size = size
self.alpha = 255
self.acc.setFromAngle(angle)
self.acc.mult(speed)
self.image = pygame.Surface([self.size, self.size])
self.image.fill(colour)
self.image.set_alpha(self.alpha)
def update(self):
self.vel.add(self.acc)
self.pos.add(self.vel)
self.alpha -= abs(self.vel.y)
self.alpha = max(0,self.alpha)
self.image.set_alpha(self.alpha)
def draw(self):
screen.blit(self.image, (self.pos.x, self.pos.y))
def isOffScreen(self):
return (self.pos.x < 0) or (self.pos.x > SCREEN_WIDTH) or (self.pos.y < 0) or (self.pos.y > SCREEN_HEIGHT)
def isDead(self):
return (self.alpha <= 0) or (self.isOffScreen())
#=======================================================================
# particlesystem class
#=======================================================================
class particlesystem():
def __init__(self, x, y, mx = 20):
self.pos = Vector2(x, y)
self.particles = []
self.max_particles = mx
def killAll(self):
self.particles = []
def burstDirection(self, angle, spread):
self.killAll()
for n in range(0, self.max_particles):
# vary the angle a little bit
angle = (angle + random.uniform(-spread, spread)) % 360
speed = random.uniform(0.1, 0.7)
size = random.randint(1, 4)
p = Partical(self.pos, angle, speed, size, COLOUR_YELLOW)
self.particles.append(p)
def burstCircle(self):
self.killAll()
step = 360 // self.max_particles
for n in range(0, self.max_particles):
angle = n * step
speed = random.uniform(0.1, 0.7)
size = random.randint(1, 4)
colour = COLOUR_RED
p = Partical(self.pos, angle, speed, size, colour)
self.particles.append(p)
def update(self):
cp = [p for p in self.particles if not p.isDead()]
self.particles = cp
for p in self.particles:
p.update()
p.draw()
def isDead(self):
return len(self.particles) == 0
#=======================================================================
# particlesystemController class
#=======================================================================
class particlesystemController():
def __init__(self):
self.systems = []
def spawn(self, x, y, mx):
system = particlesystem(x, y, mx)
self.systems.append(system)
return system
def spawnBurstDirection(self, x, y, angle, spread, max_particles = 20):
system = self.spawn(x, y, max_particles)
system.burstDirection(angle, spread)
def spawnBurstCircle(self, x, y, max_particles = 20):
system = self.spawn(x, y, max_particles)
system.burstCircle()
def killAll(self):
self.systems = []
def update(self):
cp = [ps for ps in self.systems if not ps.isDead()]
self.systems = cp
for s in self.systems:
s.update()
#=======================================================================
# Star class
#=======================================================================
class Star():
def __init__(self):
self.position = Vector2(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT))
self.velocity = Vector2(0.0, 1 + random.random() * 10)
self.size = random.randint(1,4)
self.image = pygame.Surface([self.size, self.size])
self.rect = self.image.get_rect()
self.image.fill(COLOUR_STARS)
def reset(self):
self.position.y = 0
self.position.x = random.randint(0, SCREEN_WIDTH)
self.velocity.y = 1 + random.random() * 10
def update(self):
# add a little to vel each frame to make it look a bit
# like gravity is pulling it down like rain
# reset() will set vel back to a baseline
self.velocity.y += 0.05
self.position.add(self.velocity)
self.rect.x = self.position.x
self.rect.y = self.position.y
def draw(self):
screen.blit(self.image, self.rect)
#=======================================================================
# Starfield class
#=======================================================================
class StarField():
def __init__(self):
self.stars = []
self.max_stars = 40
for i in range(0, self.max_stars):
star = Star()
self.stars.append(star)
def update(self):
for star in self.stars:
star.update()
if star.position.y > SCREEN_HEIGHT:
star.reset()
def draw(self):
for star in self.stars:
star.draw()
#=======================================================================
# Player class
#=======================================================================
class Player():
def __init__(self, x, y, w, h, maxspeed):
self.width = w
self.height = h
self.maxspeedy = maxspeed
self.maxposition_y = SCREEN_HEIGHT - self.height
self.start_position = Vector2(x, y)
self.position = Vector2(self.start_position.x, self.start_position.y)
self.velocity = Vector2(0,0)
self.acceleration = Vector2(0,0)
self.acceleration_step = 1.5
self.rect = pygame.Rect([self.position.x, self.position.y, self.width, self.height])
self.image = pygame.Surface([self.width, self.height])
self.image.fill(COLOUR_WHITE)
def reset(self):
self.position = Vector2(self.start_position.x, self.start_position.y)
self.velocity.mult(0)
self.acceleration.mult(0)
def up(self):
self.acceleration.y -= self.acceleration_step
def down(self):
self.acceleration.y += self.acceleration_step
def constrain(self):
# constrain movement to screen bounds
if self.position.y < 0:
self.position.y = 0
self.velocity.y = 0
elif self.position.y > self.maxposition_y:
self.position.y = self.maxposition_y
self.velocity.y = 0
def update(self):
self.velocity.add(self.acceleration)
# limit the speed
self.velocity.y = clamp(self.velocity.y, -self.maxspeedy, self.maxspeedy)
# add velocity to position
self.position.add(self.velocity)
# clear out the accumulated acceleration
self.acceleration.mult(0)
self.constrain()
self.rect.x = self.position.x
self.rect.y = self.position.y
def draw(self):
screen.blit(self.image, self.rect)
#=======================================================================
# Balltrail class
#=======================================================================
class Balltrail():
def __init__(self, size):
self.size = size
self.pad = self.size // 2
self.current_frame = 0
self.last_frame = 0
self.max_length = 30
self.trail = []
def reset(self):
self.trail.clear()
def update(self, x, y):
self.current_frame += 1
# record a ball position every 3 frames
if self.current_frame - self.last_frame > 2:
self.last_frame = self.current_frame
if len(self.trail) > self.max_length:
# remove the oldest item
self.trail.pop(0)
# and add the current position of the ball
self.trail.append( (x, y) )
def draw(self):
alpha = 0
for r in self.trail:
pygame.draw.rect(screen, [100, alpha, 100 + alpha], [r[0] + self.pad,r[1] + self.pad, 1 , 1])
alpha += 2
#=======================================================================
# Ball class
#=======================================================================
class Ball():
def __init__(self, size):
self.mass = 8
self.width = size
self.height = size
self.position = Vector2(SCREEN_WIDTH // 2 - self.width // 2, SCREEN_HEIGHT // 2 - self.height // 2)
self.velocity = Vector2(-5,0)
self.acceleration = Vector2(0,0)
self.rect = pygame.Rect([self.position.x,self.position.y, self.width, self.height])
self.image = pygame.Surface([self.width, self.height])
self.image.fill(COLOUR_WHITE)
self.balltrail = Balltrail(size)
def reset(self):
self.balltrail.reset()
bally = random.randint(-1,1)
ballx = 4
if game.playerserve:
ballx = -ballx
self.position = Vector2(SCREEN_WIDTH // 2 - self.width // 2, SCREEN_HEIGHT // 2 - self.height // 2)
self.velocity = Vector2(ballx,bally)
def applyForce(self, f):
# make a copy to preserve the original vector values
fcopy = f.getCopy()
# divide the force by our mass
fcopy.div(self.mass)
self.acceleration.add(fcopy)
def update(self):
# add acceleration to velocity
self.velocity.add(self.acceleration)
# add it to our position vector and we move a bit towards target
self.position.add(self.velocity)
# important to clear out the accumulated acceleration each frame
self.acceleration.mult(0)
self.rect.x = self.position.x
self.rect.y = self.position.y
self.balltrail.update(self.position.x,self.position.y)
def draw(self):
self.balltrail.draw()
screen.blit(self.image, self.rect)
#=======================================================================
# Arena class draws the game borders and scoreboard
#=======================================================================
class Arena():
def __init__(self):
self.width = 2
self.height = SCREEN_HEIGHT
self.position = Vector2(SCREEN_WIDTH // 2 - self.width // 2, 0)
self.player_score_position = Vector2((SCREEN_WIDTH // 2) - 300, (SCREEN_HEIGHT // 2)-52)
self.opponent_score_position = Vector2((SCREEN_WIDTH // 2) + 180, (SCREEN_HEIGHT // 2)-52)
def update(self):
pass
def draw(self):
pygame.draw.rect(screen,[100,100,100],[self.position.x,self.position.y, self.width, self.height])
screen.blit(image_numbers[game.player_score % 10 ], (self.player_score_position.x, self.player_score_position.y))
screen.blit(image_numbers[game.opponent_score % 10 ], (self.opponent_score_position.x, self.opponent_score_position.y))
#=======================================================================
# Game class
# Handles collisions and constraints and player movement
#=======================================================================
class Game():
def __init__(self):
# player size and limits
playerwidth = 20
playerheight = 80
playerspeed = 3.0
opponentspeed = 2.8
ballsize = 8
player_edge_offset = 10
# ball limits
self.ball_max_speed_x = 8.0
self.ball_max_speed_y = 8.0
self.ball_speed_step = 1.2
# these are the x positions that the ball is reset to following
# a rectscollide with either bat to prevent ball going through bat
self.ball_rebound_player_x = player_edge_offset + playerwidth + ballsize
self.ball_rebound_opponent_x = SCREEN_WIDTH - (player_edge_offset + playerwidth) - ballsize
self.playerserve = True # this toggles who serves
self.gamestate = GAME_STATE_INTRO
self.scored_frames_elapsed = 0
self.player_score = 0
self.opponent_score = 0
self.wind = Vector2(0,0)
self.wind_strength = 0.4
self.player = Player(player_edge_offset, (SCREEN_HEIGHT // 2) - playerheight // 2, playerwidth, playerheight, playerspeed)
self.opponent = Player(SCREEN_WIDTH - (player_edge_offset + playerwidth), (SCREEN_HEIGHT // 2) - playerheight // 2, playerwidth, playerheight, opponentspeed)
self.ball = Ball(ballsize)
self.arena = Arena()
self.noiseengine = NoiseEngine1D(random.randint(1,100))
self.starfield = StarField()
self.psc = particlesystemController()
def checkcollisionBallEdges(self):
# if the ball is at or past either the top or bottom edges of
# the court, reverse the y velocity and bring its position back
# within the bounds of the court. This 'bounces' the ball
# back off the edges
if (self.ball.position.y < 0) or (self.ball.position.y > SCREEN_HEIGHT-self.ball.height):
if self.ball.position.y < 0:
self.ball.position.y = 0
self.ball.velocity.y = -self.ball.velocity.y
else:
self.ball.position.y = SCREEN_HEIGHT-self.ball.height
self.ball.velocity.y = -self.ball.velocity.y
sound_blip2.play()
self.psc.spawnBurstCircle(self.ball.position.x, self.ball.position.y)
def checkcollisionBats(self):
# check if ball is colliding with either bat.
# if true reflect ball velocity x
# use the reflectAngle function to work out how much
# to add to the y velocity.
if self.ball.rect.colliderect(self.player.rect):
# work out where on the bat the ball hit here
self.ball.velocity.y = self.reflectAngle(self.player)
self.ball.velocity.x = -self.ball.velocity.x
# a fudge to stop ball penetrating bat at higher ball speeds
self.ball.position.x = self.ball_rebound_player_x
self.batHit()
# spawn a partical system
self.psc.spawnBurstDirection(30, self.ball.position.y, particles_SPAWN_FROM_PLAYER, 4)
elif self.ball.rect.colliderect(self.opponent.rect):
self.ball.velocity.y = self.reflectAngle(self.opponent)
self.ball.velocity.x = -self.ball.velocity.x
self.ball.position.x = self.ball_rebound_opponent_x
self.batHit()
self.psc.spawnBurstDirection(SCREEN_WIDTH-30, self.ball.position.y, particles_SPAWN_FROM_OPPONENT, 4)
def batHit(self):
# called when the ball has hit either bat
sound_blip.play()
self.setWind()
# increase the ball x velocity each hit
# but keep it limited to x max speed
if self.ball.velocity.x < self.ball_max_speed_x:
self.ball.velocity.x *= self.ball_speed_step
else:
self.ball.velocity.x = self.ball_max_speed_x
# reflectangle has already been applied so just
# keep vel y within bounds
if self.ball.velocity.y > self.ball_max_speed_y:
self.ball.velocity.y = self.ball_max_speed_y
def setWind(self):
# gets a random direction and strength for the wind effect
# the effect is mostly on the balls vertical movement
# called when the ball hits a bat
self.wind.x = self.noiseengine.next(100) * (self.wind_strength / 4)
self.wind.y = self.noiseengine.next() * self.wind_strength
def reflectAngle(self, player):
# returns an y velocity to reflect the ball at.
# subtract paddle y pos from ball y pos to get the position that
# the ball is on the paddle
# then / by player height to get a normalised 0 to 1.0 value
# then add -0.5 to shift the range from -0.5 to 0.5 which
# can then be multiplied to produce a y velocity for the ball
return (((self.ball.position.y - player.position.y) / player.height) + -0.5) * 8
def moveOpponent(self):
# TODO:
# Make the enemy more intelligent.
# seek to position towards one end of bat for more angle
if self.opponent.position.y < self.ball.position.y - self.opponent.height // 2:
self.opponent.down()
elif self.opponent.position.y > self.ball.position.y - self.opponent.height // 2:
self.opponent.up()
def checkBallInScorePosition(self):
# if true score and reset game
if self.ball.position.x < 0:
self.opponent_score += 1
self.psc.spawnBurstDirection(1, self.ball.position.y, particles_SPAWN_FROM_PLAYER, 20, 100)
sound_score.play()
self.gamestate = GAME_STATE_SCORED
elif self.ball.position.x > SCREEN_WIDTH:
self.player_score += 1
self.psc.spawnBurstDirection(SCREEN_WIDTH-1, self.ball.position.y, particles_SPAWN_FROM_OPPONENT, 20, 100)
sound_score.play()
self.gamestate = GAME_STATE_SCORED
def resetFromScore(self):
# called after a score has been made
# reset the player positions and
# zero the wind effect and
# toggle the server
self.playerserve = not self.playerserve
self.resetPositions()
# check if either player has won the game
# and switch the gamestate to gameover if it has
if self.player_score == 5 or self.opponent_score == 5:
self.gamestate = GAME_STATE_OVER
else:
self.gamestate = GAME_STATE_IN_PROGRESS
def resetFromWin(self):
# called after a game win
# resets score and positions etc
self.player_score = 0
self.opponent_score = 0
self.resetPositions()
def resetPositions(self):
# called at each serve
self.ball.reset()
self.player.reset()
self.opponent.reset()
self.wind.mult(0)
self.psc.spawnBurstCircle(ORIGINX, ORIGINY, 100)
sound_boom.play()
def switchGameState(self):
if self.gamestate == GAME_STATE_INTRO:
self.gamestate = GAME_STATE_IN_PROGRESS
image_pong_title.set_alpha(75)
self.resetPositions()
elif self.gamestate == GAME_STATE_OVER:
self.resetFromWin()
self.gamestate = GAME_STATE_INTRO
def drawGameOver(self):
randoff1 = self.noiseengine.next()
randoff2 = self.noiseengine.next(1000)
jitter = 10
image_pong_game.set_alpha(150 + randoff1 * 50)
image_pong_over.set_alpha(150 + randoff2 * 50)
screen.blit(image_pong_game, (200 + randoff1 * jitter, 150 + randoff2 * jitter))
screen.blit(image_pong_over, (400 + randoff2 * jitter, 250 + randoff1 * jitter))
def drawGameWon(self):
randoff1 = self.noiseengine.next()
randoff2 = self.noiseengine.next(1000)
jitter = 10
image_pong_you.set_alpha(100 + randoff1 * 50)
image_pong_won.set_alpha(100 + randoff2 * 50)
screen.blit(image_pong_you, (200 + randoff1 * jitter, 150 + randoff2 * jitter))
screen.blit(image_pong_won, (400 + randoff2 * jitter, 250 + randoff1 * jitter))
def drawGameIntro(self):
randoff1 = self.noiseengine.next()
randoff2 = self.noiseengine.next(1000)
jitter = 10
image_pong_title.set_alpha(200 + randoff1 * 50)
screen.blit(image_pong_title, (200 + randoff1 * jitter, 200 + randoff2 * jitter))
def draw(self):
if self.gamestate == GAME_STATE_INTRO:
self.starfield.update()
self.starfield.draw()
self.drawGameIntro()
elif self.gamestate == GAME_STATE_IN_PROGRESS:
self.checkcollisionBallEdges()
self.checkcollisionBats()
self.moveOpponent()
self.checkBallInScorePosition()
self.arena.update()
self.ball.applyForce(self.wind)
self.ball.update()
self.player.update()
self.opponent.update()
self.starfield.update()
self.starfield.draw()
self.arena.draw()
self.player.draw()
self.opponent.draw()
self.ball.draw()
self.psc.update()
elif self.gamestate == GAME_STATE_SCORED:
self.arena.draw()
self.starfield.update()
self.starfield.draw()
self.psc.update()
self.scored_frames_elapsed += 1
if self.scored_frames_elapsed > 180:
self.scored_frames_elapsed = 0
self.resetFromScore()
elif self.gamestate == GAME_STATE_OVER:
self.starfield.update()
self.starfield.draw()
if self.player_score > self.opponent_score:
self.drawGameWon()
else:
self.drawGameOver()
def run(self):
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_ESCAPE):
done = True
elif (event.key == pygame.K_SPACE):
game.switchGameState()
elif (event.key == pygame.K_UP):
game.player.up()
elif (event.key == pygame.K_DOWN):
game.player.down()
screen.fill(COLOUR_BLACK)
game.draw()
clock.tick(60)
pygame.display.flip()
game = Game()
game.run()
pygame.quit()