Skip to content

Commit 5f59fb4

Browse files
Add files via upload
1 parent 094754c commit 5f59fb4

File tree

1 file changed

+389
-0
lines changed

1 file changed

+389
-0
lines changed

turtle.py

Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
# import casioplot
2+
from math import sqrt, pi, cos, sin, atan2
3+
import gint
4+
5+
turtleshapes={"classic":[[-9,5],[-9,4],[-8,4],[-8,3],[-8,2],[-8,-2],[-8,-3],[-8,-4],[-9,-4],[-9,-5],[-7,4],[-7,1],[-7,0],[-7,-1],[-7,-4],[-6,3],[-6,-3],[-5,3],[-5,-3],[-4,2],[-4,-2],[-3,2],[-3,-2],[-2,1],[-2,-1],[-1,1],[-1,-1],[0,0]],"turtle": [[-3,3],[2,3],[-2,2],[-1,2],[0,2],[1,2],[-2,1],[-1,1],[1,1],[0,1],[2,1],[3,1],[-2,0],[-1,0],[0,0],[1,0],[-1,-1],[-2,-1],[0,-1],[1,-1],[2,0],[3,0],[-3,-2],[2,-2]]}
6+
turtle_name="classic"
7+
turtle_data=turtleshapes[turtle_name]
8+
turtle_pos=[0,0]
9+
turtle_angle=0
10+
turtle_color=(0,0,0)
11+
writing=True
12+
pen_pixels=[[0,0]]
13+
turtle_buffer=[]
14+
turtle_speed=5
15+
frame_count=0
16+
turtle_visible=True
17+
pen_size=1
18+
19+
def _draw_turtle(x,y,a,c):
20+
global turtle_buffer
21+
def inbuffer(x,y):
22+
inlist=False
23+
for i in range(len(turtle_buffer)):
24+
if x==turtle_buffer[i][0] and y==turtle_buffer[i][1]:
25+
inlist=True
26+
return inlist
27+
if turtle_visible==True:
28+
u=cos(a*pi/180)
29+
v=sin(a*pi/180)
30+
for point in turtle_data:
31+
xx=x+(point[0]*u-point[1]*v)
32+
yy=y+(point[1]*u+point[0]*v)
33+
xpixel=int(round(xx+192))
34+
ypixel=int(round(-yy+96))
35+
if (0<=xpixel<=383 and 0<=ypixel<=191):
36+
old_color = gint.dgetpixel(xpixel, ypixel)
37+
turtle_buffer += [[xpixel, ypixel, old_color]]
38+
gint.dpixel(xpixel, ypixel, c)
39+
40+
# if not inbuffer(xpixel,ypixel):
41+
# turtle_buffer+=[[xpixel,ypixel,casioplot.get_pixel(xpixel,ypixel)]]
42+
# casioplot.set_pixel(xpixel,ypixel,c)
43+
44+
def _erase_turtle():
45+
global turtle_buffer
46+
for i in range(len(turtle_buffer)):
47+
xpixel=turtle_buffer[i][0]
48+
ypixel=turtle_buffer[i][1]
49+
if turtle_buffer[i][2]!=None :
50+
lastcolor = turtle_buffer[i][2]
51+
# lastcolor = gint.C_RGB(int(turtle_color[0]*31), int(turtle_color[1]*31), int(turtle_color[2]*31))
52+
else:
53+
lastcolor= gint.C_WHITE # (255,255,255)
54+
gint.dpixel(xpixel, ypixel, lastcolor)
55+
# casioplot.set_pixel(xpixel,ypixel,lastcolor)
56+
turtle_buffer.clear()
57+
58+
def _pen_brush(x,y,turtle_color):
59+
global frame_count
60+
_erase_turtle()
61+
xpixel=int(round(x+192))
62+
ypixel=int(round(-y+96))
63+
if writing==True and (0<=xpixel<=383 and 0<=ypixel<=191) :
64+
colorpixel = gint.C_RGB(int(turtle_color[0]*31), int(turtle_color[1]*31), int(turtle_color[2]*31))
65+
# colorpixel=(int(turtle_color[0]*255), int(turtle_color[1]*255),int(turtle_color[2]*255))
66+
67+
for point in pen_pixels:
68+
gint.dpixel(xpixel + point[0], ypixel + point[1], colorpixel)
69+
# casioplot.set_pixel(xpixel+point[0],ypixel+point[1],colorpixel)
70+
71+
frame_count+=1
72+
if turtle_speed!=0:
73+
if frame_count%(turtle_speed*4)==0:
74+
_draw_turtle(x,y,turtle_angle,colorpixel)
75+
gint.dupdate()
76+
# casioplot.show_screen()
77+
else :
78+
if frame_count%500==0:
79+
_draw_turtle(x,y,turtle_angle,colorpixel)
80+
gint.dupdate()
81+
# casioplot.show_screen()
82+
83+
def _refresh_turtle():
84+
c = gint.C_RGB(int(turtle_color[0]*31), int(turtle_color[1]*31), int(turtle_color[2]*31))
85+
# c=(int(turtle_color[0]*255), int(turtle_color[1]*255),int(turtle_color[2]*255))
86+
_erase_turtle()
87+
_draw_turtle(turtle_pos[0],turtle_pos[1],turtle_angle,c)
88+
# casioplot.show_screen()
89+
gint.dupdate()
90+
91+
def _conv_angle(a):
92+
a=a%360
93+
if a < 0:
94+
a=360+a
95+
return a
96+
97+
def back(n):
98+
forward(-n)
99+
100+
def backward(n):
101+
back(n)
102+
103+
def bk(n):
104+
backward(n)
105+
106+
def circle(radius,extent=360):
107+
global turtle_angle, turtle_pos
108+
x1=turtle_pos[0]
109+
y1=turtle_pos[1]
110+
if round(radius)==0:
111+
_pen_brush(x1,y1,turtle_color)
112+
turtle_angle+=extent
113+
elif round(extent,8)==0:
114+
_pen_brush(x1,y1,turtle_color)
115+
else:
116+
e=radius/abs(radius)
117+
theta=extent*pi/180*e
118+
Rx=cos(theta)
119+
Ry=sin(theta)
120+
Dx=radius*sin(turtle_angle*pi/180)
121+
Dy=-radius*cos(turtle_angle*pi/180)
122+
xcenter=x1-Dx
123+
ycenter=y1-Dy
124+
nbpixelarc=int(round(abs(radius*theta*1.05)))
125+
angle=turtle_angle
126+
if nbpixelarc!=0:
127+
alpha=theta/nbpixelarc
128+
for k in range(nbpixelarc+1):
129+
x=xcenter+Dx*cos(alpha*k)-Dy*sin(alpha*k)
130+
y=ycenter+Dx*sin(alpha*k)+Dy*cos(alpha*k)
131+
turtle_angle+=alpha*180/pi
132+
_pen_brush(x,y,turtle_color)
133+
turtle_pos[0]=xcenter+Dx*Rx-Dy*Ry
134+
turtle_pos[1]=ycenter+Dx*Ry+Dy*Rx
135+
turtle_angle=angle+extent*e
136+
turtle_angle=_conv_angle(turtle_angle)
137+
_refresh_turtle()
138+
139+
def clear():
140+
_erase_turtle()
141+
gint.dclear(gint.C_WHITE)
142+
# casioplot.clear_screen()
143+
gint.dupdate()
144+
# casioplot.show_screen()
145+
_refresh_turtle()
146+
147+
def distance(x,y):
148+
return sqrt((x-turtle_pos[0])**2+(y-turtle_pos[1])**2)
149+
150+
def down():
151+
global writing
152+
writing=True
153+
154+
def fd(d):
155+
forward(d)
156+
157+
def forward(d):
158+
global turtle_pos
159+
dx=d*cos(turtle_angle*pi/180)
160+
dy=d*sin(turtle_angle*pi/180)
161+
x1=turtle_pos[0]
162+
y1=turtle_pos[1]
163+
if round(abs(d))==0:
164+
_pen_brush(x1+dx,y1+dy,turtle_color)
165+
elif abs(dx)>=abs(dy):
166+
e=int(dx/abs(dx))
167+
m=dy/dx
168+
p=y1-m*x1
169+
for x in range(int(round(x1)),int(round(x1+dx)),e):
170+
_pen_brush(x,m*x+p,turtle_color)
171+
else:
172+
e=int(dy/abs(dy))
173+
m=dx/dy
174+
p=x1-m*y1
175+
for y in range(int(round(y1)),int(round(y1+dy)),e):
176+
_pen_brush(m*y+p,y,turtle_color)
177+
turtle_pos[0]+=dx
178+
turtle_pos[1]+=dy
179+
_refresh_turtle()
180+
181+
def goto(x,y):
182+
a=turtle_angle
183+
setheading(towards(x,y))
184+
forward(distance(x,y))
185+
setheading(a)
186+
_refresh_turtle()
187+
188+
def heading():
189+
return turtle_angle
190+
191+
def hideturtle():
192+
global turtle_visible
193+
turtle_visible=False
194+
_refresh_turtle()
195+
196+
def home():
197+
global turtle_angle
198+
goto(0,0)
199+
turtle_angle=0
200+
_refresh_turtle()
201+
202+
def ht():
203+
hideturtle()
204+
205+
def isdown():
206+
return writing
207+
208+
def isvisible():
209+
return turtle_visible
210+
211+
def left(a):
212+
right(-a)
213+
214+
def lt(a):
215+
left(a)
216+
217+
def pd():
218+
down()
219+
220+
def pencolor(*c):
221+
global turtle_color
222+
colornames={"black":(0,0,0),"blue":(0,0,1),"green":(0,1,0),"red":(1,0,0),"cyan":(0,1,1),"yellow":(1,1,0),"magenta":(1,0,1),"white":(1,1,1),"orange":(1,0.65,0),"purple":(0.66,0,0.66),"brown":(0.75,0.25,0.25),"pink":(1,0.75,0.8),"grey":(0.66,0.66,0.66)}
223+
if c==():
224+
return turtle_color
225+
elif c[0] in colornames:
226+
turtle_color=colornames[c[0]]
227+
elif isinstance(c[0],(list,tuple)) and len(c[0])==3 and isinstance(c[0][0],(int,float)) and isinstance(c[0][1],(int,float)) and isinstance(c[0][2],(int,float)) and 0<=c[0][0]<=1 and 0<=c[0][1]<=1 and 0<=c[0][2]<=1:
228+
turtle_color=list(c[0])
229+
230+
elif len(c)==3 and isinstance(c[0],(int,float)) and isinstance(c[1],(int,float)) and isinstance(c[2],(int,float)) and 0<=c[0]<=1 and 0<=c[1]<=1 and 0<=c[2]<=1:
231+
turtle_color=list(c)
232+
233+
else:
234+
raise ValueError('error using pencolor : enter a color text or 3 floats between 0 and 1')
235+
_refresh_turtle()
236+
237+
def pendown():
238+
down()
239+
240+
def pensize(n=None):
241+
global pen_pixels,pen_size
242+
penshape=[[0,0],[1,0],[0,1],[-1,0],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1],[2,0],[0,2],[-2,0],[0,-2],[2,1],[1,2],[-2,1],[-1,2],[2,-1],[1,-2],[-2,-1],[-1,-2]]
243+
if n==None:
244+
return pen_size
245+
elif isinstance(n,(int,float)) and n>=0:
246+
pen_size=n
247+
if round(n)==0 or round(n)==1 :
248+
pen_pixels=[[0,0]]
249+
elif round(n)==2 :
250+
pen_pixels=penshape[0:5]
251+
elif round(n)==3 :
252+
pen_pixels=penshape[0:9]
253+
elif round(n)==4 :
254+
pen_pixels=penshape[0:13]
255+
elif round(n)==5 :
256+
pen_pixels=penshape[0:21]
257+
elif round(n)>5 :
258+
pen_pixels=penshape[0:21]
259+
pen_size=5
260+
print('Userwarning: pensize over 5 automatically set to 5.')
261+
else:
262+
raise ValueError('Error using function pensize: enter a real between 0 & 5')
263+
_refresh_turtle()
264+
265+
def penup():
266+
global writing
267+
writing=False
268+
269+
def pos():
270+
return position()
271+
272+
def position():
273+
return (xcor(),ycor())
274+
275+
def pu():
276+
penup()
277+
278+
def reset():
279+
global turtle_color,writing,pen_pixels,turtle_speed,turtle_visible,pen_size
280+
turtle_color=(0,0,0)
281+
clear()
282+
hideturtle()
283+
penup()
284+
home()
285+
pendown()
286+
writing=True
287+
pen_size=1
288+
pen_pixels=[[0,0]]
289+
turtle_speed=5
290+
shape("classic")
291+
turtle_visible=True
292+
_refresh_turtle()
293+
294+
def right(a):
295+
global turtle_angle
296+
if isinstance(a, (int, float)):
297+
turtle_angle = _conv_angle(turtle_angle-a)
298+
else:
299+
raise ValueError('error')
300+
_refresh_turtle()
301+
302+
def rt(a):
303+
right(a)
304+
305+
def seth(a):
306+
setheading(a)
307+
308+
def setheading(a):
309+
global turtle_angle
310+
turtle_angle=_conv_angle(a)
311+
_refresh_turtle()
312+
313+
def setpos(x,y):
314+
goto(x,y)
315+
316+
def setposition(x,y):
317+
setpos(x,y)
318+
319+
def setx(x):
320+
goto(x,turtle_pos[1])
321+
322+
def sety(y):
323+
goto(turtle_pos[0],y)
324+
325+
def shape(name=None):
326+
global turtle_name,turtle_data
327+
if name==None:
328+
return turtle_name
329+
elif name in turtleshapes:
330+
turtle_name=name
331+
turtle_data=turtleshapes[name]
332+
else:
333+
raise ValueError('available shapes: "classic" or "turtle"')
334+
_refresh_turtle()
335+
336+
def showturtle():
337+
global turtle_visible
338+
turtle_visible=True
339+
_refresh_turtle()
340+
341+
def speed(speed=None):
342+
global turtle_speed
343+
speedwords = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 }
344+
if speed==None:
345+
return turtle_speed
346+
elif isinstance(speed,(int,float)) and (speed<=0.5 or speed>=10.5):
347+
turtle_speed=0
348+
elif isinstance(speed,(int,float)) and (0.5<speed<10.5):
349+
turtle_speed=int(round(speed))
350+
elif isinstance(speed,str) and speed in speedwords:
351+
turtle_speed=speedwords[speed]
352+
else:
353+
raise ValueError("Error using function speed: enter a real between 0 & 10")
354+
355+
def st():
356+
showturtle()
357+
358+
def towards(x,y):
359+
if round(x-turtle_pos[0],8)==0 and round(y-turtle_pos[1],8)==0:
360+
return 0
361+
else:
362+
ang=atan2(y-turtle_pos[1],x-turtle_pos[0])*180/pi
363+
if ang>=0:
364+
return (ang)
365+
else:
366+
return (360+ang)
367+
368+
def up():
369+
penup()
370+
371+
def width(n=None):
372+
return pensize(n)
373+
374+
def write(text):
375+
_refresh_turtle()
376+
xpixel=int(round(turtle_pos[0]+192))
377+
ypixel=int(round(-turtle_pos[1]+96))
378+
# c=(int(turtle_color[0]*255), int(turtle_color[1]*255),int(turtle_color[2]*255))
379+
c = gint.C_RGB(int(turtle_color[0]*31), int(turtle_color[1]*31), int(turtle_color[2]*31))
380+
gint.dtext(xpixel, ypixel, c, str(text))
381+
# casioplot.draw_string(xpixel,ypixel,str(text),c,"small")
382+
gint.dupdate()
383+
# casioplot.show_screen()
384+
385+
def xcor():
386+
return round(turtle_pos[0],6)
387+
def ycor():
388+
return round(turtle_pos[1],6)
389+
# type: ignore

0 commit comments

Comments
 (0)