-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong_Game.py
295 lines (236 loc) · 8.89 KB
/
Pong_Game.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
#Comments
import pygame
import random
import os
# from Reading_Functions import readBall,readPaddle
# initialize pygame
pygame.init()
pygame.mixer.init()
# set the height and width of the screen
screen_width = 800
screen_height = 800
screen = pygame.display.set_mode([screen_width, screen_height])
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'Sprites')
# import paddles here
paddleSprite = pygame.image.load(os.path.join(img_folder, 'Spritealpha.png')).convert()
paddleSpriteCranberry = pygame.image.load(os.path.join(img_folder, 'spritecranalpha.png')).convert()
paddleCranberryDry = pygame.image.load(os.path.join(img_folder, 'Canada Dry Cranberry-1.png.png')).convert()
paddleDry = pygame.image.load(os.path.join(img_folder, 'Canada Dry-1.png.png')).convert()
paddleCoca = pygame.image.load(os.path.join(img_folder, 'cocacola.png')).convert()
paddleFanta = pygame.image.load(os.path.join(img_folder, 'fanta.png')).convert()
paddleMDEWC = pygame.image.load(os.path.join(img_folder, 'mountain dew cranberry-1.png.png')).convert()
paddleMDEW = pygame.image.load(os.path.join(img_folder, 'mountain dew-1.png.png')).convert()
paddlePep1 = pygame.image.load(os.path.join(img_folder, 'pepsi.png')).convert()
# import balls here
ball1 = pygame.image.load(os.path.join(img_folder, 'redball18x18.png'))
ball2 = pygame.image.load(os.path.join(img_folder, 'ball sprite-1.png.png'))
ball3 = pygame.image.load(os.path.join(img_folder, 'Trump for ball-1.png.png'))
#music
pygame.mixer.music.load('Trance - 009 Sound System Dreamscape.mp3')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
# Insert all the ball choices here
WHITE = (255, 255, 255)
BACKGROUND = (0, 100, 255)
font = pygame.font.SysFont(None, 24)
def textScreen(writtentext):
text = font.render(writtentext, True, WHITE, BACKGROUND)
textRect = text.get_rect()
textRect.centerx = screen_width/2
textRect.centery = screen_height/2
screen.blit(text, textRect)
pygame.display.update()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
screen.fill(BACKGROUND)
run = False
def showText(text):
t = font.render(text, True, WHITE, BACKGROUND)
tRect = t.get_rect()
tRect.centerx = 250
tRect.centery = 250
screen.blit(t, tRect)
pygame.display.flip()
#################### Main program starts here
run = True
screen.fill(BACKGROUND)
textScreen("Welcome to Pong Game! Press Enter to continue...")
textScreen("Player 1: Use the up and down arrows to move the paddle")
textScreen("Player 2: Use the W and S keys to move the paddle")
textScreen("Goal: don't let ball beyond the paddle, first to 5 points wins")
textScreen("Good luck!")
textScreen("Go to the console to enter your paddle choices. press enter.")
UP = 1
DOWN = -1
FLAT = 0
RIGHT = 1
LEFT = -1
score1 = 0
score2 = 0
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.direction = FLAT # 0: no move, 1: moving up: -1: moving down
# Reading chosen sprites
def readPaddle(self):
paddleChoice = input("Choose a paddle 1/2/3/4/5/6/7/8/9: ")
try:
if paddleChoice is "1":
self.image = paddleSprite
if paddleChoice is "2":
self.image = paddleSpriteCranberry
if paddleChoice is "3":
self.image = paddleCranberryDry
if paddleChoice is "4":
self.image = paddleDry
if paddleChoice is "5":
self.image = paddleCoca
if paddleChoice is "6":
self.image = paddleFanta
if paddleChoice is "7":
self.image = paddleMDEWC
if paddleChoice is "8":
self.image = paddleMDEW
if paddleChoice is "9":
self.image = paddlePep1
self.rect = self.image.get_rect()
self.image.set_colorkey((255, 255, 255))
self.image.set_colorkey((0, 0, 0))
except ValueError or AttributeError:
print("Value not accepted")
def moveUp(self, pixels):
self.rect.y -= (pixels * 1.1)
self.direction = UP
def moveDown(self, pixels):
self.rect.y += (pixels * 1.1)
self.direction = DOWN
def stop(self):
self.direction = FLAT
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.xstep = 5 # positive: moving to the right, negative: moving to the left
self.ystep = 0 # 0: no move, positive: moving down: negative: moving up
def ballpos(self):
self.rect.x = screen_width/2
self.rect.y = screen_height/2
def readball(self):
ballChoice = input("Choose a ball 1/2/3: ")
try:
if ballChoice is "1":
self.image = ball1
if ballChoice is "2":
self.image = ball2
if ballChoice is "3":
self.image = ball3
self.rect = self.image.get_rect()
self.image.set_colorkey((255, 255, 255))
except ValueError or AttributeError:
print("Value not accepted")
def ballreset(self):
self.rect.x = screen_width/2
self.rect.y = screen_height/2
self.xstep = 5
self.ystep = 0
def moveball(self, yDirection):
self.rect.x += self.xstep
if (yDirection == UP): # move up screen
self.ystep = -5
elif (yDirection == DOWN): # move down screen
self.ystep = 5
if (self.rect.y <= 0) or (self.rect.y >= screen_height): # if it reaches the bottom or the top of the screen
self.reverseY()
self.rect.y += self.ystep
def reverseX(self):
self.xstep *= -1.1
def reverseY(self):
self.ystep *= -1.1
all_sprites_list = pygame.sprite.Group()
player1 = Player()
player2 = Player()
player1.readPaddle()
player2.readPaddle()
player1.rect.x = 10
player1.rect.y = screen_height/2
player2.rect.x = screen_width-50
player2.rect.y = screen_height/2
ball12 = Ball()
ball12.readball()
ball12.ballpos()
ball12.moveball(FLAT)
all_sprites_list.add(player1, player2, ball12)
clock = pygame.time.Clock()
turn_done = False
while run:
screen.fill(BACKGROUND)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.constants.USEREVENT:
pygame.mixer.music.load('Trance - 009 Sound System Dreamscape.mp3')
pygame.mixer.music.play()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player2.moveUp(5)
if player2.rect.y <= 0:
player2.rect.y = 0
if keys[pygame.K_DOWN]:
player2.moveDown(5)
if player2.rect.y >= screen_height - player2.rect.height:
player2.rect.y = screen_height - player2.rect.height
if keys[pygame.K_w]:
player1.moveUp(5)
if player1.rect.y <= 0:
player1.rect.y = 0
if keys[pygame.K_s]:
player1.moveDown(5)
if player1.rect.y >= screen_height - player1.rect.height:
player1.rect.y = screen_height - player1.rect.height
ball12.moveball(FLAT)
if pygame.sprite.collide_rect(ball12, player2):
ball12.reverseX()
ball12.moveball(player2.direction)
elif pygame.sprite.collide_rect(player1, ball12):
ball12.reverseX()
ball12.moveball(player1.direction)
# P1 scores a point
if ball12.rect.x >= screen_width - 10:
ball12.ballreset()
score1 += 1
turn_done = True
if turn_done:
textScreen("Player 1 has scored! Press ENTER to continue")
turn_done = False
elif ball12.rect.x <= 10:
ball12.ballreset()
score2 += 1
turn_done = True
if turn_done:
textScreen("Player 2 has scored! Press ENTER to continue")
turn_done = False
# Print the score :)
scoreprint = "Player 1: " + str(score1)
text = font.render(scoreprint, 1, WHITE)
textpos = (0, 0)
screen.blit(text, textpos)
scoreprint = "Player 2: " + str(score2)
text = font.render(scoreprint, 1, WHITE)
textpos = (screen_width - 100, 0)
screen.blit(text, textpos)
all_sprites_list.update()
all_sprites_list.draw(screen)
pygame.display.update()
clock.tick(60)
if score1 == 5:
run = False
screen.fill(BACKGROUND)
textScreen("Game over, Player 1 wins")
if score2 == 5:
run = False
screen.fill(BACKGROUND)
textScreen("Game over, Player 2 wins")
pygame.quit()