aGPJ_Tutorial_4(2).doc

(252 KB) Pobierz
A GameProgrammer's Journey

A GameProgrammer’s Journey – Tutorial 4

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 4 – How to create a platform game              3

Here we go again!              3

Sound & Images              3

What's the plan              3

Drawing the Items              3

Drawing the npc's              6

The first steps              10

Adding gravity              13

The first jumps              14

Beam me up scotty              15

Finishing the items              16

The End              17

Full source code              18


Tutorial 4 – How to create a platform game

Here we go again!

Welcome to the next part of the platform series. In this tutorial we are going to finish the work we've started in the previous tutorial.

 

If you have not finished the previous tutorial, I strongly suggest you finish that one first.

 

To make sure you have the correct code and images, download the file at the bottom of the page The images in this file are made by me, you are of course allowed to use your own if you want to.

 

Right, grab yourself a soda, unplug the phone, lock the doors so you can't be disturbed... here we go!

Sound & Images

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

 

What's the plan

Usually the big idea in platform games is not very spectacular. You've got your basic hero, some coins or items to collect, a few enemies to get in the hero's way and an exit of somesort, usually to go to the next level.

 

All these ingredients are also going to be in our game. Note that I'm not going to dicuss all parts in great detail. I do not want this tutorial to reach the size of a book, there are still going to be things for you to figure out yourself, in case you're planning to write the next Prince of Persia or something :)

Drawing the Items

We are going to start with something simple: Items. The procedure for creating items and drawing them is fairly easy.

 

To get the items on our screen we are going to use the same methode as we used for the tiles. Let's have a look at the new fgland.bmp, that was in the zip you've downloaded a few minutes ago. There are two new colors in the image.

 

 

One, purple, wil be used in a later chapter, the other, light blue, is meant for the items.

 

To make this work we need to do four things:

 

1.      create the items class and variables

2.      adjust the procedure Loadmap a little

3.      write a procedure which loads the actual items, and make sure it gets called when the game starts

4.      draw the items.

 

Ok, here we go with the first part;

 

type TDirection = (dLeft, dRight, rNone);

     TGameItemType = (banana, apple);

 

type

  TGameItem = class(TImageSprite)

     SpriteImg : TDirectDrawSurface;

     isDead : boolean;

     kind   : TGameItemType;

  end;

 

const   maxSprites = 10;

           maxItems = 10;

 

var

     gameitem  : array [0..maxItems] of TGameItem;   

 

Then, the modification to the loadMap procedure:

 

procedure TForm1.LoadMap(level:byte);

var xpos,ypos : integer;

    currentColor : TColor;

    Bitmap: TBitmap;

begin

  try

  Bitmap:= TBitmap.Create;

  Bitmap.LoadFromFile(ExtractFilePath(application.ExeName)+'fgland.bmp');

 

  for xpos:=0 to MAPWIDTH do

    for ypos:=0 to MAPHEIGHT do

    begin

      currentColor := bitmap.Canvas.Pixels[xpos,ypos];

      case currentColor of

        clfuchsia..clAqua  :

                       begin

                         if TileInfo[xpos-1,ypos].tilenr = 100 then

                            TileInfo[xpos,ypos].tilenr:=100

                         else TileInfo[xpos,ypos].tilenr:=6;

                       end;

        clwhite  : TileInfo[xpos,ypos].tilenr:=100;

        (..)

 

And the new procedure called loadItems;

 

procedure TForm1.LoadItems(level:byte);

var x,y : integer;

    currentColor:  TColor;

    Bitmap: TBitmap;

    itemCounter : integer;

    itemKind : byte;

begin

  try

    Bitmap:= TBitmap.Create;

    Bitmap.LoadFromFile(ExtractFilePath(application.ExeName)+'fgland.bmp');

    itemCounter := 0;

   

    for x:=0 to MAPWIDTH do

      for y:=0 to MAPHEIGHT do

      begin

        itemKind := random(2);

        currentColor := bitmap.Canvas.Pixels[x,y];

        if (currentColor = clAqua) and (itemCounter <= maxItems) then

        begin

           gameItem[itemCounter] := TGameItem.Create(Form1.DXSpriteEngine1.Engine);

...

Zgłoś jeśli naruszono regulamin