How To Make Snake Game In Pascal ??

Lazarus IDE For Pascal

 

 

Save This File As

snakegame.lpi

 

 

Code:

 

uses crt;

const
MAX_LENGTH = 10;
SNAKE_LENGTH = 5;
WALL_LENGTH = 3;
START_SPEED = 800;
type
Snake_Part = record
x,y : integer;
path: integer; { 0 – nothing; 1 – up; 2 – down; 3 – left; 4 – right}
end;
Other_Part = record
x,y : integer;
end;
var
snake : array [1..SNAKE_LENGTH] of Snake_Part;
map : array [1..MAX_LENGTH,1..MAX_LENGTH] of integer;
wall : array [1..WALL_LENGTH] of Other_Part;
eate : Other_Part;
YesNo : boolean = false;
sch,sch2,sch3 : integer;
score : longint;
path : char = #0;
q : text;
name : string = ”;
speed : integer = 0;

begin
RandomIze;
{set stats}
{start score}
score := 0;
{/start score}
{start eate}
eate.x := (Random(MAX_LENGTH-1)+1);
eate.y := (Random(MAX_LENGTH-1)+1);
{/start eate}
{start snake}
snake[1].x := 5;
for sch := 1 to SNAKE_LENGTH do begin
snake[sch].y := 4;
if sch > 1 then snake[sch].x := snake[sch-1].x-1;
snake[sch].path := 4;
end;
{/start snake}
{start wall}
wall[1].x := Random(MAX_LENGTH-1)+1;
wall[1].y := Random(MAX_LENGTH-1)+1;
for sch := 2 to WALL_LENGTH do begin
wall[sch].y := wall[1].y;
wall[sch].x := wall[sch-1].x + 1;
end;
{/start wall}
{/set stats}
delay(400);
repeat {start main prog}
{wait for key (readkey)} {OLOLOLOLOLOL PPC *WALL*} {HE TPOrATb!!! PA6OTAET u JIAdHO}
delay(START_SPEED-speed);
if keypressed then path := ReadKey;
if path = ‘w’ then snake[1].path := 1;
if path = ‘s’ then snake[1].path := 2;
if path = ‘a’ then snake[1].path := 3;
if path = ‘d’ then snake[1].path := 4;
if path = #27 then halt;
if path = ‘p’ then begin
repeat
until keypressed;
end;
{/wait for key (readkey)}
{move}
{1}
if snake[1].path = 1 then begin
snake[1].y := snake[1].y – 1;
end;
if snake[1].path = 2 then begin
snake[1].y := snake[1].y + 1;
end;
if snake[1].path = 3 then begin
snake[1].x := snake[1].x -1;
end;
if snake[1].path = 4 then begin
snake[1].x := snake[1].x + 1;
end;
{/1}
{other}
for sch := 2 to SNAKE_LENGTH do begin
if snake[sch].path = 1 then begin
snake[sch].y := snake[sch].y – 1;
end;
if snake[sch].path = 2 then begin
snake[sch].y := snake[sch].y + 1;
end;
if snake[sch].path = 3 then begin
snake[sch].x := snake[sch].x -1;
end;
if snake[sch].path = 4 then begin
snake[sch].x := snake[sch].x + 1;
end;
end;
for sch := SNAKE_LENGTH downto 2 do snake[sch].path := snake[sch-1].path;
{/other}
{speed}
if score >= 100 then speed := 40;
if score >= 200 then speed := 60;
if score >= 300 then speed := 110;
if score >= 500 then speed := 170;
if score >= 660 then speed := 200;
if score >= 740 then speed := 300;
if score >= 1000 then speed := 350;
if score >= 1500 then speed := 400;
{/speed}
{/move}
{pick eate}
if ((snake[1].x = eate.x) and (snake[1].y = eate.y)) then begin
score := score + 20;
eate.x := (Random(MAX_LENGTH-1)+1);
eate.y := (Random(MAX_LENGTH-1)+1);
end;
{/pick eate}
{die}
{FTB}
for sch := 2 to SNAKE_LENGTH do if ((snake[1].x = snake[sch].x) and (snake[1].y = snake[sch].y)) then begin
clrscr;
writeln(‘End of game’);
readln;
Assign(q,’record’); reset(q);
readln(q,sch2);
Close(q);
if score > sch2 then begin
writeln(‘You win! New record! Enter your name:’);
readln(name);
Assign(q,’record’); ReWrite(q);
writeln(q,score);
writeln(q,name);
Close(q);
end;
halt;
end;
{/FTB}
{out of range}
if ( ( (snake[1].x < 1) or (snake[1].x > MAX_LENGTH) ) or ( (snake[1].y < 1) or (snake[1].y > MAX_LENGTH) ) ) then begin
clrscr;
writeln(‘End of game’);
readln;
Assign(q,’record’); reset(q);
readln(q,sch2);
Close(q);
if score > sch2 then begin
writeln(‘You win! New record! Enter your name:’);
readln(name);
Assign(q,’record’); ReWrite(q);
writeln(q,score);
writeln(q,name);
Close(q);
end;
halt;
end;
{/out of range}
{wall}
for sch := 1 to WALL_LENGTH do if ( ((snake[1].x = wall[sch].x) and (snake[1].y = wall[sch].y)) ) then begin
clrscr;
writeln(‘End of game’);
readln;
Assign(q,’record’); reset(q);
readln(q,sch2);
Close(q);
if score > sch2 then begin
writeln(‘You win! New record! Enter your name:’);
readln(name);
Assign(q,’record’); ReWrite(q);
writeln(q,score);
writeln(q,name);
Close(q);
end;
halt;
end;
{/wall}
{/die}
{draw}
begin
clrscr;
{generate matrix} {TyT TO>|<E OCO6O HE JIA3uTb}
for sch := 1 to MAX_LENGTH do begin
for sch2 := 1 to MAX_LENGTH do begin
for sch3 := 1 to SNAKE_LENGTH do if ((snake[sch3].x = sch2) and (snake[sch3].y = sch)) then begin
map[sch,sch2] := 1;
YesNo := true;
end;
if ((eate.x = sch2) and (eate.y = sch)) then begin
map[sch,sch2] := 2;
YesNo := true;
end;
for sch3 := 1 to WALL_LENGTH do if ((wall[sch3].x = sch2) and (wall[sch3].y = sch)) then begin
map[sch,sch2] := 4;
YesNo := true;
end;
if YesNo = false then begin
map[sch,sch2] := 0;
YesNo := false;
end;

end;
end;
{/generate matrix}
{draw matrix}
for sch := 1 to MAX_LENGTH do begin
for sch2 := 1 to MAX_LENGTH do write(map[sch,sch2]);
writeln(”);
end;
writeln(‘Score: ‘,score);
{/draw matrix}
{clean up}
for sch := 1 to MAX_LENGTH do
for sch2 := 1 to MAX_LENGTH do map[sch,sch2] := 0;
{/clean up}
end;
{/draw}
{clean up}
path := #0;
{/clean up}
until false;{end main prog}
end.

renewdev

I Am Developer And Want To Share My Knowledge With The World..:) ;) . I Have Another Social Networking Website (Renewchat) And For Designing Or Developing Of Website Purpose You Can Contact Me On Renew Coder .. :) ;)

2 thoughts on “How To Make Snake Game In Pascal ??

Leave a Reply

Your email address will not be published. Required fields are marked *