Program Game_Snake;
Uses crt;
const
Max_X = 60;
Max_Y = 25;
Min_X = 1;
Min_Y = 1;
Than =#219;
Dau = #219;
Var
dai,foodx,foody,foodcl,rancl,speed, score, MaxScore: integer;
ranx,rany: array[1..100] of integer;
dir_food,time,t: integer;
f: text;
ReGame: char;
Procedure Draw_Range;
var i: integer;
Begin
TextColor(Green);
for i:= Min_X to Max_X do
begin
Gotoxy(i,Min_Y);
Write(#178);
Gotoxy(i,Max_Y);
Write(#178);
end;
for i:= Min_Y to Max_Y do
begin
Gotoxy(Min_X,i);
Write(#178);
Gotoxy(Max_X,i);
Write(#178);
end;
End;
Procedure Write_Snake;
var i: integer;
Begin
Gotoxy(ranx[dai],rany[dai]);Write(#32);
for i := dai downto 2 do
begin
ranx[i] := ranx[i-1];
rany[i] := rany[i-1];
TextColor(rancl);
gotoxy(ranx[i],rany[i]);
write(than);
end;
TextColor(White);
gotoxy(ranx[1],rany[1]);
write(dau);
{Draw Food}
Textcolor(foodcl);
Gotoxy(foodx,foody);Write(#220);
End;
Procedure Create_Food;
Begin
Randomize;
repeat
foodx := Random(Max_X - 1);
foody := Random(Max_Y - 1);
foodcl := random(10);
Until (foodx > 1) and (foody > 1) and (foodcl > 1);
Textcolor(foodcl);
Gotoxy(foodx,foody);Write(#220);
End;
Function Die: Boolean;
var x,y,i: integer;
Begin
Die := False;
x := ranx[1];
y := rany[1];
if (x > Min_X) and (x < Max_X) and (y > Min_Y) and (y < Max_Y) then
begin
for i := dai downto 4 do
if (x = ranx[i]) and (y = rany[i]) then
begin
Die := True;
break;
end;
end
else Die := True;
End;
Procedure Set_Dir_Food;
Begin
repeat
time := 5 + random(5);
dir_food := random(5);
until (time > 0) and (dir_food > 0);
t:= 0;
End;
Procedure Move_Food;
Begin
t := t+1;
if t mod 3 = 0 then
case dir_food of
1: if foody > Min_Y + 1 then begin
gotoxy(foodx,foody);Write(#32);
foody := foody - 1;
end;
2: if foodx > Min_X + 1 then begin
gotoxy(foodx,foody);Write(#32);
foodx := foodx - 1;
end;
3: if foody < Max_Y - 1 then begin
gotoxy(foodx,foody);Write(#32);
foody := foody + 1;
end;
4: if foodx < Max_X - 1 then begin
gotoxy(foodx,foody);Write(#32);
foodx := foodx + 1;
end;
end;
if t = time then Set_Dir_Food;
End;
Procedure Move;
var k,lk: char;
Begin
Set_Dir_Food;
k := lk;
repeat until Keypressed;
repeat
Move_Food;
if Keypressed then begin lk:= k;k := ReadKey;end;
case k of
#72: rany[1] := rany[1] - 1;
#75: ranx[1] := ranx[1] - 1;
#77: ranx[1] := ranx[1] + 1;
#80: rany[1] := rany[1] + 1;
'p':
begin
TextColor(Red);
repeat
gotoxy(63,10);
Write('Bam p de tiep tuc!');
until readkey = 'p';
Gotoxy(63,10);
Write(' ');
end;
end;
if k = 'p' then k := lk;
if (ranx[1] = foodx) and (rany[1] = foody) then
Begin
dai := dai + 1;
score := score + 1;
rancl := Foodcl;
Create_Food;
TextColor(Cyan);
Gotoxy(63,3);
Write('Diem: ',score);
End;
Write_Snake;
Delay(Speed);
Until (k = #27) or Die;
if k = #27 then ReGame := #27;
End;
Procedure Choose_Level;
var level: integer;
Begin
TextColor(White);
Writeln('Chon che do: ');
Writeln('1. Kho');
Writeln('2. Vua');
Writeln('3. De');
repeat readln(level); until (level >=1) and (level <= 3);
speed := level*50;
clrscr;
TextColor(Cyan);
Gotoxy(63,7);
Write('Muc do: ');
case level of
1: Write('Kho');
2: Write('Vua');
3: Write('De');
end;
End;
Procedure OpenFile;
Begin
reset(f);
read(f,MaxScore);
close(f);
End;
Procedure ReWriteFile;
Begin
if Score > MaxScore then
begin
ReWrite(f);
Write(f,Score);
Close(f);
end;
End;
Procedure Main;
var i: integer;
Begin
Repeat
clrscr;
Assign(f,'MaxScore.txt');
OpenFile;
Choose_Level;
dai := 5;
rancl := Blue;
score := 0;
TextColor(Cyan);
Gotoxy(63,3);
Write('Diem: ',score);
Gotoxy(63,5);
Write('Diem cao nhat:',MaxScore);
for i := 1 to dai do
begin
ranx[i] := i+3;
rany[i] := 5;
TextColor(rancl);
gotoxy(ranx[i],rany[i]);
write(than);
end;
TextColor(White);
gotoXy(ranx[1],rany[1]);Write(dau);
rancl := Blue;
Draw_Range;
Create_Food;
Move;
ReWriteFile;
if ReGame <> #27 then
begin
readln;
clrscr;
TextColor(White);
Write('Ban co muon choi lai khong. Bam "y" neu co.');
repeat until keypressed;
ReGame := readkey;
end;
Until ReGame <> 'y';
End;
BEGIN
clrscr;
Main;
Gotoxy(20,12);
Write('Bam Enter de thoat...');
readln;
END.