Unit gameSpaceShooter

The code of gameSpaceShooter

unit gameSpaceShooter;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, spaceShooterProcedures, gameObjectsPlayer, sdl, gl, glu,
  ch_sdl_items, gameVariables, gameInput, gameObjectsBullet, gameObjectsSprite,
  gameClassTimer, gameObjectsRock, gameScrollingBackground, gameScreen, gameFontUnit,
  gameGraphicsUnit, gameClassHealthbar, importantRead;

var
  PlayerShip : TPlayer;
  points : U16;
  count : U16;
  TText : TFont;
  timerfps, timerupdate : TTimer;
  fps : F32;
  healthBox : THealthBar;
  tempPic : GLUint;

procedure Game;

implementation

procedure Game;
begin
  Initialise;//Set up evvveeerrything

  fps := 0;
  timerfps := TTimer.create;
  timerupdate := TTimer.create;
  timerfps.start;
  timerupdate.start;

  repeat
    repeat
       //Check key states
      keyArrayUpdate;

      //Empty the screen
      glCLEAR(GL_COLOR_BUFFER_BIT);
      glLoadIdentity;

      ArrayBackground[1].update;
      ArrayBackground[2].update;

      //Update all objects on screen
      PlayerShip.update;

      for count := 1 to maxBullets do
        begin
          ArrayBullets[count].update;
        end;
      for count := 1 to currentRocks do
        begin
          ArrayRocks[count].update;
        end;

      //healthBox.update;
      TText.SetZoom(1.5, false);
      TText.print(PChar('Points: ' + IntToStr(points)), 32, 32);

      TText.SetZoom(1, false);
      TText.print(PChar(FloatToStr(round(fps))), 0, 0);

      //DRAWING THE HEALTHBAR
      glPushMatrix;
        glDisable(gl_blend);
        glTranslatef(400, 0, 0);
        glBindTexture(GL_TEXTURE_2D, whiteTex);

        glBegin(gl_QUADS);
          glColor3f(0, 0, 0);
          glVertex3f(10, 320, 0);
          glVertex3f(10, 370, 0);
          glVertex3f(110, 370, 0);
          glVertex3f(110, 320, 0);
          glColor3f(1, 0, 0);
          glVertex3f(20, 340, 0);
          glVertex3f(20, 360, 0);
          glVertex3f(100, 360, 0);
          glVertex3f(100, 340, 0);
          glColor3f(0, 1, 0);
          glVertex3f(20, 340, 0);
          glVertex3f(20, 360, 0);
          glVertex3f(20 + (80 * (PlayerShip.GetHealth / 100)), 360, 0);
          glVertex3f(20 + (80 * (PlayerShip.GetHealth / 100)), 340, 0);
        glEnd;

        TText.print(PChar('Health: ' + IntToStr(PlayerShip.GetHealth)), 20, 324);
        //glTranslatef(-300,0,0);
      glPopMatrix;

      SDL_Delay(constFrameDelay - ((SDL_GetTicks - fpsTimer.GetTicks) mod constFrameDelay));

      flipScreen;
      inc(frames);

      if (timerupdate.getTicks > 1000) then
        begin
          fps := (frames / (timerfps.getTicks / 1000)) + 3;
          timerupdate.start;
        end;
      inc(points);

      if (PlayerShip.getHealth = 0) then
        begin
          writeln('Game over');
        end;

    until (PlayerShip.getHealth < 1) or (checkKeyState(SDLK_escape));

    glCLEAR(GL_COLOR_BUFFER_BIT);
    glLoadIdentity;

    ArrayBackground[1].update;
    ArrayBackground[2].update;

    TText.SetZoom(1.4, false);
    TText.print(PCHAR('You died! :( At least you'), 100, 150);
    TText.print(PCHAR('scored ' + IntToStr(points) + ' points!'), 100, 182);

    flipScreen;

    while (checkKeyState(SDLK_escape) = false) do
      begin
        keyArrayUpdate;
      end;

  until checkKeyState(SDLK_escape);
  //QuitGame;  //Needs to be programmed in - will free surfaces and memory

end;

end.

Remarks

Could you write a game unit like this that uses routines in other units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16