I am trying to code a program that allows the user to choose out of several options of a scene to draw. I come to an issue with drawing the sun. When I run the program the sun wont stop drawing and fill in the color. Everything else works.
import turtle
import random
t = turtle.Turtle()
turtle.setup(800,600)
def draw_sunshine():
t.penup()
t.goto(350,250)
t.pendown()
t.color('black', 'yellow')
t.speed(9)
t.begin_fill()
while True:
t.forward(100)
t.left(170)
t.right(100)
if abs(t.pos()) < 1:
break
t.end_fill()
t.done()
weather = False
while weather == False:
weather = input("Would you like to draw rain or sunshine? ")
if weather == "rain":
draw_rain()
else:
draw_sunshine()
The problem is likely this termination test:
if abs(t.pos()) < 1:
break
The pos()
method returns a pair of numbers, eg (0.0, 0.0), as a turtle.Vec2D
type, which abs()
only operates on the first, x . In this situation, x will never be less than 1 as that's near the center of the screen and we're far off to the right.
Below is a different approach where we control the loop based on the number of iterations it will take to complete the drawing of the sun. To make this work, I've adjusted the sun drawing to finish in a reasonable (integral) number of iterations and still fill with color nicely:
from turtle import Turtle, Screen
def draw_sunshine(turtle):
turtle.penup()
turtle.goto(350, 250)
turtle.pendown()
turtle.color('black', 'yellow')
turtle.speed("fast")
turtle.begin_fill()
for _ in range(24):
turtle.forward(100)
turtle.left(75)
turtle.end_fill()
screen = Screen()
screen.setup(800, 600)
yertle = Turtle()
weather = ''
while not weather:
weather = input("Would you like to draw 'rain' or 'sunshine'? ")
if weather == "rain":
draw_rain(yertle)
else:
draw_sunshine(yertle)
yertle.hideturtle()
screen.exitonclick()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.