And every time I finish eating one I can press a button to get another one BIG

And every time i finish eating one i can press a

This preview shows page 105 - 108 out of 318 pages.

And every time I finish eating one, I can press a button to get another one! BIG-BANG do your MAGIC ! on-key... on-draw... stop-when... 5.4 Guessing Gooey Let’s write a GUI version of our Guess My Number game. It should display the program’s guesses and respond to the player’s key presses. We want the player to be able to press the up and down arrow keys to instruct the program. He will use the “ ” key if the program’s guess should be higher and the “ ” key if the number should be lower. If his number has been guessed, he can press “=” to terminate the game. The player can also press “q” to quit the game at any point. Throughout the game, we would also like to display the play- ing instructions on the scene. Overall, the game console will look something like this: for larger numbers, for smaller ones Press when your number is guessed, q to quit. The Data As with all the games in this book, it is important to clearly identify and interpret the data we use to represent the states of the world before we start coding. A well-chosen data rep- resentation makes all the difference, because it almost always dictates how the code will be organized.
86 Chapter 5 Since we need to keep track of the lower and upper bounds in this game, we’ll create a structure with two fields, called small and big : (struct interval (small big)) We use the word “interval” for this structure because the number we are guessing must be in the interval. We also define a number of constants, as discussed in chapter 4½: (define HELP-TEXT (text " for larger numbers, for smaller ones" TEXT-SIZE "blue")) (define HELP-TEXT2 (text "Press = when your number is guessed; q to quit." TEXT-SIZE "blue")) (define COLOR "red") The constants HELP-TEXT and HELP-TEXT2 are the instructions for the game, and the sizes of these texts are determined by TEXT-SIZE . Note that text translates the instruc- tion strings into images. The size of the game window is defined by WIDTH and HEIGHT . For the definitions of WIDTH and HEIGHT , see the code that comes with this chapter. The constant COLOR determines the display color. Finally, we’ll define a background scene as a constant: (define MT-SC (place-image/align HELP-TEXT TEXT-X TEXT-UPPER-Y "left" "top" (place-image/align HELP-TEXT2 TEXT-X TEXT-LOWER-Y "left" "bottom" (empty-scene WIDTH HEIGHT)))) We use place-image/align to place and align the images created with HELP-TEXT and HELP-TEXT2 on top of an empty scene. The image HELP-TEXT2 will be aligned along the bottom left of the screen, and HELP-TEXT will be aligned along the top left of the screen. The Main Function To explain the code, we start with the main function: (define (start lower upper) (big-bang (interval lower upper)
big-bang 87 (on-key deal-with-guess) (to-draw render) (stop-when single? render-last-scene))) As with our text-based version of Guess My Number, we let the player supply the lower and upper limits of the guesses. The initial world is created with these arguments. When the player presses a key, big-bang calls deal-with-guess to create a new state of the world if necessary. Furthermore, we use the handler function render

You've reached the end of your free preview.

Want to read all 318 pages?

• Spring '08
• Daily
• Chapter Challenges