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
#导入simplegui以及math模块
import simplegui
import math

#定义全局变量

WIDTH = 450
HEIGHT = 300
ball_pos = [WIDTH / 2, HEIGHT / 2]
BALL_RADIUS = 15
ball_color = "red"

#计算两点距离函数
def distance(a, b):
    return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)

#点击函数定义
#若点击位置在圈内,则颜色变绿并半径+1
#若点击位置在圈外,则圆心位置移至该处
def click(pos):
    global ball_pos, ball_color, BALL_RADIUS
    if distance(pos, ball_pos) > BALL_RADIUS:
        ball_pos = list(pos)
        ball_color = "red"
    else:
        ball_color = "green"
        BALL_RADIUS += 1
        
#定义画布函数
def draw(canvas):
    canvas.draw_circle(ball_pos, BALL_RADIUS, 1, "Black", ball_color)

#新建画布
frame = simplegui.create_frame("Ball", WIDTH, HEIGHT)
frame.set_canvas_background("White")

#注册鼠标及绘画事件
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)

#开始游戏
frame.start()