-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl6.py
134 lines (92 loc) · 2.88 KB
/
l6.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
import pgzrun
import random
import l6_ball as ball
################################# CONSTANTS #################################
WIDTH = 500
HEIGHT = 500
BCKG_COLOR = 127, 127, 127
LINES = 8
COLUMNS = 7
BRICK_WIDTH = 50
BRICK_HEIGHT = 20
BRICK_SIZE = BRICK_WIDTH, BRICK_HEIGHT
BRICK_H_DISTANCE = 5
BRICK_V_DISTANCE = 3
H_OFFSET = 40
V_OFFSET = 30
############################# Brick Generation #############################
def generate_bricks():
bricks = []
for i in range(LINES):
line = []
for j in range(COLUMNS):
horizontal_pos = (BRICK_WIDTH + BRICK_H_DISTANCE) * j + H_OFFSET
vertical_pos = (BRICK_HEIGHT + BRICK_V_DISTANCE) * i + V_OFFSET
brick_pos = horizontal_pos, vertical_pos
brick = Rect(brick_pos, BRICK_SIZE)
line.append(brick)
bricks.append(line)
return bricks
bricks = generate_bricks()
################################# Colors #################################
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()
################################# 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
################################ PGZ Framework ################################
def update():
global pad_velocity
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
def draw():
screen.clear()
screen.fill(BCKG_COLOR)
for i in range(LINES):
for j in range(COLUMNS):
screen.draw.filled_rect(bricks[i][j], colors[i][j])
screen.draw.filled_rect(pad, pad_color)
ball.draw(screen)
pgzrun.go()