-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
198 lines (152 loc) · 6.8 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
#-------------
#Michael Bateman
#Janurary 19, 2017
#-------------
# pong: hit the ball with the paddle
#
# use the escape key to exit
#
# on the XO, the escape key is the top lefthand key,
# circle with an x in it.
#import pippy
import pygame
import sys
from pygame.locals import *
from random import *
# always need to init first thing
pygame.init()
# create the window and keep track of the surface
# for drawing into
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# ask for screen's width and height
size = width, height = screen.get_size()
# turn off the cursor
pygame.mouse.set_visible(False)
# turn on key repeating (repeat 40 times per second)
pygame.key.set_repeat(25, 25)
# start the screen all black
bgcolor = (0, 0, 0)
screen.fill(bgcolor)
# paddle constants
paddle_width = 20
paddle_length = 100
paddle_radius = paddle_length / 2
paddle_color = (250, 250, 250)
step = 6 # paddle moves 3 pixels at a go
# ball constants
ball_color = (250, 250, 250)
ball_radius = 25
Background_Red = 0
Background_Green = 0
Background_Blue = 0
# game constants
fsize = 48
msg = 'Press \'s\' to start game'
font = pygame.font.Font(None, fsize)
text = font.render(msg, True, (250, 250, 250))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
#scoreboard - MB
score = 0
scoretext = font.render("Score: " + str(score), True, (250, 250, 250))
scoreRect = scoretext.get_rect()
scoreRect.x = 50
scoreRect.y = 500
#while pippy.pygame.next_frame():
while True:
# display msg
screen.fill(bgcolor)
screen.blit(text, textRect)
pygame.display.flip()
# chill until a key is pressed
for idle_event in pygame.event.get():
if idle_event.type == QUIT:
sys.exit()
if idle_event.type == KEYDOWN:
if idle_event.key == K_ESCAPE:
sys.exit()
if idle_event.key == 115: # g key
# play a game!
# start the paddle in the center
paddle_location = height / 2
# number of balls to a game
balls = 4
while balls > 0:
ball_position = [ball_radius, ball_radius]
ball_mvect = [randint(3, 5), randint(3, 5)]
ball_limit = size
balls = balls - 1
while ball_position[0] + ball_radius < ball_limit[0]: # in play
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
sys.exit()
elif event.key == 273 \
or event.key == 265 \
or event.key == 264: # up
paddle_location = paddle_location - step
elif event.key == 274 \
or event.key == 259 \
or event.key == 258: # down
paddle_location = paddle_location + step
# make sure the paddle is in-bounds
if paddle_location - paddle_radius < 0:
paddle_location = paddle_radius
elif paddle_location + paddle_radius >= height:
paddle_location = height - 1 - paddle_radius
# clear the screen
#screen.fill(bgcolor)
screen.fill((Background_Red,Background_Green,Background_Blue))
#show score on screen - MB
scoretext = font.render("Score: " + str(score), True, (250, 250, 250))
screen.blit(scoretext,scoreRect)
# draw the paddle on the right side of the screen
pygame.draw.line(screen,
paddle_color,
(width - paddle_width, paddle_location -
paddle_radius),
(width - paddle_width,
paddle_location + paddle_radius),
paddle_width)
# draw the ball
pygame.draw.circle(screen, ball_color, ball_position, ball_radius)
# draw the unused balls
for i in range(balls):
pygame.draw.circle(screen, ball_color,
(int(round(30 + i * ball_radius * 2.4)), 30),
ball_radius)
# update the display
pygame.display.flip()
screen.fill((Background_Red,Background_Green,Background_Blue))
# update the ball
for i in range(2):
ball_position[i] = ball_position[i] + ball_mvect[i]
# bounce on top and left
if ball_position[i] < ball_radius:
ball_position[i] = ball_radius
ball_mvect[i] = -1 * ball_mvect[i]
Background_Red = randint(0,254)
Background_Green = randint(0,254)
Background_Blue = randint(0,254)
# bounce on bottom
elif i == 1 \
and ball_position[i] >= ball_limit[i] - ball_radius:
ball_position[i] = ball_limit[i] - ball_radius - 1
ball_mvect[i] = -1 * ball_mvect[i]
Background_Red = randint(0,254)
Background_Green = randint(0,254)
Background_Blue = randint(0,254)
elif i == 0 \
and ball_position[i] >= ball_limit[i] - ball_radius - paddle_width \
and ball_position[1] > paddle_location - paddle_radius \
and ball_position[1] < paddle_location + paddle_radius:
ball_position[i] = ball_limit[i] - ball_radius - paddle_width - 1
ball_mvect[i] = (-1) * ball_mvect[i]
#add score every time - MB
score = score + 1
if balls == 0:
screen.fill((0,0,0))
score = 0