week4_motion_of_ball_1(小球运动)——改进

# Ball motion with an explicit timer
import simplegui

# Initialize globals
width = 600
height = 600
ball_pos = [width/2, height/2]
ball_radius = 20
ball_vel = [0, 1.0]#(pixels per 1/60seconds)

# define event handlers

def draw(canvas):
    global ball_pos, ball_vel
  
    # calculate ball position,use p(t+1) =  p(t) + v(t)
    ball_pos[0] = ball_pos[0] + ball_vel[0]
    ball_pos[1] = ball_pos[1] + ball_vel[1]
    
    # draw ball
    canvas.draw_circle(ball_pos, ball_radius, 6, 'white')

# create frame
frame = simplegui.create_frame('ball_motion', 600, 600)

# register event handlers
frame.set_draw_handler(draw)
# start frame
frame.start()

你可能感兴趣的:(IO)