-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtleDrawer.py
More file actions
203 lines (174 loc) · 6.1 KB
/
TurtleDrawer.py
File metadata and controls
203 lines (174 loc) · 6.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Python program to draw square
# using Turtle Programming
import turtle
import datetime
from ErrorHandling import *
from datetime import datetime
import time
from main import Start, Update
import Tools
t = turtle.Turtle()
wn = turtle.Screen()
wn.tracer(0)
t.hideturtle()
def RGB(R : float, G : float, B : float):
"""Converts 3 RGB values to their decimal equivelent
Parameters
----------
R : float, required
Red chanel of the color
G : float, required
Green chanel of the color
B : float, required
Blue chanel of the color
"""
return R/255, G/255, B/255
def teleport(x1 : float, y1 : float):
"""Moves the tutle to the specified Canvis position without drawing
Parameters
----------
x1 : float, required
X positions destination
y1 : float, required
Y position destination
"""
t.penup()
t.goto(Tools.CanvisToScreenPosition(x1, y1))
def DrawQuad(x1 : float, y1 : float, x2 : float, y2 : float, PenColor = None, FillColor = None, Thickness : float = 1, curve : float = 0):
"""Draws a rectangle at the corasponding position
Parameters
----------
x1 : float, required
X positions of first corner
y1 : float, required
Y position of first corner
x2 : float, required
X position of the second corner
y2 : float, required
Y position of the second corner
PenColor : var, optional (Defalt: None)
Color of the boarder, if omited will use fill color
FillColor : var, optional (Defalt: None)
Fill color of the quad, if omited the shape will not be filled
Thickness : float, optional (Defalt: 1)
The size of the quads boarder
curve : float, optional (Defalt: 0)
Defines the radius of the curve of the corners
"""
#adjusts the size of the pen based on canvas size
t.pensize(Tools.PenToScreenSize(Thickness))
#Sets starting conditons based on input values
if curve == 0:
teleport(x1, y1)
t.begin_fill()
if PenColor == None and FillColor != None:
PenColor = FillColor
try:
t.pencolor(PenColor[0], PenColor[1], PenColor[2])
except:
raise "No provided pen color or fill color"
#Draws Typical quad
#? Could be removed, else will work without this
if curve == 0:
t.pendown()
t.goto(*Tools.CanvisToScreenPosition(x2, y1))
t.goto(*Tools.CanvisToScreenPosition(x2, y2))
t.goto(*Tools.CanvisToScreenPosition(x1, y2))
t.goto(*Tools.CanvisToScreenPosition(x1, y1))
t.penup()
else:
#Creates a new set of some starting conditions to curve the corners
teleport(x2 - curve, y2)
t.pendown()
t.begin_fill()
bez = Tools.BezierCurve3p(x2 - curve, y2, x2, y2 , x2, y2 - curve)
for i in bez:
t.goto(*Tools.CanvisToScreenPosition(i[0], i[1]))
bez = Tools.BezierCurve3p(x2, y1 + curve, x2, y1 , x2 - curve, y1)
for i in bez:
t.goto(*Tools.CanvisToScreenPosition(i[0], i[1]))
bez = Tools.BezierCurve3p(x1 + curve, y1, x1, y1, x1, y1 + curve)
for i in bez:
t.goto(*Tools.CanvisToScreenPosition(i[0], i[1]))
bez = Tools.BezierCurve3p(x1, y2 - curve, x1, y2, x1 + curve, y2)
for i in bez:
t.goto(*Tools.CanvisToScreenPosition(i[0], i[1]))
bez = Tools.BezierCurve3p(x2 - curve, y2, x2, y2 , x2, y2 - curve)
t.goto(*Tools.CanvisToScreenPosition(*bez[0]))
t.penup()
#Ends the shape and resets changed values and conditions
if FillColor != None:
t.fillcolor(FillColor)
t.end_fill()
t.pensize(1)
def WriteText(text : str, x : float, y : float, size : float, color = "White", HAlign : str = "center", VAlign : str = "center", font : str = "Arial"):
"""Writes text with the specified peramiters at the given position
Parameters
----------
text : str, required
The Text to be writen
x : float, required
X position of the text
y : float, required
Y position of the text
size : float, required
The size in CP of the text
color : str or triplet, optional (Defalt: "white")
Color of the text
HAlighn : str (left, center, right), optional (Defalt: "center")
The horazontal alignment of the text
VAlign : str (up, center, down), optional (Defalt: "center")
The vertical alignment of the text
Font : str, optional (Defalt: "Arial")
The font of the text
Raises
------
InvalideFunctionArg
If VAlign is not one of the 3 valade conditions
TurtleError
If HAlign is not one of the 3 valade conditions
"""
if VAlign == "up":
teleport(x, y)
elif VAlign == "center":
teleport(x, y - size * 0.75)
elif VAlign == "down":
teleport(x, y - size * 1.5)
else:
raise InvalideFunctionArg("VAlign", VAlign, ("up, center, down"))
#print(t.pos())
#print(f"{x} + {y}")
t.pencolor(color)
t.write(text, align=HAlign, font= (font, Tools.PenToScreenSize(size), "normal"))
frameDelay = 0
def SetFramerate(FPS : int):
"""Sets the max framerate of the display
Parameters
----------
FPS : int, required
Max FPS of the display, use -1 for no max
"""
global frameDelay
frameDelay = 1/FPS
timeRate = 1
#Time since last frame
def DeltaTime() -> float:
"""Returns the time since the last fram
"""
return (datetime.now().timestamp() - lastFramTime) * timeRate
def ClearScreen():
"""Clears the screen
"""
t.clear()
#now_utc = datetime.now().timestamp()
#print(now_utc)
Start()
lastFramTime = 0
frame = 0
while True:
if datetime.now().timestamp() > lastFramTime + frameDelay:
lastFramTime = datetime.now().timestamp()
Update(frame)
frame += 1
wn.update()
wn.mainloop()