forked from franapps/Brick_Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick.py
256 lines (226 loc) · 9.26 KB
/
brick.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, pygame, random, math
# pylint: disable-msg=w0614
from pygame.locals import *
pygame.init()
def wait_for_player_to_press_key():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
pygame.quit()
return
# creates a function to draw text
def draw_text(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
return textrect
# Based on pythagoras theorem, func to find the height given the base
# (x or horizontal) and hypoteneuse (ballspeed/velocity).
def pythag(x, speed):
if speed**2 != x**2 + BALLSPEED[1]**2:
BALLSPEED[1] = math.sqrt(speed**2 - x**2)
return BALLSPEED[1]
else:
return BALLSPEED[1]
# Lays out bricks in 8x8 grid
def draw_level():
for i in range(3, 11):
for j in range(3, 11):
newBrick = [pygame.Rect((i * WIDTH), (j * HEIGHT), WIDTH, HEIGHT), COLOR[(random.randint(1, 5)-1)], None]
BRICKS.append(newBrick)
# Function to check if the ball has hit the paddle - Under construction - currently does not recognise side collisions.
def ball_has_hit_paddle(ball, paddle):
if (paddle.top - DEVIATION_Y) <= ball.bottom <= (paddle.top + DEVIATION_Y):
if ball.right >= paddle.left and ball.left <= paddle.right:
return True
else:
return False
# Function to check if the ball has hit a brick.
def ball_has_hit_brick(ball, b):
if (b[0].top - DEVIATION_Y) <= ball.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= ball.top <= (b[0].bottom + DEVIATION_Y):
if ball.right >= b[0].left and ball.left <= b[0].right:
return True
elif (b[0].right - DEVIATION_X) <= ball.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= ball.right <= (b[0].left + DEVIATION_X):
if ball.bottom >= b[0].top and ball.top <= b[0].bottom:
return True
else:
return False
# centre window
os.environ['SDL_VIDEO_CENTERED'] = '1'
# basic pygame window stuff
SCREENWIDTH = 700
SCREENHEIGHT = 500
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("Brick")
CLOCK = pygame.time.Clock()
# set up colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PURPLE = (205, 0, 115)
# Set up the Paddle
PADDLEWIDTH = 100
PADDLEHEIGHT = 20
PLAYER = pygame.Rect(int((SCREENWIDTH/2)-(PADDLEWIDTH/2)), int(SCREENHEIGHT-PADDLEHEIGHT), PADDLEWIDTH, PADDLEHEIGHT)
# Handle paddle horizontal movement (the paddle will never move vertically)
PADDLESPEED = 5
# Set up the ball
BALLWIDTH = 10
BALL = pygame.Rect(int(SCREENWIDTH/2), int(SCREENHEIGHT/2), BALLWIDTH, BALLWIDTH)
# Handle ball movement and deviation.
SPEED = 5
BALLSPEED = [3, 3] # Values for x and y
BALLSPEED[1] = pythag(BALLSPEED[0], SPEED)
# Deviation for collision detection. Returns Int value.
DEVIATION_X = int(round(BALLSPEED[0]/2))
DEVIATION_Y = int(round((BALLSPEED[1]/2)+0.6))
# set up fonts
FONT = pygame.font.SysFont(None, 36)
TEXTCOLOR = (255, 255, 255)
# Setup the bricks
WIDTH = 50
HEIGHT = 20
COLOR = [BLACK, WHITE, RED, GREEN, PURPLE]
BRICKS = []
# Game's opening screen
draw_text('Welcome to Brick!', FONT, SCREEN, 250, 200)
draw_text('Press any key to play or Esc to quit.', FONT, SCREEN, 150, 280)
pygame.display.update()
wait_for_player_to_press_key()
while True:
is_running = True
lives = 3
score = 0
pause = False
moveLeft = moveRight = False
draw_level()
# Run game loop
while is_running:
for event in pygame.event.get():
# handles quit event
if event.type == pygame.QUIT:
is_running = False
# Handles keybindings
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = True
moveLeft = False
if event.type == KEYUP:
if event.key == K_ESCAPE:
is_running = False
if event.key == K_SPACE:
pause = True
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
# Handles control using the mouse
if event.type == MOUSEMOTION:
# If the mouse moves, move the paddle where the cursor is.
PLAYER.move_ip(event.pos[0] - PLAYER.centerx, 0)
# Move the paddle left and right.
if moveLeft and PLAYER.left > 0:
PLAYER.move_ip(-1 * PADDLESPEED, 0)
if moveRight and PLAYER.right < SCREENWIDTH:
PLAYER.move_ip(PADDLESPEED, 0)
# Stop the paddle from moving off SCREEN
if PLAYER.left < 0:
PLAYER.left = 0
if PLAYER.right > SCREENWIDTH:
PLAYER.right = SCREENWIDTH
# Move the mouse cursor to match the paddle.
pygame.mouse.set_pos(PLAYER.centerx, PLAYER.centery)
# Ball bounces off the walls and ceiling, PLAYER loses a life when the BALL hits the bottom of the SCREEN.
BALL = BALL.move(BALLSPEED)
if BALL.left < 0 or BALL.right > SCREENWIDTH:
BALLSPEED[0] = -BALLSPEED[0]
if BALL.top < 0:
BALLSPEED[1] = -BALLSPEED[1]
if BALL.bottom > SCREENHEIGHT:
lives -= 1
BALL.centerx = int(SCREENWIDTH/2)
BALL.centery = int(SCREENHEIGHT/2)
pause = True
if ball_has_hit_paddle(BALL, PLAYER):
if (PLAYER.top - DEVIATION_Y) <= BALL.bottom <= (PLAYER.top + DEVIATION_Y):
if PLAYER.left <= BALL.centerx <= (PLAYER.centerx - 25):
BALLSPEED[0] = -3
BALLSPEED[1] = pythag(BALLSPEED[0], SPEED)
if (PLAYER.centerx - 25) <= BALL.centerx < PLAYER.centerx:
BALLSPEED[0] = -1
BALLSPEED[1] = pythag(BALLSPEED[0], SPEED)
if PLAYER.centerx < BALL.centerx <= (PLAYER.centerx + 25):
BALLSPEED[0] = 1
BALLSPEED[1] = pythag(BALLSPEED[0], SPEED)
if (PLAYER.centerx + 25) <= BALL.centerx <= PLAYER.right:
BALLSPEED[0] = 3
BALLSPEED[1] = pythag(BALLSPEED[0], SPEED)
BALLSPEED[1] = -BALLSPEED[1]
SCREEN.fill((20, 50, 150))
# Bricks collision detection, if the BALL hits a brick, the brick is removed and the BALL changes direction.
for b in BRICKS:
if ball_has_hit_brick(BALL, b):
if (b[0].right - DEVIATION_X) <= BALL.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= BALL.right <= (b[0].left + DEVIATION_X):
BALLSPEED[0] = -BALLSPEED[0]
elif (b[0].top - DEVIATION_Y) <= BALL.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= BALL.top <= (b[0].bottom + DEVIATION_Y):
BALLSPEED[1] = -BALLSPEED[1]
else:
None
BRICKS.remove(b)
score += 100
pygame.draw.rect(SCREEN, b[1], b[0])
# Every 5000 points the PLAYER gains an extra life
if score != 0 and score % 5000 == 0:
lives += 1
score += 1
# Draw the no. of lives and current score on the SCREEN
draw_text('Score: %s' % (score), FONT, SCREEN, 10, 0)
draw_text('Lives: %s' % (lives), FONT, SCREEN, 200, 0)
# Draw the BALL and the paddle
pygame.draw.rect(SCREEN, WHITE, BALL)
pygame.draw.rect(SCREEN, BLACK, PLAYER)
# Pause Loop.
while pause == True:
draw_text('PAUSED', FONT, SCREEN, int((SCREENWIDTH/2)-50), int((SCREENHEIGHT/2)-7))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pause = False
is_running = False
if event.type == KEYUP:
if event.key == K_ESCAPE:
pause = False
is_running = False
if event.key == K_SPACE:
pause = False
if lives == 0:
break
if lives == 0 or len(BRICKS) == 0:
is_running = False
pygame.display.flip()
CLOCK.tick(60)
if lives == 0:
SCREEN.fill((20, 50, 150))
draw_text('GAME OVER, YEAH', FONT, SCREEN, 250, 200)
draw_text('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240)
pygame.display.update()
wait_for_player_to_press_key()
elif len(BRICKS) == 0:
draw_text('You Won!', FONT, SCREEN, 250, 200)
draw_text('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240)
pygame.display.update()
wait_for_player_to_press_key()
else:
break
pygame.quit()