使用Python龟

时间:2017-01-04 19:01:15

标签: python turtle-graphics

我的龟背代码有什么问题?我希望她在布拉德的方形圈内旋转。

我的代码:

import turtle

def draw_square(some_turtle):

    for i in range (1,5):
        some_turtle.forward(200)
        some_turtle.right(90)

def draw_art():
    window = turtle.Screen()
    window.bgcolor("black")
    #Turtle Brad
    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(6)
    brad.pensize(2)
    for i in range(1,37):
        draw_square(brad)
        brad.right(10)
    #Turtle Angie
    angie = turtle.Screen()
    angie.shape("turtle")
    angie.color("blue")
    angie.speed(5)
    angie.pensize(2)
    size=1
    while (True):
        angie.forward(size)
        angie.right(91)
        size = size + 1

    window.exitonclick()

draw_art()

以下是我希望它看起来像的照片。我希望布拉德的外部显示,然后内部的圆圈包括螺旋。它应该看起来像附加的螺旋图像。谢谢!

brad angie

6 个答案:

答案 0 :(得分:3)

除了angie = turtle.Turtle()(不是turtle.Screen())之外,您可能会注意到的另一个问题是您的windowexitonclick()声明无效。即点击窗口不会退出并关闭窗口,因为它是在无限while True:循环之后出现的:

while (True):
        angie.forward(size)
        angie.right(91)
        size = size + 1

window.exitonclick()

所以从未达成过。解决此问题的最简单方法是,在不增加计时器复杂性的情况下,将其作为for循环使用其他地方使用的范围,以便angie最终停止并让下一行代码执行

最后,它看起来并不像你的目标,因为brad正在向他的方块而不是四个方向画五边。一旦我们修复了它,它看起来是正确的,angie从中间而不是边缘开始。

使用上述和其他样式更改对代码进行返工:

from turtle import Turtle, Screen

def draw_square(some_turtle):

    for _ in range(4):
        some_turtle.forward(200)
        some_turtle.right(90)

def draw_art():

    # Turtle Brad
    brad = Turtle(shape="turtle")
    brad.color("yellow")
    brad.pensize(2)
    brad.speed("normal")  # 6/normal is the default so don't need to do it

    for _ in range(36):
        draw_square(brad)
        brad.right(10)

    # Turtle Angie
    angie = Turtle(shape="turtle")
    angie.color("blue")
    angie.pensize(2)
    angie.speed(5)  # slightly slower than brad

    size = 1

    for _ in range(300):
        angie.forward(size)
        angie.right(91)
        size += 1

window = Screen()
window.bgcolor("black")

draw_art()

window.exitonclick()

一旦angie完成设计,您应该只需点击窗口即可让它消失。对于像这样的复杂设计,我很想将turtle.speed()设置为"快速"并且"最快"因为我没有耐心。 (而不是数字使用“最快'”,“快速','正常','慢'&'最慢和' #39;相反,除非你需要对速度进行非常精细的控制,否则要避免意外。)

enter image description here

答案 1 :(得分:1)

这一行错了:

angie = turtle.Screen()

应该是:

angie = turtle.Turtle()

答案 2 :(得分:1)

angieTurtle而不是Screen

将第22行更改为 angie = turtle.Turtle()

答案 3 :(得分:0)

更简单的事情是这样的:

import turtle

t = turtle.Turtle()
t.speed(0)
def kuadrado(lado): 
  for i in range(36): #draw the circle of squares
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(100)
  for i in range(100): #draw the spiral inside the circle of squares
   t.forward(i)
   t.right(80)   

kuadrado(50)

input("<enter>")

您只需要调整螺旋的大小即可使音调和谐

答案 4 :(得分:-1)

enter image description here

来自龟龟导入龟,屏幕

def draw_square(some_turtle):

for _ in range(4):
    some_turtle.forward(290)
    some_turtle.right(90)

def draw_art():

# Turtle Brad
brad = Turtle(shape="turtle")
brad.color("cyan")
brad.pensize(2)
brad.speed("fast")  # 6/normal is the default so don't need to do it

for _ in range(36):
    draw_square(brad)
    brad.right(10)

# Turtle Angie
angie = Turtle(shape="turtle")
angie.color("darkcyan")
angie.pensize(2)
angie.speed(0)  # slightly slower than brad

size = 1

for _ in range(250):
    angie.forward(size)
    angie.right(91)
    size += 0

window =屏幕() window.bgcolor( “黑”)

draw_art()

window.exitonclick()

答案 5 :(得分:-2)

#made by jonny big smokes©™®❤ this code  is illegal to use without permission of the king of gypos jonny big smokes
import turtle
import random
colorlistsize = 3
wn = turtle.Screen()
wn.bgcolor("black")
tess = turtle.Turtle()

def random_color():
    levels = range(32,256,32)
    return list(random.choice(levels) for _ in range(3))


def color_selector():
  listo = []
  for i in range(colorlistsize):
    listo.append(random_color())
  return listo


while True:
  tess.speed(-1000)
  sz = 2 
  ang = random.randint(58, 302)
  color_list = color_selector()
  for i in range(500):
    tess.right(ang)
    tess.forward(sz)
    color = color_list [i % 3]
    tess.color(color)

    sz = sz+1
  tess.clear()
  tess.reset()