forked from tjguk/breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbo4.py
50 lines (34 loc) · 914 Bytes
/
bo4.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
from collections import namedtuple
import sys
W = 800
H = 600
RED = 200, 0, 0
BLUE = 0, 0, 200
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))
def draw():
screen.clear()
screen.draw.filled_rect(ball, RED)
screen.draw.filled_rect(bat, RED)
def on_key_down():
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)
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):
ball_dir = Direction(ball_dir.x, - abs(ball_dir.y))
if ball.y > H:
sys.exit()
def update():
move(ball)