用Delphi写一个控制台魔塔游戏
时间: 2023-12-21 22:05:23 浏览: 129
delphi编写控制台程序
好的,下面是一个简单的控制台魔塔游戏的示例代码:
```delphi
program ConsoleMota;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TMap = array[1..10, 1..10] of Char;
var
Map: TMap;
PlayerX, PlayerY: Integer;
Level: Integer;
procedure InitializeMap;
var
X, Y: Integer;
begin
for Y := 1 to 10 do
for X := 1 to 10 do
Map[X, Y] := ' ';
Map[1, 1] := 'S'; // Start point
Map[10, 10] := 'E'; // End point
Map[3, 4] := 'W'; // Wall
Map[4, 4] := 'W'; // Wall
Map[5, 4] := 'W'; // Wall
Map[6, 4] := 'W'; // Wall
Map[7, 4] := 'W'; // Wall
Map[8, 4] := 'W'; // Wall
Map[9, 4] := 'W'; // Wall
Map[4, 7] := 'W'; // Wall
Map[5, 7] := 'W'; // Wall
Map[6, 7] := 'W'; // Wall
end;
procedure PrintMap;
var
X, Y: Integer;
begin
for Y := 1 to 10 do
begin
for X := 1 to 10 do
begin
if (X = PlayerX) and (Y = PlayerY) then
Write('@')
else
Write(Map[X, Y]);
end;
Writeln;
end;
end;
function CanMoveTo(X, Y: Integer): Boolean;
begin
Result := False;
if (X >= 1) and (X <= 10) and (Y >= 1) and (Y <= 10) then
Result := Map[X, Y] <> 'W';
end;
procedure MovePlayer(DX, DY: Integer);
begin
if CanMoveTo(PlayerX + DX, PlayerY + DY) then
begin
PlayerX := PlayerX + DX;
PlayerY := PlayerY + DY;
end;
end;
procedure PlayGame;
var
Input: Char;
begin
while True do
begin
PrintMap;
Write('> ');
Readln(Input);
case Input of
'w': MovePlayer(0, -1);
'a': MovePlayer(-1, 0);
's': MovePlayer(0, 1);
'd': MovePlayer(1, 0);
end;
if (PlayerX = 10) and (PlayerY = 10) then
begin
Writeln('Congratulations, you have completed level ', Level, '!');
Inc(Level);
InitializeMap;
PlayerX := 1;
PlayerY := 1;
end;
end;
end;
begin
Randomize;
Level := 1;
InitializeMap;
PlayerX := 1;
PlayerY := 1;
PlayGame;
end.
```
这个示例代码实现了一个简单的控制台魔塔游戏,玩家通过控制台输入 w, a, s, d 来移动人物,通过到达终点来完成关卡。每次完成关卡后,会重新初始化地图并进入下一关。
阅读全文