Basic Programming Using Delphi

With the aspiring Game Programmer In Mind

By DragonPhinn [Howard N Smith]

This tutorial was designed for those fairly new to the Delphi environment. It's purpose is to familiarize you with the Delphi program, as well as show you how to use this compiler as a tool for building video games.

Load up your copy of Delphi and you will see a screen similar to this [depending on your version].

A Screenshot of Delphi

This program is broken down into four parts.

  1. The Toolbars/Menu
  2. The Forms
  3. The Object Inspector
  4. The Code View

The toolbars/menu area houses all of the components and commands that you will need in the production of your program. It's fairly self explanitory.


DEFINITION: Components are controls that add functionality to your window. They include buttons, edit boxes, labels, and every other item you have ever seen on a window. You can view each of the controls your version of Delphi includes by viewing the upper bar on the menu space. Note: The words component and control can be used virtually interchangeably


The Forms area is the blank canvas, if you will, of your program. When you run your program this will be the window in which it will operate.

The Object Inspector shows all the properties of the selected component. These properties include color, caption, width, height, position, etc. They vary from control to control, but many share similar properties. If you take notice there is a second tab on the Object Inspector window. The second tab is called the "Events Tab"

A closeup on the Object Inspector

On it are the various actions the selected control can perform. Some may include OnCreate, OnClick, OnMouseDown, etc. These are where you actually add functionality to the controls. Double click on the form to bring up the next section, which links to the Events tab.

This window is called the code view. By double clicking on most controls it brings you here to edit the default command. If you notice the default command of the Form is OnCreate. This is what the code should look like:


procedure TForm1.FormCreate(Sender: TObject);
begin

end;

Lets take a look at the code. "procedure" means it is a command. TForm1.FormCreate is a function that says "when Form1 is created" then next part (Sender: TObject) is a variable passed that says who called the function. There are many useful ways to use this, for example, you can write a singe piece of code that is reused by numerous controls that refrences itself using the sender command.

The Begin and End statements surround your code.

This is where we add commands, or instructions, for the program to execute when form1 is created. Type the following inbetween the begin and end statements:

ShowMessage('Hello World!');


Note: Please notice that the end of the line is followed by a semi colon ";". This symbol ends each single statment, and lets the program know where each instruction ends.

Press F9, which means compile and run, and you will see a message box appear with the words "Hello World!" and an OK button. When you press the OK button, the window will appear.


Note: Remember that the OnCreate command occurs before the OnShow command, which means instructions entered in the OnCreate even will be executed before the form is shown. Also note that the forms are created in numerical order. For example Form1 is created before Form2. This is important when calling functions on other forms, because if Form1.OnCreate command calls an instruction on Form2, it will solicit an error due to the fact that Form2 hasn't been created yet!

Congradulations! You've created your first Delphi program! Every programmer, no matter how great, began with this humble beginning. You can take this moment to reflect on the glory of your own achivement.

So now you understand the very basics of programming with delphi. There is still a lot more to learn, but this should give you a solid start to work off of.

For more practice, try attaching the ShowMessage command to other events, such as a buttons OnClick, or a timers OnTimer. Play around with these functions to get a better understanding on how they work.

DragonPhinn




© 2001 The Dragon's Lair and Howard N Smith.