forked from moehuster/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion_explicit.py
More file actions
39 lines (29 loc) · 793 Bytes
/
motion_explicit.py
File metadata and controls
39 lines (29 loc) · 793 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
# Ball motion with an explicit timer
import simplegui
# Initialize globals
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
init_pos = [WIDTH/2, HEIGHT/2]
vel = [0, 3] # pixels per tick
time = 9
# define event handlers
def tick():
global time
time = time + 1
def draw(canvas):
# create a list to bold ball position
ball_pos = [0, 0]
# calculate ball position
ball_pos[0] = init_pos[0] + time*vel[0]
ball_pos[1] = init_pos[1] + time*vel[1]
# draw ball
canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "Red", "White")
# create frame
frame = simplegui.create_frame("Motion", WIDTH, HEIGHT)
# regiser event
frame.set_draw_handler(draw)
timer = simplegui.create_timer(100, tick)
# start frame
frame.start()
timer.start()