forked from ernestas-poskus/interactive-programming-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_input3.py
More file actions
44 lines (34 loc) · 952 Bytes
/
mouse_input3.py
File metadata and controls
44 lines (34 loc) · 952 Bytes
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
# Examples of mouse input
import simplegui
import math
# intialize globals
width = 450
height = 300
ball_list = []
ball_radius = 15
ball_color = "Red"
# helper function
def distance(p, q):
return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)
# define event handler for mouse click, draw
def click(pos):
remove = []
for ball in ball_list:
if distance(ball, pos) < ball_radius:
remove.append(ball)
if remove == []:
ball_list.append(pos)
else:
for ball in remove:
ball_list.pop(ball_list.index(ball))
def draw(canvas):
for ball in ball_list:
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)
# start frame
frame.start()