;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname Game) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;; Patrick Lindquist ;; Problem Set 10 (Game) ;; ============== These are the two predefined functions ==================== ;; You don't need to understand their code, only the contract and the purpose ;; =========================================================================== ;; ===================== You may skip to the sample code below ================ ;; get-choice: string, list of strings -> integer ;; the function displays a simple menu form with the given message ;; and a drop-down menu with the list of choices. The menu ;; initially displays "Make a choice". Once a different item is chosen, ;; that item is returned. ;; When a "Close" button is pressed, the form disappears. (define (get-choice message list-of-choices) (local ( (define the-choices (make-choice (cons "Make a choice" list-of-choices))) (define w (create-window (list (list (make-message message)) (list the-choices) (list (make-button "Close" (lambda (e) (hide-window w))))))) (define (process-result n) (cond [(> n 200) (hide-window w)] ;; timeout after 200 sec [(= (choice-index the-choices) 0) (and (sleep-for-a-while 4) (process-result (+ n 1)))] [else true]) ) ) (cond [(process-result 0) (list-ref list-of-choices (- (choice-index the-choices) 1))]))) ;; get-answer: string number -> string ;; The function displays a simple text input with a question ;; and returns the answer typed in by the user ;; The second parameter (time-delay) is the number of seconds ;; the function waits after the typing of the first character ;; before it returns the answer. If the user takes longer ;; than that, a partial answer may be returned ;; 5 sec delay is reasonable for a one-word answer (define (get-answer question time-delay) (local ((define the-answer (make-text question)) (define w (create-window (list (list the-answer) (list (make-button "O.K." (lambda (e) (hide-window w))))))) (define (process-result n) (cond [(> n 200) (hide-window w)] ;; timeout after 200 sec [(string=? (text-contents the-answer) "") (and (sleep-for-a-while 1) (process-result (+ n 1)))] [else true] ;; the user starts typing ))) (cond [(process-result 0) (cond [(sleep-for-a-while time-delay) (text-contents the-answer)]) ]) )) ;; This is an example of one round of the accummulation stage of a game. ;; Your game will have multiple rounds. (start 500 300) ;; Variables to name Strings of Welcome Messages (define wm "Hello, Welcome to the land of your Dreams") (define wm2 "or your... Nightmares...") (define wm3 "The point of this game is to get out of this building alive...") (define wm4 "but... it's too late to turn around, The door just shut and locked.") (define wm5 "But know this, There is a key, Somewhere...") (define wm6 "You better Find It, and I'd do it quick.") ;; Functions to Write Strings of Introduction (and (draw-solid-string (make-posn 20 20) wm) (draw-solid-rect (make-posn 440 120) 60 180 'black) (draw-solid-rect (make-posn 441 121) 28 178 'brown) (draw-solid-rect (make-posn 471 121) 28 178 'brown) (sleep-for-a-while 1) (draw-solid-string (make-posn 30 40) wm2) (sleep-for-a-while 1) (draw-solid-string (make-posn 40 60) wm3) (sleep-for-a-while 3) (clear-solid-string (make-posn 20 20) wm) (clear-solid-string (make-posn 30 40) wm2) (clear-solid-string (make-posn 40 60) wm3) (sleep-for-a-while 1) (draw-solid-string (make-posn 20 60) wm4) (sleep-for-a-while 1) (draw-solid-string (make-posn 30 80) wm5) (sleep-for-a-while 1) (draw-solid-string (make-posn 40 100) wm6) (sleep-for-a-while 6) (clear-solid-string (make-posn 20 60) wm4) (clear-solid-string (make-posn 30 80) wm5) (clear-solid-string (make-posn 40 100) wm6) (clear-solid-rect (make-posn 440 120) 60 180) (clear-solid-rect (make-posn 441 121) 28 178) (clear-solid-rect (make-posn 471 121) 28 178) (sleep-for-a-while 1) ) ;; calling a get-answer function to let the user type in their name ;; 5 is the number of seconds you expect the user to spend on typing ;; the answer (from the fisrt character to the last one). ;; Too short times may result in the program getting a partial answer ;; Feel free to adjust this time as needed (define character-name (get-answer "What's your name?" 5)) ;; the first list of choices: (define floor-choice (list "Floor 1" "Floor 2" "Basement Level" "The Sewer")) ;; calling "get-choice" to get the user's choice, adding the choice to ;; the list of weapons. weapon-list is the new list (define floor-option (cons (get-choice "Choose your Starting Floor" floor-choice) empty)) (sleep-for-a-while 2) ;; the first list of choices: (define weapon-choices (list "Broom Handle" "Wal*Mart Light Saber")) ;; calling "get-choice" to get the user's choice, adding the choice to ;; the list of weapons. weapon-list is the new list (define weapon-list (list empty)) (cond [(string=? (first floor-option) "Floor 2") (set! weapon-list (cons (get-choice "Choose your weapon" weapon-choices) empty))] [else true]) (sleep-for-a-while 2) ;; using string-append to combine strings. In this case the strings are: ;; the character name, " now has a ", and the newly-added item in the ;; weapons list (define message (list empty)) ;; draws a string on the canvas at the given position ;; no control over the font or the color, sorry... ;; you might want to add a drawing, too (cond [(string=? (first floor-option) "Floor 2") (set! message (string-append character-name " is now on the " (first floor-option) " with a " (first weapon-list) "."))] [else (set! message (string-append character-name " is now on the " (first floor-option) "."))] ) (draw-solid-string (make-posn 20 20) message) (sleep-for-a-while 6) ;; clear the string (clear-solid-string (make-posn 20 20) message) ;; Result of choosing Basement Level (and (string=? (first floor-option) "Basement Level") (draw-solid-rect (make-posn 0 0) 500 300 'black) (draw-solid-rect (make-posn 18 15) 422 42 'gray) (draw-solid-string (make-posn 20 30) "You made it to the basement, sadly the lights were off and you ") (draw-solid-string (make-posn 20 50) "tripped on... Something... Sorry to tell you, but you died. Goodbye") (sleep-for-a-while 5) (stop) (exit)) ;;;;;; Result of choosing Floor 1 (define floor-to-sewer (list empty)) (cond [(string=? (first floor-option) "Floor 1") (and (draw-solid-string (make-posn 20 20) "You Stumble into a room and find a tunnel in the middle of the room" ) (draw-solid-disk (make-posn 250 150) 75 'black))] [else true] ) (cond [(string=? (first floor-option) "Floor 1") (set! floor-to-sewer (cons (get-choice "Would you like to go through the Tunnel?" (list "yes" "no")) empty))] [else (set! floor-to-sewer empty)] ) (cond [(empty? floor-to-sewer) true] [(string=? (first floor-to-sewer) "yes") (set! floor-option (list "The Sewer"))] [else true] ) (cond [(empty? floor-to-sewer) false] [(string=? (first floor-to-sewer) "no") (and (draw-solid-rect (make-posn 0 0) 500 300 'red) (draw-solid-string (make-posn 20 20) "Because you chose not to go through the tunnel, you died. Goodbye") (sleep-for-a-while 3) (exit) )] [else true] ) (cond [(string=? (first floor-option) "Floor 1") (and (sleep-for-a-while 4) (draw-solid-string (make-posn 20 20) message) (sleep-for-a-while 5) (clear-solid-string (make-posn 20 20) message))] [else true] ) ;; Result of choosing Floor 2 (define room-search (list "yes" "no")) (define search-option1 (list empty)) (define fight-option (list empty)) (define game-win (list empty)) (cond [(string=? (first floor-option) "Floor 2") (and (draw-solid-rect (make-posn 0 0) 500 300 'blue) (draw-solid-rect (make-posn 150 10) 200 280 'white) (draw-solid-line (make-posn 250 0) (make-posn 250 300) 'black))] [else true] ) (cond [(string=? (first floor-option) "Floor 2" )(set! floor-option (cons (get-choice "Which way would you like to look?" (list "left" "right")) empty))] [else true] ) (sleep-for-a-while 2) (cond [(string=? (first floor-option) "left" ) (and (draw-solid-rect (make-posn 0 0) 500 300 'blue) (draw-solid-rect (make-posn 200 60) 100 250 'brown))] [else true] ) (cond [(string=? (first floor-option) "right") (and (clear-solid-rect (make-posn 0 0) 500 300) (draw-solid-string (make-posn 20 20) "You Look Up and See a Key on a small Side Table.."))] [else true] ) (cond [(string=? (first floor-option) "right") (and (draw-solid-rect (make-posn 240 135) 20 60 'gray) (draw-solid-disk (make-posn 250 140) 30 'blue) (draw-solid-rect (make-posn 245 135) 5 7 'gold) (draw-solid-disk (make-posn 240 135) 5 'gold) (sleep-for-a-while 3)) ] [else true] ) (cond [(string=? (first floor-option) "right") (set! floor-option (cons (get-choice "Would you like to grab the key and try it on the front door?" (list "yes!" "no!")) empty))] [(string=? (first floor-option) "left") (set! search-option1 (cons (get-choice "Would You Like To Search The Room?" room-search) empty))] [else true] ) (cond [(string=? (first floor-option) "yes!") (and (clear-solid-rect (make-posn 0 0) 500 300)(sleep-for-a-while 1)(draw-solid-rect (make-posn 440 120) 60 180 'black) (draw-solid-rect (make-posn 441 121) 28 178 'brown)(draw-solid-rect (make-posn 471 121) 28 178 'brown)(draw-solid-string (make-posn 20 20) "The Small Key seems to fit in the hole... and...")(sleep-for-a-while 4) (draw-solid-rect (make-posn 0 0) 500 300 'gold)(sleep-for-a-while 1) (draw-solid-rect (make-posn 0 0) 500 200 'blue)(draw-solid-string (make-posn 20 40) "Congradulations, You Survived!")(draw-solid-rect (make-posn 0 200) 500 100 'green) (draw-solid-line (make-posn 250 170) (make-posn 240 190) 'black)(draw-solid-line (make-posn 250 170) (make-posn 260 190) 'black)(draw-solid-line (make-posn 250 140) (make-posn 250 170) 'black) (draw-solid-line (make-posn 250 140) (make-posn 260 160) 'black) (draw-solid-line (make-posn 250 140) (make-posn 240 160) 'black)(draw-solid-disk (make-posn 250 130) 10 'black) (draw-solid-disk (make-posn 250 130) 9 'blue)(exit))] [else true]) (cond [(string=? (first floor-option) "no!" )(and (draw-solid-rect (make-posn 0 0) 500 300 'red) (draw-solid-string (make-posn 20 20) "As you were sitting in the room, Something snuck up on you") (draw-solid-string (make-posn 20 40) "as a result, you died. I'm sorry, Goodbye") (sleep-for-a-while 5) (exit))] [else true]) (cond [(empty? (first search-option1)) false] [(string=? (first search-option1) "yes") (and (draw-solid-rect (make-posn 0 0) 500 300 'black) (sleep-for-a-while 2) (clear-solid-rect (make-posn 0 0) 500 300) (draw-solid-string (make-posn 10 20) "Whoa, The Light Turned on... But.. This Room is Empty!!!") (draw-solid-string (make-posn 10 40) "Wait, WHAT WAS THAT NOISE?") )] [else true]) (cond [(string=? (first search-option1) "yes") (set! game-win (cons (get-choice "Would you like to equip your weapon?" (list "yes!!" "no!!")) empty))] [(string=? (first search-option1) "no") (and (draw-solid-rect (make-posn 0 0) 500 300 'red) (draw-solid-string (make-posn 20 20) "As you were sitting in the room, Something snuck up on you") (draw-solid-string (make-posn 20 40) "as a result, you died. I'm sorry, Goodbye") (sleep-for-a-while 5) (exit))] [else true]) (cond [(string=? (first game-win) "yes!!") (and (clear-solid-rect (make-posn 0 0) 500 300)(draw-solid-string (make-posn 20 20) "You See a monster, luckily you equipped your weapon!"))] [(string=? (first game-win) "no!!") (and (draw-solid-rect (make-posn 0 0) 500 300 'red) (draw-solid-string (make-posn 20 20) "Because you didn't equip your weapon, Something snuck up on you") (draw-solid-string (make-posn 20 40) "as a result, you died. I'm sorry, Goodbye") (sleep-for-a-while 5) (exit))] [else true]) (cond [(empty? weapon-list) true] [(string=? (first weapon-list) "Broom Handle") (and (clear-solid-rect (make-posn 0 0) 500 300) (draw-solid-string (make-posn 20 20) "The Beast attempts to attack,") (draw-solid-string (make-posn 25 40) "you have a Broom Handle though and knock the beast unconcious.") (draw-solid-string (make-posn 30 60) "You Run Quickly down into the sewer...")(sleep-for-a-while 3))] [(string=? (first weapon-list) "Wal*Mart Light Saber") (and (sleep-for-a-while 2)(clear-solid-rect (make-posn 0 0) 500 300) (draw-solid-string (make-posn 20 20) "The Beast attempts to attack,") (draw-solid-string (make-posn 25 40) "you have a Wal*Mart Light Saber though and") (draw-solid-string (make-posn 30 60) "it's difficult to even defend yourself...")(sleep-for-a-while 2)(draw-solid-rect (make-posn 0 0) 500 300 'red) (draw-solid-string (make-posn 20 20) "You swung at the beast with the Light Saber, but it snapped in half") (draw-solid-string (make-posn 20 40) "as a result, you died. I'm sorry, Goodbye") (sleep-for-a-while 5) (exit))] [else true]) (cond [(string=? (first weapon-list) "Broom Handle") (set! floor-option (list "The Sewer"))] [else true] ) ;;Result of choosing The Sewer (cond [(string=? (first floor-option) "The Sewer") (and (draw-solid-rect (make-posn 0 0) 500 300 'gray) (draw-solid-disk (make-posn 250 150) 50 'white) (sleep-for-a-while 3) (draw-solid-line (make-posn 250 170) (make-posn 240 190) 'black) (draw-solid-line (make-posn 250 170) (make-posn 260 190) 'black)(draw-solid-line (make-posn 250 140) (make-posn 250 170) 'black) (draw-solid-line (make-posn 250 140) (make-posn 260 160) 'black) (draw-solid-line (make-posn 250 140) (make-posn 240 160) 'black)(draw-solid-disk (make-posn 250 130) 10 'black) (draw-solid-disk (make-posn 250 130) 9 'white) ;;You Win (draw-solid-string (make-posn 20 20) "You see light up ahead and decide to walk towards it!") (draw-solid-string (make-posn 20 40) "Congradulations, You Survived!") (sleep-for-a-while 4) (draw-solid-rect (make-posn 0 0) 500 200 'blue)(draw-solid-rect (make-posn 0 200) 500 100 'green) (draw-solid-disk (make-posn 500 0) 100 'yellow) (draw-solid-line (make-posn 250 170) (make-posn 240 190) 'black)(draw-solid-line (make-posn 250 170) (make-posn 260 190) 'black)(draw-solid-line (make-posn 250 140) (make-posn 250 170) 'black) (draw-solid-line (make-posn 250 140) (make-posn 260 160) 'black) (draw-solid-line (make-posn 250 140) (make-posn 240 160) 'black)(draw-solid-disk (make-posn 250 130) 10 'black) (draw-solid-disk (make-posn 250 130) 9 'blue))] [else true] ) (cond [(string=? (first floor-option) "The Sewer") (and (sleep-for-a-while 5) (exit))] [else true])