-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvelocity_control.py
More file actions
45 lines (34 loc) · 942 Bytes
/
velocity_control.py
File metadata and controls
45 lines (34 loc) · 942 Bytes
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
__author__ = 'wanderson'
# control the velocity of a ball using the arrow keys
import simpleguitk as simplegui
# Initialize globals
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
ball_pos = [WIDTH / 2, HEIGHT / 2]
vel = [0, 0]
# define event handlers
def draw(canvas):
# Update ball position
ball_pos[0] += vel[0]
ball_pos[1] += vel[1]
# Draw ball
canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "Red", "White")
def keydown(key):
acc = 1
if key==simplegui.KEY_MAP["left"]:
vel[0] -= acc
elif key==simplegui.KEY_MAP["right"]:
vel[0] += acc
elif key==simplegui.KEY_MAP["down"]:
vel[1] += acc
elif key==simplegui.KEY_MAP["up"]:
vel[1] -= acc
print ball_pos, vel
# create frame
frame = simplegui.create_frame("Velocity ball control", WIDTH, HEIGHT)
# register event handlers
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
# start frame
frame.start()