;; 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! 12-7 final|) (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"))))) ;;Abby Johnson & Nicole Nyberg ;;Problem Set 10 GAME ;; 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 (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 "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 time-delay) (text-contents the-answer)]) ]) )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 1 round 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; first list of choices: (define balloon-choices (list "red balloon" "blue balloon" "green balloon")) ;;calling "get-choice" to get the user's choice, adding to the list of balloons. balloon-list is the new list (define balloon-list (cons (get-choice "Choose Your Balloon" balloon-choices) empty)) (sleep-for-a-while 2) ;; 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). (define character-name (get-answer "What's your name?" 5)) ;; 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 (string-append character-name " now has a " (first balloon-list))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (start 800 800) ;;drawing the background (draw-solid-rect (make-posn 0 0) 800 750 'cyan) ;sky (draw-solid-rect (make-posn 0 750) 800 750 'green) ;grass ;;person: dumbie variable -> boolean ;;takes a dumbie variable and draws the character on the ground (define (person x) (and (draw-solid-disk (make-posn 550 650) 20 'tan) ;head (draw-solid-line (make-posn 550 670) (make-posn 550 750) 'tan) ;body (draw-solid-line (make-posn 550 710) (make-posn 520 670) 'tan) ;left arm (draw-solid-line (make-posn 550 710) (make-posn 580 670) 'tan) ;right arm (draw-solid-line (make-posn 550 750) (make-posn 520 800) 'tan) ;left leg (draw-solid-line (make-posn 550 750) (make-posn 580 800) 'tan) ;right leg )) (sleep-for-a-while 1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;end of background;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; balloon: integer integer integer symbol -> boolean ;;takes x and y positions of the balloon center, the radius, and color and draws the corresponding balloon with a string (define (balloon x y r color) (and (draw-solid-disk (make-posn x y) r color) ;balloon (draw-solid-line (make-posn (+ x 45) (+ y 170)) (make-posn x (+ y 40)) 'black))) ;;string ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice (define (draw-balloon x) (cond [(string=? "red balloon" (first balloon-list)) (balloon 475 500 40 'red)] [(string=? "blue balloon" (first balloon-list)) (balloon 475 500 40 'blue)] [(string=? "green balloon" (first balloon-list)) (balloon 475 500 40 'green)])) ;;draws the character on the ground (person 1) ;;draws balloon choice (draw-balloon 1) (sleep-for-a-while 1) ;; draws a string (draw-solid-string (make-posn 100 100) message) ;; clear the string (and (sleep-for-a-while 2)(draw-solid-rect (make-posn 100 50) 400 100 'cyan)) (sleep-for-a-while 1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;end of phase 1 round 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 2 round 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;now we use the balloon choice to fly to level 1 ;;background for level 1 (draw-solid-rect (make-posn 0 500) 150 50 'orange) ;;Level 1 (draw-solid-string (make-posn 50 530) "Level 1") ;;person1: dumbie variable -> boolean ;;takes a dumbie variable and draws the character on level 1 (define (person1 x) (and (draw-solid-disk (make-posn 105 350) 20 'tan) ;head (draw-solid-line (make-posn 105 370) (make-posn 105 450) 'tan) ;body (draw-solid-line (make-posn 105 410) (make-posn 75 370) 'tan) ;left arm (draw-solid-line (make-posn 105 410) (make-posn 135 370) 'tan) ;right arm (draw-solid-line (make-posn 105 450) (make-posn 75 500) 'tan) ;left leg (draw-solid-line (make-posn 105 450) (make-posn 135 500) 'tan) ;right leg )) ;;defining our string (define fly-message "Fly to Level 1!") ;;draws the string (and (draw-solid-string (make-posn 100 100) fly-message) (sleep-for-a-while 2) (draw-solid-rect (make-posn 100 50) 400 100 'cyan)) ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice of color (define (draw-balloon1 x) (cond [(string=? "red balloon" (first balloon-list)) (balloon 30 200 40 'red)] [(string=? "blue balloon" (first balloon-list)) (balloon 30 200 40 'blue)] [(string=? "green balloon" (first balloon-list)) (balloon 30 200 40 'green)])) ;;clears the drawing of the character and their balloon on the ground (and (draw-solid-rect (make-posn 500 750) 800 750 'green) ;grass (draw-solid-rect (make-posn 300 400) 300 350 'cyan) ;sky (sleep-for-a-while 1)) ;;draws the character (person1) and their balloon choice on level 1 (and (person1 2)(draw-balloon1 2)) (sleep-for-a-while 1) ;;defines the three possible colors for the balloon (define balloon-colors (list 'red 'blue 'green)) ;;deflate-balloon: integer integer integer symbol-> boolean ;;deflate-balloon: takes an x-posn and a y-posn of the center of the circle with a radius r and recursively makes the circle smaller and smaller until it is gone (define (deflate-balloon x y r color) (cond [(<= r 1) true] [else (and (draw-solid-disk (make-posn x y) r color) ;balloon (sleep-for-a-while 1) (draw-solid-line (make-posn (+ x 45) (+ y 170)) (make-posn x (- y 10)) 'cyan) ;string (draw-solid-disk (make-posn x y) r 'cyan);clear balloon (draw-solid-line (make-posn (+ x 45) (+ y 170)) (make-posn x (- y 10)) 'black) ;clear (draw-solid-line (make-posn 75 370) (make-posn 30 240) 'cyan) ;;string covering original string (draw-solid-line (make-posn 665 220) (make-posn 620 90) 'cyan) (deflate-balloon x y (- r 10) color) (draw-solid-line (make-posn (+ x 45)(+ y 170)) (make-posn x (- y 10)) 'cyan))])) ;;chosen-balloon: dumbie variable -> symbol ;;takes the player's chosen balloon color and corresponds it with it's color. This is a helper function for deflate-balloon. When we call color in deflate-balloon, choosen-color returns a color (symbol) (define (chosen-balloon x) (cond [(string=? "red balloon" (first balloon-list)) 'red] [(string=? "blue balloon" (first balloon-list)) 'blue] [(string=? "green balloon" (first balloon-list)) 'green])) ;;deflates the balloon on level 1 and draws string (and (deflate-balloon 30 200 40 (chosen-balloon 1)) (draw-solid-string (make-posn 100 100) "Uh Oh! Choose Another Balloon!")) (sleep-for-a-while 2) ;;clear the string (draw-solid-rect (make-posn 75 75) 350 100 'cyan) (sleep-for-a-while 2) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END ROUND 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 1 round 2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;character (person1) is now standing on level 1 without a balloon ;;calling "get-choice" again to get the user's choice, adding to the list of balloons. balloon-list2 is the new list (define balloon-list2 (cons (get-choice "Choose Your Balloon" balloon-choices) empty)) ;;message2: string string -> string ;;takes two strings and combines them using string-append to create one new string (define message2 (string-append character-name " now has a " (first balloon-list2))) ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice of color (define (draw-balloon2 x) (cond [(string=? "red balloon" (first balloon-list2)) (balloon 30 200 40 'red)] [(string=? "blue balloon" (first balloon-list2)) (balloon 30 200 40 'blue)] [(string=? "green balloon" (first balloon-list2)) (balloon 30 200 40 'green)])) (sleep-for-a-while 1) ;;draws person1 with their balloon choice on level 1 (and (person1 1) (draw-balloon2 1)) (sleep-for-a-while 2) ;; draws a string on the canvas at the given position (draw-solid-string (make-posn 100 100) message2) ;;clear the string (and (sleep-for-a-while 2) (draw-solid-rect (make-posn 100 50) 400 100 'cyan)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 2 round 2;;;;;;;;;;;;;;;;;;;;;;;; ;;background for level 2 (draw-solid-rect (make-posn 650 350) 150 50 'purple) ;;Level 2 (draw-solid-string (make-posn 700 380) "Level 2") ;;person2: dumbie variable -> boolean ;;takes a dumbie variable and draws the character on level 2 (define (person2 x) (and (draw-solid-disk (make-posn 695 200) 20 'tan) ;head (draw-solid-line (make-posn 695 220) (make-posn 695 300) 'tan) ;body (draw-solid-line (make-posn 695 260) (make-posn 665 220) 'tan) ;left arm (draw-solid-line (make-posn 695 260) (make-posn 725 220) 'tan) ;right arm (draw-solid-line (make-posn 695 300) (make-posn 665 350) 'tan) ;left leg (draw-solid-line (make-posn 695 300) (make-posn 725 350) 'tan) ;right leg )) (set! fly-message "Fly to Level 2!") ;;draws string and clears string (and (draw-solid-string (make-posn 100 100) fly-message) (sleep-for-a-while 2) (draw-solid-rect (make-posn 100 50) 300 100 'cyan)) ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice of color (define (draw-balloon3 x) (cond [(string=? "red balloon" (first balloon-list2)) (balloon 620 50 40 'red)] [(string=? "blue balloon" (first balloon-list2)) (balloon 620 50 40 'blue)] [(string=? "green balloon" (first balloon-list2)) (balloon 620 50 40 'green)])) ;;clears drawing of character on level 1 (and (draw-solid-rect (make-posn 0 100) 200 400 'cyan) ;sky covering (sleep-for-a-while 1)) ;;draws the character on level 2 with their balloon (and (person2 2)(draw-balloon3 2)) (sleep-for-a-while 2) ;;chosen-ballon: dumbie variable -> symbol ;; takes the player's chosen balloon color and corresponds it with it's color. This is a helper function for deflate-balloon. When we call color in deflate-balloon, choosen-color returns a color (symbol) (define (chosen-balloon2 x) (cond [(string=? "red balloon" (first balloon-list2)) 'red] [(string=? "blue balloon" (first balloon-list2)) 'blue] [(string=? "green balloon" (first balloon-list2)) 'green])) ;;deflate balloon at level 2 and draw string (and (deflate-balloon 620 50 40 (chosen-balloon2 2)) (draw-solid-string (make-posn 100 100) "Uh Oh! Choose Another Balloon")) (sleep-for-a-while 1) ;;clear the string (draw-solid-rect (make-posn 75 75) 350 100 'cyan) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END ROUND 2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 1 round 3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;character (person2) is now standing on level 2 without a balloon ;;calling "get-choice" again to get the user's choice, adding to the list of balloons. balloon-list3 is the new list (define balloon-list3 (cons (get-choice "Choose Your Balloon" balloon-choices) empty)) ;;message3: string string -> string ;;takes two strings and combines them using string-append to make one new string (define message3 (string-append character-name " now has a " (first balloon-list3))) ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice of color (define (draw-balloon4 x) (cond [(string=? "red balloon" (first balloon-list3)) (balloon 620 50 40 'red)] [(string=? "blue balloon" (first balloon-list3)) (balloon 620 50 40 'blue)] [(string=? "green balloon" (first balloon-list3)) (balloon 620 50 40 'green)])) (sleep-for-a-while 1) ;;draws the character and their balloon choice on level 2 (and (person2 1) (draw-balloon4 1)) (sleep-for-a-while 2) ;; draws a string on the canvas at the given position (draw-solid-string (make-posn 100 100) message3) ;;clears the string (and (sleep-for-a-while 2)(draw-solid-rect (make-posn 100 50) 400 100 'cyan)) ;;background for level 3 (draw-solid-rect (make-posn 0 250) 150 50 'pink) ;;Level 3 (draw-solid-string (make-posn 50 280) "Level 3") ;;person3: dumbie variable -> boolean ;;takes a dumbie variable and draws the character on level 3 (define (person3 x) (and (draw-solid-disk (make-posn 105 100) 20 'tan) ;head (draw-solid-line (make-posn 105 120) (make-posn 105 200) 'tan) ;body (draw-solid-line (make-posn 105 160) (make-posn 75 120) 'tan) ;left arm (draw-solid-line (make-posn 105 160) (make-posn 135 120) 'tan) ;right arm (draw-solid-line (make-posn 105 200) (make-posn 75 250) 'tan) ;left leg (draw-solid-line (make-posn 105 200) (make-posn 135 250) 'tan) ;right leg )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;begin phase 2 round 3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;set the fly-message (set! fly-message "Fly to Level 3!") ;;draws the string (draw-solid-string (make-posn 100 100) fly-message) (sleep-for-a-while 2) ;;clears the character (person2) and their balloon on level 2 (draw-solid-rect (make-posn 550 5) 345 345 'cyan) ;;final-balloon: integer integer integer color -> boolean ;;takes the center positions of a circle, its radius, and its color and draws it (define (final-balloon x y r color) (and (draw-solid-disk (make-posn x y) r color) ;balloon (draw-solid-line (make-posn 75 120) (make-posn 40 80) 'black))) ;;string ;;draw-balloon: dumbie variable -> boolean ;;takes a dumbie variable and draws the player's balloon choice of color (define (draw-balloon5 x) (cond [(string=? "red balloon" (first balloon-list3)) (final-balloon 40 40 40 'red)] [(string=? "blue balloon" (first balloon-list3)) (final-balloon 40 40 40 'blue)] [(string=? "green balloon" (first balloon-list3)) (final-balloon 40 40 40 'green)])) ;;clears the string "Fly to level 3" (and (draw-solid-rect (make-posn 50 50) 200 200 'cyan) (sleep-for-a-while 1)) ;;draws the character with their balloon choice on level 3 (and (person3 2) (draw-balloon5 2)) (sleep-for-a-while 2) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;flower: dumbie variable -> boolean ;;takes a dumbie variable and draws the magic flower (define (flower x) (and (draw-solid-rect (make-posn 200 95) 8 100 'green) ;;stem (draw-solid-line (make-posn 200 150) (make-posn 200 100) 'green) ;leaf1 (draw-solid-line (make-posn 200 150) (make-posn 250 100) 'green) ;leaf2 (draw-solid-disk (make-posn 210 50) 20 'magenta) ;;pedals of flower (draw-solid-disk (make-posn 175 60) 20 'magenta) (draw-solid-disk (make-posn 190 90) 20 'magenta) (draw-solid-disk (make-posn 220 80) 20 'magenta) (draw-solid-disk (make-posn 200 70) 10 'white) ;;center of flower )) ;;draws the magic flower (flower 2) (sleep-for-a-while 2) ;;draw the string (draw-solid-string (make-posn 250 150) "You win the Magic Flower!") (sleep-for-a-while 2) ;;draw the background changing from yellow to red and the string "Way to Go! You won the magic flower!" (and (draw-solid-rect (make-posn 0 0) 800 800 'yellow) (sleep-for-a-while 1) (draw-solid-rect (make-posn 0 0) 800 800 'red) (sleep-for-a-while 1) (draw-solid-rect (make-posn 0 0) 800 800 'yellow) (sleep-for-a-while 1) (draw-solid-rect (make-posn 0 0) 800 800 'red) (sleep-for-a-while 1) (draw-solid-rect (make-posn 0 0) 800 800 'yellow) (draw-solid-string (make-posn 250 350) "WAY TO GO! YOU WON THE MAGIC FLOWER!"))