Created
October 9, 2019 21:09
-
-
Save samth/a2df16f49b60d32878fd6aa5f896add0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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-beginner-abbr-reader.ss" "lang")((modname h2111082019-final) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) | |
;; Questions | |
;; Lists | |
;; Space Invaders! | |
(require 2htdp/image) | |
(define background (empty-scene 200 200)) | |
;; A Posn is (make-posn Number Number) | |
;; A ListOfPosns is one of | |
;; - empty | |
;; - (cons Posn ListOfPosns) | |
(define ALIEN (circle 5 "solid" "blue")) | |
;; draw-aliens : ListOfPosns -> Image | |
;; draws alien markers at the given positions | |
(define (draw-aliens ps) | |
(cond [(empty? ps) background] | |
[(cons? ps) | |
(place-image ALIEN | |
(posn-x (first ps)) (posn-y (first ps)) | |
(draw-aliens (rest ps)))])) | |
(check-expect (draw-aliens empty) background) | |
(check-expect (draw-aliens (cons (make-posn 0 0) empty)) | |
(place-image ALIEN | |
0 0 | |
background)) | |
;; move-aliens : ListOfPosns -> ListOfPosns | |
;; increase the y coordinate of all the aliens by 1 | |
(define (move-aliens ls) | |
(cond [(empty? ls) empty] | |
[(cons? ls) (cons (move-one-alien (first ls)) | |
(move-aliens (rest ls)))])) | |
(check-expect (move-aliens empty) empty) | |
(check-expect (move-aliens (cons (make-posn 0 0) empty)) | |
(cons (make-posn 0 1) empty)) | |
;; move-one-alien : Posn -> Posn | |
;; adds 1 to the y coord | |
(define (move-one-alien p) (make-posn (posn-x p) (add1 (posn-y p)))) | |
(check-expect (move-one-alien (make-posn 0 0)) | |
(make-posn 0 1)) | |
(require 2htdp/universe) | |
(define-struct world [aliens bullet player]) | |
;; A World is (make-world ListOfPosns MaybePosn Number) | |
#; | |
(define (process-world w) | |
(... (process-listofposns (world-aliens w)) | |
(process-maybeposn (world-bullet w)) | |
(world-player w))) | |
;; draw-world : World -> Image | |
;; draw all the components of the world | |
(define (draw-world w) | |
(place-image | |
(square 10 "solid" "goldenrod") | |
(world-player w) 195 | |
(draw-bullet (world-bullet w) (draw-aliens (world-aliens w))))) | |
;; A MaybePosn is one of | |
;; - #false | |
;; - (make-posn Number Number) | |
#; | |
(define (process-maybeposn mp) | |
(cond [(false? mp) ...] | |
[(posn? mp) ... (posn-x mp) (posn-y mp)])) | |
;; draw-bullet MaybePosn Image -> Image | |
;; draws the bullet (if any) at the given position | |
(define (draw-bullet mp img) | |
(cond [(posn? mp) (place-image (circle 10 "solid" "red") | |
(posn-x mp) (posn-y mp) | |
img)] | |
[else img])) | |
(check-expect (draw-bullet #false background) background) | |
(check-expect (draw-bullet (make-posn 0 0) background) | |
(place-image (circle 10 "solid" "red") 0 0 background)) | |
;; move-world : World -> World | |
;; move the aliens and the bullet | |
(define (move-world w) | |
(make-world (move-aliens (world-aliens w)) | |
(move-bullet (world-bullet w)) | |
(world-player w))) | |
;; move-bullet : MaybePosn -> MaybePosn | |
(define (move-bullet p) | |
(cond [(false? p) p] | |
[(posn? p) (cond [(<= (posn-y p) 0) #false] | |
[else (make-posn (posn-x p) (- (posn-y p) 10))])])) | |
(check-expect (move-bullet #false) #false) | |
(check-expect (move-bullet (make-posn 10 20)) (make-posn 10 19)) | |
(check-expect (move-bullet (make-posn 10 0)) #false) | |
(draw-world (make-world (list (make-posn 0 57) (make-posn 20 77) (make-posn 10 67) (make-posn 50 107)) | |
#false | |
50)) | |
(define world1 (make-world (cons (make-posn 0 0) | |
(cons (make-posn 20 20) | |
(cons (make-posn 10 10) | |
(cons (make-posn 50 50) empty)))) | |
#false | |
100)) | |
;; player : World KeyEvent -> World | |
;; move player left, right; shoot bullet | |
(define (player w ke) | |
(cond [(string=? ke "left") | |
(make-world (world-aliens w) (world-bullet w) (max 0 (- (world-player w) 1)))] | |
[(string=? ke "right") | |
(make-world (world-aliens w) (world-bullet w) (min 200 (+ (world-player w) 1)))] | |
[(string=? ke " ") | |
(cond | |
[(posn? (world-bullet w)) w] | |
[else (make-world (world-aliens w) | |
(make-posn (world-player w) 200) | |
(world-player w))])] | |
[else w])) | |
;; update-world : World -> World | |
;; remove dead things from the world after moving | |
(define (update-world w) | |
(make-world (remove-aliens (world-aliens (move-world w)) | |
(world-bullet (move-world w))) | |
(remove-bullet (world-aliens (move-world w)) | |
(world-bullet (move-world w))) | |
(world-player w))) | |
;; remove-aliens : ListOfPosns MaybePosn -> ListOfPosns | |
(define (remove-aliens lop mp) | |
(cond [(empty? lop) empty] | |
[else (cond [(hit? mp (first lop)) | |
(remove-aliens (rest lop) mp)] | |
[else (cons (first lop) | |
(remove-aliens (rest lop) mp))])])) | |
;; remove-bullet : ListOfPosns MaybePosn -> MaybePosn | |
(define (remove-bullet l mp) | |
(cond [(empty? l) mp] | |
[else (if (hit? mp (first l)) | |
#false | |
(remove-bullet (rest l) mp))])) | |
;; hit? : MaybePosn Posn -> Boolean | |
(define (hit? mp p) | |
(cond [(false? mp) #false] | |
[else (and (<= (abs (- (posn-x mp) (posn-x p))) 5) | |
(<= (- (posn-y mp) (posn-y p)) 5))])) | |
;; aliens-landed? : ListOfPosns -> Boolean | |
(define (aliens-landed? l) | |
(cond [(empty? l) #false] | |
[else (or (>= (posn-y (first l)) 200) | |
(aliens-landed? (rest l)))])) | |
;; stop : World -> Boolean | |
;; stop if the aliens have landed | |
(define (stop w) | |
(aliens-landed? (world-aliens w))) | |
(big-bang world1 | |
[on-key player] | |
[to-draw draw-world] | |
[on-tick update-world 0.1] | |
[stop-when stop]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment