Code:
(*
* COLORS:
* White,LightGray,DarkGray,Black
* Brown
* Yellow
* Magenta,LightMagenta
* Green,LightGreen
* Red,LightRed
* Blue,LightBlue
* Cyan,LightCyan
*)
program rpgmap;
uses crt,dos;
type
map_tile = record
tile: char; // The ASCII representation
color: integer; // The Color, just an int. See top for list
move_cost: byte; // The number of turns to move through
end;
// Numpad key:
// "1" "2" "3" "4" "5" "6" "7" "8" "9"
direction = (SouthWest,South,SouthEast,West,none,East,NorthWest,North,NorthEast);
// Roll a die of *size* sides and add the result to *addto*. IE, 1d6+4
function rolldie(size,addto: integer): integer;
begin
rolldie := (1+random(size))+addto;
end;
// Roll dice of count *count* and *size* sides and add the result to *addto*.
// IE, 1d6+4
function rolldice(size,count,addto: integer): integer;
var i: integer;
begin
rolldice := addto;
for i := 1 to count do
rolldice := rolldice+(random(size)+1);
end;
const tiles: array[1..5] of map_tile = (
( tile: '.'; color: Green; move_cost: 1; ), //Grass, easy to move through
( tile: '*'; color: Brown; move_cost: 4; ), //Mud, nasty stuff! Grips the boots!
( tile: 'v'; color: Green; move_cost: 2; ), //Tall grass, hard to see where you're going
( tile: 'T'; color: Green; move_cost: 1; ), //A tall tree, just for astetics
( tile: 'f'; color: Brown; move_cost: 1; ));//A dead tree, just for astetics
//Descriptions for moving through the terrain.
desc: array[1..5] of string = ('You walk through the grass.',
'You slog through the mud.',
'You wander through the tall grass.',
'You pass beneath a green tree.',
'You walk under the dead tree.');
move_speed = 1; // I cannot remember WHY I have this... Maybe you would?
// A simple procedure to draw the tile
procedure drawtile(n: byte; x,y: byte);
begin
gotoXY(X,Y);
TextColor(tiles[n].color);
write(tiles[n].tile);
end;
// Declare the vars
var map: array[1..80,1..19] of byte; // Refrences the type of the terrain.
c,r: integer; // temp value for Column and Row, used in map generation
turn: integer; // Current turn, reference mostly
quit: boolean; // If the player has hit "Q" then this is triggered. Quits.
key: char; // Key that was hit is stored here.
x,y: byte; // Player's current map position
(***************************************************************************************
* This is the toughest function to write in the entire game! This controls movement,
* and even prevents the player from exiting the map area.
*
* This function also draws the maptile where the player is and draws the player.
***************************************************************************************)
procedure moveplayer(dir: direction; var xvar,yvar: byte);
var xmove,ymove: boolean;
begin
xmove := true; // Assume that we can move. We've no data so this is safest.
ymove := true;
if dir <> none then begin // If we're going to move, IE direction hit and not 5 (stationary)
drawtile(map[xvar,yvar],xvar,yvar); // Hide the player! He is no longer here and shouldn't be shown
(*******************************************************************************
* This section of code is essential! It determines if the player can move
* on the x and y axis. If he can't then it is flagged for later.
*******************************************************************************)
if dir in [West,NorthWest,SouthWest] then
if xvar-1 < 1 then xmove := false;
if dir in [East,NorthEast,SouthEast] then
if xvar+1 > 80 then xmove := false;
if dir in [North,NorthWest,NorthEast] then
if yvar-1 < 1 then ymove := false;
if dir in [South,SouthWest,SouthEast] then
if yvar+1 > 19 then ymove := false;
if (xmove) then // if movement is allowed on the X axis then do it based on DIR
case dir of
NorthWest,West,SouthWest: xvar := xvar-1;
NorthEast,East,SouthEast: xvar := xvar+1;
end;
if (ymove) then // if movement is allowed on the Y axis then do it based on DIR
case dir of
NorthWest,North,NorthEast: yvar := yvar-1;
SouthWest,South,SouthEast: yvar := yvar+1;
end;
end;
TextColor(white); // The Player is the WHITE "@" symbol on the screen.
gotoXY(xvar,yvar); // go to the new spot
write('@'); // and draw the player there
end;
begin
turn := 0; // Since we just began the player has just become "consious"
x := rolldie(80,0); // Set X & Y to be on the map. Somewhere, though ;)
y := rolldie(19,0);
for r := 1 to 19 do
for c := 1 to 80 do begin
case rolldie(100,0) of
1..70: map[c,r] := 1;
71..85: map[c,r] := 2;
86..90: map[c,r] := 3; // mud is kinda prevelent
91..100: if rolldie(100,0) >= 15 then map[c,r] := 4 else map[c,r] := 5; // Trees are mainly alive here
else map[c,r] := 1;
end;
drawtile(map[c,r],c,r); // Draw the current tile.
end;
moveplayer(none,x,y); //Draw the player so he's visible, though don't move him!
repeat
key := ReadKey;
case key of
'Q': quit := true;
'1': MovePlayer(SouthWest, x, y); // SW
'2': MovePlayer(South, x, y); // S
'3': MovePlayer(SouthEast, x, y); // SE
'4': MovePlayer(West, x, y); // W
'5': MovePlayer(none, x, y); // NO MOVEMENT
'6': MovePlayer(East, x, y); // E
'7': MovePlayer(NorthWest, x, y); // NW
'8': MovePlayer(North, x, y); // N
'9': MovePlayer(NorthEast, x, y); // NE
end;
turn := turn + tiles[map[x,y]].move_cost; // increment time by transit cost
gotoxy(1,20); // go to the status line
clreol; // Clear previous message
write(desc[map[x,y]]);
gotoxy(75,20);// Go further right
write(turn); // and draw the turn number
until quit = true;
end.
Bookmarks