Question

So I was planning to draw the stars on the European Union flag using turtle and I have somehow managed to draw them but they are not standing straight. refer to the link to see how the stars should stand; http://en.wikipedia.org/wiki/Flag_of_Europe

thanks in advance

my code ———

import turtle
aze = turtle.Turtle()

aze.shape('turtle')

def makeStar():
        for k in range(5):
        aze.forward(20)
        aze.right(144)

for i in range(12):
        makeStar()
        aze.left(30)
        aze.penup()
        aze.forward(50) 
        aze.pendown()
turtle.mainloop()
Était-ce utile?

La solution

So below, I've posted some code that I believe does what you want. In order to get the star to always point upward, you want to save the current heading (like I do in the makeStar function) to a local variable (heading_before_star) and then reset it to 0, draw your star, and reset it the heading before.

Also, another I noticed is that your current code wasn't drawing the stars with one at 12 and 6 o'clock, they entire circle was rotated slightly. To fix this, I added a setHeading(-15) at the beginning of the program to correct for that.

import turtle
aze = turtle.Turtle()

aze.shape('turtle')

def makeStar():
  heading_before_star = aze.heading()
  aze.setheading(0)
  for k in range(5):
    aze.forward(20)
    aze.right(144)
  aze.setheading(heading_before_star)

aze.setheading(-15)
for i in range(12):
  makeStar()
  aze.left(30)
  aze.penup()
  aze.forward(50)
  aze.pendown()

turtle.mainloop()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top