Home > Enterprise >  Why is my turtle code only drawing three rectangles when it is supposed to draw four? Starts bugging
Why is my turtle code only drawing three rectangles when it is supposed to draw four? Starts bugging

Time:11-14

This is a branch from my main code of a lottery ticket generator. The purpose of these two functions: (1) drawButton(length) to create rectangular buttons. (2) createMenu() to call drawButton(length) and to fill the buttons with labels.

My issue is when the main code attempts to return to the main menu, it runs turtle.clearscreen(). Shortly after writing the main menu, it did not draw the first rectangle/button.

Let me know if you guys get a different result than I do.

import turtle
import time

t1 = turtle.Turtle()
t1.speed(0)
t1.penup()

def drawButton(length):
    length1 = length*5
    for i in range(2):
        t1.fd(length1)
        t1.lt(90)
        t1.fd(length)
        t1.lt(90)

def createMenu():
    t1.sety(-13)
    down = t1.ycor()

    for i in range(4):
        t1.goto(-150, down)
        t1.pendown()
        drawButton(60)
        t1.penup()
        down = t1.ycor()-100

createMenu()
time.sleep(2)

turtle.clearscreen()

createMenu()

turtle.done()

This is what the program should draw on the second function call: This is what the program should draw on the second function call

This is what I get after the second function call: This is what I get after the second function call

CodePudding user response:

There is a Simple fix, just change turtle.clearscreen() to t1.clear(). They are two different commands and work differently. https://stackoverflow.com/a/42260054/18554284 This Answer has a better explanation of their working

CodePudding user response:

Try using turtle.resetscreen() to also reset the state of the turtle.

  • Related