8.14

Lecture 18: Space invaders🔗

This assignment is due on Tuesday, October 22 at 11:59pm. Submit it using Handin as assignment lecture18.

Exercise 1. Write the template for a function that processes a MaybePosn. Make it look like a function called process-maybeposn, and do not put it in a comment. Here is the data definition:
; A MaybePosn is one of:
; - (make-none)
; - Posn
(define-struct none [])

Then write the template for a function that processes a World. Make it look like a function called process-world, and do not put it in a comment. Here is the data definition:
; A World is (make-world ListOfPosns Number MaybePosn)
(define-struct world [invaders player bullet])
; *Interpretation*: the player is at the given X coordinate
;                   and the bottom of the screen
Hint: your template process-world should use process-listofposns and process-maybeposn in the places corresponding to where the data definition of World uses ListOfPosns and MaybePosn.

Exercise 2. Below are some constant definitions from the video above:
(define b1 (make-none))
(define b2 (make-posn 70 50))
(define bullet-sprite (circle 5 "solid" "green"))
Finish designing the following function. Feel free to write your own examples, but be sure to use the bullet-sprite we define above.
; draw-bullet : MaybePosn Image -> Image
; draw the bullet on the given image
(check-expect (draw-bullet b1
                           (place-image player-sprite
                                        80 295
                                        (draw-invaders ps2)))
              (place-image player-sprite
                           80 295
                           (draw-invaders ps2)))
(check-expect (draw-bullet b2
                           (place-image player-sprite
                                        100 295
                                        (draw-invaders ps1)))
              (place-image bullet-sprite
                           70 50
                           (place-image player-sprite
                                        100 295
                                        (draw-invaders ps1))))
(define (draw-bullet b img)
  (cond [(none? b) (... img ...)]
        [(posn? b) (... (posn-x b) (posn-y b) img ...)]))
Hint: Watch the next video.

The code written in the videos above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

Optional: Read Chapters 10 and 11 of the textbook. You can also find more ideas for games to build in Chapter 12, such as Snake and Tetris.

Optional: Enjoy the song Rubber Biscuit by the Blues Brothers.