How can I start drawing from a specific place in Python turtle?

Viewed 95

I'm trying to draw grass and I'm struggling to make my drawing start from the bottom left then go all the way to the bottom right. I know there is something wrong with my code but can't figure it out.

The code I have starts from the middle and that's not what I want.

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
1 Answers

You need to understand that the origin (0, 0) is in the center of the window and you have to work with positive and negative numbers. The screen methods window_width() and window_height() are useful for figuring out the size of a window that you didn't configure yourself. Here's a rework of your code that incorporates these ideas:

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()
 
Related