;; 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 bang-bang) (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"))))) (start 350 300) ; This draws the ground! (define ground (draw-solid-rect (make-posn 0 290) 350 11 'green) ) ; This defines the horizontal position of the first cannon (define c1-x 6) ; This defines the horizontal position of the second cannon (define c2-x 200) ; This defines the horizontal position of the explosion (define e-x 147) ; recursive function that draws the first cannon by calling on cannon1 (define (draw-cannon1 n) (cond [(> n 7) true] [else (and (cannon1 c1-x n) (draw-cannon1 (+ n 1)))] ) ) ; recursive function that draws the second cannon by calling on cannon2 (define (draw-cannon2 n) (cond [(> n 7) true] [else (and (cannon2 c2-x n) (draw-cannon2 (+ n 1)))] ) ) ; draws the first cannon (define (cannon1 c1-x n) (and (draw-solid-line (make-posn (+ c1-x n) (+ n 280)) (make-posn (+ c1-x (+ n 20)) (+ n 266)) 'black) (draw-solid-disk (make-posn (+ c1-x 4) 285) 3 'black) ) ) ; Draws cannon2 (define (cannon2 c2-x n) (and (draw-solid-line (make-posn (+ c2-x n) (- 272 n)) (make-posn (+ c2-x (+ n 19)) (- 286 n)) 'black) (draw-solid-disk (make-posn (+ c2-x 19) 285) 3 'black) )) ;(sleep-for-a-while 5) ; explosion (define (explosion n) (and (draw-solid-disk (make-posn e-x 285) 10 'red) (sleep-for-a-while 1) ) ) ; clears the explosion (define (c-explosion n) (and (clear-solid-disk (make-posn e-x 285) 10 'red) (sleep-for-a-while 1)) ) ; draws the explosion using explosion (define (draw-explosion n) (and (explosion n) (sleep-for-a-while 1) (c-explosion n))) ; function that "undraws" cannon1 line by line (define (clear-cannon1 c1-x n) (and (clear-solid-line (make-posn (+ c1-x n) (+ n 280)) (make-posn (+ c1-x (+ n 20)) (+ n 266)) 'black) (clear-solid-disk (make-posn (+ c1-x 4) 285) 3 'black) ) ) ; function that "undraws" cannon2 line by line (define (clear-cannon2 c2-x n) (and (clear-solid-line (make-posn (+ c2-x n) (- 272 n)) (make-posn (+ c2-x (+ n 19)) (- 286 n)) 'black) (clear-solid-disk (make-posn (+ c2-x 19) 285) 3 'black) ) ) ; function that actually clears cannon1 (define (anti-cannon1 n) (cond [(> n 7) true] [else (and (clear-cannon1 c1-x n) (anti-cannon1 (+ n 1)))] ) ) ; function that actually clears cannon2 (define (anti-cannon2 n) (cond [(> n 7) true] [else (and (clear-cannon2 c2-x n) (anti-cannon2 (+ n 1)))] ) ) ; draws both cannons (define (draw-all n) (and (draw-cannon1 n) (draw-cannon2 n))) (draw-all 2) ;(sleep-for-a-while 2) ;############################### Mechanics ################ ;g: gravity, valued at 10 (define g 10) ;wind: wind value, changes how far the projectile shoots, randomly set (define wind (- (random 40) 20)) (define (draw-wind w) (and (draw-solid-string (make-posn 20 20) "wind speed: ") (draw-solid-string (make-posn 110 20) w) (sleep-for-a-while 2) (clear-solid-string (make-posn 110 20) w) )) (draw-wind (number->string wind)) ;radians: converts an number (angle) into radians (define (radians ang) (* ang (/ 3.14159265 180))) ;t (time): takes a number (power) and number (angle) and finds the ammount of time it took the projectile to fall ;number + number -> number (define (t pow ang) (* 2 (/ (* pow (sin ang)) g))) ;;fire: takes a number (power) and another number (angle) and calculates the horizontal distance where the ball hit ;number & number -> number (define (fire pow ang can) (+ (* (+ (- (* pow (cos (radians ang))) wind) (t pow (radians ang)))) can)) ;(check-expect ( <(fire 10 45) (fire 20 45) ) true) ;hit? determines whather the shot hit the target or not ;number & number -> boolean (define (cannon1-hit? shot) (and (>= shot (- c1-x 10)) (<= shot (+ c1-x 10)))) (define (cannon2-hit? shot) (and (>= shot (- c2-x 10)) (<= shot (+ c2-x 10)))) ;############################Graphical user interface############################## (define choice2 200) (define choice3 300) (define choice4 400) ;copyed and moddified from the origional game code, uses a graphical interface to display a message pertaining to which cannon is firing and to gain input for power/angle (define (get-answer a-message question1 question2 time-delay) (local ((define the-answer (make-text question1)) (define the-answer2 (make-text question2)) (define b-message (make-message a-message)) ;(define the-answer3 (make-text question3)) (define w (create-window (list (list b-message) (list the-answer the-answer2 ) (list (make-button "FIRE!" (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) (list (text-contents the-answer) (text-contents the-answer2))]) ]) )) ;bang-bang: this is the bulk of the recursive action: it sets all the new variables (choice2-4) and inputs them into various functions. it outputs the raw data for the shot (fire choice3 choice4), draws/removes the explosion, re-draws the cannons, checks to see if the enemy was destroyed, sets the new wind value and displays it, then recursively goes to bang-bang2. bang-bang2 is identical to bang-bang except that the values that pertained to cannon 1 are now pertaining to cannon 2. (define (bang-bang n) ( (set! choice2 (get-answer "CANNON 1 READY!" "enter power (- for left):" "enter angle:" 8)) (set! choice3 (string->number (first choice2))) (set! choice4 (string->number (first (rest choice2)))) (fire choice3 choice4 c1-x) (set! e-x (fire choice3 choice4 c1-x)) (draw-explosion 2) (draw-all 2) (sleep-for-a-while 2) (cond [(cannon2-hit? (fire choice3 choice4 c1-x)) (and (anti-cannon2 0) (draw-solid-string (make-posn 130 150) "Player 1 Wins!")(sleep-for-a-while 2) (exit))] [else true] ) (set! wind (- (random 40) 20)) (draw-wind (number->string wind)) (bang-bang2 2) )) ;see bang-bang above (define (bang-bang2 n) ( (set! choice2 (get-answer "CANNON 2 READY!" "enter power (- for left):" "enter angle:" 8)) (set! choice3 (string->number (first choice2))) (set! choice4 (string->number (first (rest choice2)))) (fire choice3 choice4 c2-x) (set! e-x (fire choice3 choice4 c2-x)) (draw-explosion 2) (draw-all 2) (cond [(cannon1-hit? (fire choice3 choice4 c2-x)) (and (anti-cannon1 0) (draw-solid-string (make-posn 130 150) "Player 2 Wins!") (sleep-for-a-while 2) (exit))] [else true] ) (set! wind (- (random 40) 20)) (draw-wind (number->string wind)) (bang-bang 2) )) ;############################# Master Function ################### ;executes the main function (bang-bang 2) (sleep-for-a-while 2) (stop)