program Zombies;
uses crt, math;
const
max_x = 30; // Columns [cite: 30]
max_y = 20; // Rows [cite: 29]
num_zombies = 6; [cite: 57, 74]
num_trees = 25; [cite: 27, 33]
num_pits = 12; [cite: 35]
type
TZombie = record
x, y: integer;
active: boolean;
end;
var
board: array[0..max_x, 0..max_y] of char;
zombies: array[1..num_zombies] of TZombie;
px, py: integer;
i, tx, ty: integer;
ch: char;
gameOver: boolean;
zombiesInPits: integer;
procedure InitializeBoard;
var
cx, cy: integer;
begin
clrscr;
zombiesInPits := 0;
// Initialize board with empty spaces [cite: 49, 53]
for cx := 0 to max_x do
for cy := 0 to max_y do
board[cx, cy] := ' ';
// Place Trees (T) [cite: 7, 80]
for i := 1 to num_trees do
begin
tx := random(max_x);
ty := random(max_y);
board[tx, ty] := 'T';
GotoXY(tx + 1, ty + 1);
textcolor(Green);
write('T');
end;
// Place Pot-holes (O) [cite: 6, 36]
for i := 1 to num_pits do
begin
tx := random(max_x);
ty := random(max_y);
board[tx, ty] := 'O';
GotoXY(tx + 1, ty + 1);
textcolor(Blue);
write('O');
end;
end;
procedure PlaceZombies;
begin
for i := 1 to num_zombies do
begin
// Ensure zombies don't spawn on trees or pits [cite: 37]
repeat
tx := random(max_x);
ty := random(max_y);
until board[tx, ty] = ' ';
zombies[i].x := tx;
zombies[i].y := ty;
zombies[i].active := true;
GotoXY(tx + 1, ty + 1);
textcolor(Red);
write('Z'); // [cite: 40, 68]
end;
end;
begin
randomize; [cite: 16]
repeat
InitializeBoard;
// Starting Player position [cite: 22, 23]
px := 15;
py := 10;
while board[px, py] <> ' ' do
inc(px);
PlaceZombies;
gameOver := false;
while not gameOver do
begin
// Draw Player [cite: 46, 56]
GotoXY(px + 1, py + 1); textcolor(Yellow); write('P');
// Control: 5=Left, 6=Down, 7=Up, 8=Right [cite: 10, 48]
ch := readkey;
GotoXY(px + 1, py + 1); write(' '); // Erase old P [cite: 49]
tx := px; ty := py;
case ch of
'7': dec(ty); // Up
'6': inc(ty); // Down
'5': dec(tx); // Left
'8': inc(tx); // Right
end;
// Check Player Collision [cite: 52, 66]
if (tx >= 0) and (tx <= max_x) and (ty >= 0) and (ty <= max_y) then
begin
if board[tx, ty] = 'O' then
begin
GotoXY(1, 22);
textcolor(LightRed);
write('Fool, you fell down a pit!'); [cite: 89, 91]
gameOver := true;
end
else if board[tx, ty] = ' ' then
begin
px := tx;
py := ty;
end;
end;
// Move Zombies [cite: 11]
for i := 1 to num_zombies do
begin
if zombies[i].active then
begin
GotoXY(zombies[i].x + 1, zombies[i].y + 1); write(' '); // Erase old Z [cite: 63]
// Zombies move toward player
tx := zombies[i].x + Sign(px - zombies[i].x);
ty := zombies[i].y + Sign(py - zombies[i].y);
if board[tx, ty] = 'O' then
begin
zombies[i].active := false;
inc(zombiesInPits);
GotoXY(1, 23); textcolor(LightCyan); write('Whoops goes another zombie'); [cite: 83]
end
else if board[tx, ty] = ' ' then
begin
zombies[i].x := tx;
zombies[i].y := ty;
end;
if zombies[i].active then
begin
GotoXY(zombies[i].x + 1, zombies[i].y + 1); textcolor(Red); write('Z'); [cite: 67]
// Check if zombie caught player [cite: 69]
if (zombies[i].x = px) and (zombies[i].y = py) then
begin
GotoXY(1, 22); textcolor(LightRed); write('The zombies claim another victim'); [cite: 92]
gameOver := true;
end;
end;
end;
end;
// Check for Win condition
if zombiesInPits = num_zombies then
begin
GotoXY(1, 22); textcolor(LightGreen); write('You have escaped the zombies!'); [cite: 77]
gameOver := true;
end;
end;
GotoXY(1, 24); textcolor(White); write('Press any key for a new game...'); [cite: 95]
readkey;
until false;
end.