CSC 226

CSC 226 logo

Software Design and Implementation

T15: Interactive Graphics

Assignment T15 should be completed in pairs.


  • Work with events in Python

Event Driven Interactivity

We will now incorporate event driven interactivity to graphics objects (turtles and the screen) by associating mouse clicks and key presses with them.

Here are three example programs which you can run as examples:

Note the following:

  1. The onclick() method on a turtle object:
    an instance of a turtle class has an onclick() method in which you can place the name of a function that runs when the user clicks a mouse on the object. As shown in the examples, to use the onclick() method without writing our own loop, we need to import a new library called Tkinter.

  2. The onclick() method on a Screen object:

    Firstly, note first that the handler has two parameters when we define it, but they do NOT appear when inside the onclick() method. However, a function that is called on a mouse click MUST have the two parameters into which the x and y coordinates of the mouse are inserted. If you define a function that does not have two parameters, you will not be able to associate that function to the mouse click event. Secondly, notice that the screen object is the one that has a handler associated with its onclick() method; it is possible to tie a different function to the turtle's onclick() method.
  3. The onkey() and listen() methods:

    The onkey(...) function takes two parameters; the first is the function to call, and the second is the key that needs to be pressed. As explained in the text, you must call the listen(...) method in order for the object to be listening to the key presses.

The instructions

This assignment should be completed in a new pair in your team.

Rename the program koch-event.pyyourusername(s)

Then modify it so uses recursion to make it interactive via events. For example, you might consider drawing the Koch snowflake in the place you click on the screen.

Modify yourusername(s) and to make a program that has the following requirements:
  1. A complete and descriptive header comments which fully describes the purpose of your program and why it is fun. Be sure to include game rules if you write a game.
  2. Text which appears on the graphics window which directs the user.
  3. A graphics window in which there are multiple turtles which use recursion.
  4. The user can interact with your turtles or the screen using at least two different mouse click events or key press events.
  5. You designed something that is fun to interact with.
  6. Your program must have good structure and style:
    1. It must include a main() function.
    2. The highest level of the program (i.e., no indenting) must only contain the following:
      • the header
      • any import statements
      • function definitions
      • a call to the main() function
    3. It does not need to be object-oriented, but it may be. At a minimum, it must be designed in a modular fashion, correctly using functions for each task with correct parameter passing and appropriate use of returns.
    4. Be sure to use only meaningful variable and function names.
    5. Insert a descriptive docstring for each function you are designing and implementing.
When you are finished writing and testing your program, submit your source code, yourusername(s) into the correct place in Moodle.

Copyright 2016 | | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License