On this page:
1 Retrieve the third version
2 Removing the bullet
3 Generating the invaders
4 Game over

Lab 9: Invaders

You may submit this lab to lab9 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 third version

This week we finish our Invaders game. Download this Invaders code (with "Save link as...") and modify it for Lab 9.

2 Removing the bullet

Exercise 1. Modify move-bullet so that if the bullet’s y-coordinate is less than 0 (we visualize this condition as the bullet marker going over the top of the background), it is removed. Play the game with this modification and make sure that if you miss, you can fire again.

Exercise 2. Design a function remove-bullet which takes a ListOfPosns and a MaybePosn and returns a MaybePosn. Just as remove-invaders removed any invaders which were too close to the bullet, remove-bullet should remove the bullet if it is too close to some invader.

Exercise 3. Modify the tick handler move-world so that it uses remove-bullet as a helper function. Play the game with this modification and make sure that if you hit, you can fire again.

3 Generating the invaders

In this section, we’ll create a function which generates a list of invaders for us. We’ll randomize the x-coordinates of the invaders to give the game some variety.

Exercise 4. The BSL function we’ll use is random. Experiment with random in the interactions window. Be sure to run it multiple times with the same input. Is the output always the same? Note that all the functions we’ve designed so far this semester (other than the big-bang wrappers) always produce the same output when run multiple times on a given input.

Exercise 5. Design a function random-invader which takes a Number and returns a Posn whose y-coordinate is given and whose x-coordinate is random. The x-coordinate should nevertheless be chosen so that the invader will be visible when drawn. To test this function, use check-random. Here are some example tests:

(check-random (random-invader 50)  (make-posn (random 401) 50))
(check-random (random-invader 100) (make-posn (random 401) 100))

Exercise 6. One way to define the natural numbers is with a self-referential definition: a natural number is either 0 or it is the successor of a natural number. Here is the corresponding data definition:

; a NaturalNumber is one of:
; - 0
; - (add1 NaturalNumber)

Instead of defining structures and relying on courtesy functions, we will use arithmetic operations present in BSL. For example, we use 0 and we use add1 as constructors. To "unpack" a non-zero NaturalNumber, we use sub1 as a selector. For predicates, we use zero? and positive?.

Finish writing the template for NaturalNumber.

; process-nat : NaturalNumber -> ...
(define (process-nat n) ...)

Exercise 7. Define a number named invader-y which is the starting vertical position for invaders. Next, design a function gen-invaders which takes a NaturalNumber and produces a ListOfPosns. The given number is how many invaders gen-invaders will create with y-coordinate invader-y. Use random-invader.

Exercise 8. Modify the call to big-bang to use gen-invaders to create the initial world. Each time you play you will likely see a different batch of invaders.

4 Game over

Exercise 9. Design a function invaders-landed? which takes a ListOfPosns and returns true if and only if an invader has landed. Next, design a function game-over? which takes a World and returns true if and only if either there are no invaders or an invader has landed. Modify the call to big-bang to use game-over? as the stop-when function.

Exercise 10. Big-bang can optionally take a second function for stop-when which takes a World (the final world) and outputs an Image. Design a function game-over-screen to use as the second stop-when function.