Objective

In this unit, we will learn about handling both keyboard and mouse events in the Turtle graphics library. Events allow your program to respond to user input, such as key presses or mouse clicks. By the end of this unit, you will understand how to bind keyboard keys and mouse events to functions and create interactive drawings.

Understanding Events in Turtle

In the Turtle library, an event is something that happens when a user interacts with the graphics window. This could be a mouse click, a key press, or other types of user input. You can make your Turtle graphics interactive by responding to these events.

To respond to an event, you need to bind a function to the event. This function will be called whenever the event occurs. The function should take no arguments for keyboard events. For mouse events, it should take two arguments representing the x and y coordinates of the mouse click.

Binding Keyboard Keys to Functions

The onkey function is used to bind a function to a key press. The first argument is the function to be called, and the second argument is the key that triggers the event. The key is given as a string, like "Up", "Down", "Left", "Right", or "space".

Here's an example of binding a function to the "Up" key:

import turtle

def move_forward():
    turtle.forward(100)

turtle.onkey(move_forward, "Up")

turtle.listen()
turtle.done()

In this example, the move_forward function moves the turtle forward by 100 units. We bind this function to the "Up" key using the onkey function. Now, whenever the "Up" key is pressed, the turtle will move forward.

Binding Mouse Events to Functions

The onclick function is used to bind a function to a mouse click. The function should take two arguments, which represent the x and y coordinates of the mouse click.

Here's an example of binding a function to a mouse click:

import turtle

def move_to(x, y):
    turtle.goto(x, y)

turtle.onscreenclick(move_to)

turtle.listen()
turtle.done()

In this example, the move_to function moves the turtle to the specified coordinates. We bind this function to a mouse click using the onscreenclick function. Now, whenever the mouse is clicked, the turtle will move to the location of the click.

Responding to Keypress and Mouse Events

To start listening for key press and mouse events, you need to call the listen function. This function tells Turtle to start waiting for events. Events that occur before listen is called will be ignored.

Once listen has been called, whenever a key that has been bound with onkey is pressed, or the mouse is clicked on the screen, the corresponding function will be called.

Creating Interactive Drawings

By binding functions to key presses and mouse events, you can create interactive Turtle graphics. For example, you could create a drawing program where the user can control the turtle using the arrow keys, change the color of the turtle using the number keys, and move the turtle to a specific location by clicking on the screen.

Project: Create a Program Where the Turtle Moves in Response to Mouse Clicks and Arrow Key Presses

In this project, you'll create a program where the turtle moves in response to mouse clicks and arrow key presses. Here's an example:

import turtle

# Create a new turtle screen and set its background color
screen = turtle.Screen()
screen.bgcolor("white")

# Initialize a turtle object
t = turtle.Turtle()

# Functions to move the turtle
def move_forward():
    t.forward(100)

def move_backward():
    t.backward(100)

def turn_left():
    t.left(90)

def turn_right():
    t.right(90)

def move_to(x, y):
    t.goto(x, y)

# Bind the functions to the arrow keys
screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")

# Bind the function to mouse clicks
screen.onscreenclick(move_to)

# Start listening for key presses and mouse clicks
screen.listen()

# Wait until the window is closed
turtle.done()

In this program, the turtle moves forward when the "Up" key is pressed, moves backward when the "Down" key is pressed, turns left when the "Left" key is pressed, and turns right when the "Right" key is pressed. Additionally, the turtle moves to the location of a mouse click when the mouse is clicked on the screen.

Try to modify this program to add more interactivity. For example, you could bind other keys to change the color of the turtle or clear the screen.

In the next unit, we'll look at Python's built-in functions and explore how we can use them to enhance our turtle drawing program.