Tuesday, 7 August 2012

Special Exercise - Space Invaders Part 1

Design a complete Space Invaders Game

The description of the space invader game we are given is very sparse. This is probably because the details don't matter so much as the act of actually designing the game.  For lack of further specification I'm going to try for something similar to the original space invader game. 


We will have a  few rows of aliens that will be moving slowly closer (and left and right). At the moment I am not planning on letting them shoot, but that may change. On the ground there will be a tank that can move left and right and shoot at and destroy the aliens. (With possibly some blocks to hide behind if the aliens end up being able to shoot.  If any of the aliens reach earth, the player loses. If he manages to destroy all the ufo's the player wins. 


For this  first iteration I'm going to create the functions to:

  1.  draw the ufo,
  2.  draw a collection of ufos
  3.  draw our tank

Code



(require racket/base)
(define-struct ufo (x y) #:transparent)
(define-struct tank (x y) #:transparent)


(define TEST-UFO (make-ufo 30 30))   ; for testing
(define TEST-UFOS (list (make-ufo 30 30)
                        (make-ufo 80 30)
                        (make-ufo 130 30))); for testing
(define TANK (make-tank 100 100))





; physical constants
(define WIDTH 300) ; the maximal number of blocks horizontally
(define HEIGHT 300) ; the maximal number of blocks horizontally




; graphical constants
(define SIZE 10) ; the size of the block making up the ufo
(define BLOCK ; they are rendered as red squares with black rims  - stolen from tetris
  (overlay (rectangle (- SIZE 1) (- SIZE 1) "solid" "red")
           (rectangle SIZE SIZE "outline" "black")))


; draw a triangular shaped alien
(define (draw-ufo ufo background)
  (place-image/align BLOCK (ufo-x ufo) (ufo-y ufo) "left" "bottom"
      (place-image/align BLOCK (- (ufo-x ufo) SIZE) (ufo-y ufo) "left" "bottom"
       (place-image/align BLOCK (+ (ufo-x ufo) SIZE) (ufo-y ufo) "left" "bottom"
       (place-image/align BLOCK (ufo-x ufo) (+ (ufo-y ufo) SIZE) "left" "bottom" 
                background)))))


; draw a tank shaped tank
(define (draw-tank tank background)
  (place-image/align BLOCK (tank-x tank) (tank-y tank) "left" "bottom"
   (place-image/align BLOCK (- (tank-x tank) SIZE) (tank-y tank) "left" "bottom"
    (place-image/align BLOCK (+ (tank-x tank) SIZE) (tank-y tank) "left" "bottom"
     (place-image/align BLOCK (tank-x tank) (+ (tank-y tank) SIZE) "left" "bottom"
      (place-image/align BLOCK (- (tank-x tank) SIZE) (+ (tank-y tank) SIZE ) 
            "left" "bottom"
       (place-image/align BLOCK (+ (tank-x tank) SIZE) (+ (tank-y tank) SIZE) 
            "left" "bottom"
        (place-image/align BLOCK (tank-x tank) (- (tank-y tank) SIZE)
            "left" "bottom" 
          background))))))))


(define (draw-ufos ufos background)
           (cond ((empty? ufos)  background)
                 (else (draw-ufo (first ufos) 
                                 (draw-ufos (rest ufos) background)))))




; tests


; simple example - a single block in an empty landscape
(check-expect (draw-ufo TEST-UFO (empty-scene WIDTH HEIGHT))
  (place-image/align BLOCK (ufo-x TEST-UFO) (ufo-y TEST-UFO) "left" "bottom"
      (place-image/align BLOCK (- (ufo-x TEST-UFO) SIZE) (ufo-y TEST-UFO) 
         "left" "bottom"
       (place-image/align BLOCK (+ (ufo-x TEST-UFO) SIZE) (ufo-y TEST-UFO) 
         "left" "bottom"
       (place-image/align BLOCK (ufo-x TEST-UFO) (+ (ufo-y TEST-UFO) SIZE) 
         "left" "bottom" 
                (empty-scene WIDTH HEIGHT))))))


; test drawing three ufo's via the draw-ufos function
(check-expect (draw-ufos TEST-UFOS (empty-scene WIDTH HEIGHT))
              (draw-ufo (first TEST-UFOS) 
                (draw-ufo (second TEST-UFOS) 
                   (draw-ufo (third TEST-UFOS) (empty-scene WIDTH HEIGHT)))))


; test the tank drawing works
(check-expect (draw-tank TANK (empty-scene WIDTH HEIGHT))
  (place-image/align BLOCK (tank-x TANK) (tank-y TANK) "left" "bottom"
   (place-image/align BLOCK (- (tank-x TANK) SIZE) (tank-y TANK) "left" "bottom"
    (place-image/align BLOCK (+ (tank-x TANK) SIZE) (tank-y TANK) "left" "bottom"
     (place-image/align BLOCK (tank-x TANK) (+ (tank-y TANK) SIZE) "left" "bottom"
      (place-image/align BLOCK (- (tank-x TANK) SIZE) (+ (tank-y TANK) SIZE ) 
           "left" "bottom"
       (place-image/align BLOCK (+ (tank-x TANK) SIZE) (+ (tank-y TANK) SIZE) 
            "left" "bottom"
        (place-image/align BLOCK (tank-x TANK) (- (tank-y TANK) SIZE)
            "left" "bottom" 
        (empty-scene WIDTH HEIGHT)))))))))

No comments:

Post a Comment