
通过运用crt单元,可以编出一些简单的游戏。例如贪吃蛇、推箱子、扫雷等。这些都是我编过的游戏下面附上代码。贪吃蛇:program she;uses crt;label 1,2,3;type point=record x,y:1..20;end;type shuzu=array[1..20,1..20] of char;var a:shuzu;s:string;b:array[1..1000]of point;i,j,f,fen:integer; head,tail:0..1001;c:boolean;procedure ran2;var p,q:integer;beginrandomize;p:=random(17)+2;q:=random(17)+2;if a[p,q]=' ' then a[p,q]:='#' else ran2;end;procedure ran;var p,q:integer;beginrandomize;p:=random(17)+2;q:=random(17)+2;if a[p,q]=' ' then a[p,q]:=chr(2) else ran;end;procedure print(x:shuzu);var i,j:1..20;beginfor i:=1 to 20 dofor j:=1 to 20 dobegintextcolor(15);if a[i,j]=chr(2) then textcolor(12);write(a[i,j]);if j=20 then writelnend;writeln('Score:',fen);end;begintextmode(1);cursoroff;3:fillchar(a,sizeof(a),' ');head:=0;tail:=3;fen:=0;f:=4;b[1].x:=2;b[1].y:=2;b[2].x:=2;b[2].y:=3;b[3].x:=2;b[3].y:=4;for i:=1 to 20 dobegina[1,i]:='#';a[i,1]:='#';a[20,i]:='#';a[i,20]:='#';end;a[2,2]:='o';a[2,3]:='o';a[2,4]:='?;ran;1:c:=false;clrscr;print(a);for i:=1 to 300 dobegindelay(1);if (keypressed)and(not(c)) thencase readkey of#72:if (f=3)or(f=4) then begin f:=1;c:=true; end;#80:if (f=3)or(f=4) then begin f:=2;c:=true end;#75:if (f=1)or(f=2) then begin f:=3;c:=true end;#77:if (f=1)or(f=2) then begin f:=4;c:=true end;#27:beginwriteln('Do you want to exit(Y/N)?');repeatreadln(s);if (s='Y')or(s='y') then halt;until (s='N')or(s='n');goto 1;end;end;end;a[b[tail].x,b[tail].y]:='o';case f of1:beginif (a[b[tail].x-1,b[tail].y]='#')or((a[b[tail].x-1,b[tail].y]='o')and(not((b[tail].x-1=b[head mod 1000+1].x)and(b[tail].y=b[head mod 1000+1].y)))) then goto 2;if a[b[tail].x-1,b[tail].y]=chr(2) thenbeginfen:=fen+10;ran;ran2endelsebeginhead:=head mod 1000+1;a[b[head].x,b[head].y]:=' ';end;tail:=tail+1;if tail=1001 thenbegintail:=1;b[1].x:=b[1000].x-1;b[1].y:=b[1000].y;endelsebeginb[tail].x:=b[tail-1].x-1;b[tail].y:=b[tail-1].y;end;end;2:beginif (a[b[tail].x+1,b[tail].y]='#')or((a[b[tail].x+1,b[tail].y]='o')and(not((b[tail].x+1=b[head mod 1000+1].x)and(b[tail].y=b[head mod 1000+1].y)))) then goto 2;if a[b[tail].x+1,b[tail].y]=chr(2) thenbeginfen:=fen+10;ran;ran2endelsebeginhead:=head mod 1000+1;a[b[head].x,b[head].y]:=' ';end;tail:=tail+1;if tail=1001 thenbegintail:=1;b[1].x:=b[1000].x+1;b[1].y:=b[1000].y;endelsebeginb[tail].x:=b[tail-1].x+1;b[tail].y:=b[tail-1].y;end;end;3:beginif (a[b[tail].x,b[tail].y-1]='#')or((a[b[tail].x,b[tail].y-1]='o')and(not((b[tail].x=b[head mod 1000+1].x)and(b[tail].y-1=b[head mod 1000+1].y)))) then goto 2;if a[b[tail].x,b[tail].y-1]=chr(2) thenbeginfen:=fen+10;ran;ran2endelsebeginhead:=head mod 1000+1;a[b[head].x,b[head].y]:=' ';end;tail:=tail+1;if tail=1001 thenbegintail:=1;b[1].x:=b[1000].x;b[1].y:=b[1000].y-1;endelsebeginb[tail].x:=b[tail-1].x;b[tail].y:=b[tail-1].y-1;end;end;4:beginif (a[b[tail].x,b[tail].y+1]='#')or((a[b[tail].x,b[tail].y+1]='o')and(not((b[tail].x=b[head mod 1000+1].x)and(b[tail].y+1=b[head mod 1000+1].y)))) then goto 2;if a[b[tail].x,b[tail].y+1]=chr(2) thenbeginfen:=fen+10;ran;ran2endelsebeginhead:=head mod 1000+1;a[b[head].x,b[head].y]:=' ';end;tail:=tail+1;if tail=1001 thenbegintail:=1;b[1].x:=b[1000].x;b[1].y:=b[1000].y+1;endelsebeginb[tail].x:=b[tail-1].x;b[tail].y:=b[tail-1].y+1;end;end;end;a[b[tail].x,b[tail].y]:='?;goto 1;2:writeln('Game Over!Score:',fen);writeln('Play again(Y/N)?');repeatreadln(s);if (s='Y')or(s='y') then goto 3;if (s='N')or(s='n') then halt;until (s='Y')or(s='y')or(s='N')or(s='n');end.推箱子(主文件):program tuixiang;uses crt,tx,dos;label 1,2,3,4,5;var f:text;n,p,q,i,j:integer;s1,s:string; a:sz1;b:sz2;top:integer;ren:poi;procedure wrong;beginsound(300);delay(100);nosound;end;function over:boolean;var i:integer;beginfor i:=1 to top doif a[b[i].x,b[i].y]<>'? then exit(false);exit(true);end;begintextmode(1);cursoroff;highvideo;window(15,7,30,25);write('Please choose a unit(1~11):');read(n);2:str(n,s1);s:='c:\map'+s1+'.in';print(n,ren,a,b,top);assign(f,s);reset(f);readln(f,i);for j:=1 to i doreadln(f,p,q);readln(f,p,q);close(f);1:case readkey of#72:if (a[ren.x-1,ren.y]=' ')or(a[ren.x-1,ren.y]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x-1,ren.y]:=chr(2); ren.x:=ren.x-1; endelse if a[ren.x-1,ren.y]=chr(233) then if (a[ren.x-2,ren.y]=' ')or(a[ren.x-2,ren.y]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x-1,ren.y]:=chr(2);a[ren.x-2,ren.y]:=chr(233); ren.x:=ren.x-1; end else wrongelse wrong;#80:if (a[ren.x+1,ren.y]=' ')or(a[ren.x+1,ren.y]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x+1,ren.y]:=chr(2); ren.x:=ren.x+1; endelse if a[ren.x+1,ren.y]=chr(233) then if (a[ren.x+2,ren.y]=' ')or(a[ren.x+2,ren.y]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x+1,ren.y]:=chr(2);a[ren.x+2,ren.y]:=chr(233); ren.x:=ren.x+1; end else wrongelse wrong;#75:if (a[ren.x,ren.y-1]=' ')or(a[ren.x,ren.y-1]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x,ren.y-1]:=chr(2); ren.y:=ren.y-1; endelse if a[ren.x,ren.y-1]=chr(233) then if (a[ren.x,ren.y-2]=' ')or(a[ren.x,ren.y-2]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x,ren.y-1]:=chr(2);a[ren.x,ren.y-2]:=chr(233); ren.y:=ren.y-1; end else wrongelse wrong;#77:if (a[ren.x,ren.y+1]=' ')or(a[ren.x,ren.y+1]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x,ren.y+1]:=chr(2); ren.y:=ren.y+1; endelse if a[ren.x,ren.y+1]=chr(233) then if (a[ren.x,ren.y+2]=' ')or(a[ren.x,ren.y+2]='o') then begin if dong(ren.x,ren.y,top,b) then a[ren.x,ren.y]:='o' else a[ren.x,ren.y]:=' '; a[ren.x,ren.y+1]:=chr(2);a[ren.x,ren.y+2]:=chr(233); ren.y:=ren.y+1; end else wrongelse wrong;#27: begin write('Are you sure to exit(Y/N)?'); 4:readln(s1); if (s1='y')or(s1='Y') then begin textmode(lo(lastmode)); halt end else if (s1<>'n')and(s1<>'N') then goto 4; end;else goto 1;end;pr(p,q,top,a,b);if over thenbeginerase(f);if n=11 thenbeginwrite('Congratulations!Play again(Y/N)?');5:readln(s1);if (s1='y')or(s1='Y') thenbeginn:=1;goto 2;endelseif (s1='n')or(s1='N') then haltelse goto 5;end;write('Congratulations!Go to next unit(Y/N)?');3:readln(s1);if (s1='y')or(s1='Y') thenbeginn:=n+1;goto 2;endelseif (s1='n')or(s1='N') then haltelse goto 3;end;goto 1;end.推箱子(附带单元):unit tx;interfaceuses crt;type poi=record x,y:integer; end;type sz1=array[1..50,1..50]of char;type sz2=array[1..10]of poi;function dong(x,y,top:integer;var b:sz2):boolean;procedure print(x:integer;var ren:poi;var a:sz1;var b:sz2;var top:integer);procedure pr(x,y,top:integer;a:sz1;b:sz2);implementationfunction dong(x,y,top:integer;var b:sz2):boolean;var i:integer;beginfor i:=1 to top doif (b[i].x=x)and(b[i].y=y) then exit(true);exit(false);end;procedure print(x:integer;var ren:poi;var a:sz1;var b:sz2;var top:integer);var f:text;s1,s:string;procedure prsc;var i,j,m,n:integer;beginclrscr;assign(f,s);reset(f);readln(f,top);for i:=1 to top doreadln(f,b[i].x,b[i].y);readln(f,m,n);for i:=1 to m dofor j:=1 to n dobegintextcolor(15);if dong(i,j,top,b) then textcolor(12);read(f,a[i,j]);write(a[i,j]);if a[i,j]=chr(2) thenbeginren.x:=i;ren.y:=j;end;if j=n thenbeginreadln(f);writelnend;end;close(f);end;beginstr(x,s1);s:='c:\map'+s1+'.in';assign(f,s);rewrite(f);case x of1:beginwriteln(f,3);writeln(f,5,' ',2);writeln(f,6,' ',2);writeln(f,7,' ',2);writeln(f,9,' ',8);writeln(f,' #####');writeln(f,'#### #');writeln(f,'# # ?#');writeln(f,'# ? #');writeln(f,'#o ####');writeln(f,'#o# ?# ');writeln(f,'#o# # # ');writeln(f,'### # ');writeln(f,' ##### ');end;2:beginwriteln(f,5);writeln(f,6,' ',2);writeln(f,7,' ',2);writeln(f,7,' ',3);writeln(f,7,' ',4);writeln(f,7,' ',5);writeln(f,8,' ',6);writeln(f,' #### ');writeln(f,'## # ');writeln(f,'#?# ');writeln(f,'##?##');writeln(f,'## ?#');writeln(f,'#o? #');writeln(f,'#oo閛# ');writeln(f,'###### ');end;3:beginwriteln(f,3);writeln(f,4,' ',8);writeln(f,5,' ',8);writeln(f,6,' ',8);writeln(f,9,' ',9);writeln(f,'##### ');writeln(f,'# # ');writeln(f,'# 殚# ###');writeln(f,'# ?# #o#');writeln(f,'### ###o#');writeln(f,' ## o#');writeln(f,' # # #');writeln(f,' # ####');writeln(f,' ##### ');end;4:beginwriteln(f,4);writeln(f,3,' ',5);writeln(f,3,' ',6);writeln(f,4,' ',5);writeln(f,4,' ',6);writeln(f,10,' ',7);writeln(f,' #### ');writeln(f,'### ##');writeln(f,'# ?oo#');writeln(f,'# # oo#');writeln(f,'# #?##');writeln(f,'# # #');writeln(f,'# ?#');writeln(f,'## ? #');writeln(f,' # ###');writeln(f,' #### ');end;5:beginwriteln(f,3);writeln(f,5,' ',2);writeln(f,6,' ',2);writeln(f,7,' ',2);writeln(f,8,' ',8);writeln(f,' #### ');writeln(f,' # ### ');writeln(f,' # ? # ');writeln(f,'### # ##');writeln(f,'#o# # #');writeln(f,'#o? # #');writeln(f,'#o ?#');writeln(f,'########');end;6:beginwriteln(f,5);writeln(f,2,' ',7);writeln(f,3,' ',7);writeln(f,4,' ',7);writeln(f,5,' ',7);writeln(f,6,' ',7);writeln(f,10,' ',8);writeln(f,' ###');writeln(f,' #o#');writeln(f,' #####o#');writeln(f,'## ? o#');writeln(f,'# 殚o#');writeln(f,'# ? o#');writeln(f,'### ## #');writeln(f,'# ? #');writeln(f,'# ###');writeln(f,'###### ');end;7:beginwriteln(f,5);writeln(f,2,' ',4);writeln(f,2,' ',5);writeln(f,3,' ',3);writeln(f,3,' ',4);writeln(f,3,' ',5);writeln(f,10,' ',7);writeln(f,' #### ');writeln(f,' ##oo# ');writeln(f,' #ooo# ');writeln(f,'## ?# ');writeln(f,'# ?? ');writeln(f,'# #?##');writeln(f,'# # ?#');writeln(f,'# #');writeln(f,'######');writeln(f,' ### ');end;8:beginwriteln(f,6);writeln(f,5,' ',5);writeln(f,5,' ',6);writeln(f,6,' ',5);writeln(f,6,' ',6);writeln(f,7,' ',5);writeln(f,7,' ',6);writeln(f,11,' ',9);writeln(f,' ####');writeln(f,'###### #');writeln(f,'# ? ?#');writeln(f,'# ## #');writeln(f,'## #oo #');writeln(f,'##?oo?#');writeln(f,'# #oo ##');writeln(f,'# ## #');writeln(f,'# ? ?#');writeln(f,'###### #');writeln(f,' ####');end;9:beginwriteln(f,5);writeln(f,4,' ',5);writeln(f,5,' ',4);writeln(f,5,' ',5);writeln(f,6,' ',4);writeln(f,6,' ',5);writeln(f,8,' ',7);writeln(f,' #### ');writeln(f,' # # ');writeln(f,'### ? ');writeln(f,'# 殚o##');writeln(f,'# 閛o #');writeln(f,'# 閛o #');writeln(f,'# ###');writeln(f,'##### ');end;10:beginwriteln(f,4);writeln(f,5,' ',4);writeln(f,6,' ',4);writeln(f,7,' ',4);writeln(f,8,' ',4);writeln(f,12,' ',6);writeln(f,' #### ');writeln(f,'## ##');writeln(f,'# ? #');writeln(f,'# ?#');writeln(f,'###o #');writeln(f,' #o #');writeln(f,' #o##');writeln(f,'###o #');writeln(f,'# ?#');writeln(f,'# ?#');writeln(f,'# ##');writeln(f,'##### ');end;11:beginwriteln(f,4);writeln(f,5,' ',4);writeln(f,5,' ',5);writeln(f,6,' ',4);writeln(f,6,' ',5);writeln(f,9,' ',7);writeln(f,'##### ');writeln(f,'# ###');writeln(f,'# ? #');writeln(f,'# ? #');writeln(f,'# 閛o #');writeln(f,'###oo #');writeln(f,' ##?#');writeln(f,' # #');writeln(f,' ####');end;end;close(f);prsc;end;procedure pr(x,y,top:integer;a:sz1;b:sz2);var i,j:integer;beginclrscr;for i:=1 to x dofor j:=1 to y dobegintextcolor(15);if dong(i,j,top,b) then textcolor(12);write(a[i,j]);if j=y then writelnend;end;end.扫雷:program saolei;uses crt;label 1,2,3,4;var a,b:array[1..14,1..14]of char;i,j,t,t2,l:integer;s:string;procedure ran;var p:integer;nu:integer;beginrandomize;for i:=1 to t dofor j:=1 to t dobeginb[i,j]:='?;a[i,j]:='?;end;for p:=1 to t2 dobeginrepeati:=random(t)+1;j:=random(t)+1;until (a[i,j]='?)and(not((i=1)and(j=1)));a[i,j]:='';end;for i:=1 to t dofor j:=1 to t doif a[i,j]='? thenbeginnu:=0;if (i>1)and(j>1) then if a[i-1,j-1]='' then inc(nu);if (i>1) then if a[i-1,j]='' then inc(nu);if (i>1)and(j
