Search code examples
pythonfunctionfillturtle-graphics

Filling in a turtle drawing after assigning to a key


I've set up so far sort of an etchi-sketch style of program which moves a certain distance on keypress and changes colour using onkey functions. However I want to fill in the things you draw with the program by assigning it to another key, such as "space". So when "space" is executed it will fill in what I have drawn e.g. a square with the colour that I am currently using. Space bar has already been defined to stop drawing but I would also like it to execute a fill command as well.

Thanks in advance.

screen_size = 600
setup(screen_size, screen_size)
maximum_coord = (screen_size/2) - 20
bgcolor("white")
goto(0,0)
speed = 5
pensize(3)
color("red")
pendown()


# Listen for the key presses
listen()

# Define all the functions that will control forward, back, left and right
    def up():
        if ycor() < maximum_coord:
    setheading(90)
    forward(speed)
def down():
    if ycor() > -maximum_coord:
        setheading(270)
        forward(speed)
def left():
    if xcor() > -maximum_coord:
        setheading(180)
        forward(speed)
def right():
    if xcor() < maximum_coord:
        setheading(0)
        forward(speed)
def undo_move():
    undo()


#Define space bar to make the pen go up and therefore stop drawing
current_state = penup
next_state = pendown
def space_bar():
    global current_state, next_state
    next_state()
    current_state, next_state = next_state, current_state


#Define colours when keys are pressed
def red():
        color("red")

def green():
    c    olor("green")

def blue():
        color("blue")


#Define space bar to make the pen go up and therefore stop drawing

current_state = penup
next_state = pendown
def space_bar():
     global current_state, next_state
     next_state()
     current_state, next_state = next_state, current_state

# Define the function to clear all the currently drawn lines on the page,
# but keep the turtle in the same position
def clear_drawing():
    clear()


# Define all the functions that will control forward, back, left and right
s= getscreen()
s.onkey(up,"Up")
s.onkey(down,"Down")
s.onkey(left,"Left")
s.onkey(right,"Right")
s.onkey(space_bar,"space")
s.onkey(red,"r")
s.onkey(green,"g")
s.onkey(blue,"b")
s.onkey(undo_move,"z")
s.onkey(clear_drawing, "c")

    done()

Solution

  • There are two functions in Turtle called begin_fill() and end_fill() which fills in whatever shape Turtle has traced in the colour that Turtle is. The tricky thing is differentiating between when to begin_fill() or when to end_fill().

    There are many ways of doing this (e.g. changing a variables boolean value when key is pressed) but for simplistic sake I'll show you how to do it with a counter.

    First change pendown() to penup()

    global counter
    counter = 0
    
    def space_bar():
        global counter
        counter =  counter + 1
        if counter % 2 != 0:
            pendown()
            begin_fill()
        else:
            penup()
            end_fill()
    

    This function will be able to switch in and out of the function whenever it is pressed and also will fill in whatever shape you trace in Turtle.

    Edit: Get rid of one of the other space_bar() and replace one with this code to get results.