Run update21, if you haven't already, to create the cs21/labs/06 directory. Then cd into your cs21/labs/06 directory and create the python program for lab 6 in this directory (handin21 looks for your lab 6 assignments in your cs21/labs/06 directory):
$ update21 $ cd ~/cs21/labs/06 $ pwd /home/your_user_name/cs21/labs/06
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.
For this lab we will continue using the
Zelle graphics library. We will also
introduce the while loop and continue to practice writing
functions.
In this lab you will write a graphical typing tutor, in the file typingTutor.py, in which a player attempts to stop falling letters (all lowercase) from hitting the bottom of the screen by typing those letters on the keyboard. The goal of this lab is to give you more practice using functions, as well as give practice using an indefinite loop.
As in Lab 05, the key data in your program will be a list. However this time the list will be of Text objects, with one Text object for each letter currently on the screen. You will use an indefinite loop to create Text objects (with each Text object representing one letter) at random locations near the top of the screen, and animate the letters as they gradually drop towards the bottom of the screen. The player earns points by typing letters on the keyboard; when the player types a letter on the screen, you should undraw the letter and stop animating the letter.
Here are some screen shots of my sample solution as I am playing:
The start: | A few seconds later: | The letters are falling: |
Ooooh, I typed a letter: | The letters are getting low: | Game over: |
Below we suggest several functions that you should write to help implement your game. As usual, you should write these functions and then extensively test them in main. After you know that they work, you can remove your test code and write the main function of your program.
Write a function createLetter(window) that creates a new letter. It should take the following parameter:
This function should return the letter (Text object) it creates. Test your function by creating a window and an (initially) empty list of letters in main, use createLetter to create new letters, and append those returned letters to the list.
Hint: You can use the randrange function from the random library to get a random integer from a given range. To use this, you can put the statement:
from random import randrangenear the top of your program. To get a random character, you can use the choice function from the random library. To use this, you can put the statement:
from random import choicenear the top of your program. Your code to get a random character could look something like this:
ch = choice("abcdefghijklmnopqrstuvwxyz")
Text objects have a move method which works just like the move method for Circle objects. That is, the move method takes two arguments, a dx and dy, which will move the letter dx pixels in the x direction and dy units in the y direction. We will have our letters drop straight down towards the bottom of the screen so we will not have to modify the x coordinate of the letters once they have been created. Once you are able to create and move letters, use a loop in main and Python's sleep function (in main) to animate the letters so they fall gradually toward the bottom of the screen. To use the sleep function you will need to import it from the time library:
from time import sleep
The game ends when any of the letters hit the bottom of the screen. To help determine this, write a function nonePastBottom(window, letters) that takes two parameters:
Once you've written nonePastBottom, test it in main by
using it as the condition in an indefinite (while) loop, ending
the game when any letters reach the bottom of the screen.
When a user types a letter, you should undraw that letter and remove it from the list of visible letters so that it is no longer animated and no longer ends the game if it hits the bottom of the screen.
To help determine if a letter has been typed, write a function matchLetters(letters, character) that takes two parameters:
As usual, test your matchLetters function
inside main before continuing your implementation. To test
this, you might consider using
<window>.getKey()
or <window>.checkKey(). The method getKey is
similar to getMouse but instead of returning
the Point the user clicked, getKey will wait until
the user presses a key on the keyboard and returns the key the user
pressed as a one character string. checkKey will also return
a string of the last key pressed but not wait for the user to press a
key. If the user hasn't pressed a key since the last call, this
function will return None.
Once you've implemented and tested the above functions, you should have some list of letters that are animated down the screen in an indefinite loop, with the game ending when any letters hit the bottom of the screen. You can turn this into a game as follows:
>>> list = ['one', 'two', 'three'] >>> list ['one', 'two', 'three'] >>> list.pop(1) 'two' >>> list ['one', 'three']
You may run handin21 as many times as you like, and only the
most recent submission will be recorded. This is useful if you realize,
after handing in some programs, that you'd like to make a few more
changes to them.