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
|
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)
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()
|