-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
211 lines (148 loc) · 5.16 KB
/
start.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
import ball
import random
import pgzrun
################################# CONSTANTS #################################
# region
WIDTH = 500
HEIGHT = 500
BCKG_COLOR = 127, 127, 127
LINES = 8
COLUMNS = 8
BRICK_WIDTH = 50
BRICK_HEIGHT = 20
BRICK_SIZE = BRICK_WIDTH, BRICK_HEIGHT
BRICK_H_DISTANCE = 5
BRICK_V_DISTANCE = 3
H_OFFSET = (WIDTH - (BRICK_WIDTH * COLUMNS + BRICK_H_DISTANCE * (COLUMNS - 1))) // 2 # se poate hardcoda
V_OFFSET = 30
# endregion
############################# Brick Generation #############################
# region
def generate_bricks():
bricks = []
for i in range(LINES):
line = []
for j in range(COLUMNS):
h_pos = (BRICK_WIDTH + BRICK_H_DISTANCE) * j + H_OFFSET
v_pos = (BRICK_HEIGHT + BRICK_V_DISTANCE) * i + V_OFFSET
brick = Rect((h_pos, v_pos), BRICK_SIZE)
line.append(brick)
bricks.append(line)
return bricks
bricks = generate_bricks()
# endregion
################################# Colors #################################
# region
def random_color():
r = random.randrange(50, 256)
g = random.randrange(50, 256)
b = random.randrange(50, 256)
return r, g, b
def generate_colors():
colors = []
for i in range(LINES):
line = []
for j in range(COLUMNS):
line.append(random_color())
colors.append(line)
return colors
colors = generate_colors()
# colors = [[random_color() for j in range(COLUMNS)] for i in range(LINES)]
# endregion
################################# Pad #################################
pad_size = 120, 15
PAD_V_OFFSET = 50
pad = Rect((WIDTH // 2 - pad_size[0] // 2, HEIGHT - PAD_V_OFFSET), pad_size)
pad_color = 0, 0, 0
PAD_ACC = 1
pad_velocity = 0
################################# Ball #################################
def center_ball_to_padd():
ball.x = pad.centerx
ball.y = pad.top - ball.radius - 1
center_ball_to_padd()
ball_is_moving = False
################################# Lives #################################
lives = 3
################################ PGZ Framework ################################
def on_key_down(key):
if key == keys.SPACE:
global ball_is_moving
ball_is_moving = True
def update():
global pad_velocity # modific o variabila globala
global ball_is_moving
global lives
global bricks
global colors
if keyboard.left and keyboard.right:
pad_velocity = 0
elif keyboard.left:
if pad.left > 0:
pad_velocity += PAD_ACC
pad.centerx -= pad_velocity
# check if pad has slightly gone offscreen
if pad.left < 0:
pad.left = 0
if not ball_is_moving:
center_ball_to_padd()
elif keyboard.right:
if pad.right < WIDTH:
pad_velocity += PAD_ACC
pad.centerx += pad_velocity
# check if pad has slightly gone offscreen
if pad.right > WIDTH:
pad.right = WIDTH
if not ball_is_moving:
center_ball_to_padd()
else:
pad_velocity = 0
if ball_is_moving:
ball.move()
if ball.x < ball.radius or ball.x > WIDTH - ball.radius: # left or right wall
ball.delta_x = -ball.delta_x
if ball.y < ball.radius: # top wall
ball.delta_y = -ball.delta_y
if ball.y < pad.top and pad.collidepoint((ball.x, ball.y + ball.radius)): # pad collision
# if the center of the ball is above the pad
ball.delta_y = -ball.delta_y
ball.y = pad.top - ball.radius
check_bricks_collision()
if ball.y > HEIGHT - ball.radius: # bottom wall
ball_is_moving = False
center_ball_to_padd()
ball.delta_x = abs(ball.delta_x)
ball.delta_y = abs(ball.delta_y)
# Lives system
lives -= 1
if lives == 0:
lives = 3
bricks = generate_bricks()
colors = generate_colors()
def check_bricks_collision():
for i in range(LINES):
for j in range(COLUMNS):
if bricks[i][j] is not None:
left = ball.left(), ball.y
right = ball.right(), ball.y
top = ball.x, ball.top()
bottom = ball.x, ball.bottom()
if bricks[i][j].collidepoint(left) or bricks[i][j].collidepoint(right):
bricks[i][j] = None
ball.delta_x = -ball.delta_x
elif bricks[i][j].collidepoint(top) or bricks[i][j].collidepoint(bottom):
bricks[i][j] = None
ball.delta_y = -ball.delta_y
def draw():
screen.clear()
screen.fill(BCKG_COLOR)
for i in range(LINES):
for j in range(COLUMNS):
if bricks[i][j] is not None:
screen.draw.filled_rect(bricks[i][j], colors[i][j])
screen.draw.filled_rect(pad, pad_color)
ball.draw(screen)
# conteaza ordinea apelurilor; pentru ca textul e dupa bila, va ramane deasupra bilei
screen.draw.text(f'Lives: {lives}', (10, 10))
################################# Main #################################
pgzrun.go()