# Ball motion with an explicit timer
import simplegui
# Initialize globals
width = 600
height = 600
ball_pos_init = [width/2, height/2]
ball_radius = 20
ball_vel = [1, 1]
t = 0
# define event handlers
def draw(canvas):
global ball_pos, ball_vel
ball_pos = [0, 0]
# calculate ball position, 所用p(t+1) = p(0) + (t+1)*v,每0.1s移动一个像素pixels
ball_pos[0] = ball_pos_init[0] + t * ball_vel[0]
ball_pos[1] = ball_pos_init[1] + t * ball_vel[1]
# draw ball
canvas.draw_circle(ball_pos, ball_radius, 6, 'white')
def tick():
global t
t = t + 1
# create frame
frame = simplegui.create_frame('ball_motion', 600, 600)
timer = simplegui.create_timer(100,tick)
# register event handlers
frame.set_draw_handler(draw)
# start frame
frame.start()
timer.start()
rt()