Skip to content

Instantly share code, notes, and snippets.

@vietlq
Last active May 1, 2019 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vietlq/04854aef227bc53a71ee096976a1c1f1 to your computer and use it in GitHub Desktop.
Save vietlq/04854aef227bc53a71ee096976a1c1f1 to your computer and use it in GitHub Desktop.
Answer to the puzzle of a square and 3 angles
import turtle, math
def p_line(t, n, length, angle):
"""Draws n line segments."""
for i in range(n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
"""Draws a polygon with n sides."""
angle = 360/n
p_line(t, n, length, angle)
def move(t, dx, dy):
t.up()
x, y = t.pos()
t.setpos(x + dx, y + dy)
t.down()
def main():
window = turtle.Screen() # create a screen
window.bgcolor("#f5fffa")
ivy = turtle.Turtle()
ivy.shape("arrow")
ivy.color("#EF597B")
ivy.width(2)
ivy.speed(20)
ivy.up()
ivy.goto(-100, -100)
ivy.down()
polygon(ivy, 4, 150)
move(ivy, 0, 50)
ivy.fd(150)
move(ivy, 0, 50)
ivy.back(150)
move(ivy, 50, 50)
ivy.rt(90)
ivy.fd(150)
move(ivy, 50, 0)
ivy.back(150)
move(ivy, -50, -50)
ivy.color("green")
ivy.setpos(50, 50)
ivy.color("blue")
ivy.setpos(0, -100)
ivy.color("green")
ivy.setpos(-50, 0)
ivy.ht()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment