Skip to content

Commit

Permalink
slowed with asyncio, fixed heading #5, implemented write #3
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Aug 24, 2024
1 parent 5a3a8fd commit d802c74
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 17 deletions.
29 changes: 26 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,34 @@ def check_type(arg, *types):
end_fill ()
done ()

#import time
import asyncio

#ada = Turtle(shape='img/turtle.svg')
ada = Turtle()

for i in range(4):
ada.forward(100)
ada.left(90)
#await asyncio.sleep(1)

#for i in range(3):
ada.color('green')
ada.write("Ciao mondo!", align="right", font=("Courier", 18, "bold"))
ada.forward(100)
#time.sleep(1)
#await asyncio.sleep(1)
ada.done()
ada.left(90)

#time.sleep(1)
ada.done()

ada.circle(40)
ada.done()

ada.forward(100)

ada.color('blue')
ada.write("La la", align="center", font=("Times New Roman", 24, "italic"))
ada.done()
ada.left(90)
ada.forward(100)
ada.done()
95 changes: 81 additions & 14 deletions turtleps.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,14 @@ def setDefaultElement(element):

class Turtle:


def __init__(self,
shape=_CFG["shape"], # NOTE: this is meant to be an id
visible=_CFG["visible"]):

shape_svg_id = f'#{shape}'
_allTurtles.append (self)


self._position = [0,0]
self._paths = []
self._track = []
self._shape = shape
Expand All @@ -523,8 +523,7 @@ def __init__(self,
#use_node.setAttribute('y', 0 + _offset[1])

shape_el = document.getElementById(shape)
transform = f"translate({0 + _offset[0]},{0 + _offset[1]})"


if shape_el.tagName == 'polygon':
use_node.setAttribute('fill', _CFG["fillcolor"])
use_node.setAttribute('stroke', _CFG["pencolor"])
Expand All @@ -535,7 +534,8 @@ def __init__(self,
use_node.setAttribute('id', f"turtle-{id(self)}")

use_node.setAttribute('transform', self._svg_transform())


self.svg = use_node

self._screen.svg.appendChild(use_node)

Expand All @@ -548,9 +548,11 @@ def _svg_transform(self):
tilt_fix = 0
if shape_el.tagName == 'polygon':
tilt_fix = -90 # polygons are designed pointing top, images look natural pointing right :-/
print(f"{tilt_fix=}")

rot = math.degrees(self._heading) + tilt_fix
return f"translate({0 + _offset[0]},{0 + _offset[1]}) rotate({rot})"
rot = math.degrees(-self._heading) + tilt_fix
print(f"{rot=}")
return f"translate({self._position[0] + _offset[0]},{self._position[1] + _offset[1]}) rotate({rot})"


def reset(self):
Expand Down Expand Up @@ -615,6 +617,7 @@ def goto(self, x, y = None):
self._position[0] + _offset[0],
self._position[1] + _offset[1])
)
self._update_transform()

def _moveto(self, x, y = None):
wasdown = self.isdown()
Expand Down Expand Up @@ -665,10 +668,12 @@ def forward(self, length):
self._position[0] + _offset[0],
self._position[1] + _offset[1])
)
self._update_transform()

def back(self, length):
self.forward(-length)


def circle(self, radius):
self.left(90)
opposite = self._predict(2 * (radius + 1) + 1)
Expand Down Expand Up @@ -708,6 +713,9 @@ def heading(self):
"""
return math.degrees(self._heading)

def _update_transform(self):
self.svg.setAttribute('transform', self._svg_transform())

def setheading(self, to_angle):
"""Set the orientation of the turtle to to_angle.
Expand All @@ -731,18 +739,16 @@ def setheading(self, to_angle):
>>> turtle.heading()
90
"""
angle = (to_angle - self.heading())*self._angleOrient
full = self._fullcircle
angle = (angle+full/2.)%full - full/2.
self._rotate(angle)


self._heading = (to_angle * math.pi / 180.0) % (2 * math.pi)
self._update_transform()

def left(self, angle):
print(f"left: prev heading {self._heading}")
self._heading = (self._heading + (angle * math.pi / 180)) % (2 * math.pi)

self._heading = (self._heading + (angle * math.pi / 180.0)) % (2 * math.pi)

print(f" : new heading {self._heading}")
self._update_transform()

def right(self, angle):
self.left(-angle)
Expand All @@ -758,6 +764,67 @@ def end_fill(self):
def speed(speed = None):
pass

def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
"""Write text at the current turtle position.
Arguments:
arg -- info, which is to be written to the TurtleScreen
move (optional) -- True/False
align (optional) -- one of the strings "left", "center" or right"
font (optional) -- a triple (fontname, fontsize, fonttype)
Write text - the string representation of arg - at the current
turtle position according to align ("left", "center" or right")
and with the given font.
If move is True, the pen is moved to the bottom-right corner
of the text. By default, move is False.
Example (for a Turtle instance named turtle):
>>> turtle.write('Home = ', True, align="center")
>>> turtle.write((0,0), True)
"""
"""
<text x="20" y="35" class="small">My</text>
"""
txt = document.createElementNS (_ns, 'text')
txt.setAttribute('x', self._position[0] + _offset[0])
txt.setAttribute('y', self._position[1] + _offset[1])
txt.textContent = arg

#for now let's use text-anchor https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor
if align == "left":
text_anchor = "end"
elif align == "center":
text_anchor = "middle"
elif align == "right":
text_anchor = "start"
else:
raise ValueError(f"Unknown align: {align}")

style = f"""font-family: {font[0]};
font-weight: {font[2]};
font-size: {font[1]}px;
fill: {self._pencolor};
text-anchor:{text_anchor};
alignment-baseline:central;
"""
txt.setAttribute('style', style)

self._screen.svg.appendChild(txt)


"""
if self.undobuffer:
self.undobuffer.push(["seq"])
self.undobuffer.cumulate = True
end = self._write(str(arg), align.lower(), font)
if move:
x, y = self.pos()
self.setpos(end, y)
if self.undobuffer:
self.undobuffer.cumulate = False
"""

fd = forward
bk = back
backward = back
Expand Down

0 comments on commit d802c74

Please sign in to comment.