Graphics Version of Twenty One
You can use these gifs of dice faces or you can choose whatever others you like.
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 files with those names.
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.
All input statements and print statements have to be replaced by a
graphics library equivalent. Hint: you will use both Entry objects and win.getMouse() for inputs.
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.
Screen Shots
Changes to existing functions
Changes to play_turn:
- Add a GraphWin object as another parameter to play_turn.
- Add a call to draw_die at the end of the function to draw the die rolled.
If the player chose to pass, draw die "0".
- Add a getMouse call to pause so the players can see.
- Don't forget to clean up the screen after that!
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 just Rectangles.
- Draw two boxes - squares if you want? Label them with Pass and Roll. Arrange on the screen as you like. You MAY use .getCenter method if you want.
- Prompt the user with a Text message "click a box". Reword if you want.
- Let the user click.
- Check the user's click with the inbetween function below, to see if the point they clicked is inside
one of the two Rectangles drawn above.
- If the user didn't click in one of the two boxes, then just get another click. No need for an
error message, the lack of response to the click will be sufficient.
- This uses the same kind of input validation code that you wrote for the shell version. Yes, using a while loop.
- The while loop will have TWO calls to inbetween as the conditions, not
one like Homework 9. You need to check to see if the user click is in the
Pass box or the Roll box.
- 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 inbetween again).
- Return either "R" or "P" just as the old version's function did.
Two New Functions
- draw_die
- has two parameters, the number rolled and the GraphWin object
- Use the number rolled to form the file name (must end with .gif) and use it in an Image constructor.
Draw the Image object.
- The return value is the Image object .
This is so the function which called this one can undraw the Image when not needed (to clean up screen).
- This function will be no more than 5 lines long! it is NOT a long series of if's and elif's. Think of how you would build a string which is the filename
to use in the Image constructor.
- inbetween
- has 3 parameters which are Points (call them p1, p2, p3 for now)
- The return value is True if the Point p2 falls between Point p1 and Point p3.
There are NO graphics drawn in this function; nothing new is on the screen.
- YES, you SHOULD use the function from Homework 9! free function!
Cleanup
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 must use setText here.
Hint: set up your Text objects before you enter the main game loop.
Put blank strings in them and give them their locations.