|
program pac_man;
uses crt,graph,grafpac;
type
casi = (Vuoto,Muro,Cibo,Pac,Gosth);
fantasmi =(man,rosso,rosa,azzurro,arancione);
schermo= array [1..33,0..24] of casi;
direzione= (N,S,E,O,Stop);
posto = record
tipo:fantasmi;
mov:direzione;
scia:casi;
x,y:integer;
end;
var
wait:char;
vite:byte;
t:boolean;
campo : schermo;
mur:text;
movtemp : direzione;
coor:integer;
pospac:posto;
x,y,zx,zz:integer;
l:char;
score:integer;
modo,grafica:smallint;
z:integer;
jonny,pinky,toni,speed:posto;
k:integer;
CONST
Su=72;Zu=80;Sn=75;Dx=77;
Veloc=27000;
MX=33;MY=24;
diff=50;
function sbati(campo:schermo; movimento:direzione;pos:posto):boolean;
var x,y:integer; t:boolean;
begin
t:=false;
x:=pos.x;
y:=pos.y;
case movimento of
S: if campo[x,y+1]=Muro then t:= true;
N: if campo[x,y-1]=Muro then t:= true;
E: if campo[x+1,y]=Muro then t:= true;
O: if campo[x-1,y]=Muro then t:= true;
end;
sbati:=t;
end;
procedure shift(var movimento:direzione; var campo:schermo;var pos:posto;var score:integer);
var x,y,l: integer;
begin
case pospac.mov of
N :l:=1;
S :l:=2;
E :l:=3;
O :l:=4;
end;
x:=pos.x;
y:=pos.y;
case movimento of
N:
begin
if campo[x,y-1]=Cibo then
score:=score+10;
campo[x,y]:=Vuoto;
disegna(3,x,y,l);
campo[x,y-1]:=Pac;
disegna(2,x,y-1,l);
pos.y:=y-1;
end;
S:
begin
if campo[x,y+1]=Cibo then
score:=score+10;
campo[x,y]:=Vuoto;
disegna(3,x,y,0);
campo[x,y+1]:=Pac;
disegna(2,x,y+1,l);
pos.y:=y+1;
end;
O:
begin
if campo[x-1,y]=Cibo then
score:=score+10;
campo[x,y]:=Vuoto;
disegna(3,x,y,0);
campo[x-1,y]:=Pac;
disegna(2,x-1,y,l);
pos.x:=x-1;
end;
E:
begin
if campo[x+1,y]=Cibo then
score:=score+10;
campo[x,y]:=Vuoto;
disegna(3,x,y,0);
campo[x+1,y]:=Pac;
disegna(2,x+1,y,l);
pos.x:=x+1;
end;
end;
end;
function movete:direzione;
var movimento:direzione;
begin
repeat
l:=readkey;
until (ord(upcase(l))=Su) or (ord(upcase(l))=Zu) or (ord(upcase(l))=Sn) or (ord(upcase(l))=Dx);
case ord(upcase(l)) of
Su : movimento:=N;
Zu : movimento:=S;
Sn : movimento:=O;
Dx : movimento:=E;
end;
movete:=movimento;
end;
procedure intrameze(var campo:schermo);
var mur:text;
x,y,i,n:integer;
begin
assign(mur,'mur1.txt');
reset(mur);
while not eof(mur) do
begin
readln(mur,x);
readln(mur,y);
campo[x,y]:=Muro;
end;
end;
procedure shiftjonny(var jonny:posto; var campo:schermo);
var k,x,y,z:integer;
begin
x:=jonny.x;
y:=jonny.y;
case jonny.tipo of
rosso : k:=4;
rosa : k:=13;
azzurro: k:=11;
arancione: k:=12;
end;
case jonny.mov of
E: begin
campo[x,y]:=jonny.scia;
jonny.scia:=campo[x+1,y];
campo[x+1,y]:=Gosth;
jonny.x:=x+1;
disfant(k,x+1,y);
case campo[x,y] of
Vuoto: z:=3;
Cibo: z:=4;
Muro: z:=1;
end;
disegna(z,x,y,0);
end;
O: begin
campo[x,y]:=jonny.scia;
jonny.scia:=campo[x-1,y];
campo[x-1,y]:=Gosth;
jonny.x:=x-1;
disfant(k,x-1,y);
case campo[x,y] of
Vuoto: z:=3;
Cibo: z:=4;
Muro: z:=1;
end;
disegna(z,x,y,0);
end;
N: begin
campo[x,y]:=jonny.scia;
jonny.scia:=campo[x,y-1];
campo[x,y-1]:=Gosth;
jonny.y:=y-1;
disfant(k,x,y-1);
case campo[x,y] of
Vuoto: z:=3;
Cibo: z:=4;
Muro: z:=1;
end;
disegna(z,x,y,0);
end;
S:begin
campo[x,y]:=jonny.scia;
jonny.scia:=campo[x,y+1];
campo[x,y+1]:=Gosth;
jonny.y:=y+1;
disfant(k,x,y+1);
case campo[x,y] of
Vuoto: z:=3;
Cibo: z:=4;
Muro: z:=1;
end;
disegna(z,x,y,0);
end;
end;
end;
function dir_fant(campo:schermo;pospac,jonny:posto):direzione;
var
px,py,x,y,i,k,z:integer;
sit: array [1..4] of direzione;
temp,t:direzione;
collisione,tx,ty:boolean;
begin
px:=pospac.x;py:=pospac.y;
randomize;
x:=jonny.x;
y:=jonny.y;
k:=0;
for i:=1 to 4 do sit[i]:=Stop;
if not(campo[x,y-1]=Muro) then sit[1]:=N;
if not(campo[x,y+1]=Muro) then sit[2]:=S;
if not(campo[x-1,y]=Muro) then sit[3]:=O;
if not(campo[x+1,y]=Muro) then sit[4]:=E;
collisione:=sbati(campo,jonny.mov,jonny);
z:=0;
for i:= 1 to 4 do if sit[i]=Stop then z:=z+1;
if not collisione then
case jonny.mov of
N:sit[2]:=Stop;
S:sit[1]:=Stop;
E:sit[3]:=Stop;
O:sit[4]:=Stop;
end;
if collisione or (z<=1) then
begin
repeat
k:=random(3999);
k:=k div 1000+1;
dir_fant:=sit[k];
until sit[k]<>stop;
if (jonny.tipo=Rosso)or (jonny.tipo=arancione) then
begin
if (px=x)and(y-py<diff)and(y-py>0)and not sbati(campo,N,jonny) then dir_fant:=N;
if (px=x)and(py-y<diff)and(py-y>0)and not sbati(campo,S,jonny) then dir_fant:=S;
if (py=y)and(x-px<diff)and(x-px>0)and not sbati(campo,O,jonny) then dir_fant:=O;
if (py=y)and(px-x<diff)and(px-x>0)and not sbati(campo,E,jonny) then dir_fant:=E;
end;
end
else
dir_fant:=jonny.mov;
end;
function gnam(campo:schermo;pospac:posto):boolean;
begin
gnam:=False;
if not(campo[pospac.x,pospac.y]=Pac) then
gnam:=True;
end;
procedure cuori(vite:byte);
begin
setviewport(552,40,getmaxx-2,100,ClipOn);
clearviewport;
for i:=1 to vite do
begin
setcolor(red);
setfillstyle(1,red);
arc(i*15-2,7,10,190,2);
line(i*15-4,8,i*15,13);
arc(i*15+2,7,350,190,2);
line(i*15+4,8,i*15,13);
floodfill(i*15,10,red);
end;
setviewport(0,0,getmaxx,getmaxy,clipon);
end;
begin {MAIN}
vite:=3;
score:=0;
with jonny do
begin
scia:=Vuoto;
tipo:=rosso;
mov:=E;
x:=2;
y:=22;
end;
with pinky do
begin
scia:=Cibo;
tipo:=rosa;
mov:=E;
x:=2;
y:=2;
end;
with toni do
begin
scia:=Cibo;
tipo:=azzurro;
mov:=O;
x:=27;
y:=2;
end;
with speed do
begin
scia:=Cibo;
tipo:=arancione;
mov:=O;
x:=27;
y:=22;
end;
campo[27,22]:=Gosth;
campo[2,22]:=Gosth;
campo[2,2]:=Gosth;
campo[27,2]:=Gosth;
for x:=1 to MX do
for y:=1 to MY do
if (x=1) or (x=MX-4) or (y=1) or (y=MY) then
campo[x,y]:=Muro
else
campo[x,y]:=Cibo;
modo:=detect;grafica:=detect; initgraph(grafica,modo,'c:\tp\bgi');
setbkcolor(black);
setcolor(green);
setfillstyle(2,black);
rectangle(550,1,638,getmaxy-1);
floodfill(580,20,green);
{ inizio;}
intrameze(campo);
with pospac do
begin
tipo:=Man;
x:=20;
y:=7;
end;
campo[20,7]:=Pac;
for x:=1 to MX-6 do
for y:=1 to MY do
begin
case campo[x,y] of
Muro: coor:=1;
Pac: coor:=2;
Vuoto: coor:=3;
Cibo: coor:=4;
end;
disegna(coor,x,y,0);
end;
jonny.mov:=S;
cuori(vite);
repeat
repeat
movtemp:=pospac.mov;
pospac.mov:=movete;
if sbati(campo,pospac.mov,pospac) then pospac.mov:=movtemp;
repeat
if sbati(campo,pospac.mov,pospac) then pospac.mov:=Stop;
shift(pospac.mov,campo,pospac,score);
shiftjonny(jonny,campo);
shiftjonny(pinky,campo);
shiftjonny(toni,campo);
shiftjonny(speed,campo);
speed.mov:=dir_fant(campo,pospac,speed);
jonny.mov:=dir_fant(campo,pospac,jonny);
pinky.mov:=dir_fant(campo,pospac,pinky);
toni.mov:=dir_fant(campo,pospac,toni);
delay(Veloc);
until gnam(campo,pospac) or keypressed or (score>=3940);
until gnam(campo,pospac)or (score>=3940);
if gnam(campo,pospac) then
begin
vite:=vite-1;
SetViewPort(552,100,637,200,Clipon);
OutTextXY(1,30,'Premi');
OuttextXY(1,50,'Invio');
while ord(wait)<>13 do
read(wait);
ClearViewport;
Setviewport(0,0,getmaxx,getmaxy,clipon);
campo[pospac.x,pospac.y]:=Vuoto;
disegna(3,pospac.x,pospac.y,1);
campo[20,7]:=Pac;
with pospac do
begin
x:=20;
y:=7;
mov:=O;
end;
disegna(2,20,7,3);
cuori(vite);
end;
until (vite=0) or (score>=3940);
if vite=0 then
OutTextXY(555,100,'GAME_OVER')
else
OuttextXY(555,100,'Hai_Vinto');
readln;
closegraph;
clrscr;
write('Il Tuo punteggio Š: ',score);
readln;
end.
|
|