-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
167 lines (135 loc) · 4.41 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
import pygame
import time
import sys
pygame.init()
# Colours
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 32, 32
GREEN = 64, 255, 64
BLUE = 0, 128, 255
# Pygame related
WIDTH, HEIGHT = 800, 800
ROOT = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
VEL = 8
clock = pygame.time.Clock()
FONT = pygame.font.Font(None, 24)
win_text = FONT.render('YOU WIN', False, GREEN)
win_screen = win_text.get_rect(center=(WIDTH / 2, HEIGHT / 2))
game_over_text = FONT.render('GAME OVER', False, RED)
game_over_screen = game_over_text.get_rect(center=(WIDTH / 2, HEIGHT / 2))
brick_list = []
border_list = []
# This class handles the player's platform
class PlayerClass:
def __init__(self, posx, posy, vel, length, width):
self.posx = posx
self.posy = posy
self.vel = vel
self.length = length
self.width = width
self.rect = pygame.Rect(self.posx, self.posy, self.length, self.width)
# This class handles the ball
class BallClass:
def __init__(self, posx, posy, x_vel, y_vel, size):
self.posx = posx
self.posy = posy
self.y_vel = y_vel
self.x_vel = x_vel
self.size = size
self.rect = pygame.Rect(self.posx, self.posy, self.size, self.size)
def update_pos(self):
self.rect.x += self.x_vel
self.rect.y += self.y_vel
# This class is for the borders so the ball bounces off the egdes of the screen
class BorderClass:
def __init__(self, posx, posy, width, height):
self.posx = posx
self.posy = posy
self.width = width
self.height = height
self.rect = pygame.Rect(self.posx, self.posy, self.width, self.height)
# This is the template to spawn the bricks
class BrickClass:
def __init__(self, posx, posy, size):
self.posx = posx
self.posy = posy
self.size = size
self.rect = pygame.Rect(self.posx, self.posy, self.size * 3, self.size)
def particles(posx, posy):
pass
def win():
while True:
ROOT.fill(BLACK)
ROOT.blit(win_text, win_screen)
pygame.display.update()
time.sleep(4)
pygame.quit()
sys.exit()
def game_over():
while True:
ROOT.fill(BLACK)
ROOT.blit(game_over_text, game_over_screen)
pygame.display.update()
time.sleep(2)
pygame.quit()
sys.exit()
def check_events():
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.rect.x > 8:
player.rect.x -= player.vel
if keys[pygame.K_RIGHT] and player.rect.x < WIDTH - player.length - 4:
player.rect.x += player.vel
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def update_root():
ROOT.fill(BLACK)
if ball.rect.y > HEIGHT:
print('game over')
game_over()
if ball.rect.colliderect(player.rect):
ball.y_vel *= -1
for item in brick_list:
pygame.draw.rect(ROOT, RED, item.rect)
if ball.rect.colliderect(item.rect):
ball.y_vel *= -1
brick_list.pop(brick_list.index(item))
for item in border_list:
if ball.rect.colliderect(item.rect):
if item == border_list[0]:
ball.x_vel *= -1
ball.y_vel *= -1
ball.x_vel *= -1
ball.update_pos()
pygame.draw.circle(ROOT, BLUE, (ball.rect[0] + 12, ball.rect[1] + 12), ball.size / 1.5)
#pygame.draw.rect(ROOT, GREEN, ball.rect)
pygame.draw.rect(ROOT, WHITE, player.rect)
pygame.display.update()
def fps_counter():
count = str(int(clock.get_fps()))
fps_txt = FONT.render(count, True, WHITE)
ROOT.blit(fps_txt, (fps_txt.get_width() - fps_txt.get_width()//2, fps_txt.get_height() - fps_txt.get_height()//2))
def main():
check_events()
update_root()
clock.tick(FPS)
if __name__ == '__main__':
ball = BallClass(WIDTH // 2, HEIGHT // 2, 4, 4, 24)
player = PlayerClass(WIDTH - WIDTH / 2, HEIGHT - 50, VEL, 164, 12)
top_border = BorderClass(0, 0, WIDTH, 20)
right_border = BorderClass(WIDTH, 0, 20, HEIGHT)
left_border = BorderClass(0, 0, 20, HEIGHT)
border_list = [top_border, right_border, left_border]
posx, posy = 20, 40
for x in range(66):
print(posy)
brick_list.append(BrickClass(posx, posy, 20))
posx += 70
if x == 10 or x == 21 or x == 32 or x == 43 or x == 54:
posx = 20
posy += 40
while True:
main()