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:
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
TheFirstly, note first that the handler has two parameters when we define it, but they do NOT appear when inside the
onclick()method on a Screen object:
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
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.
This assignment should be completed in a new pair in your team.
Rename the program koch-event.py yourusername(s)-T15.py
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)-T15.py and to make a program that has the following requirements:
- 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.
- Text which appears on the graphics window which directs the user.
- A graphics window in which there are multiple turtles which use recursion.
- The user can interact with your turtles or the screen using at least two different mouse click events or key press events.
- You designed something that is fun to interact with.
- Your program must have good structure and style:
- It must include a main() function.
- 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
- 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.
- Be sure to use only meaningful variable and function names.
- Insert a descriptive docstring for each function you are designing and implementing.