-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
134 lines (110 loc) · 4.29 KB
/
Copy pathpong.py
File metadata and controls
134 lines (110 loc) · 4.29 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
import sys, pygame, random
from pygame.locals import *
############CONSTANTS#########################
BLACK = 0,0,0
WHITE = 255,255,255
FPS = 30
WINDOWWIDTH = 600
WINDOWHEIGHT = 400
BALLSIZE = BALLWIDTH, BALLHEIGHT = 50, 50
PADDLESIZE = PADDLEWIDTH, PADDLEHEIGHT = 25, 100
DOWN = 'down'
UP = 'up'
############CLASSES##########################
class Object:
def __init__(self, img, scale, x):
self.img = pygame.image.load(img)
self.img = pygame.transform.scale(self.img,scale)
self.x = x
self.y = random.randint(0,WINDOWHEIGHT-PADDLEHEIGHT)
self.rect = pygame.Rect(x,self.y,scale[0],scale[1])
self.score = 0
############MAIN#############################
def main():
init()
runGame()
def init():
global SCREEN, FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption("PONG")
def runGame():
theBall = Object("ball.png",(BALLWIDTH,BALLHEIGHT),randomX())
pPlayer = Object("paddle.png", (PADDLEWIDTH,PADDLEHEIGHT),0)
pComputer = Object("paddle.png", (PADDLEWIDTH,PADDLEHEIGHT),
WINDOWWIDTH-PADDLEWIDTH)
position = [10,10] #movement of ball
sPressed = False #move paddle down
wPressed = False #move paddle up
computerScore = False
playerScore = False
# computer - player score display
textSurface, textRect = updateScore(pPlayer, pComputer)
#main event loop
#PART 1: HANDLING USER INPUT
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
#KEYDOWN assignments
elif event.type == KEYDOWN:
if event.key == K_w:
wPressed = True #"up" is pressed
elif event.key == K_s:
sPressed = True #"down" is pressed
elif event.key == K_ESCAPE:
terminate()
#KEYUP assignments - when key goes up, stop movement
elif event.type == KEYUP:
if event.key == K_s:
sPressed = False
elif event.key == K_w:
wPressed = False
#PART 2: UPDATING THE GAME STATE
#paddle movement
if sPressed and pPlayer.rect.bottom < WINDOWHEIGHT:
pPlayer.rect = pPlayer.rect.move([0,5])
elif wPressed and pPlayer.rect.top > 0:
pPlayer.rect = pPlayer.rect.move([0,-5])
#ball movement
theBall.rect = theBall.rect.move(position)
#ball reflection from walls
if theBall.rect.left < 0 or theBall.rect.right > WINDOWWIDTH:
position[0] = -position[0]
if theBall.rect.top < 0 or theBall.rect.bottom > WINDOWHEIGHT:
position[1] = -position[1]
# collision
if theBall.rect.colliderect(pPlayer.rect):
position[0] = -position[0]
position[1] = -position[1]
if theBall.rect.x < 1:
pComputer.score += 1
# update the score
textSurface, textRect = updateScore(pPlayer, pComputer)
elif theBall.rect.x > WINDOWWIDTH-BALLWIDTH:
pPlayer.score += 1
# update the score
textSurface, textRect = updateScore(pPlayer, pComputer)
#PART 3: RERENDERING THE SCENE
SCREEN.fill(BLACK)
SCREEN.blit(theBall.img,theBall.rect)
SCREEN.blit(pPlayer.img,pPlayer.rect)
SCREEN.blit(pComputer.img,(pComputer.x,pComputer.y))
SCREEN.blit(textSurface,textRect)
pygame.display.update()
FPSCLOCK.tick(FPS)
def terminate():
pygame.quit()
sys.exit()
def randomX():
return random.randint(PADDLEWIDTH, WINDOWWIDTH-(PADDLEWIDTH*2))
def updateScore(pPlayer, pComputer):
font = pygame.font.Font('freesansbold.ttf',20)
scoreText = "Player: " + str(pPlayer.score) + " || Computer: " + str(pComputer.score)
textSurface = font.render(scoreText, True, WHITE)
textRect = textSurface.get_rect()
textRect.center = ((WINDOWWIDTH/2),(WINDOWHEIGHT/10))
return textSurface, textRect
if __name__ == '__main__':
main()