home *** CD-ROM | disk | FTP | other *** search
- { VIDAL charles ld 217-18 }
-
-
- program SNAKE_GAME;
-
- { the maze is in a text file : - title.txt - maze.txt }
-
- uses crt;
- const nbl=78; { number of line }
- nbc=22; { number of column }
- left=':';
- right='!';
- up='d';
- down='c';
-
- type MAZE=array[1..nbl,1..nbc] of byte;
- SNAKE=array[1..300] of byte;
-
- var x,y,body,i,j,dir,dirx,diry: byte;
- t: MAZE;
- a: char;
- test,byebye: boolean;
- tx,ty:SNAKE;
- f:text;
- line:string;
- name:string;
- s: integer;
-
- PROCEDURE LOAD2; { load title }
- BEGIN
- for i:=1 to nbl do
- for j:=1 to nbc do t[i,j]:=0;
- name:='title.pas';
- assign(f,name);
- reset(f);
- i:=0;
- while NOT eof(f) do
- BEGIN
- i:=i+1;
- readln(f,line);
- for j:=1 to length(line) do
- BEGIN
- if line[j]=' ' then t[j,i]:=0
- else
- t[j,i]:=ord(line[j])-ord('0');
- END;
- END;
- close(f);
- END;
-
- PROCEDURE LOAD; { load maze }
- BEGIN
- name:='maze.pas';
- assign(f,name);
- reset(f);
- i:=0;
- while NOT eof(f) do
- BEGIN
- i:=i+1;
- readln(f,line);
- for j:=1 to length(line) do
- BEGIN
- if line[j]=' ' then t[j,i]:=0
- else
- t[j,i]:=ord(line[j])-ord('0');
- END;
- END;
- close(f);
- END;
-
-
- PROCEDURE DISPLAY_MAZE;
- BEGIN
-
- for i:=1 to nbl do
- BEGIN
- for j:=1 to nbc do
- BEGIN
- GOTOXY(i,j);
- CASE t[i,j] of
- 0: write(' '); { empty place }
- 3: BEGIN
- textcolor(2); { color:green }
- write(chr(235)); { apples }
- END;
- 2:BEGIN
- textcolor(8); { color: gray }
- write(chr(219)); { walls }
- END;
- 1: BEGIN
- textcolor(4); { color red }
- write('@'); { snake's head }
- END;
- END;
- END;
- END;
- END;
-
- PROCEDURE DISPLAY_SNAKE;
- BEGIN
- gotoxy(2,24);write(body-1);
- gotoxy(tx[body],ty[body]);
- write(' '); { erase then end of then snake}
- gotoxy(tx[1],ty[1]);
- textcolor(4);
- write('@'); { display snake's head }
- gotoxy(tx[2],ty[2]);
- textcolor(4);
- write('0'); {display the begining of the }
- END; { snake }
-
- FUNCTION COLLISION(x,y:byte;tx,ty:snake):boolean;
- BEGIN
- CASE dir OF
- 0: tX[1]:=tX[1]+1;
- 1: tY[1]:=tY[1]+1;
- 2: tX[1]:=tX[1]-1;
- 3: tY[1]:=tY[1]-1;
- END;
- if (t[tx[1],ty[1]]<>0) and (t[tx[1],ty[1]]<>3) then collision:=true
- else collision:=false;
- END;
-
- PROCEDURE MEET(var x,y:byte);
- BEGIN
- if dir=3 then dir:=0 else dir:=dir+1;
- if collision(x,y,tx,ty) then
- BEGIN
- CASE dir of
- 0: dir:=2;
- 1: dir:=3;
- 2: dir:=0;
- 3: dir:=1;
- END;
- if collision(x,y,tx,ty) then test:=false
- else BEGIN
- x:=tx[1];y:=ty[1];
- END;
- END
- else BEGIN
- x:=tx[1];y:=ty[1];
- END;
- END;
-
- PROCEDURE DIRECTION(var dir:byte);
- var d: byte;
- BEGIN
- CASE a OF
- right: d:=0; { change direction }
- left: d:=2;
- up: d:=3;
- down: d:=1;
- else d:=dir;
- END;
- if (d<>dir+2) and (d<>dir-2) then dir:=d;
- END;
-
- PROCEDURE CHANGE_ARRAY(var t: MAZE);
- BEGIN
- CASE dir OF
- 0: X:=X+1;
- 1: Y:=Y+1;
- 2: X:=X-1;
- 3: Y:=Y-1;
- END;
- if (t[x,y]<>0) and (t[x,y]<>3) then BEGIN
- MEET(x,y);
- if not test then
- BEGIN
- byebye:=true; { you lost }
- exit;
- END;
- END
- else
-
- if (t[x,y]=3) and (body<300) then BEGIN
- body:=body+1; {body is growing }
- s:=0;
- repeat
- s:=s+1;
- sound(s*2+1000); { goulpssss!!!}
- until s=1000;
- nosound;
- END;
- for i:=body downto 2 do BEGIN
- tx[i]:=tx[i-1];
- ty[i]:=ty[i-1];
- END;
- tx[1]:=x;
- ty[1]:=y;
- t[tx[body],ty[body]]:=0;
- t[x,y]:=1;
- END;
-
- PROCEDURE INTRODUCTION;
- BEGIN
- clrscr; { clear screen }
- LOAD2;
- x:=17;
- y:=12;
- t[x,y]:=1;
- a:=' ';
- dir:=2;
- test:=true;
- body:=4;
- tx[1]:=x;ty[1]:=y;tx[2]:=x;ty[2]:=y-1;tx[3]:=x;ty[3]:=y-2;
- DISPLAY_MAZE;
- gotoxy(25,21);
- write(' PRESS RETURN TO PLAY ');
- gotoxy(18,23);
- write('gauche=: droite=! haut=d bas=c fin=q');
-
- while not keypressed do
- BEGIN
- delay(50);
- CHANGE_ARRAY(t);
- DISPLAY_SNAKE;
- END;
- END;
-
-
- BEGIN { program's spine }
- INTRODUCTION;
- repeat
- byebye:=false;
- clrscr; {clear screen }
- LOAD;
- x:=10;
- y:=10;
- t[x,y]:=1;
- a:=' ';
- dir:=2;
- test:=true;
- body:=4;
- tx[1]:=x;ty[1]:=y;tx[2]:=x;ty[2]:=y-1;tx[3]:=x;ty[3]:=y-2;
- DISPLAY_MAZE;
- while (a<>'Q') and (not byebye) do
- BEGIN
- CHANGE_ARRAY(t);
- DISPLAY_SNAKE;
- i:=1;
- repeat
- i:=i+1;
- j:=1;
- repeat
- j:=j+1;
- if keypressed then
- BEGIN
- a:=readkey;
- DIRECTION(dir);
- i:=255;
- END;
- until (j=10) or (a='Q') or (a='q') or (byebye);
- until (i=255) or (a='Q') or (a='q') or (byebye);
- END;
- clrscr;
- gotoxy(35,15);
- write('finish Yes or No :');
- a:=readkey;
- write(a);
- until (a='Y') or (a='y');
- readln;
- END.
-