Graphics Version of Twenty One
You can use these gifs of dice faces or you can choose others.
They must be called 1.gif, 2.gif, 3.gif, etc. The gif for the "pass" move
must be named 0.gif.
You won't have to upload them; we will have our own copies of the images.
You should NOT have to change the program logic in any substantial way. Loops are
still where they are; ifs are still the same logic.
Make sure that the function cleans up after itself. All prompts, messages and Entry boxes
should be undrawn before the function returns.
Feel free to be creative with the design of the game. Colors, fonts, locations, etc.
are up to you. The only requirements are that you have the information on the screen
at the appropriate times as shown in the screen shots below.
Changes to existing functions
Changes to play_turn:
- Add a GraphWin object as another parameter to play_turn.
- Add a "click to continue" at the end of the function so players can see the results of one turn before
- Don't forget to clean up the screen!
Changes to main function:
- The main function is the ONLY one to create a GraphWin. It is passed as an argument to other functions
as needed. Don't pass it unless the function needs to draw on the graphics window.
- Don't forget to add the usual "click to close" and getMouse to get the program to pause at the end and
don't forget to close the window!
Changes to pass_or_roll:
- Add a GraphWin object to the parameter list.
- Instead of using Entry objects and have the user type in P or R, use "buttons". These are NOT new classes.
- Draw two boxes - squares? Label them with Pass and Roll. Arrange on the screen as you like.
- Prompt the user with a Text object.
- Let the user click.
- Check the user's click with the is_between function below, to see if the point they clicked is inside
one of the two Rectangles drawn above. This is literally just comparing x's and comparing y's, nothing new.
- If the user didn't click in one of the two boxes, then just get another click. I considered giving an
error message but considered the lack of response to the click to be sufficient.
- This uses the same kind of input validation code that you wrote for the shell version. Yes, using a loop.
- When the user clicks in one of the boxes, then fall out of the loop and check to see WHICH box the Point
was in (using is_between again).
- Return either "R" or "P" just as the old version's function did.
- When displaying the buttons Pass and Roll, after you have identified which button has been chosen,
set the color of the button to something that stands out. This is feedback to the user that they did
make that choice. You want that to stay on the screen long enough that they can see it, but you don't
really want to make them click yet AGAIN to go on to the next player. I used a function called sleep()
from the time library. Import as usual, the name really is "time". And the argument for sleep is how many
seconds (roughly) that you want the program to pause. One second or even half a second seems to be
about right - use your own judgement but don't make it LONG! The sleep function does not return anything.
Two New Functions
- draw_die - has two parameters, the number rolled and the GraphWin
The return value is an Image object constructed as described next.
Use the number rolled to form the file name (must end with .gif) and use it in an Image constructor.
Draw the image object. Then, as said above, return that object. The reason for returning the object
is that so the function which called this one can undraw it.
This function will be no more than 5 lines long! it is NOT a long series of if's and elif's.
is_between - has 3 parameters which are Points (call them p1, p2, p3 for now)
The return value is True if the Point p3 falls between Point p1 and Point p2.
There is NO graphics done in this function; nothing is on the screen.
To be safest, you should not assume that p1 is on the left and p2 is on the right,
so check both directions. Remember to use just ONE return statement at the bottom!
You notice in the screen captures that all the information that is displayed is
"clean". This means that it is NOT just displayed on top of the old values that were there.
When the Points changes from 2 to 3, the 3 is not displayed on top of the 2.
You can use setText very nicely here.
Hint: set up your Text objects before you enter the main game loop.
Put blank strings in them and give them their locations.
If you need for the message to stay on the screen most of the time, like "Points", draw
the Text object before the loop starts. When you are ready in the loop to change the value of
the Points, and use setText to change the string in the object.