tolgaatam / ColabTurtle

An HTML based Turtle implementation, in order to work in Google Colab
MIT License
55 stars 30 forks source link

Turtle example #6

Closed bdklahn closed 3 years ago

bdklahn commented 3 years ago

I realize this may not be a main priority for you. And I should probably get back to my real work (also?). :-)

But I thought I'd share my daughter's (Avery Klahn) code. You are welcome to copy/adapt (any part of) it, to use as an example in this repo at some point. :-)

import turtle
from math import sqrt

turtle.shape('turtle')
turtle.speed(3)
start_pos = (-170,-20)
egg_radius = 133
center_pos = (start_pos[0] + egg_radius, start_pos[1])
baby_turtle_radius = egg_radius/1.3
head_radius = baby_turtle_radius/3
foot_radius = baby_turtle_radius/4

def zigzag(steps, dist):
  '''Create a zigzag with the given number
  of steps to go the given distance.
  '''

  #Use Pythagorean theorem to help
  #calculate the step length for the
  #desired zigzag distance
  z = sqrt(((dist/steps)**2)/2.0)

  for i in range(steps):
    turtle.right(45)
    turtle.forward(z)
    turtle.left(90)
    turtle.forward(z)
    turtle.right(45)

turtle.penup()
turtle.goto(start_pos)
turtle.right(90)
turtle.pensize(1)
turtle.pendown()
turtle.circle(egg_radius)
turtle.write("Egg!")

turtle.left(90)
zigzag(5,2*egg_radius)
turtle.write("Cracked?!")

turtle.left(90)
turtle.pensize(5)
turtle.pencolor('ghost white')
turtle.circle(egg_radius, extent=180)

turtle.penup()
turtle.pencolor('black')
turtle.pensize(1)

turtle.left(90)
turtle.goto(center_pos)
turtle.forward(baby_turtle_radius)
turtle.left(90)
turtle.pencolor('saddle brown')
turtle.pendown()
turtle.pensize(3)
turtle.circle(baby_turtle_radius, extent=180)

turtle.penup()
turtle.pensize(1)
turtle.goto(center_pos)
turtle.write("Hatching!!")
turtle.left(180)
turtle.forward(baby_turtle_radius)
turtle.right(90)
turtle.pencolor('green')
turtle.pendown()
turtle.circle(head_radius)
turtle.penup()

leg_angle=35
leg_adjust=10
turtle.goto(center_pos)
turtle.setheading(90 + leg_angle - leg_adjust)

turtle.forward(baby_turtle_radius)
turtle.right(33)
turtle.pendown()
turtle.circle(foot_radius,steps=3,extent=270)
turtle.penup()

turtle.goto(center_pos)
turtle.setheading(90 - leg_angle - leg_adjust)
turtle.forward(baby_turtle_radius)
turtle.right(33)
turtle.pendown()
turtle.circle(foot_radius,steps=3,extent=270)

turtle.speed(2)

turtle.write("Turtle buddy??")
turtle.penup()

turtle.setheading(0)
turtle.forward(70)
turtle.setheading(180)
turtle.write('Follow me to the ocean!!!')
turtle.setheading(0)
turtle.forward(150)

If the following Trinket URL is still valid, you should be able to see it in action there. https://trinket.io/python/2d4218daeb

tolgaatam commented 3 years ago

I am now releasing a new version where some more of the original API is supported. With that update only shape(), circle() and write() would not work from this example.

I assume that I can add write() and shape() to this library as well, but not sure of circle(). I know that some exercises/homeworks involve drawing a circle manually (by a loop of 360 iteration etc.) ; so including the functionality itself into this library would harm some instructor's purposes.

For shape(), i guess we can find some svg images. For write(), svg should support written content as well.

I am releasing these changes to github now. I will be glad if you do a little test and let me know. In the meanwhile, I will be working on shape() and write(). After we are done with them as well, I can release the project to pip (PyPi).

(Disclaimer: this library handles angles differently than the classical library and I cannot alter the behaviour now, due to backward-compatibility. 0 angle is east and the angles increase in the clockwise direction. I guess this behavior was borrowed by blockly games' turtle game.)

bdklahn commented 3 years ago

I'm not a huge fan of the circle function, anyway: a) It almost seems more appropriate to call it "polygon" (but I guess it is contained in a circle). b) I agree that something like that might be good to save as an exercise.

Yeah . . . The/a maintainer of the other Turtle doesn't seem to care to continue work on that, and thinks that somewhat patched together computer GUI code isn't worth leveraging for a/this web-based version. So, while I think it is nice to have/wrap things to generally match that API, I think it is OK to have this be a Turtle "reboot", with some differences.

tolgaatam commented 3 years ago

Yes, my initial thought with this library was something like a "reboot". I had no intention to replace the standard turtle library fully. But now, it's like we are really close to that, and that should be enough for most people. If this library is for educational purposes mostly, all students see Python for the first time. So, they can just learn Turtle in this library's way :)

For circle, yes that is an even polygon, hard to call it a circle if you don't make it like 360 pieces.

tolgaatam commented 3 years ago

Closing as the discussion on v2.1.0 has ended and the version is submitted to PyPi. For any further recommendations, please feel free to open an issue. I will try to add as much to this project as possible ! 👍 Thanks for your great efforts to lead this project and its API for the best