Unit spaceShooterProcedures

The code of spaceShooterProcedures

unit spaceShooterProcedures;
{
    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+}

interface

uses
  Classes, SysUtils, sdl, ch_sdl_items, gameVariables, gameInput, gameSoundUnit, gameScreen, gl, glu,
  gameObjectsPlayer, gameObjectsSprite, gameObjectsRock, gameFontUnit, gameScrollingBackground,
  gameClassHealthbar;

var
  pauseKey, musicVolumeKey : boolean; //For toggling key presses
  textYCount : U16;

procedure Initialise;

implementation

uses gameSpaceShooter, gameObjectsBullet;

procedure loadingMessage(messageX : PChar; newline : boolean = true);
//We want to print a message to the screen, and then update the screen immediately.
begin
  SDL_GL_SWAPBUFFERS;
  TText.print(messageX, 270, textYCount);
  if (newLine) then
    textYCount += 16;
  SDL_GL_SWAPBUFFERS;
end;

procedure Initialise;
var
  count : U16;
  waitingForInput : boolean = true;
begin
  randomize;//Set the random seed to time
  pauseKey := false;
  keyFireBullet := false;
  fireKey := false;
  stage := 1;
  currentBullet := 1;
  textYCount := 175;
  Ch_SDL_Initialise(InitVideo or InitAudio); //RUN MY CUSTOM SDL INIT FUNCTION
  Ch_SDL_SetUpScreen(640, 400, gameName); //Set up a new openGL screen
  glCLEAR(GL_COLOR_BUFFER_BIT); //Clear the screen
  glLoadIdentity; //Load the current gl matrix

  points := 0; //Initialise the global score

  TText := TFont.create; //Custom font system
  TText.init('systemFontWhite');

  //This is a stationary background that text will be drawn onto
  ArrayBackground[3] := TBackground.create;
  ArrayBackground[3].init('loader1');
  ArrayBackground[3].SetXY(0, 0, false, false);
  ArrayBackground[3].UpdateGeneric(SpriteUpdate);
  flipScreen;

  //Initialise the input utilities
  keyArraySetup;
  //loadingMessage('Loading music... ');
  //Load music
  //musicVolume := 100;
  //musicVolumeKey := false;
  //Custom sound system
  //LoadSound('SpaceStation9', 'SpaceMusic');
  //SetVolume('SpaceMusic', musicVolume);
  //PlaySound('SpaceMusic');

  //loadingMessage('Music loaded.');

  //Load sprites
  loadSpriteArray('stars', false);

  loadingMessage('Loading objects... ');

  //Set up the objects
  PlayerShip := TPlayer.create;
  PlayerShip.Init;
  for count := 1 to maxBullets do    //Set up bullets
    begin
      ArrayBullets[count] := TBullet.create;
      ArrayBullets[count].init;
    end;
  for count := 1 to maxRocks do      //Set up rocks
    begin
      ArrayRocks[count] := TRock.create;
      ArrayRocks[count].init;
    end;
  currentRocks := 5;
  ArrayBackground[1] := TBackground.create;
  ArrayBackground[2] := TBackground.create;
  ArrayBackground[1].init('stars');
  ArrayBackground[2].init('stars');
  ArrayBackground[2].SetXY(640, 0, false, false);

  LoadSpriteArray('fireball1', true);
  LoadSpriteArray('bulletbase', true);

  healthBox := THealthBar.create;
  healthBox.init;

  loadingMessage('Objects loaded.');

  loadingMessage('Reticulating splines...');
  loadingMessage('Making the game awesome...');
  sdl_delay(400);
  loadingMessage(gameName + ' is loaded!');
  TText.SetZoom(1.5, false);
  loadingMessage('Press Space to start!');
  TText.SetZoom(1, false);

  writeln('Loaded!');

  while(waitingForInput) do
    begin
      KeyArrayUpdate;
      if(checkKeyState(SDLK_Space)) then
        waitingForInput := false;
      if(checkKeyState(SDLK_Escape)) then
        halt;
      sdl_delay(200);
      flipScreen;
      flipScreen;
    end;
end;

end.

Remarks

Could you write a game that uses routines in these SpaceShooter units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16