写一个BCB版的贪吃蛇源码
时间: 2023-03-15 08:20:52 浏览: 144
贪吃蛇源码
我可以提供一份简单的贪吃蛇源码,它可以在BCB中运行:program snake;
const
Max_X = 10;
Max_Y = 10;type
Position = record
x,y : integer;
end;var
field : array[0..Max_X, 0..Max_Y] of boolean;
snake : array[0..Max_X*Max_Y] of Position;
food : Position;
direction : (left, up, right, down);
length, score : integer;// 初始化地图
procedure InitMap;
var
x,y : integer;
begin
for x:=1 to Max_X do
for y:=1 to Max_Y do
field[x,y] := false;
// 生成食物
repeat
food.x := random(Max_X);
food.y := random(Max_Y);
until field[food.x, food.y] = false;
field[food.x, food.y] := true;
// 初始化蛇
snake[0].x := random(Max_X);
snake[0].y := random(Max_Y);
field[snake[0].x, snake[0].y] := true;
length := 1;
score := 0;
end;// 移动蛇
procedure Move;
var
i : integer;
begin
for i:=length-1 downto 1 do
snake[i] := snake[i-1];
case direction of
left: Dec(snake[0].x);
up: Dec(snake[0].y);
right: Inc(snake[0].x);
down: Inc(snake[0].y);
end;
field[snake[0].x, snake[0].y] := true;
// 检查是否吃到食物
if (snake[0].x = food.x) and (snake[0].y = food.y) then
begin
Inc(length);
Inc(score);
repeat
food.x := random(Max_X);
food.y := random(Max_Y);
until field[food.x, food.y] = false;
field[food.x, food.y] := true;
end;
end;begin
InitMap;
repeat
// 按键控制方向
if KeyPressed then
case ReadKey of
'A','a': direction := left;
'W','w': direction := up;
'D','d': direction := right;
'S','s': direction := down;
end;
// 移动蛇
Move;
// 绘制
// ...
until false;
end.
阅读全文