-
Notifications
You must be signed in to change notification settings - Fork 0
/
week4 - wrapping ball with velocity
46 lines (38 loc) · 1.1 KB
/
week4 - wrapping ball with velocity
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
# control the position of a ball using the arrow keys
import simplegui
# Initialize globals
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
ball_pos = [WIDTH / 2, HEIGHT / 2]
velocity = [0,0]
# define event handlers
def draw(canvas):
canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "Red", "White")
if ball_pos[0] == 0:
ball_pos[0] += velocity[0]
print "Width=0"
else:
ball_pos[0] = (ball_pos[0] + velocity[0]) % WIDTH
if ball_pos[1] == 0:
print "Height=0"
ball_pos[1] += velocity[1]
else:
ball_pos[1] = (ball_pos[1] + velocity[1]) % HEIGHT
def keydown(key):
if key == simplegui.KEY_MAP["left"]:
velocity[0] -= 1
elif key == simplegui.KEY_MAP["right"]:
velocity[0] += 1
elif key == simplegui.KEY_MAP["down"]:
velocity[1] += 1
elif key == simplegui.KEY_MAP["up"]:
velocity[1] -= 1
print velocity
# create frame
frame = simplegui.create_frame("Positional ball control", WIDTH, HEIGHT)
# register event handlers
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
# start frame
frame.start()