Skip to content

Instantly share code, notes, and snippets.

@pakLebah
Last active Feb 2, 2020
Embed
What would you like to do?
Pascal car racing on console using ansiCRT unit.
program CarRace;
(*****************************************************************************
This program demonstrates ansiCRT unit instead of the standard Pascal's CRT
unit. Also showing how to use emoji/unicode on console screen so the output
looks more fancy and as if on graphics screen. Of course, this can also be
done using the CRT unit.
ansiCRT is here: gist.github.com/pakLebah/c5e2bbd0b93c863b2122660111db68d1
Author: @pakLebah a.k.a Mr. Bee
Update: April, 2019
*****************************************************************************)
uses
SysUtils, ansiCRT;
const
THIN_SPACE = ' ';
CARS_COUNT = 6;
LANE_LENGTH = 60;
FRAME_DELAY = 90;
FINISH_TEXT = 'FINISH'; // must be 6 chars as CARS_COUNT
var
cars : array[0..CARS_COUNT-1] of string = ('πŸš—', 'πŸš•', 'πŸš™', 'πŸš“', '🚐', '🏎');
dist : array[0..CARS_COUNT-1] of integer = (0, 0, 0, 0, 0, 0); // distance to finish line
finish: boolean = false;
// multiply operator for string (only on FPC)
operator * (s: string; r: integer) txt: string;
var
i: integer;
begin
txt := '';
if r <= 0 then exit;
for i := 1 to r do txt += s;
end;
procedure prepareRace(const count: integer = 0);
var
i: integer;
begin
write(ansiText('Β« WELCOME TO PASCAL RACING COMPETITION ',tsBold)); Reset;
if count > 0 then writeln('[', count, '] Β»') else writeln('Β»');
// put race flags
writeln(' ', THIN_SPACE, '🏁', THIN_SPACE * LANE_LENGTH, '🏳️');
writeln(THIN_SPACE, 'β”Œ', '─' * LANE_LENGTH, '┐');
// cars start position
StoreCursorPosition;
for i := 0 to CARS_COUNT-1 do
begin
writeln(FINISH_TEXT[i+1], 'β•‘', THIN_SPACE * LANE_LENGTH, 'β”‚', cars[i]);
dist[i] := LANE_LENGTH + 1; // +1 for additional start line char
end;
writeln(THIN_SPACE, 'β””', '─' * LANE_LENGTH, 'β”˜');
// race starter
write('Ready? Press ', ansiText('ENTER', fcLightGreen),
' to start or ', ansiText('^C', fcLightRed), ' to quit!');
readln;
end;
procedure race;
var
i: integer;
min: integer = 999;
winner: integer = -1;
begin
finish := false;
// cars are advancing with random speed
RestoreCursorPosition;
for i := 0 to CARS_COUNT-1 do
begin
ClrLine;
// measure distance to finish line
if not finish then
begin
dist[i] -= random(2) + 1;
if dist[i] <= 0 then dist[i] := 0;
// find the winner car
if dist[i] < min then
begin
min := dist[i];
winner := i;
finish := (min = 0);
end;
end;
// redraw cars position
write(FINISH_TEXT[i+1], 'β•‘', THIN_SPACE * dist[i], cars[i], ' ');
if dist[i] < LANE_LENGTH-2 then
writeln(THIN_SPACE * (LANE_LENGTH - dist[i] - 3), 'β”‚') else writeln;
end;
writeln(THIN_SPACE, 'β””', '─' * LANE_LENGTH, 'β”˜');
// declare winner
ClrLine;
if not finish then write('Currently leading the race is ', cars[winner], ' ')
else write('Winner of the race is ', cars[winner], ' ! Congratulation!');
sleep(FRAME_DELAY);
end;
(* MAIN PROGRAM *)
begin
repeat
ClrScr;
randomize;
prepareRace;
while not finish do race;
finish := false;
sleep(FRAME_DELAY * 10);
until false;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment