How To Write A Game In Pascal

Table of contents:

How To Write A Game In Pascal
How To Write A Game In Pascal

Video: How To Write A Game In Pascal

Video: How To Write A Game In Pascal
Video: Making a game in Free Pascal 2024, November
Anonim

A novice programmer should not immediately start writing a complex, multi-level game program in Pascal. You should start with a logic or math game with a text-based interface. Gradually gaining experience, you can move on to work on larger projects.

How to write a game in Pascal
How to write a game in Pascal

Instructions

Step 1

Start working on the program with a title and a list of plugins used:

program reshiprimer;

uses crt;

Step 2

Tell the compiler which variables will be included in the program:

var

a, b, c: integer;

d: string;

Here a and b are terms, c is the sum, d is the answer to the question of whether the user wants to continue playing.

Step 3

Mark the beginning of the program and endless loop, and also initialize the random number generator:

begin

randomize;

while 0 = 0 do

begin

Step 4

Program to generate two random numbers between 0 and 1000:

a: = round (random (1000));

b: = round (random (1000));

Step 5

Show the user an example they need to solve and then ask for the result:

writeln ('How much will be'), a, ('+'), b, ('?');

readln (c);

Step 6

Tell the user if he solved the example correctly:

if a + b = c then writeln ('Right!') else writeln ('Wrong!);

Step 7

Ask the user if he wants to play more:

writeln ('Shall we play further?');

readln (d);

Step 8

If the answer is no, terminate the program:

if upcase (d) 'Y' then halt (0);

Step 9

End the loop first, and then the program itself:

end

end.

Notice the dot after the second end statement.

Step 10

Run the program by pressing Ctrl + F9. The first example will be displayed. Do the math in your head and enter the answer. The machine will immediately tell you if it has been solved correctly. Then a question will be asked if you want to continue playing. Answering Y or y will output the following example, and typing any other letter will terminate the program.

Step 11

Once the game is working properly, start improving it. For example, add a screen clear before showing each new example with the cls command. Using the textcolor procedure, make the labels appear in different colors. Think about how you can make the program automatically change the complexity of the examples: if you answer correctly, complicate it, and if it is incorrect, simplify it. To diversify the game, add a function for generating examples for various mathematical actions.

Recommended: