0
Python turtle, how can i put more moves and figures to one screen?
Python turtle is really intetesting, but how can i put more different shapes to one display screen? For example: star, hexagon etc. And i don't know why my exitonclick() doesn't work, where should i put it? My code: from turtle import * color('blue', 'yellow') begin_fill() while True: forward(200) left(50) if abs (pos()) < 3: break end_fill() done()
2 Réponses
0
I can't answer your exitonclick() question but I can share code for drawing the star and hexagon.
Turn at 60 degree angles instead of 50 to make a hexagon:
from turtle import *
color('blue', 'yellow')
begin_fill()
while True:
forward(100)
left(60)
if abs (pos()) < 3:
break
end_fill()
done()
Here is a star pattern:
from turtle import *
color('blue', 'yellow')
begin_fill()
toggle = True
while True:
forward(50)
if toggle:
right(120)
else:
left(60)
toggle = not toggle
if abs (pos()) < 3:
break
end_fill()
done()
In general, I would carefully think about what the turtle needs to do to make the pattern you want and give it a few tries. It might take lots of tries and patience but eventually you can draw anything you can dream.
For the exitonclick, I came across this but you likely already saw it:
https://stackoverflow.com/questions/41548813/using-turtle-module-exitonclick
0
# You can put more figures to one screen by
# using 'if t.heading() == 0:' instead of 'if abs (pos()) < 3:'.
# If my understanding isn't too incorrect,
# when 'turtle.heading == 0' then the turtle is facing 'east',
# the direction it started drawing from.
# This has worked for all the angles I've tried so far.
# Using 'if t.heading() == 0:'
# it's possible to draw figures anywhere on the screen.
#=====================================
import turtle
wn = turtle.Screen()
wn.title("Drawing Geometric Shapes")
t = turtle.Turtle()
t.color('red', 'yellow')
t.speed(0)
#=====================================
def star(x, y, length, angle):
t.penup()
t.goto(x, y)
t.pendown()
t.begin_fill()
while True:
t.forward(length)
t.left(angle)
if t.heading() == 0: #===============
break
t.end_fill()
#=====================================
# ( x, y, length, angle)
star(-470, 300, 100, 120)
star( 360, 320, 100, 160)
star(-450, -340, 100, 100)
star( 360, -340, 100, 170)
star(-360, 0, 750, 178)
#=====================================