Week 5a - Mouse input and more lists ----mouse input

时间:2021-11-27 01:48:36
<span style="font-size:14px;">import simplegui
import math
# global variables
ball_pos = [50,50]
ball_radius = 12
ball_color = 'red'
# help fuctions
# classes
# define event handlers
# define click()
def click(pos):
global ball_pos,ball_color
if math.sqrt((pos[0]-ball_pos[0])**2 + (pos[1]-ball_pos[1])**2)>ball_radius:
ball_pos = list(pos)
ball_color = 'red'
else:
ball_color = 'green'
#draw canvas
def draw(canvas):
canvas.draw_circle(ball_pos, ball_radius, 4,ball_color,ball_color)
# create a frame
frame = simplegui.create_frame('list_test', 600, 600)
# register event handlers
frame.set_canvas_background('white')
frame.set_draw_handler(draw)
frame.set_mouseclick_handler(click)
# start frame
frame.start()</span>

代码实现了小球点击的变换,主要在于体会 list 的用法。

CodeSkulpor