Reputation: 9263
Turtle and Logo is a great way to explore programming. Python 3 includes a great turtle
module.
Unfortunately, the Python 3 turtle doesn't seem to support wrap around. If the turtle goes off the screen, it stays there, instead of coming to other side. This can be very frustrating for kids who can't figure out how to bring it back.
Is there a way or work around to get wrap around in Python 3 turtle
?
Upvotes: 1
Views: 681
Reputation: 41905
Is there a way or work around to get wrap around in Python 3 turtle?
Yes, you can implement it! And at the same time demonstrate to the kids the power of object-oriented programing! A simple-minded example:
from turtle import Screen, Turtle, _CFG
class WrappedTurtle(Turtle):
def __init__(self, shape=_CFG['shape'], undobuffersize=_CFG['undobuffersize'], visible=_CFG['visible']):
super().__init__(shape=shape, undobuffersize=undobuffersize, visible=visible)
def forward(self, distance):
super().forward(distance)
screen = self.getscreen()
x_flag = abs(self.xcor()) > screen.window_width()/2
y_flag = abs(self.ycor()) > screen.window_height()/2
if x_flag or y_flag:
down = self.isdown()
if down:
self.penup()
x, y = self.position()
self.hideturtle()
self.setposition(-x if x_flag else x, -y if y_flag else y)
self.showturtle()
if down:
self.pendown()
if __name__ == '__main__':
screen = Screen()
yertle = WrappedTurtle('turtle')
yertle.speed('fastest')
yertle.left(31)
while True:
yertle.forward(2)
screen.mainloop() # never reached
Upvotes: 2