;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname TimReed) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;;Tim Snyder & Reed Simpson ;;When the game is run, empty boxes occasionally show up, but I could not find a way to fix this bug at the moment. ;;Also, the GUI windows do not close. ;; 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 1) (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 (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 delay) (local ((define the-answer (make-text question)) (define w (create-window (list (list the-answer) (list (make-button "Close" (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 delay) (text-contents the-answer)]) ]) )) ;;Position for text (define posn (make-posn 20 20)) (define posn2 (make-posn 20 35)) ;;Contract: draw-treasure: null -> true (start 1 1) (define draw-treasure (and (draw-solid-rect (make-posn 400 400) 50 25 'Yellow) (draw-solid-disk (make-posn 410 410) 10 'Orange) (draw-solid-disk (make-posn 422 408) 10 'Orange))) (stop) ;;Contract: end: boolean -> boolean ;;purpose: to output if you won or lost (define (end win?) (and (stop) (start 1000 1000) (cond [win? (and (draw-solid-string posn "You killed the dragon and survived! Congratulations! You're now rich!") draw-treasure)] [else (draw-solid-string posn "You wake up in your bed. You remember you have to fight a dragon.")] ) (sleep-for-a-while 10) (stop)) ) ;;Contract: name : string -> string ;;To get a person's name (define name (get-answer "What is your name?" 5)) ;;Contract: bard: string string -> string ;;Make the bard's songs from name and objective. (define (bard name objective) (and (draw-solid-string bardposn (string-append "Oh brave, oh brave Sir " name " so bravely " objective ".")) (sleep-for-a-while 3) (clear-solid-string bardposn (string-append "Oh brave, oh brave Sir " name " so bravely " objective "."))) ) ;;Contract: check-bard: list -> string ;;purpose to check if the character took the bard (define (check-bard charlist name objective) (cond [(empty? charlist) (draw-solid-string bardposn "")] [(string=? (first charlist) "gnome bard") (bard name objective)] [else (check-bard (rest charlist) name objective)] ) ) ;;The position for bard text (define bardposn (make-posn 20 50)) (start 1000 1000) ;;Gets name name (draw-solid-string posn (string-append "Sir " name " wakes up remembering there is a dragon to be killed today.")) (sleep-for-a-while 4) (and (stop) (start 1000 1000)) ;;Gets what weapon they want (and (draw-solid-string posn "You see in your closet your sword (gleaming sharp and deadly), your daggers (small and agile little points),") (draw-solid-string posn2 "your wand (an extremely magical stick) and your pet bunny (he's so fluffy!).")) (sleep-for-a-while 4) (define weapon (get-choice "Choose your weapon." (list "sword" "daggers" "wand" "rabbit"))) (and (stop) (start 1000 1000)) ;;Gets what armor they want (and (draw-solid-string posn "You see on your hangers your plate armor (it's shiny and heavy), your pink armor (why did you even buy this?") (draw-solid-string posn2 "you'd be a laughing stock!), your asbestos armor (the cancer is a bonus) and your normal clothes (it's made for comfort).")) (sleep-for-a-while 4) (define armor (get-choice "Choose your armor." (list "plate" "pink armor" "asbestos" "cloth"))) (and (stop) (start 1000 1000)) ;;Gets the helping item they want (and (draw-solid-string posn "You open your nightstand. Inside is your torch (fire... good...), your chest o' gold (it's worth more than") (draw-solid-string posn2 "your life), your shield (protecting yourself is always a good idea) and a box lunch (yay food).")) (sleep-for-a-while 4) (define helping-item (get-choice "Choose your secondary item." (list "torch" "chest o' gold" "shield" "box lunch"))) (and (stop) (start 1000 1000)) ;;Gets what companion they want (and (draw-solid-string posn "You exit your house. You see outside a hobbit their looking revenge (AKA looking for loot), a gnome bard (they're funny") (draw-solid-string posn2 "little things, right?), an elven archer (elves are the best archers) and human cleric (something seems... off about him).")) (sleep-for-a-while 4) (define companion (get-choice "Choose your companion." (list "hobbit theif" "gnome bard" "elf archer" "human cleric"))) (and (stop) (start 1000 1000)) ;;Gets whether they eat a jellybean or not (draw-solid-string posn "On the ground, you see jellybeans strewn about. Obviously, the dragon has been crying. They're probably magical.") (sleep-for-a-while 4) (define jelly-beans (get-choice "Choose which you want to eat a jelly bean." (list "red jellybean" "green jellybean" "gold jellybean" "none"))) ;;defines character's parameters ;;Need two for runtime purposes (define character (list weapon armor companion helping-item jelly-beans)) (define character2 (list weapon armor companion helping-item jelly-beans)) (check-bard character name "eats a bean") (sleep-for-a-while 4) (stop) ;;This area is for definitions (start 1 1) ;;Contract: eat-jellybean? : list -> string ;;Determine what happened at jellybean event (define (eat-jellybean? charlist) (cond [(empty? charlist) (draw-solid-string posn "YOU BROKE THE GAME?! HOW?! WHY?!?!?")] [(string=? (first charlist) "red jellybean") (and (draw-solid-string posn "You feel a fire in your heart. It spreads to your lungs. It hurts... A lot. Then, it erupts from your chest.") (check-bard character name "explodes"))] [(string=? (first charlist) "gold jellybean") (draw-solid-string posn "You feel renewed confidence. You could probably kill the dragon with your bare hands.")] [(string=? (first charlist) "green jellybean") (draw-solid-string posn "It tastes like watermelon.")] [(string=? (first charlist) "none") (draw-solid-string posn "You don't eat a jellybean.")] [else (eat-jellybean? (rest charlist))] ) ) ;;Contract: draw-spiral-disk: posn number number symbol -> boolean ;;purpose to draw a spiral disk (define (draw-spiral-disk center angle scale color) (draw-solid-disk (make-posn (ceiling (+ (posn-x center) (* scale (sin angle)))) (floor (+ (posn-y center) (* scale (cos angle))))) (ceiling (/ scale 10)) color) ) ;;Contract: draw-spiral: posn number number symbol number number -> boolean ;;purpose to make a spiral of fire (define (draw-spiral center angle scale color n rate) (cond [(= n 0) (draw-spiral-disk center angle scale color)] [else (and (draw-spiral-disk center angle scale color) (draw-spiral center (+ angle (/ pi 32)) (* scale (- 1 (* .001 rate))) color (- n 1) rate) )] ) ) ;;Contract: armor2 : charlist -> true (define (armor2 charlist) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(empty? charlist) (end true)] [(string=? "asbestos" (first charlist)) (and (draw-solid-string posn "You killed the dragon and got its horde of treasure.") (draw-solid-string posn2 "Too bad you died of cancer. The government then took the treasure to fund pro-dragon projects.") (end false))] [else (armor2 (rest charlist))] )) ) ;;Code to check for victory (define (victory character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "You killed the dragon and lived through it exploding.") (sleep-for-a-while 3) (armor2 character))) ;;Contract: attack-choice: choice -> true ;;purpose to try to kill the dragon (define (attack-choice choice character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(string=? "rabbit" choice) (and (draw-solid-string posn "You release the rabbit. It launches itself at the dragon's neck and tears it open with it's teeth.") (draw-solid-string posn2 "The dragon proceeds to explode.") (victory character))] [(string=? "sword" choice) (and (draw-solid-string posn "You rush forward and thrust your sword through the dragon's jaw. He goes down. Then you hear a ticking. Who knew that dragons explode after they die?") (end false))] [(string=? "daggers" choice) (and (draw-solid-string posn "You run up to the dragon and stab it, but the daggers bounce off.") (end false))] [(string=? "wand" choice) (and (draw-solid-string posn "You raise your wand to attack, but then you remember that you don't know how to use any magic.") (end false))] [else (and (draw-solid-string posn "Why would you use THAT against a dragon?!") (end false))] )) ) ;;Code to check to continue after confronting the dragon (define (continue5 character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "Somehow, you weren't eaten by the beast.") (sleep-for-a-while 3) (local ((define choice (get-choice "What do you attack with" character))) (attack-choice choice character)))) ;;Contract: armor-check: charlist -> true ;;purpose to see if the dragon eats you (define (armor-check charlist character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(empty? charlist) (and (draw-solid-string posn "The dragon eats you as a part of this nutritious breakfast.") (end false))] [(or (string=? "asbestos" (first charlist)) (string=? "pink armor" (first charlist))) (and (draw-solid-string posn "The dragon is laughing too hard to eat you.") (continue5 character))] [else (armor-check (rest charlist) character)] )) ) ;;Code to continue after sneaking by dragon (define (continue4 character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "You luckily survived entering the dragon's keep.") (sleep-for-a-while 3) (armor-check character character))) ;;Contract: sidekick-check: charlist -> true ;;purpose to check for death after flames (define (sidekick-check charlist character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(string=? (first charlist) "hobbit theif") (and (draw-solid-string posn "The hobbit trips infront of the dragon and promptly gets eaten.") (continue4 character))] [(string=? (first charlist) "gnome bard") (and (draw-solid-string posn "The bard's obnoxious singing gets him eaten. As you run the draong eats you too.") (check-bard character name "digests") (sleep-for-a-while 5) (end false))] [(string=? (first charlist) "human cleric") (and (draw-solid-string posn "You get clawed by the dragon and, instead of healing you, the cleric faints at your blood.") (sleep-for-a-while 5) (end false))] [(string=? (first charlist) "elf archer") (and (draw-solid-string posn "The elf shoots the dragon in the snout. Who knew dragons' sneezes' had that much force?") (sleep-for-a-while 5) (end false))] [else (sidekick-check (rest charlist) character)] )) ) ;;Code to continue after fire jet (define (continue3 character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "You forge ahead undamaged") (check-bard character name "marches ahead") (sleep-for-a-while 3) (sidekick-check character character))) ;;Contract: flame-check: charlist -> true ;;purpose to check for death at flames (define (flame-check charlist character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-spiral (make-posn 500 500) 0 1000 'Red 200 10) (cond [(empty? charlist) (and (draw-solid-string posn "The fires engulf you in a burning embrace.") (check-bard character name "burns away") (end false))] [(or (string=? (first charlist) "asbestos") (string=? (first charlist) "green jellybean")) (and (draw-solid-string posn "You survive the onslaught of flames.") (continue3 character))] [else (flame-check (rest charlist) character)] )) ) ;;Code to continue after cave entrance (define (continue2 character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "You walk into the cave and see a light in front of you.") (draw-solid-disk (make-posn 500 500) 50 'Yellow) (sleep-for-a-while 3) (flame-check character character))) ;;Contract: cave-death: choice -> true ;;purpose to output death if you died at cave entrance (define (cave-choice choice character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(string=? "torch" choice) (and (draw-solid-string posn "Good thing you brought a torch") (check-bard character name "lights a torch") (continue2 character))] [else (and (draw-solid-string posn "You trip over something in the darkness and hit your head on a rock. Smooth. It occurs to you") (draw-solid-string posn2 "that your head is suddenly damp and you can't tell which ways is up.") (check-bard character name "gets a concussion") (end false))] )) ) ;;Code to continue after the jellybean incident (define (continue1 character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (draw-solid-string posn "You find yourself at a dark cave.") (check-bard character name "carries forth") (sleep-for-a-while 2) (local ((define choice (get-choice "What do you do?" character))) (cave-choice choice character)))) (stop) ;;Gets what happens from jellybeans (start 1000 1000) (eat-jellybean? character) (sleep-for-a-while 5) (and (stop) (start 1000 1000)) ;;Contract: jelly-death: charlist -> true ;;purpose to output death if you died of jellybeans (define (jelly-death charlist character) (and (sleep-for-a-while 5) (stop) (start 1000 1000) (cond [(empty? charlist) (continue1 character)] [(and (string? (first charlist)) (string=? (first charlist) "red jellybean") ) (end false)] [else (jelly-death (rest charlist) character)] )) ) (jelly-death character character2)