-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
299 lines (255 loc) · 10.7 KB
/
main.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
296
297
298
299
import os
from os.path import abspath, dirname
import pygame
class Breakout:
def __init__(self):
pygame.init()
try:
pygame.mixer.music.load(os.path.join('audio', 'game_theme'))
pygame.mixer.music.set_volume(2)
pygame.mixer.music.play(-1)
except pygame.error as err:
print(err)
self.__screenSize = (640, 480)
self.__screen = pygame.display.set_mode(self.__screenSize)
pygame.display.set_caption("Breakout, Atari Inc.")
self.__clock = pygame.time.Clock()
self.__score = 0
self.__lives = 3
self.__state = 0
self.__blockX = 300
self.__block = pygame.Rect((self.__blockX, 445, 60, 10))
self.__ballX = self.__blockX + 23
self.__ball = pygame.Rect((self.__ballX, 430, 7, 7))
self.__ballVelocity = [5,-5]
self.__bricks = []
self.__brick = None
self.__completed = False
self.__gameOver = False
FONT = abspath(dirname(__file__)) + '/font/' + 'pixelar.ttf'
self.__font = pygame.font.SysFont(FONT, 20, bold=True, italic=False)
self.__createBricks()
def __createColor(self, startY):
if startY == 35:
# red
color = (255, 0, 0)
elif startY == 55:
# orange
color = (255, 140, 0)
elif startY == 75:
# yellow
color = (255, 255, 0)
elif startY == 95:
# green
color = (50, 205, 50)
elif startY == 115:
# light blue
color = (0, 191, 255)
elif startY == 135:
# dark blue
color = (0, 0, 205)
else:
# purple
color = (148, 0, 211)
return color
def __createBricks(self):
startY = 35
for rows in range(7):
startX = 29
for columns in range(8):
rowColor = self.__createColor(startY)
self.__brick = pygame.Rect(startX, startY, 60, 15)
self.__brick = pygame.draw.rect(self.__screen, rowColor,
self.__brick)
self.__bricks.append(self.__brick)
startX += 75
startY += 20
def __findInput(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if self.__state == 0:
self.__block.left -= 10
if self.__block.left < 0:
self.__block.left = 0
self.__ball.left -= 10
if self.__ball.left < 23:
self.__ball.left = 23
if self.__state > 0:
self.__block.left -= 10
if self.__block.left < 0:
self.__block.left = 0
if keys[pygame.K_RIGHT]:
if self.__state == 0:
self.__block.left += 10
if self.__block.right > 640:
self.__block.right = 640
self.__ball.left += 10
if self.__ball.right > 617:
self.__ball.right = 617
if self.__state > 0:
self.__block.left += 10
if self.__block.right > 640:
self.__block.right = 640
if keys[pygame.K_SPACE] and self.__state == 0:
self.__ballVelocity = [5, -5]
self.__state = 1
if self.__lives < 3:
try:
pygame.mixer.music.play(-1)
except pygame.error as err:
print(err)
if keys[pygame.K_q]:
if self.__state != 2:
self.__state = 3
try:
pygame.mixer.music.stop()
gameOver = pygame.mixer.Sound(os.path.join('audio', 'GameOver.ogg'))
gameOver.set_volume(.5)
gameOver.play(0)
except pygame.error as err:
print(err)
self.__gameOver = True
def __moveBall(self):
# To change the ball speed multiply the velocity by a number
self.__ball.left += (1.2 * self.__ballVelocity[0])
self.__ball.top += (1.2 * self.__ballVelocity[1])
# stops the left side of ball from going further than screen on left
if self.__ball.left <= 0:
self.__ball.left = 0
self.__ballVelocity[0] = -self.__ballVelocity[0]
# stops right side of ball from going further than screen on right
elif self.__ball.right >= 640:
self.__ball.right = 640
self.__ballVelocity[0] = -self.__ballVelocity[0]
# stops the top of ball from going further than top of screen
if self.__ball.top < 0:
self.__ball.top = 0
self.__ballVelocity[1] = -self.__ballVelocity[1]
# stops the bottom of ball from going below bottom of screen
elif self.__ball.bottom > 480:
self.__ball.bottom = 480
self.__ballVelocity[1] = -self.__ballVelocity[1]
def __collision(self):
for self.__brick in self.__bricks:
if self.__ball.colliderect(self.__brick):
self.__score += 1
self.__ballVelocity[1] = -self.__ballVelocity[1]
self.__bricks.remove(self.__brick)
self.__brick = pygame.draw.rect(self.__screen, (0, 0, 0),
self.__brick)
try:
brickSmash = pygame.mixer.Sound(os.path.join('audio', 'smb_breakblock.wav'))
brickSmash.set_volume(.5)
brickSmash.play(0)
except pygame.error as err:
print(err)
break
if len(self.__bricks) == 0:
# state 2 = game won
self.__state = 2
try:
pygame.mixer.music.stop()
# gameWon = pygame.mixer.Sound(os.path.join('Sounds', 'World Clear.ogg'))
# gameWon.play(0)
except pygame.error as err:
print(err)
self.__gameOver = True
if self.__ball.colliderect(self.__block):
self.__ball.top = (445 - 14)
self.__ballVelocity[1] = -self.__ballVelocity[1]
elif self.__ball.bottom == 480:
self.__lives -= 1
if self.__lives > 0:
try:
pygame.mixer.music.stop()
except pygame.error as err:
print(err)
self.__state = 0
self.__blockX = 300
self.__block = pygame.Rect((self.__blockX, 445, 60, 10))
self.__ballX = self.__blockX + 20
self.__ball = pygame.Rect(self.__ballX, 430, 7, 7)
else:
# state 3 = game over
self.__state = 3
try:
pygame.mixer.music.stop()
gameOver = pygame.mixer.Sound(os.path.join('audio', 'GameOver.ogg'))
gameOver.set_volume(.5)
gameOver.play(0)
except pygame.error as err:
print(err)
self.__gameOver = True
def __displayScoreLives(self):
pygame.draw.rect(self.__screen, (0, 0, 0), (0, 0, 640, 25))
fontSurface = self.__font.render("SCORE: " + str(self.__score) +
" LIVES: " + str(self.__lives), True,
(255, 255, 255))
self.__screen.blit(fontSurface, (245, 5))
def __message(self):
pygame.draw.rect(self.__screen, (0, 0, 0), (40, 270, 640, 100))
if self.__state == 0:
message = "Press SPACE to launch the ball or Q to quit"
arrowKeyMsg = "Use ARROW KEYS to move the block"
arrowKeyMsgSurface = self.__font.render(arrowKeyMsg, True,
(255, 255, 255))
self.__screen.blit(arrowKeyMsgSurface, (200, 290))
elif self.__state == 2:
message = "You won! Press ENTER to play again or Q to quit"
else:
message = "Game over. Press ENTER to play again or Q to quit"
fontSurface = self.__font.render(message, True, (255, 255, 255))
self.__screen.blit(fontSurface, (170, 270))
def runBreakout(self):
while not self.__completed:
while self.__gameOver:
self.__displayScoreLives()
if self.__state != 1:
self.__message()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.__gameOver = False
self.__completed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
self.__gameOver = False
self.__completed = True
if event.key == pygame.K_RETURN and \
self.__state == 3:
self.__init__()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.__state = 3
try:
pygame.mixer.music.stop()
gameOver = pygame.mixer.Sound(os.path.join('audio', 'GameOver.ogg'))
gameOver.set_volume(.5)
gameOver.play(0)
except pygame.error as err:
print(err)
self.__gameOver = True
self.__clock.tick(50)
self.__findInput()
if self.__state == 1:
pygame.draw.rect(self.__screen, (0, 0, 0), (40, 270, 640, 100))
self.__moveBall()
self.__collision()
else:
self.__message()
self.__displayScoreLives()
self.__block = pygame.draw.rect(self.__screen, (0, 128, 255),
self.__block)
self.__ball = pygame.draw.circle(self.__screen, (255, 255, 255),
(self.__ball.left + 7,
self.__ball.top + 7), 7)
pygame.display.flip()
self.__block = pygame.draw.rect(self.__screen, (0, 0, 0),
self.__block)
self.__ball = pygame.draw.circle(self.__screen, (0, 0, 0),
(self.__ball.left + 7,
self.__ball.top + 7), 7)
pygame.mixer.stop()
if __name__ == '__main__':
game = Breakout()
game.runBreakout()