This program uses records as a demonstration on how they can be used in a roll playing game. This program is just a demonstration and not a true RPG game. If you wish, you can use this code and logic to create one.
program Project1;
{$mode objfpc}{$H+}
uses
crt, sysutils,
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
type
creature = record
Name:string;
Armor, Health, Magic, Power:integer;
end;
{armor doesn't do anything in this program
but you are free add code to make the armor protect the creature}
var
Hero, JellBall, SmallSpider, LargeSpider, Opponent:creature;
enemNum:integer; {EnemyNumber used to select the opponent monster}
selection:string; {used for user input}
score:integer; {keeps score}
procedure LoadSettings; {loads the settings for the hero and monsters}
begin
score := 0; {start score at 0}
with Hero do
begin
Name:= 'Sir Bill';
Armor := 0;
Health := 10;
Magic := 2;
Power := 5;
end;
with JellBall do
begin
Name:= 'JellBall';
Armor := 0;
Health := 1;
Magic := 0;
Power := 1;
end;
with SmallSpider do
begin
Name:= 'Small Spider';
Armor := 0;
Health := 1;
Magic := 0;
Power := 2;
end;
with LargeSpider do
begin
Name:= 'Large Spider';
Armor := 0;
Health := 2;
Magic := 0;
Power := 2;
end;
end;
Procedure GetEnemy; {gets the enemy to fight}
begin
randomize;
enemNum := random(3) + 1;
if enemNum = 1 then
Opponent := JellBall
else if enemNum = 2 then
Opponent := SmallSpider
else
Opponent := LargeSpider;
end;
procedure WriteStats;
begin
with Hero do
begin
writeln(Name, ' Score = ', score);
writeln('Health = ', Health);
writeln('Armor = ', Armor);
writeln('Magic = ', Magic);
writeln('Power = ', Power);
end; {with Hero}
writeln;
writeln;
with Opponent do
begin
writeln('Opponent: ', Name);
writeln('Health = ', Health);
writeln('Armor = ', Armor);
writeln('Magic = ', Magic);
writeln('Power = ', Power);
end; {with Opponent}
end; {procedure}
procedure Menu;
begin
repeat
writeln;
writeln('(F)ight');
writeln('(M)agic');
write('Selection: ');
readln(selection);
selection := UpperCase(selection);
randomize;
if selection = 'F' then
begin
Hero.Health := Hero.Health - random(Opponent.Power + 1);
Opponent.Health := Opponent.Health - random(Hero.Power + 1);
end
else if (selection = 'M') and (Hero.Magic > 0) then
begin
Opponent.Health := Opponent.Health - 10;
Hero.Magic := Hero.Magic - 1;
if Opponent.Health > 0 then
Hero.Health := Hero.Health - random(Opponent.Power + 1);
end;
until (Selection = 'F') or (Selection = 'M'); {until a correct selection is given}
end;
procedure Battle;
begin
GetEnemy;
repeat
clrscr();
WriteStats;
Menu;
until (Hero.Health <= 0) or (Opponent.Health <= 0); {something is dead}
if Opponent.Health <= 0 then {opponent killed}
begin
writeln(Opponent.Name, ' Was Killed');
score := score + Opponent.Power;
sleep(1000); {pauses the program for one second}
end;
end;
begin {Main Program}
repeat
LoadSettings;
repeat
Battle;
until Hero.Health <= 0; {until hero is killed}
clrscr();
writeln(Hero.Name , ' Was Killed');
writeln('Final Score: ', score);
repeat
write('Play Again Y/N: ');
readln(selection);
selection := UpperCase(selection); {convert to uppercase}
until (selection = 'Y') or (selection = 'N'); {keep asking until correct selection is given}
until selection = 'N';
end.
On Your Own:
1) Expand the game to include protection from attack that is based upon the armor settings.
-
2)Create some more creatures to battle
3) See if you can create different levels of the game based upon the Hero’s score or experience.
Video Tutorial: Free Pascal Tutorial 14 - Records For Role Playing Games - Lazarus
