Example of a Small Program

Delphi

Icone.zip

If Delphi no longer works properly after a Windows update, change the program's compatibility mode so that it runs in Administrator mode.

Right-click the program and click Properties

Then in Compatibility check Run this program as administrator.

I created a directory that brings together all the Delphi programs I make.

Example for the dice game Fifty

The aim of the game is to be the first player to reach fifty points.

But only doubles score points.

Instead of counting, there is a set number of points based on doubles thrown.

The table below shows the rating:

Double 6 = 25 points Double 5 = 10 points

Double 4 = 5 points Double 3 = lose all points

Double 2 = 5 points Double 1 = -5 points

There are two methods of play.

First method: there is only one set of dice. Each player takes turns rolling the dice.

If no doubles are obtained, play passes to the next player.

If doubles are thrown, the score is noted and play passes to the next player.

Second method: This requires a set of dice for each player.

Each player rolls their dice and continues rolling, adding up their own scores until someone reaches fifty points.

Create a new application

In file -> New -> Application

Save the program

File -> Save All

In my directory My documents\Computer\Dephi program\[Dice

I created directory 50

In the directory 50

Click Save for the file Unit1.pas

Rename the project Fifty and click Save

Now in directory 50 there are these files:

Click on Run to compile the project, on the Green Triangle

The program appears (an empty form)

In directory 50 Fifty.exe appeared

Save and Click Run to compile the project after each step to check if everything is working well.

Reduce the size of Form1 with the mouse (like a windows window)

Now Give the name Fifty to Form1 and the project

In Propeties -> Capture change Form1 to Fifty

   -> 

Now Form1 is called Fifty

In the Project tab click on Options

Click on the Application tab and in Tilte type Fifty and then click on OK

Save the project.

Change Icon

You can download the Icone.zip pack

To put everything in one place, I copied the icon to the project directory.

In Propeties -> Icon (None) -> click on the three small dots ...

Click Load... and the icon you want. And then click Open

Click on OK

Now Form 1 has our Icon

Change the project icon.

In the Project tab click on Options

Click on Application tab -> Click on Load Icon

Click on the icon and then on Open

Click on OK

At this step if you downloaded an icon incompatible with Delphi, this will cause a Delphi error:

Out Of Memory

Run the project... Our project has our icon

As well as the Fifty.exe program

Configuring Font and Style properties

By configuring the properties of Form1, all the components we create will inherit these properties.

In Font -> Size Change 8 to 12 for character size

AND in Style -> fsBold select True for Bold character

All future components will have these properties...

 

Creating parameters when running the program

When compiling the program the position of the window will be the one that Form1 has in Delphi.

These parameters are Left and Top.

   

Here Left = 385 and Top = 129.

Which is not always convenient.

Double click on the Form1 window, this will create the procedure TForm1.FormCreate

Here we can give the position parameters we want to have.

Don't forget ; at the end of a line.

To do this in the Unit1.pas window

type Form1. and a menu will appear after tapping the point .

Then type L

Form1.L   -> which will display this menu

(here we see that Left is of type Integer)

Click on Left which will give

Form1.Left

now add := 300;

Form1.Left := 300;

Do the same for the Top property.

Form1.To ->

Form1.Top := 100;

Save and run the project.

To select a property of a component, we proceed in the same way.

We type the name of the component and then the point .

 

As we use dice we will use the Random function.

To make this function more random we add Randomize; in TForm1.FormCreate

Now we will create 2 global variables of integer type, Tot and Round

At the start of the program Under

var
    Form1: TForm1;

we add Tot, Round : integer;

Tot for the total points and Round for the Tour total.

In TForm1.FormCreate add

Tot := 0;
Round := 0;

What initializes the variables Tot et Round à 0 to the execution of the program.

Adding components toForm1

We add a button Roll Dice

Click on the component OK (Button (StdCtrls) Appears)

Click with the mouse on Form1 where you want to have the button

By default the button is named Button1

Here the Button component has inherited from Form1 the Font.size and Style.fsBold properties that we defined above.

In Object TreeView the Button1 component has appeared and we see that Button1 is a child of Form1.

Change the properties Caption de Button1 (Button1) for Roll Dice

Which give

However, the Button1 component is always named Button1 (Properties Name)

 

Which is not too practical if we have several Buttons (Button1, Button2, Button3.. and.)

Change the Name property of Button1 to Roll which is more significant ...

We see the name change in Object TreeView

Now we will add the two dice.

Click on the component A (Label (StdCtrls))

Click with the mouse on Form1 where you want to have the component Label

Which give

Rename Label1 for De1

Later we will use the Caption property of De1 to display the number of the die.

For now put 6 in Caption to give us the desired dimension of the die.

Change the properties Size of Font for 14 to have a larger character.

To always have a die of the same size whatever the number of the die change AutoSize to False

Change Caption again to *

Because when executing the program we don't want the player to believe they have had a 6...

Here we have a first die installed...

To save time, to install the second die make a copy and paste of De1.

Right-click on De1 -> Edit -> Copy

Or click De1 and press Ctrl-C

Then Press Ctrl-V (The second die will have the same properties as De1)

Move the second die to the desired location

Now rename the second die De2

Check that the two dice are aligned and that they have the same value Top (46 here)

   

We have our two dice ....

Roll Dice Button programming

Double click on the Roll Dice button, in Unit1.pas the procedure TForm1.RollClick appears.

Add 3 local variables i, j for dice and x for pointing.

before begin add

var

i, j, x : integer;

We use the Random function for a random number.

See Randomize, Random

After begin add

i := 1 + Random(6);
j := 1 + Random(6);

We will display the numbers i for De1 and j for De2.

Add these lines:

De1.Caption := IntToStr(i);
De2.Caption := IntToStr(j);

Caption is of type string, so we need to convert i and j which are of type integer to string with function

IntToStr();

Save the project and run the program

Click on Run to compile the project, on the Green Triangle

The Roll Dice button works

Now the label Score / Points

Add a Label component

Component A (Label (StdCtrls))

and modify

Caption for Score / Points

Font -> Color -> clNavy

BelowScore / Points (Label1) add another label and modify

Caption pour 0

Font -> Color -> clMaroon

Font -> size -> 14

Name -> Points

Which give

The label Total

Add a Label component and edit

Caption for Total

Font -> Color -> clGreen

Next to Total add another Label and modify

Caption for 0

Font -> size -> 14

Name -> Total

Add a label Round

Font -> Color -> clNavy

and under Round a label with Caption with 0

Font -> Color -> clRed

Save the project and run the program

Click on Run to compile the project, on the Green Triangle

Continued Roll Dice button programming

In the procedure TForm1.RollClick (Double click again on Roll Dice)

We increment the Round variable by 1 and display its value in Label3

Round := Round +1;
Label3.Caption := IntToStr(Round);

We put

Points.Caption := '0';

Configuring points and total score

There are points only if both dice are identical.

We add the if function and use case to assign the points

if i = j then

    case i of

    1: // The dice are 1s, we take away 5 points
        begin
            x := -5;
            Tot := Tot + x;
        end;

    2: // The dice are 2, we add 5 points
        begin
        x := 5;
        Tot := Tot + x;
        end;

    3: // The dice are 3, we lose our points
        begin
            if Tot > 0 then
            begin
                x := 0;
                Tot := x;
            end;
        end;

    4: // The dice are 4, we add 5 points
        begin
            x := 5;
            Tot := Tot + x;
        end;

    5: // The dice are 5, add 10 points
        begin
        x := 10;
        Tot := Tot + x;
        end;

    6: // The dice are 6, add 25 points
        begin
            x := 25;
            Tot := Tot + x;
        end;

end; // case




After the end; /case

We display points and total score

Points.Caption := IntToStr(x);
Total.Caption := IntToStr(Tot);

Save the project and run the program

Click on Run to compile the project, on the Green Triangle

The program is functional

Add a New Game Button

Double click on the New Game button to program it

procedure TForm1.Button1Click(Sender: TObject);
begin

De1.Caption := '*';
De2.Caption := '*';

Tot := 0;
Round := 0;

Points.Caption := '0';
Total.Caption := '0';
Label3.Caption := '0';

end;

Add labels to indicate dice combinations and number of points.

All that remains is to configure Form1 with a border of 15 (BorderWidth 15) and a AutoSize True

Save the project and run the program

Click on Run to compile the project, on the Green Triangle

Now you can use Fifty.exe and give a copy to all your friends ....

 

 

 

 

 

 

 

Recherche personnalisée