forked from tjguk/breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbo5.py
More file actions
59 lines (45 loc) · 1.18 KB
/
bo5.py
File metadata and controls
59 lines (45 loc) · 1.18 KB
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
from collections import namedtuple
import random
W = 800
H = 600
RED = 200, 0, 0
BLUE = 0, 0, 200
GREEN = 0, 200, 0
ball = Rect((W/2, H/2), (30, 30))
Direction = namedtuple('Direction', 'x y')
ball_dir = Direction(5, 5)
bat = Rect((W/2, 0.96 * H), (150, 15))
N_BLOCKS = 8
BLOCK_W = W / N_BLOCKS
BLOCK_H = BLOCK_W / 4
blocks = []
for n_block in range(N_BLOCKS):
block = Rect((n_block * BLOCK_W, 0), (BLOCK_W, BLOCK_H))
blocks.append((block, random.choice([RED, GREEN, BLUE])))
def draw_blocks():
for block, colour in blocks:
print(block)
screen.draw.filled_rect(block, colour)
def draw():
screen.clear()
screen.draw.filled_rect(ball, RED)
screen.draw.filled_rect(bat, RED)
draw_blocks()
def on_key_down():
import sys
sys.exit()
def on_mouse_move(pos):
x, y = pos
bat.center = (x, bat.center[1])
def move(ball):
"""returns a new ball at a new position
"""
global ball_dir
ball.move_ip(ball_dir)
def boubce():
if ball.x > W or ball.x <= 0:
ball_dir = Direction(-1 * ball_dir.x, ball_dir.y)
if ball.y > H or ball.y <= 0:
ball_dir = Direction(ball_dir.x, ball_dir.y * -1)
def update():
move(ball)