aGPJ_Tutorial_2(2).doc

(322 KB) Pobierz
A GameProgrammer's Journey

A GameProgrammer’s Journey – Tutorial 2

A GameProgrammer’s Journey

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Disclaimer: The tutorial below, including all images, is copyrighted by Alexander Rosendal. You are allowed to use the Delphi code and images for your own purposes. You are not allowed to copy (parts of) these tutorials without my prior written permission!

Give credit where credit is due. Thank you!
 


Table of contents

A GameProgrammer’s Journey              1

Table of contents              2

Tutorial 2 – How to create a spaceshooter              3

Before you start              3

Images & Sound              3

The Game              3

To the stars              3

The player              5

Enemy formations              7

Shooting the player              10

Shooting the enemy              12

Almost done              13

The End              15


Tutorial 2 – How to create a spaceshooter

Before you start

Before we embark on the second tutorial let me point out that some of the code used in this tutorial comes from the first tutorial. If you haven't completed that tutorial, I strongly suggest you follow that one first.

 

Also, while I do encourage you to make your own graphics/sounds, it's not really necessary to do so. If you've been following tutorial 1 then you already have the graphics otherwise get the graphics and the sounds from the link at the bottom.

 

Ok, now if you are ready. Here we go...

Images & Sound

(Doubleclick on the icon to get the images for this tutorial)

The Game

Like the title says, we're going to make a very cool little space shooter. I'll try to go through all main features and, hopefully, after finishing this tutorial, you'll have the knowlegde to add new, cooler, better and greater things to this game yourself.

 

Ok, now let me tell you exactly what we're going to make. The player has a spaceship that can move all over the screen. The ship will of course be equipped with a gun. Not just a gun, but a really, really cool gun. One shot usually is enough to blow the enemy in to a billion pieces.

 

The enemy, who for some weird reason want to destroy the player, are going to present themselves in formations. We know three kinds of formations in this game.

 

 

In this game, the player will have three lives. When the player gets shot, he will lose a life. Three lives lost means game over. If the players shoots an enemy he receives points and the enemy is destroyed.

 

That is in short what we are going to make.

To the stars

Let's start with the easy things first: the stars. These are tiny pictures that move in a vertical direction. To make it look better, we'll apply different speedrates to them.

 

The first thing we have to do is creating a sprite:

 

type

   TStar = class(TImageSprite)

   protected

      starType: byte;

      procedure DoMove(MoveCount: Integer); override;

   end;

 

Then add a new variable, an array of Tstar:

 

  ...

  SelectorImage : TDirectDrawSurface;

  InstructionImage : TDirectDrawSurface;

  stars : array [1..75] of TStar;

 

Given the size of our screen I think 75 stars, should be enough. Like all the other sprites, we are going to create these in the doReset procedure:

 

procedure doReset;

var numberOfStars : byte;

begin

  // put the things before the game starts

Form1.DXDraw1.surface.Canvas.Font.Color:=clwhite;

  Form1.DXDraw1.surface.Canvas.Brush.Style:=bsclear;

 

  for numberOfStars := 1 to 75 do

  begin

     Stars[numberOfStars]:= TStar.Create(Form1.DXSpriteEngine1.Engine);

     with Stars[numberOfStars] do

     begin

       case numberOfStars of

         0..25: starType := 1;

         26..50: starType := 2;

         51..75: starType := 3;

       end;

       Image := Form1.DXImageList1.Items.Find('star 1');

       X := Random(form1.width);

       Y := Random(form1.height);

       Z := 1;

       Width := Image.Width;

       Height := Image.Height;

     end;

   end;

   gameCounter := 0;

   GameState := gsGame;

...

 

To actually make the stars visible in the game, we have to add these lines in the doGame procedure:

 

procedure doGame;

begin

  // put the game stuff in here

 

  inc(gameCounter);

  Form1.DXDraw1.surface.Fill(0);

  Form1.Dxinput1.Update;

 

  Form1.DXSpriteEngine1.Move(0);

  Form1.DXSpriteEngine1.Draw;

  Form1.DXSpriteEngine1.Dead;

end;

 

Before you can actually run the game you need to write the doMove procedure of the stars:

 

procedure TStar.DoMove(MoveCount: Integer);

begin

    case starType of

       1:  y:=y+1;

       2:  y:=y+2;

       3:  y:=y+3;

    end;

    if y > form1.height then

    begin

...

Zgłoś jeśli naruszono regulamin