-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_mouse_click_ver2.py
69 lines (59 loc) · 1.81 KB
/
simple_mouse_click_ver2.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#example of mouse input
import simplegui
import math
#Globals
WIDTH = 450
HEIGHT = 300
#ball_pos = [WIDTH / 2, HEIGHT / 2]
#BALL_RADIUS = 15
ball_color = "Blue"
ball_list = []
ball_radius=15
#Helper function
def distance(p, q):
'''
The function verifies the distance between the circles
'''
return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)
#define event handler for mouse click, draw
def click(pos):
#global ball_pos,ball_color
#In this way the ball change the position every single
#click doing a copy of a list of positions
#ball_pos = list(pos).
#Change the color if the distance is grather than the radius
#if distance(pos, ball_pos) < BALL_RADIUS:
# ball_color = "Yellow"
#else:
# ball_pos = list(pos)
# ball_color = "Blue"
#ball_list.append(pos)
#With this append we can draw mane circles in the screen
#changed = False
#for ball in ball_list:
# if distance([ball[0],ball[1]],pos) < ball_radius:
# ball[2]="Yellow"
# changed= True
#if not changed:
# ball_list.append([pos[0],pos[1],"Blue"])
#To remove the circles
remove = []
for ball in ball_list:
if distance(ball,pos) < ball_radius:
remove.append(ball)
if remove == []:
ball_list.append(pos)
else:
ball_list.pop(ball_list.index(ball))
def draw(canvas):
for ball in ball_list:
#canvas.draw_circle(ball_pos, BALL_RADIUS, 1, "Black", ball_color)
canvas.draw_circle([ball[0],ball[1]],ball_radius,1,"Black",ball_color)
#create frame
frame = simplegui.create_frame("Mouse selection", WIDTH, HEIGHT)
frame.set_canvas_background("White")
#register event handler
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)
#star frame
frame.start()