forked from tjguk/breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakout7.py
More file actions
94 lines (71 loc) · 1.93 KB
/
breakout7.py
File metadata and controls
94 lines (71 loc) · 1.93 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
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
from collections import namedtuple
import random
import sys
import time
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
BLOCK_COLOURS = RED, GREEN, BLUE
class Block(Rect):
def __init__(self, colour, rect):
Rect.__init__(self, rect)
self.colour = colour
blocks = []
for n_block in range(N_BLOCKS):
colour = BLOCK_COLOURS[n_block % len(BLOCK_COLOURS)]
block = Block(colour, ((n_block * BLOCK_W, 0), (BLOCK_W, BLOCK_H)))
blocks.append(block)
def draw_blocks():
for block in blocks:
screen.draw.filled_rect(block, block.colour)
def draw():
screen.clear()
screen.draw.filled_rect(ball, RED)
screen.draw.filled_rect(bat, RED)
draw_blocks()
def on_key_down():
sys.exit()
def on_mouse_move(pos):
x, y = pos
bat.center = (x, bat.center[1])
def on_mouse_down():
global ball_dir
ball_dir = Direction(ball_dir.x * 1.5, ball_dir.y * 1.5)
def move(ball):
"""returns a new ball at a new position
"""
global ball_dir
ball.move_ip(ball_dir)
if ball.x > W or ball.x <= 0:
ball_dir = Direction(-1 * ball_dir.x, ball_dir.y)
if ball.y <= 0:
ball_dir = Direction(ball_dir.x, ball_dir.y * -1)
if ball.colliderect(bat):
sounds.blip.play()
ball_dir = Direction(ball_dir.x, - abs(ball_dir.y))
to_kill = ball.collidelist(blocks)
# # print(to_kill)
if to_kill >= 0:
sounds.block.play()
ball_dir = Direction(ball_dir.x, abs(ball_dir.y))
blocks.pop(to_kill)
if not blocks:
sounds.win.play()
sounds.win.play()
time.sleep(1)
sys.exit()
if ball.y > H:
sounds.die.play()
time.sleep(1)
sys.exit()
def update():
move(ball)