-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong_Game_v2.py
219 lines (183 loc) · 8.07 KB
/
Pong_Game_v2.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
import pygame
import random
import math
# problems with paddle freezing and ball hitting on the top/bottom
pygame.init()
pygame.font.init()
# colors
Tan = (210, 180, 140)
Black = (0, 0, 0)
Red = (255, 0, 0)
Blue = (0, 0, 255)
Green = (0, 255, 0)
# Paddle Info
paddle_Width = 10
paddle_Height = 80
paddle_spawning_yposition = 248 # using spawning positions to make it easier to reset
player_Paddle_Xposition = 450
player_Paddle_Yposition = paddle_spawning_yposition
computer_Paddle_Xposition = 50
computer_Paddle_Yposition = paddle_spawning_yposition
Velocity = 5.0
computer_paddle_Velocity = Velocity
player_paddle_Velocity = Velocity
# ball info
ball_radius = 10
ball_Spawning_Xposition = 250 # set as a constant to revert back to
ball_Spawning_Yposition = 288 # middle of the game screen, can't be a float
ball_Xposition = ball_Spawning_Xposition
ball_Yposition = ball_Spawning_Yposition
# velocity algorithm
sym = random.choice(['1', '-1'])
sign = int(sym)
sym_2 = random.choice(['1', '-1'])
sign_2 = int(sym_2)
ball_spawning_xVelocity = sign * 1
ball_spawning_yVelocity = sign_2 * 1
ball_xVelocity = ball_spawning_xVelocity
ball_yVelocity = ball_spawning_yVelocity
# window
display_width = 500
display_height = 550
win = pygame.display.set_mode((display_width, display_height)) # creates the game window size
pygame.display.set_caption("Pong")
win.fill(Tan)
ending = ".jpg"
# Clock
clock = pygame.time.Clock()
frame_rate = 60
clock.tick(frame_rate) # 60 frames per second
# scoreboard info
computer_score = 0
computer_score_string = str(computer_score)
player_score = 0
player_score_string = str(player_score)
myfont = pygame.font.SysFont('arial', 60)
computer_score_surface = myfont.render(computer_score_string, False, (Blue))
player_score_surface = myfont.render(computer_score_string, False, (Blue))
# Scoreboard
win.blit(computer_score_surface, (100, 0)) # call the string name and then the position(x,y)
win.blit(player_score_surface, (350, 0))
pygame.draw.rect(win, Black, (0, 65, 500, 10))
# paddle
class paddle:
def __init__(self, color, width, height, xposition, yposition, velocity):
self.color = color
self.width = width
self.height = height
self.xposition = xposition
self.yposition = yposition
self.velocity = velocity
def draw(self, win):
pygame.draw.rect(win, self.color, (self.xposition, self.yposition, self.width, self.height))
# ball
class ball:
def __init__(self, color, radius, xposition, yposition):
self.color = color
self.radius = radius
self.xposition = xposition
self.yposition = yposition
def draw(self, win):
pygame.draw.circle(win, self.color, (self.xposition, self.yposition), self.radius)
# game loop
run = True
while (run):
win.fill((Tan)) # or else rectangles grow. this is color tan
for event in pygame.event.get(): # event is anything that happens from user, this gets a list of anything that happens
if event.type == pygame.QUIT: # if you hit the red quit X button
run = False
if (event.type == pygame.MOUSEMOTION):
mouse_position = pygame.mouse.get_pos()
if(mouse_position[1] < player_Paddle_Yposition):
player_Paddle_Yposition -= player_paddle_Velocity
if(mouse_position[1] > player_Paddle_Yposition):
player_Paddle_Yposition += player_paddle_Velocity
# player's paddle
Player_Paddle = paddle(Red, paddle_Width, paddle_Height, player_Paddle_Xposition, player_Paddle_Yposition,
player_paddle_Velocity)
paddle.draw(Player_Paddle, win)
if (player_Paddle_Yposition >= 420): # restricts movement of paddles-lower bound
player_paddle_Velocity = 0
if (event.type == pygame.MOUSEMOTION):
mouse_position = pygame.mouse.get_pos()
if (mouse_position[1] < player_Paddle_Yposition):
player_paddle_Velocity = Velocity
player_Paddle_Yposition -= player_paddle_Velocity
if (player_Paddle_Yposition <= 75): # restricts movement of paddles-upper bound
player_paddle_Velocity = 0
if (event.type == pygame.MOUSEMOTION):
mouse_position = pygame.mouse.get_pos()
if (mouse_position[1] > player_Paddle_Yposition):
player_paddle_Velocity = Velocity
player_Paddle_Yposition += player_paddle_Velocity
# computer's paddle
Computer_Paddle = paddle(Red, paddle_Width, paddle_Height, computer_Paddle_Xposition, computer_Paddle_Yposition,
computer_paddle_Velocity)
paddle.draw(Computer_Paddle, win)
computer_Paddle_Yposition += computer_paddle_Velocity # this allows the paddle to run up and down in bounds
if (computer_Paddle_Yposition >= 420):
computer_paddle_Velocity = random.randrange(1, 7, 1) ##between 0.5 and 2
computer_paddle_Velocity *= -1
if (computer_Paddle_Yposition <= 75):
computer_paddle_Velocity = -random.randrange(1, 7, 1) #randomizes the speed of paddle
computer_paddle_Velocity *= -1
# ball
computer_ball = ball(Blue, ball_radius, ball_Xposition, ball_Yposition)
ball.draw(computer_ball, win)
# ball motion
ball_Xposition += ball_xVelocity
ball_Yposition += ball_yVelocity * 3
# collisions
if (ball_Yposition >= 500): #controls bouncing off of the bottom
ball_yVelocity = -ball_yVelocity
if (ball_Yposition <= 75):
ball_yVelocity = -ball_yVelocity #controls bouncing off the top
if (ball_Xposition == 440):
if (ball_Yposition >= player_Paddle_Yposition and ball_Yposition <= player_Paddle_Yposition + 80):
ball_xVelocity = -ball_xVelocity
if (ball_Xposition == 70):
if (ball_Yposition >= computer_Paddle_Yposition and ball_Yposition <= computer_Paddle_Yposition + 80):
ball_xVelocity = -ball_xVelocity
# scoring
if (ball_Xposition >= 500): # meaning the computer scored, resets ball position, velocity, paddle position
ball_Xposition = ball_Spawning_Xposition
ball_Yposition = ball_Spawning_Yposition
sym = random.choice(['1', '-1'])
sign = int(sym)
sym_2 = random.choice(['1', '-1'])
sign_2 = int(sym_2)
ball_spawning_xVelocity = sign * 1
ball_spawning_yVelocity = sign_2 * 1
ball_yVelocity = ball_spawning_yVelocity
ball_xVelocity = ball_spawning_xVelocity
computer_Paddle_Yposition = paddle_spawning_yposition
player_Paddle_Yposition = paddle_spawning_yposition
computer_score = computer_score + 1
if (ball_Xposition <= 0): # meaning the player scored
ball_Xposition = ball_Spawning_Xposition
ball_Yposition = ball_Spawning_Yposition
sym = random.choice(['1', '-1'])
sign = int(sym)
sym_2 = random.choice(['1', '-1'])
sign_2 = int(sym_2)
ball_spawning_xVelocity = sign * 1
ball_spawning_yVelocity = sign_2 * 1
ball_yVelocity = ball_spawning_yVelocity
ball_xVelocity = ball_spawning_xVelocity
computer_Paddle_Yposition = paddle_spawning_yposition
player_Paddle_Yposition = paddle_spawning_yposition
player_score = player_score + 1
# Scoreboard
computer_score_surface = myfont.render("{}".format(computer_score), False, Blue)
player_score_surface = myfont.render("{}".format(player_score), False, Blue)
win.blit(computer_score_surface, (100, 0)) # call the string name and then the position(x,y)
win.blit(player_score_surface, (350, 0))
pygame.draw.rect(win, Black, (0, 65, 500, 10))
pygame.draw.rect(win, Black, (0, 500, 500, 10)) # bottom line
#recording images still in progress
#image_number = 1 #right now, it records one image at the end, but now we know the number can increment. Need to take mulitple images
#image_name = "Pong_Game"
#image_number += 1
#pygame.image.save(win, image_name + str(image_number) + ending)
clock.tick(frame_rate) # controls frame rate--60 is a good number for smoothness
pygame.display.update() # need to refresh display so everything shows----this must happen after everything. ORDER MATTERS