-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.py
More file actions
131 lines (109 loc) · 3.55 KB
/
paint.py
File metadata and controls
131 lines (109 loc) · 3.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Paint Application based on the FreeGames collection, with modifications made
# for a better drawing experience.
# Authors: Regina Luna, A01655821
# Diego Samperio, A01662935
# Abigail Curiel, A01655892
# Date: 23/03/2023
from turtle import *
from freegames import vector
# Define state variables for the current drawing tool
state = {'shape': 'line'} # Current shape to draw (line, square, circle, etc.)
color = 'black' # Default drawing color
# Function to draw a line from one point to another
def line(start, end):
"Draw a line from start to end."
up()
goto(start.x, start.y) # Move to the start position
down()
goto(end.x, end.y) # Draw the line to the end position
# Function to draw a square at a given position
def square(start, end):
"Draw a square from start to end."
up()
goto(start.x, start.y)
down()
begin_fill()
for count in range(4):
forward(end.x - start.x)
left(90)
end_fill()
# Function to draw a circle based on start and end positions
def circle(start, end):
"Draw a circle from start to end."
up()
goto(start.x, start.y)
down()
begin_fill()
turtle_circle(abs(end.x - start.x))
end_fill()
# Helper function to draw a circle
def turtle_circle(radius):
"Draw a circle with a given radius."
for _ in range(360):
forward(radius * 0.0175) # Draw the circle using small steps
left(1)
# Function to draw a rectangle from start to end
def rectangle(start, end):
"Draw a rectangle from start to end."
up()
goto(start.x, start.y)
down()
begin_fill()
forward(end.x - start.x)
left(90)
forward((end.y - start.y))
left(90)
forward(end.x - start.x)
left(90)
forward((end.y - start.y))
left(90)
end_fill()
# Function to draw a triangle from start to end
def triangle(start, end):
"Draw a triangle from start to end."
up()
goto(start.x, start.y)
down()
begin_fill()
for count in range(3):
forward(end.x - start.x)
left(120)
end_fill()
# Function to handle tap events and trigger drawing
def tap(x, y):
"Store starting point or draw shape."
start = state.get('start', None)
if start is None:
state['start'] = vector(x, y) # Store the starting point
else:
shape = state['shape']
end = vector(x, y)
if shape == 'line':
line(start, end)
elif shape == 'square':
square(start, end)
elif shape == 'circle':
circle(start, end)
elif shape == 'rectangle':
rectangle(start, end)
elif shape == 'triangle':
triangle(start, end)
state['start'] = None # Reset the starting point after drawing
# Function to set the current drawing tool
def store(key, value):
"Store the shape and color to use."
state[key] = value
# Set up the drawing window
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
# Bind keyboard keys to select shapes
onkey(lambda: store('shape', 'line'), 'l') # Press 'l' to draw a line
onkey(lambda: store('shape', 'square'), 's') # Press 's' to draw a square
onkey(lambda: store('shape', 'circle'), 'c') # Press 'c' to draw a circle
onkey(lambda: store('shape', 'rectangle'), 'r') # Press 'r' to draw a rectangle
onkey(lambda: store('shape', 'triangle'), 't') # Press 't' to draw a triangle
# Set up mouse click event to trigger drawing
onscreenclick(tap)
done()