On this page:
1 Introduction(s)
2 Purpose
3 A Simplified Space Invader
4 From Maybe  Shot to Shots
6.12

Lab 6: A John Woo Joint

1 Introduction(s)

You’ll work in labs in pairs. Find someone to work with for this first lab and introduce yourself.

Make sure at least one of you have a laptop to work on for this lab.

The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

2 Purpose

In this lab, you’ll practice
  • refining programs designed by others and

  • using lists to represent arbitrarily large collections of data.

3 A Simplified Space Invader

In this lab, you will be adapting a program designed to implement (the simplified beginnings of) a Space Invader style game.

In this simplified version of the game, a Duke student who’s behind on their HtDP reading has implemented the base and shooting functionality of the game. Unfortunately, only one shot at a time can be fired. Your task is to understand the Duke student’s program (luckily they did follow the design recipe, so their code should be fairly clear) and adapt the program so that the player can shoot an unbounded number of shots by repeatedly pressing the space key.

Here are the critical data definitions and the main function of their program:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main
 
;; main : Number -> Game
;; Launches a game of Space Invaders (simplified)
;; Example: (main 0)
(define (main n)
  (big-bang (make-si (+ n (* 1/2 WIDTH)) #false)
    [to-draw si-draw]
    [on-tick si-advance]
    [on-key si-handle-key]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Definitions
 
;; A SI (Space Invader game) is a (make-si Base MaybeShot)
;; Interpretation: x is the x-coordinate of the base, s is the current shot
(define-struct si (x s))
 
;; A Base is an Integer
 
;; A MaybeShot is one of:
;; - #false
;; - Shot
;; Interpretation: shot or #false to indicate no shot
 
;; A Shot is a (make-posn Integer Integer)
;; Intepretation: coordinates of a shot position

The complete code is available here: invader.rkt. Download and open in DrRacket (copy and pasting will not work). Read and understand the code. Give it a try with (main 0).

4 From MaybeShot to Shots

As you’ll notice in the design above, a Space Invader game state includes a MaybeShot which represents either 0 or 1 shot having been fired. Better would be to represent an arbitrarily large collection of shots.

Lab problem 1: Shots

Develop a data definition called Shots for representing an arbitrarily large collection of shots.

Revise the data defintions of SI to use Shots.

Revise main so that the initial state of the game uses Shots.

Lab problem 2: Shots functions

Design the following functions for operating on shots:

;; Shots -> Shots
;; Advance all shots in the list upward.
(define (shots-advance ss) ss)
 
;; Shots -> Shots
;; Remove any shot in the list that has gone off-screen.
(define (shots-remove-offscreen ss) ss)
 
;; Shots -> Shots
;; Advance all shots and remove any that are off-screen
(define (shots-advance/remove ss) ss)
 
;; Shots Image -> Image
;; Draw each of the shots on given image
(define (shots-draw-on ss scn) scn)
 
;; Shots Integer -> Shots
;; Add a new shot to given list at position x
(define (shots-new ss x) ss)

Lab problem 3: Revise SI

Revise si-draw, si-advance, and si-handle-key to use the functions you designed above.