Delphi and DelphiX 2d games programming tutorial

Updated on 2-Okt-2002

Hello.., I started programming when I was still young. My language of choice was Basic. I did do a little Assembler and even a language called 'E'. I did not program a lot but started doing this when I got older. I programmed in Visual Basic and later Blitz Basic. Use Blitz Basic if you want to program and want it to be easy. Blitz Basic comes as a 2d and a 3d+2d package. It is not free however but the 2d blitz basic demo only has limited features removed.

Borland are kind enough to make their personal editions of Delphi free of charge. They only bother you with registration and they want to be allowed to send you email. I cannot remember when I got one however so it is not anoying. The limits with the personal version are there but it is still a powerfull package to produce freeware and open source games in. If you make a good enough product you can always buy the Proffesional edition and sell your game.


Tips :

Your code needs to be ended with a ';' character.

example :

If overlaprect(rect(0,0,100,100),rect(50,50,100,100)) = true then dostuff;


Another example

If pointinrect(point(mousex,mousey),rect(0,0,10,10)) = true then begin
dosstuff;
end;

You can see the 'If' word. This we use if we want to check stuff. For isntance if a integer is of a certain value.
The overlaprect(rect,rect) is a command. You can use this command to check if a rectangular region is above another rectangular region. This command is usefull for detecting sprite collisions.
The rect(left,top,right,bottom) command is to create a rectangle in the memory that you can use in this case for overlaprect.
The pointinrect(point,rect) command is used to check if a certain x,y location is inside a rectangular region. You need to use the point command to convert the x,y location to a point.

The 'begin' word is to let Delphi know we are beginning a code block. We end it with 'end'


Lets take another example.

dxinput1.update;
if dxinput1.keyboard.keys[32] = 32 then jump := true;

Above we see two lines. The first line tells DelphiX to update its keys. You only have to call it once every loop.
The second line checks if the space bar has been pressed and if so it loads true into the jump variable.
dxinput1 is the name of the component we have on our form.
keyboard is a location in the component and so is keys[].
32 is the number of the space bar in DelphiX.

There is a way to check which number is which key.

For i := 0 to 255 do
begin
if dxinput1.keyboard.keys[i] = true then keypressed := i;
end;
textout(0,0,keypressed);


The 'For' word means we want to start a For loop. A For loop is a loop between two numbers. We give assign it a variable that will hold the current loop count. In this case 'i'. 'Do' is a formality.
In this example keypressed holds any key that is pressed. You can btw only press one key at a time for it wont register multiple key presses.
The 'textout(x,y,string)' tells delphi to draw text on the canvas. The canvas is the name of the screen in which you are working.


Another example :

Map to screen conversion

function returncollision(x:integer;y:integer;offsetx:integer;offsety:integer):boolean;
var
x1,y1,countery,counterswitch : integer;
begin
//
countery := 0;
counterswitch := 0;
//
// Loop thru the height of the picture
for y1 := 0 to 31 do
begin
// If the checked y position is the given y position
if y1+offsety = y then
begin
// Loop thru the width of the image
for x1 := 0 to 63 do
begin
// Check if we are inside the isometric surface
if (x1>32-countery*2) and (x1<32+countery*2) then
begin
// If the checked x position is the given x position
if (x1+offsetx = x) then
begin
// collision is true
returncollision := true;
end;
end
end;
end;
if counterswitch = 0 then countery := countery + 1;
if counterswitch = 1 then countery := countery - 1;
if countery = 16 then counterswitch := 1;
end;


This example is a function that returns true if a x and y value is inside a isometric square. The square is asumed to be sized 64*32
In the code you can see the '//' letters. These tell Delphi that we want to comment something.

..code..; // This is a comment

'var' means we want to define things. You need to define every variable you use in Delphi.
Place the 'var' before a 'begin'


Here is the map drawing code code from my isometric engine

cx := mapoffsetx; {screen location x}
cy := mapoffsety; {screen location y}
for y:=0 to mapheight do {Loop 11 times}
begin
{Copy the start drawing values into the
actual drawing location}
ax := cx;
ay := cy;
for x:=0 to mapwidth do {Loop 11 times}
begin
{Increase the actual image value}
ax := ax + 32;
ay := ay + 16;
{Draw the tile}
if (ax>-64) and (ax0-32) and (ay begin
// Here we check if we have a collision with the tile
if returncollision(mousex,mousey,ax,ay) = true then
begin
cursorx := x;
cursory := y;
end;

DXImagelist1.items[0].Draw(DXDraw1.surface,ax,ay,maparray[x,y]);
if (x = cursorx) and (y = cursory) then
begin
DXImagelist1.items[0].Draw(DXDraw1.surface,ax,ay,3);
end;
if maplayer2[x,y]>0 then
DXImagelist1.items[0].Draw(DXDraw1.surface,ax,ay,maplayer2[x,y]);
end;
end;
{Update the start drawing value}
cx := cx - 32;
cy := cy + 16;
end;


The '{' and '}' letters are used for commenting.
DXImagelist1.items[0].draw is used to draw a image from the DelphiX Imagelist. We need to tell it which surface to draw on. And x, y and the frame.

The End

Come back another time when this tutorial might be updated.