【问题标题】:How can I start drawing from a specific place in Python turtle?如何从 Python turtle 的特定位置开始绘图?
【发布时间】:2021-11-18 06:12:31
【问题描述】:

我正在尝试画草,我正在努力让我的画从左下角开始,然后一直到右下角。我知道我的代码有问题,但无法弄清楚。

我的代码是从中间开始的,这不是我想要的。

import turtle
import random

window = turtle.Screen()
bob = turtle.Turtle()
window.bgcolor("white")
window.title("TURTLE")
bob.pencolor("green")

window.colormode(255)


position = 0
height = 0
height11 = 0
height12 = 0
height13 = 0
height14 = 0
def draw_grass(bob):
    green = random.randint (100, 200)
    length = random.randint(10,20)
    bob.fillcolor(0,green,0)
    bob.begin_fill()
    bob.setheading(90)
    for i in range (2):
        
        bob.forward(length)
        bob.right(90) 
        bob.forward(3)
        bob.right(90)
    bob.end_fill()
    bob.penup()
    bob.pendown()
    return length
for i in range (10):
    height = draw_grass(bob)
    position = position + 3
    bob.goto(position, 0)
    if height == 11 :
        height11 = height11 + 1
    elif height == 12:
        height12 = height12 + 1
    elif height == 13:
        height13 = height13 + 1

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

    您需要了解原点(0, 0) 位于窗口的中心,并且您必须使用正数和负数。 screen 方法window_width()window_height() 对于确定您没有自己配置的窗口的大小很有用。以下是包含这些想法的代码修改:

    from turtle import Screen, Turtle
    from random import randint
    
    def draw_grass(turtle):
        green = randint(100, 200)
        length = randint(10, 20)
        turtle.fillcolor(0, green, 0)
    
        turtle.begin_fill()
    
        for _ in range(2):
            turtle.forward(3)
            turtle.left(90)
            turtle.forward(length)
            turtle.left(90)
    
        turtle.end_fill()
    
        turtle.penup()
        turtle.pendown()
    
        return length
    
    screen = Screen()
    screen.bgcolor('white')
    screen.title("TURTLE")
    screen.colormode(255)
    
    turtle = Turtle()
    turtle.pencolor('green')
    turtle.speed('fastest')  # because I have no patience
    
    turtle.penup()
    turtle.setposition(6 - screen.window_width()//2, 12 - screen.window_height()//2)
    turtle.pendown()
    
    height11 = 0
    height12 = 0
    height13 = 0
    height14 = 0
    
    while turtle.xcor() < screen.window_width()//2 - 6:
        height = draw_grass(turtle)
    
        if height == 11:
            height11 += 1
        elif height == 12:
            height12 += 1
        elif height == 13:
            height13 += 1
        elif height == 14:
            height14 += 1
    
        turtle.forward(3)
    
    screen.exitonclick()
     
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2015-03-20
      • 2022-01-21
      • 2019-01-01
      • 2019-05-29
      相关资源
      最近更新 更多