SpaceShooter

by Christopher Winward: L6 Age ~16

Introduction

Use the wasd keys in program SpaceShooter to guide the spaceship between the advancing rocks and use the left mouse button to fire laser beams and different types of ammunition at the rocks. The central gun rotates and always points towards the cursor. Several collisions are necessary to destroy the spaceship and some of the rocks. The following screenshot shows the program in action.
SpaceShooter in action

SpaceShooter in action

The number in the top left is the frame rate in frames per second (fps). The program performs well at around 50 fps.

Christopher makes excellent use of classes and objects and he is getting to grips with the tricky OpenGL graphics code. He achieves a wide range of effects such as animation, a smoothly scrolling background, effective motion of the spaceship, rotating rocks, haloes (resulting from the exhaust and the laser) and the display of text. Technical features include pointers, bit masks, trig functions, sound mixing, input from keyboard and mouse button using SDL, timers, direct access to data, and subroutines with variable and default parameters. C syntax is used as a shorthand, e.g. health -= healthx; instead of health := healthhealthx;.

Christopher was inspired by Jason McMillen's Artillery Game Tutorial and in early versions he used some of Jason's units and SDL graphics without OpenGL. He then progressed to OpenGL, which handles his images much faster. Christopher studied the NeHe Productions and Lazy Foo' Productions sites and numerous other web sources to learn how to use advanced graphics features. If this program inspires you to try OpenGL, we can recommend as an introduction the official OpenGL Programming Guide, which explains with the aid of helpful diagrams the underlying concepts and terminology. (As with many sources of OpenGL information, the sections of OpenGL code are within C programs rather than within Pascal. If you have advanced to this level, you should be able to transfer your knowledge appropriately).

Download here a zip file containing the main code (spaceshooter.txt), 20 units, a folder of images and another folder of sound files. You should unzip the files so that all of the units and the image and sound folders are in the program folder. In order to run the program you will need to use SDL files and additional libraries. (See Getting Started with SDL for download details of SDL.dll and SDL.pas).

The program requires the libraries SDL.dll, SDL_gfx.dll, SDL_mixer.dll, SDL_ttf.dll, SDL_image.dll, libpng12-0.dll and zlib1.dll. You can put these files in the program folder or with thousands of other libraries in your system folder.
Download the other libraries from the SDL site as follows.
  • SDL_ttf-2.0.10-win32.zip from the SDL_ttf 2.0 page contains SDL_ttf.dll.
  • SDL_mixer-1.2.11-win32.zip from the SDL_mixer 1.2 page contains SDL_mixer.dll.
  • SDL_image-1.2.10-win32.zip from the SDL_image 1.2 page contains the last three files in the list.

In order to reduce bandwidth and storage space, we have removed (with permission!) the large SpaceStation9.wav file and commented out the music-related code in spaceShooterProcedures.pas. However, you can uncomment it and change the name SpaceStation9 to the name of your own music .wav file, which you should put in the Sounds folder.

Christopher designed the program to compile using Lazarus. If the program fails to run properly within Lazarus, try running it by opening the program folder and double clicking on SpaceShooter.exe.

The main program code just calls a single procedure:
program SpaceShooter;
{
    Copyright (c) 2011 Christopher Winward
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

uses
  Classes, SysUtils, gameSpaceShooter, gameClassHealthbar;
{$IFDEF WINDOWS}{$R SpaceShooter.rc}{$ENDIF}

begin
  Game;
end.   

Click on the links below to view the code of the units, which are arranged in alphabetical order. The game procedure is in unit gameSpaceShooter. (Some of the brackets used by Christopher in his code are unnecessary, but he prefers to include them to be on the safe side. They do no harm, apart perhaps from making some code appear over-complicated to others, whereas missing brackets are problematic).

Programming - a skill for life!

A-star, Calculator, MarbleRun and SpaceShooter by Christopher Winward