-
Notifications
You must be signed in to change notification settings - Fork 2
/
pong.py
252 lines (192 loc) · 7.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
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
# --- Import libraries used for this program
import math
import pygame
import random
# Define some colors
black = (0,0,0)
white = (255,255,255)
# This class represents the ball
# It derives from the "Sprite" class in Pygame
class Ball(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block, and its x and y position
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
# Create the image of the ball
self.image = pygame.Surface([self.width, self.height])
# Color the ball
self.image.fill((white))
# Get a rectangle object that shows where our image is
self.rect = self.image.get_rect()
# Get attributes for the height/width of the screen
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
# Speed in pixels per cycle
self.speed = 0
# Floating point representation of where the ball is
self.x = 0
self.y = 0
# Direction of ball in degrees
self.direction = 0
# Height and width of the ball
self.width = 10
self.height = 10
# Set the initial ball speed and position
self.reset()
def reset(self):
self.x = random.randrange(50,750)
self.y = 350.0
self.speed=8.0
# Direction of ball (in degrees)
self.direction = random.randrange(-45,45)
# Flip a 'coin'
if random.randrange(2) == 0 :
# Reverse ball direction, let the other guy get it first
self.direction += 180
self.y = 50
# This function will bounce the ball off a horizontal surface (not a vertical one)
def bounce(self,diff):
self.direction = (180-self.direction)%360
self.direction -= diff
# Speed the ball up
self.speed *= 1.1
# Update the position of the ball
def update(self):
# Sine and Cosine work in degrees, so we have to convert them
direction_radians = math.radians(self.direction)
# Change the position (x and y) according to the speed and direction
self.x += self.speed * math.sin(direction_radians)
self.y -= self.speed * math.cos(direction_radians)
if self.y < 0:
self.reset()
if self.y > 600:
self.reset()
# Move the image to where our x and y are
self.rect.x = self.x
self.rect.y = self.y
# Do we bounce off the left of the screen?
if self.x <= 0:
self.direction = (360-self.direction)%360
print(self.direction)
#self.x=1
# Do we bounce of the right side of the screen?
if self.x > self.screenwidth-self.width:
self.direction = (360-self.direction)%360
# This class represents the bar at the bottom that the player controls
class Player(pygame.sprite.Sprite):
# Constructor function
def __init__(self, joystick, y_pos):
# Call the parent's constructor
super().__init__()
self.width=75
self.height=15
self.image = pygame.Surface([self.width, self.height])
self.image.fill((white))
self.joystick = joystick
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
self.rect.x = 0
self.rect.y = y_pos
# Update the player
def update(self):
# This gets the position of the axis on the game controller
# It returns a number between -1.0 and +1.0
horiz_axis_pos= self.joystick.get_axis(0)
# Move x according to the axis. We multiply by 15 to speed up the movement.
self.rect.x=int(self.rect.x+horiz_axis_pos*15)
# Make sure we don't push the player paddle off the right side of the screen
if self.rect.x > self.screenwidth - self.width:
self.rect.x = self.screenwidth - self.width
score1 = 0
score2 = 0
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen = pygame.display.set_mode([800, 600])
# Set the title of the window
pygame.display.set_caption('Pong')
# Enable this to make the mouse disappear when over our window
pygame.mouse.set_visible(0)
# This is a font we use to draw text on the screen (size 36)
font = pygame.font.Font(None, 36)
# Create a surface we can draw on
background = pygame.Surface(screen.get_size())
# Create the ball
ball = Ball()
# Create a group of 1 ball (used in checking collisions)
balls = pygame.sprite.Group()
balls.add(ball)
# Count the joysticks the computer has
joystick_count = pygame.joystick.get_count()
if joystick_count < 1:
# No joysticks!
print ("Error, I didn't find enough joysticks.")
else:
# Use joystick #0 and initialize it
joystick1 = pygame.joystick.Joystick(0)
joystick1.init()
joystick2 = pygame.joystick.Joystick(1)
joystick2.init()
# Create the player paddle object
player1 = Player(joystick1,580)
player2 = Player(joystick2,25)
movingsprites = pygame.sprite.Group()
movingsprites.add(player1)
movingsprites.add(player2)
movingsprites.add(ball)
clock = pygame.time.Clock()
done = False
exit_program = False
while exit_program != True:
clock.tick(30)
# Clear the screen
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_program = True
# Stop the game if there is an imbalance of 3 points
if abs(score1 - score2) > 3:
done = True
if not done:
# Update the player and ball positions
player1.update()
player2.update()
ball.update()
# If we are done, print game over
if done:
text = font.render("Game Over", 1, (200, 200, 200))
textpos = text.get_rect(centerx=background.get_width()/2)
textpos.top = 50
screen.blit(text, textpos)
# See if the ball hits the player paddle
if pygame.sprite.spritecollide(player1, balls, False):
# The 'diff' lets you try to bounce the ball left or right depending where on the paddle you hit it
diff = (player1.rect.x + player1.width/2) - (ball.rect.x+ball.width/2)
# Set the ball's y position in case we hit the ball on the edge of the paddle
ball.y = 570
ball.bounce(diff)
score1 += 1
# See if the ball hits the player paddle
if pygame.sprite.spritecollide(player2, balls, False):
# The 'diff' lets you try to bounce the ball left or right depending where on the paddle you hit it
diff = (player2.rect.x + player2.width/2) - (ball.rect.x+ball.width/2)
# Set the ball's y position in case we hit the ball on the edge of the paddle
ball.y = 40
ball.bounce(diff)
score2 += 1
# 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 = (300,0)
screen.blit(text, textpos)
# Draw Everything
movingsprites.draw(screen)
# Update the screen
pygame.display.flip()
pygame.quit()