首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >花蟒-制作更多的花

花蟒-制作更多的花
EN

Stack Overflow用户
提问于 2018-05-03 22:03:36
回答 1查看 1K关注 0票数 0

所以我正在制作这个程序,花朵只是出现,你可以设置花瓣的数量。它还有另外两个输出,一个是不同颜色的花朵,另一个是用颜色填充自己的星星。我被这个代码卡住了,因为我试图让多个花朵出现在第一个输出中,但如果没有笔重叠第一个绘制的花朵,我就不能得到它。

代码语言:javascript
复制
#Code Notes: You want three outputs. The flower (not filled), a filled 
flower with 6 different colors, and a spiraled star

#importing sys and turtle.
import sys
import turtle


#function for flower shape
def draw_square(square):
   for i in range(0,2):
      square.forward(100)
      square.right(70)
      square.forward(100)
      square.right(110)

#function to draw the flower
def draw_flower(petalNumber):
   window = turtle.Screen()
   window.bgcolor("beige")
   window.title("Flower")
#flower turtle
   flower = turtle.Turtle()
   flower.shape("triangle")
   flower.color("teal")
   flower.pencolor("beige")
   flower.pensize(3)

   flower.goto(15,70)
   flower = "teal"
   for number in range(0, petalNumber):
      flower.begin_fill()
      draw_square(flower)
      flower.end_fill()
      flower.right(360.0/petalNumber)

window.exitonclick() #when clicked the window closes

#function for colorful art (the petals are filled in with different colors)
def colorfulArt(numberOfPetals):
   colors = ["red","blue","green","orange","pink","teal","purple","yellow"]
   window = turtle.Screen()
   window.bgcolor("white")
   window.title("Pinwheel Flower")

   flower = turtle.Turtle()
   flower.shape("triangle")
   flower.color("blue")

   for petalNumber in range(numberOfPetals):
      flower.begin_fill()
      flower.color("white",colors[petalNumber%8])
      draw_square(flower)
      flower.end_fill()
      flower.right(360.0/numberOfPetals)

   window.exitonclick() #when clicked the window closes

#function for original art(which is just stars)
def originalStars(n):

   window = turtle.Screen()
   window.bgcolor("white")
   window.title("Star Power")

   star_spiral = turtle.Turtle()
   star_spiral.begin_fill()
   star_spiral.shape("triangle")
   star_spiral.color("teal")
   for count in range(20+n):
   star_spiral.forward(count*15)
   star_spiral.right(144)
   star_spiral.end_fill()
   window.exitonclick()

#let the user choose
print("Choose from the following:")
print("1. Flowers.")
print("2. Colorful Flower.")
print("3. Spiraled Star.")
UserChoice = int(input())

#if statement check the user choice
if (UserChoice == 1):
   numberOfPetals= int(input("How many petals?"))
#the next line calls the draw flower function
   draw_flower(numberOfPetals)
#elif- chapter 5 conditional statement
elif(UserChoice == 2):
   numberOfPetals = int(input("How many petals?"))
   colorfulArt(numberOfPetals) #this line calls the colorful art function
else:
   (UserChoice == 3)
   numberOfPetals= int(input("How many stars should overlap?"))
   originalStars(numberOfPetals) #this line calls the original stars 
function

EN

回答 1

Stack Overflow用户

发布于 2018-05-03 22:25:37

我注意到的一个问题是您的语句flower="teal"。通过这样做,您已经将flower对象(一个变量)转换为一个字符串,而不是一个海龟对象,因此后面的代码将失败。

下面的代码适用于我:

代码语言:javascript
复制
import sys
import turtle


def draw_square(square):
    for i in range(0, 2):
        square.forward(100)
        square.right(70)
        square.forward(100)
        square.right(110)


def draw_flower(petal_number: int):
    """
    Draws a flower on the screen with a given number of petals
    :param petal_number: 
    :return: Blocks until the user clicks on the screen
    """
    window = turtle.Screen()
    window.bgcolor("beige")
    window.title("Flower")

    flower = turtle.Turtle()
    flower.shape("triangle")
    flower.color("teal")
    flower.pencolor("beige")
    flower.pensize(3)

    flower.goto(15,70)

    for number in range(0, petal_number):
        flower.begin_fill()
        draw_square(flower)
        flower.end_fill()
        flower.right(360.0 / petal_number)

    window.exitonclick()


if __name__ == "__main__":
    draw_flower(3)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50156992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档