forked from bjarnekvae/squash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
squash_classic.py
286 lines (265 loc) · 8.82 KB
/
squash_classic.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
#License MIT 2017 Ahmad Retha
import os
import pygame
import pygame.mixer
##
# Game mode
#
WIDTH = 480
HEIGHT = 640
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (WIDTH, 25)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Squash')
clock = pygame.time.Clock()
pygame.key.set_repeat(50, 50)
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
pygame.init()
##
# Game consts
#
FONT = pygame.font.Font(None, 40)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0 ,0, 255)
GRAY = (100, 100, 100)
MODE_PLAY = 1
MODE_QUIT = 0
FRAME_RATE = 120
##
# Game Sounds and delay for losing
#
LOSE_DELAY = 500
LOSE_SOUND = pygame.mixer.Sound('beep-2.wav')
FAIL_SOUND = pygame.mixer.Sound('beep-3.wav')
LEFT_SOUND = pygame.mixer.Sound('beep-7.wav')
RIGHT_SOUND = pygame.mixer.Sound('beep-8.wav')
#LEFT_SOUND = pygame.mixer.Sound('beep-21.wav') #--too quiet!
#RIGHT_SOUND = pygame.mixer.Sound('beep-22.wav')
##
# Game Vars
#
BUDGE = 5
BALL_SPEED = 3
BALL_RADIUS = 4
PADDLE_SPEED = 3
PADDLE_SIZE = 70
PADDLE_THICKNESS = 4
LEFT_PLAYER = True
RIGHT_PLAYER = False
muted = False
speed_x = BALL_SPEED
speed_y = -BALL_SPEED
score_left = 0
score_right = 0
playerTurn = LEFT_PLAYER
current_mode = MODE_PLAY
##
# Action on player score
#
def score():
global playerTurn, score_left, score_right, leftPaddle, rightPaddle, ball, speed_y
if playerTurn == LEFT_PLAYER:
score_left += 1
leftPaddle.x = WIDTH/4 - PADDLE_SIZE/2
leftPaddle.y = HEIGHT - PADDLE_THICKNESS
rightPaddle.x = WIDTH/4 * 3 - PADDLE_SIZE/2
rightPaddle.y = HEIGHT/4 * 3 - PADDLE_THICKNESS
ball.x = WIDTH/4 * 3
else:
score_right += 1
leftPaddle.x = WIDTH/4 - PADDLE_SIZE/2
leftPaddle.y = HEIGHT/4 * 3 - PADDLE_THICKNESS
rightPaddle.x = WIDTH/4 * 3 - PADDLE_SIZE/2
rightPaddle.y = HEIGHT - PADDLE_THICKNESS
ball.x = WIDTH/4
ball.y = HEIGHT/4 * 3 - PADDLE_THICKNESS - 2 * BALL_RADIUS
speed_y = -abs(speed_y)
return not playerTurn
##
# Game Objects
#
class Paddle(pygame.sprite.Sprite):
def __init__(self, color, width, height, maxX, maxY, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.color = color
self.width = width
self.height = height
self.maxX = maxX
self.maxY = maxY
self.x = x
self.y = y
def move(self, moveX, moveY):
self.y = self.y + moveY
self.x = self.x + moveX
def update(self):
if self.y < self.maxY/2:
self.y = self.maxY/2
elif self.y > self.maxY:
self.y = self.maxY
if self.x < 0:
self.x = 0
elif self.x > self.maxX:
self.x = self.maxX
self.rect.topleft = [self.x, self.y]
class Ball(pygame.sprite.Sprite):
def __init__(self, color, x, y, radius):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([2*radius, 2*radius])
self.image.set_alpha(0)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.color = color
self.radius = radius
self.x = x
self.y = y
def update(self):
self.rect.topleft = [self.x, self.y]
self.draw(screen)
def draw(self, surface):
pygame.draw.circle(surface, self.color, [int(self.x), int(self.y)], int(self.radius))
leftPaddle = Paddle(GREEN, PADDLE_SIZE, PADDLE_THICKNESS, WIDTH - PADDLE_SIZE, HEIGHT - PADDLE_THICKNESS, WIDTH/4 - PADDLE_SIZE/2, HEIGHT/4 * 3 - PADDLE_THICKNESS)
rightPaddle = Paddle(BLUE, PADDLE_SIZE, PADDLE_THICKNESS, WIDTH - PADDLE_SIZE, HEIGHT - PADDLE_THICKNESS, WIDTH/4 * 3 - PADDLE_SIZE/2, HEIGHT - PADDLE_THICKNESS)
ball = Ball(BLACK, WIDTH/4 - BALL_RADIUS, HEIGHT/4 * 3 - PADDLE_THICKNESS - 2 * BALL_RADIUS, BALL_RADIUS)
spriteGroup = pygame.sprite.Group()
spriteGroup.add(leftPaddle)
spriteGroup.add(rightPaddle)
spriteGroup.add(ball)
##
# Game loop
#
while current_mode == MODE_PLAY:
##
# Handle keyboard
#
for event in pygame.event.get():
if event.type == pygame.QUIT:
current_mode = MODE_QUIT
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
current_mode = MODE_QUIT
elif event.key == pygame.K_m:
muted = not muted
keysPressed = pygame.key.get_pressed()
if keysPressed[pygame.K_UP] and keysPressed[pygame.K_LEFT]:
rightPaddle.move(-PADDLE_SPEED, -PADDLE_SPEED)
elif keysPressed[pygame.K_UP] and keysPressed[pygame.K_RIGHT]:
rightPaddle.move(PADDLE_SPEED, -PADDLE_SPEED)
elif keysPressed[pygame.K_DOWN] and keysPressed[pygame.K_LEFT]:
rightPaddle.move(-PADDLE_SPEED, PADDLE_SPEED)
elif keysPressed[pygame.K_DOWN] and keysPressed[pygame.K_RIGHT]:
rightPaddle.move(PADDLE_SPEED, PADDLE_SPEED)
elif keysPressed[pygame.K_UP]:
rightPaddle.move(0, -PADDLE_SPEED)
elif keysPressed[pygame.K_DOWN]:
rightPaddle.move(0, PADDLE_SPEED)
elif keysPressed[pygame.K_LEFT]:
rightPaddle.move(-PADDLE_SPEED, 0)
elif keysPressed[pygame.K_RIGHT]:
rightPaddle.move(PADDLE_SPEED, 0)
if keysPressed[pygame.K_w] and keysPressed[pygame.K_a]:
leftPaddle.move(-PADDLE_SPEED, -PADDLE_SPEED)
elif keysPressed[pygame.K_w] and keysPressed[pygame.K_d]:
leftPaddle.move(PADDLE_SPEED, -PADDLE_SPEED)
elif keysPressed[pygame.K_s] and keysPressed[pygame.K_a]:
leftPaddle.move(-PADDLE_SPEED, PADDLE_SPEED)
elif keysPressed[pygame.K_s] and keysPressed[pygame.K_d]:
leftPaddle.move(PADDLE_SPEED, PADDLE_SPEED)
elif keysPressed[pygame.K_w]:
leftPaddle.move(0, -PADDLE_SPEED)
elif keysPressed[pygame.K_s]:
leftPaddle.move(0, PADDLE_SPEED)
elif keysPressed[pygame.K_a]:
leftPaddle.move(-PADDLE_SPEED, 0)
elif keysPressed[pygame.K_d]:
leftPaddle.move(PADDLE_SPEED, 0)
##
# Draw arena, score and player turn color
#
screen.fill(WHITE)
pygame.draw.line(screen, RED, [0, HEIGHT/2], [WIDTH, HEIGHT/2], 2)
pygame.draw.line(screen, RED, [WIDTH/2, HEIGHT/2], [WIDTH/2, HEIGHT], 2)
pygame.draw.rect(screen, RED, (0, HEIGHT/2, WIDTH/4, HEIGHT/4), 2)
pygame.draw.rect(screen, RED, (WIDTH/4 * 3 - 1, HEIGHT/2, WIDTH/4, HEIGHT/4), 2)
if playerTurn == RIGHT_PLAYER:
pygame.draw.circle(screen, leftPaddle.color, [int(WIDTH/4), 20], 15)
else:
pygame.draw.circle(screen, rightPaddle.color, [int(WIDTH/4) * 3, 20], 15)
text = FONT.render("%s:%s" % (str(score_left), str(score_right)), 1, GRAY)
textpos = text.get_rect(centerx=WIDTH/2)
screen.blit(text, textpos)
##
# Move ball and update scores
#
if ball.y > HEIGHT:
if not muted:
LOSE_SOUND.play()
playerTurn = score()
pygame.time.delay(LOSE_DELAY)
elif ball.y < 0:
speed_y = -speed_y
if ball.x > WIDTH:
speed_x = -speed_x
elif ball.x < 0:
speed_x = abs(speed_x)
ball.y = ball.y + speed_y
ball.x = ball.x + speed_x
##
# Bounce ball off paddles and paddles off each other
#
if leftPaddle.rect.colliderect(ball.rect):
if playerTurn == LEFT_PLAYER:
if not muted:
FAIL_SOUND.play()
playerTurn = score()
pygame.time.delay(LOSE_DELAY)
else:
ball.y = leftPaddle.y - 2 * BALL_RADIUS
speed_y = -speed_y
playerTurn = not playerTurn
if not muted:
LEFT_SOUND.play()
elif rightPaddle.rect.colliderect(ball.rect):
if playerTurn == RIGHT_PLAYER:
if not muted:
FAIL_SOUND.play()
pygame.time.delay(LOSE_DELAY)
playerTurn = score()
else:
ball.y = rightPaddle.y - 2 * BALL_RADIUS
speed_y = -speed_y
playerTurn = not playerTurn
if not muted:
RIGHT_SOUND.play()
if leftPaddle.rect.colliderect(rightPaddle):
if leftPaddle.rect.bottom >= rightPaddle.rect.top:
leftPaddle.move(0, -BUDGE)
rightPaddle.move(0, BUDGE)
elif rightPaddle.rect.bottom >= leftPaddle.rect.top:
rightPaddle.move(0, -BUDGE)
leftPaddle.move(0, BUDGE)
elif leftPaddle.rect.right >= rightPaddle.rect.left:
leftPaddle.move(-BUDGE, 0)
rightPaddle.move(BUDGE, 0)
elif rightPaddle.rect.right >= leftPaddle.rect.left:
rightPaddle.move(-BUDGE, 0)
leftPaddle.move(BUDGE, 0)
##
# Draw paddles and ball
#
spriteGroup.draw(screen)
spriteGroup.update()
##
# Tick-tock
#
pygame.display.update()
clock.tick(FRAME_RATE)
pygame.quit()