On this page:
1 Retrieve the first version
2 Controlling invader speed
3 Extending World
4 Drawing the defender and the bullet
5 Moving the bullet
6 An updated tick handler
7 Testing the second version

Lab 8: Invaders

You may submit this lab to lab8 on Handin (this is optional).

Note: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.

1 Retrieve the first version

This week we will continue to work on our Invaders game. Lecture 9 finished with an animation in which the invaders invaded. Download the Invaders code from Lecture 9 (with "Save link as...") and modify it for Lab 8.

2 Controlling invader speed

Exercise 1. Define a number named invader-speed. We suggest starting with a fraction less than 1. Modify the tick event handler move-invaders (and any helper function which needs it) so that on each tick, the invaders advance by invader-speed pixels. (Currently, they advance always by 1 pixel.)

Uncomment the big-bang portion of the code and fiddle with the definition of invader-speed until you are happy with the invasion speed. Then recomment it.

3 Extending World

Exercise 2. Now let’s add a defender and arm the defender. Take a moment to think about what the underlying data should be for the defender and the bullet the defender would shoot. Here is a data definition for a World which includes invaders, a defender and maybe a bullet:

; a MaybePosn is one of:
; - (make-none)
; - Posn
(define-struct none [])
 
; a World is a (make-world ListOfPosns Number MaybePosn)
; *Interpretation*:
;   - the positions of the invaders
;   - the x-coordinate of the defender
;   - the position of the bullet (if there is one)
(define-struct world [invaders defender bullet])

Why is the defender just a number and not a Posn? Why is the bullet a MaybePosn and not a Posn?

Exercise 3. Finish writing the templates for MaybePosn and for World.

; process-mp : MaybePosn -> ...
(define (process-mp mp) ...)
 
; process-world : World -> ...
(define (process-world w) ...)
4 Drawing the defender and the bullet

Exercise 4. How should the defender be drawn? How should the bullet be drawn? Where should the defender be located vertically? Define an image defender-marker, an image bullet-marker and a number defender-y.

Exercise 5. Design a function draw-defender which takes a Number and an Image and returns an Image. The function should render the defender at a position determined by the given number on the given image.

; draw-defender : Number Image -> Image
; places defender-marker on img at position (x , defender-y)
(define (draw-defender x img) ...)
 
(check-expect (draw-defender 100 (empty-scene 400 400)) ...)

Exercise 6. Design a function draw-bullet which takes a MaybePosn and an Image and returns an Image. The function should render the bullet at the given Posn, if there is one, on the given image.

; draw-bullet : MaybePosn Image -> Image
; places bullet-marker on img at position given by mp, if mp is a Posn
(define (draw-bullet mp img) ...)
 
(check-expect (draw-bullet (make-none) (empty-scene 400 400)) ...)
(check-expect (draw-bullet (make-posn 100 200) (empty-scene 400 400)) ...)

Exercise 7. Design a function draw-world which takes a World and returns an Image. Use draw-invaders, draw-defender and draw-bullet as helper functions in your definition. We suggest that you also use these helper functions in your check-expects (on the right-hand side).

5 Moving the bullet

Exercise 8. Define a number named bullet-speed. We suggest starting with 1.

Exercise 9. Design a function move-bullet which takes a MaybePosn and returns a MaybePosn. If the input is a Posn, the output Posn should have its y-coordinate decreased by bullet-speed.

; move-bullet : MaybePosn -> MaybePosn
; decreases the y-coordinate of mp by bullet-speed if mp is a Posn
(define (move-bullet mp) ...)
 
(check-expect (move-bullet (make-none)) ...)
(check-expect (move-bullet (make-posn 100 200)) ...)
6 An updated tick handler

Exercise 10. Design a function move-world which takes a World and outputs a World. It will use move-invaders to move the invaders and move-bullet to move the bullet.

7 Testing the second version

Exercise 11. Use draw-world and move-world as arguments to big-bang. Fiddle with your definitions of markers and speeds until you are satisfied with the action you see.