一个经典的打飞机游戏(1)Pascal代码
十分经典,有一种街机的感觉
奇葩青年的又一控制台神作。
uses crt;
type list=record
ty,ax:integer;
end;
xy=record
bx,by:integer;
end;
l1=array[1..4,1..5]of char;
l2=array[1..5,1..6]of char;
l3=array[1..8,1..5]of char;
const
my:l1=((‘0‘,‘0‘,‘A‘,‘0‘,‘0‘),
(‘A‘,‘0‘,‘H‘,‘0‘,‘A‘),
(‘b‘,‘H‘,‘H‘,‘H‘,‘d‘),
(‘0‘,‘T‘,‘U‘,‘T‘,‘0‘));
pl:l3=((‘0‘,‘H‘,‘0‘,‘H‘,‘0‘),
(‘p‘,‘H‘,‘I‘,‘H‘,‘q‘),
(‘0‘,‘H‘,‘X‘,‘H‘,‘0‘),
(‘0‘,‘V‘,‘0‘,‘V‘,‘0‘),
(‘0‘,‘H‘,‘H‘,‘H‘,‘0‘),
(‘0‘,‘H‘,‘O‘,‘H‘,‘0‘),
(‘0‘,‘H‘,‘H‘,‘H‘,‘0‘),
(‘0‘,‘0‘,‘V‘,‘0‘,‘0‘));
boss:l2=((‘0‘,‘T‘,‘H‘,‘H‘,‘T‘,‘0‘),
(‘X‘,‘H‘,‘I‘,‘I‘,‘H‘,‘X‘),
(‘X‘,‘H‘,‘X‘,‘X‘,‘H‘,‘X‘),
(‘H‘,‘0‘,‘H‘,‘H‘,‘0‘,‘H‘),
(‘V‘,‘0‘,‘V‘,‘V‘,‘0‘,‘V‘));
var ix,hp,bshp,box,i,j,k,t,sum,every:integer;
enlist:array[1..30]of list;
ch:char;
nowen:array[1..3]of list;
bom,boe:array[1..25]of xy;
procedure myij(x:integer);
begin
for k:=1 to 4 do begin
if x>=2 then gotoxy(x-1,21+k)
else gotoxy(x,21+k);
write(‘ ‘);
for t:=1 to 5 do
if my[k,t]<>‘0‘ then write(my[k,t]) else write(‘ ‘);
if x+4<80 then write(‘ ‘);
end;
end;
procedure now(enemy:integer);
begin
gotoxy(1,1);
delline;
gotoxy(1,1);
insline;
gotoxy(1,1);
textcolor(black);
write(‘ Hp:‘,hp,‘ Remain enemy:‘,enemy,‘ Boss Hp:‘,bshp);
textcolor(red);
end;
function hiten(s:integer):integer;
begin
hiten:=0;
for t:=1 to 3 do
if (bom[s].by<=6)and(bom[s].bx-nowen[t].ax>=0)
and(bom[s].bx-nowen[t].ax<=4)then hiten:=t;
end;
function hitmy(s:integer):boolean;
begin
hitmy:=false;
if (boe[s].by>=21)and(boe[s].bx-ix>=0)
and(boe[s].bx-ix<=4)then hitmy:=true;
end;
function hitbo(s:integer):boolean;
begin
hitbo:=false;
if (bom[s].by<=7)and(bom[s].bx-box>=0)
and(bom[s].bx-box<=5)then hitbo:=true;
end;
procedure clear(s:integer);
begin
for t:=1 to 4 do begin
gotoxy(nowen[s].ax,t+1);
write(‘ ‘);
end;
end;
procedure win;
begin
clrscr;
gotoxy(28,12);
writeln(‘Congratulations,You win!‘);
write(‘ Press Enter to exit...‘);
readln;
halt;
end;
procedure plij(s,x:integer);
begin
for k:=1 to 4 do begin
if x>=2 then gotoxy(x-1,1+k)
else gotoxy(x,1+k);
write(‘ ‘);
for t:=1 to 5 do
if pl[s+k-1,t]<>‘0‘ then write(pl[s+k-1,t]) else write(‘ ‘);
if x+4<80 then write(‘ ‘);
end;
end;
procedure lost;
begin
clrscr;
gotoxy(32,12);
writeln(‘Sorry,You lost!‘);
write(‘ Press Enter to exit...‘);
readln;
halt;
end;
function canmove(x:integer;y:integer):boolean;
begin
canmove:=false;
if ((x>=2)and(y=1))or((x<=71)and(y=2)) then canmove:=true;
end;
function appear(x:integer):byte;
var f:boolean;
begin
repeat
randomize;
t:=random(70)+2;
f:=true;
for k:=1 to 3 do
if (t-nowen[k].ax>=-5)and(t-nowen[k].ax<=5)then f:=false;
until f;
appear:=t;
if enlist[x].ty=1 then plij(1,t)
else plij(5,t);
end;
function enis:integer;
begin
t:=0;
for k:=1 to 3 do
if nowen[k].ty<>-1 then t:=t+1;
enis:=t;
end;
function itmove(s,t:integer):boolean;
begin
itmove:=true;
for k:=1 to 3 do if k<>s then
if ((t=0)and(nowen[s].ax-nowen[k].ax<7))
or ((t=1)and(nowen[k].ax-nowen[s].ax<7)) then itmove:=false;
end;
procedure move(s:integer);
begin
randomize;
t:=random(100) mod 2;
if itmove(s,t) then
if t=0 then begin
if nowen[s].ty=0 then plij(1,nowen[s].ax-1)
else plij(5,nowen[s].ax-1);
nowen[s].ax:=nowen[s].ax-1;
end else if t=1 then begin
if nowen[s].ty=0 then plij(1,nowen[s].ax+1);
if nowen[s].ty=1 then plij(5,nowen[s].ax+1);
nowen[s].ax:=nowen[s].ax+1;
end;
end;
procedure bb(s:integer);
begin
if nowen[s].ty=0 then begin
for k:=1 to 25 do
if boe[k].bx=0 then begin
gotoxy(nowen[s].ax+1,6);
write(‘V‘);
boe[k].bx:=nowen[s].ax+1;
boe[k].by:=6;
break;
end;
for k:=1 to 25 do
if boe[k].bx=0 then begin
gotoxy(nowen[s].ax+3,6);
write(‘V‘);
boe[k].bx:=nowen[s].ax+3;
boe[k].by:=6;
break;
end;
end else begin
for k:=1 to 25 do
if boe[k].bx=0 then begin
gotoxy(nowen[s].ax+2,6);
write(‘V‘);
boe[k].bx:=nowen[s].ax+2;
boe[k].by:=6;
break;
end;
end;
end;
procedure main;
begin
i:=1;
while 30-i+enis>=0 do begin
gotoxy(1,1);
ch:=‘ ‘;
if keypressed then ch:=readkey;
if hp<=0 then lost;
for j:=1 to 25 do
if bom[j].bx<>0 then
if (hiten(j)<>0)or(bom[j].by=2) then begin
if hiten(j)<>0 then begin
clear(hiten(j));
now(30-i+enis);
nowen[hiten(j)].ty:=-1;
nowen[hiten(j)].ax:=0;
end;
gotoxy(bom[j].bx,bom[j].by);
write(‘ ‘);
bom[j].bx:=0;
bom[j].by:=0;
myij(ix);
end else begin
gotoxy(bom[j].bx,bom[j].by);
write(‘ ‘);
bom[j].by:=bom[j].by-1;
gotoxy(bom[j].bx,bom[j].by);
write(‘A‘);
myij(ix);
end;
if sum mod 2=0 then for k:=1 to 25 do
if boe[k].bx<>0 then
if hitmy(k)or(boe[k].by=25) then begin
if hitmy(k) then hp:=hp-1;
gotoxy(boe[k].bx,boe[k].by);
write(‘ ‘);
boe[k].bx:=0;
boe[k].by:=0;
myij(ix);
now(30-i+enis);
end else begin
gotoxy(boe[k].bx,boe[k].by);
write(‘ ‘);
boe[k].by:=boe[k].by+1;
gotoxy(boe[k].bx,boe[k].by);
write(‘V‘);
end;
case ch of
#27:halt;
‘a‘:if canmove(ix-2,1)then begin
ix:=ix-1;
myij(ix);
end;
‘d‘:if canmove(ix-2,2)then begin
ix:=ix+1;
myij(ix);
end;
‘j‘:begin
for j:=1 to 25 do
if bom[j].bx=0 then begin
gotoxy(ix+2,21);
write(‘A‘);
bom[j].bx:=ix+2;
bom[j].by:=21;
break;
end;
end;
end;
delay(every);
sum:=sum+1;
if sum mod (every*(every div 3))=0 then for j:=1 to 3 do
if nowen[j].ty<>-1 then begin
bb(j);
move(j);
sum:=0;
end;
for j:=1 to 3 do
if (nowen[j].ty=-1)and(i<31) then begin
nowen[j].ty:=enlist[i].ty;
nowen[j].ax:=appear(j);
i:=i+1;
break;
end;
end;
win;
end;
procedure welcome;
begin
clrscr;
gotoxy(29,13);
write(‘Press Enter to start:‘);
readln;
writeln(‘ use: a-left f-right j-bomb‘);
write(‘ Please input how fast(suggest 20):‘);
readln(every);
textbackground(green);
clrscr;
gotoxy(29,13);
writeln(‘Now upbilnd enemy list...‘);
for i:=1 to 30 do begin
randomize;
enlist[i].ty:=random(100) mod 2;
end;
for i:=1 to 3 do
with nowen[i] do begin
ty:=-1;
ax:=0;
end;
for i:=1 to 25 do
with bom[i] do begin
bx:=0;
by:=0;
end;
for i:=1 to 25 do
with boe[i] do begin
bx:=0;
by:=0;
end;
delay(500);
clrscr;
textcolor(red);
ix:=38;
hp:=20;
sum:=0;
bshp:=150;
myij(ix);
now(30);
end;
begin
welcome;
main;
end.
Pascal小游戏 打飞机