一段未完成的Pascal贪吃蛇
说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽。
uses crt;
const screenwidth=50;
screenheight=24; wallchar=‘#‘; snakechar=‘*‘; ; type point=record x,y:integer; end; var snake:array [0..500] of point; map:array [0..screenwidth,0..screenheight] of 0..2; direct:0..3; score:integer; wallnum,foodnum:integer; procedure copyright; begin gotoxy(55,18); textcolor(yellow); writeln(‘Version a1.0‘); gotoxy(55,19); writeln(‘Coder:RedRooT|R.39‘); gotoxy(55,20); writeln(‘QQ:‘); gotoxy(55,21); writeln(‘E-mail:‘); gotoxy(60,22); writeln(); gotoxy(55,23); writeln( Dep.‘); end; procedure rc; begin gotoxy(1,1); end; function hitself(x,y:integer):boolean; var i:integer;ret:boolean; begin ret:=false; for i:=1 to snake[0].x do if ((snake[i].x=x) and (snake[i].y=y)) then begin ret:=true; exit; end; hitself:=ret; end; function hit:boolean; var t:Point; begin t:=snake[1]; if direct=0 then t.y:=t.y-1; if direct=1 then t.x:=t.x+1; if direct=2 then t.y:=t.y+1; if direct=3 then t.x:=t.x-1; if hitself(t.x,t.y) then hit:=true else if map[t.x,t.y]=2 then hit:=true else hit:=false; end; procedure outputxy(x,y:integer;c:char); begin gotoxy(x,y); write(c); rc; end; procedure drawscreen(diff:integer); var i,j:integer; begin clrscr;textcolor(blue); for i:=1 to screenwidth do for j:=1 to screenheight do map[i,j]:=0; for i:=1 to screenwidth do begin outputxy (i,2,wallchar); outputxy (i,screenheight,wallchar); map[i,2]:=2; map[i,screenheight]:=2; end; for i:=2 to screenheight do begin outputxy (1,i,wallchar); outputxy (screenwidth,i,wallchar); map[1,i]:=2; map[screenwidth,i]:=2; end; copyright; gotoxy (15,1);textcolor(blue); write (‘Greedy Snake Game a,1.0‘); textcolor(blue); gotoxy (57,3); write (‘Score:‘); gotoxy (57,5); write (‘Level:‘,diff,‘/20‘); gotoxy (57,7); write(‘**Game Application**‘); gotoxy(57,8); write(‘Arrow keys --> contral‘); gotoxy(65,9); write(‘P --> pause‘); gotoxy(64,10); write(‘ESC --> exit.‘); rc; end; procedure createfood; var i,j:integer; begin i:=random(screenwidth-1)+1; j:=random(screenheight-2)+2; while ((map[i,j]<>0) or (hitself(i,j))) do begin i:=random(screenwidth-1)+1; j:=random(screenheight-2)+2; end; outputxy (i,j,foodchar); map[i,j]:=1; end; procedure createwall; var p,q:integer; begin p:=random(screenwidth-1)+1; q:=random(screenheight-2)+2; while ((map[p,q]<>0) or (hitself(p,q))) do begin p:=random(screenwidth-1)+1; q:=random(screenheight-2)+2; end; outputxy (p,q,wallchar); map[p,q]:=2; end; procedure initgame(foodnum,wallnum:integer); var i,j:integer; begin snake[0].x:=1; snake[1].x:=screenwidth div 2; snake[1].y:=screenheight div 2; outputxy (snake[1].x,snake[1].y,snakechar); for i:=1 to foodnum do createfood; textcolor(red); for i:=1 to wallnum do createwall; textcolor(green); score:=0; direct:=0; outputxy (65,3,‘0‘); end; procedure die; begin rc; gotoxy(22,13); write(‘Game Over‘); Delay(60000); Delay(60000); Delay(60000); clrscr; gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy (20,12); write(‘Your snake has been dead.Your final score is:‘,score); window(15,19,65,23); gotoxy(1,1); textbackground(black); textcolor(red); clrscr; writeln(‘ Buite by RedRooT|R39‘); writeln(‘ QQ: Email:[email protected]‘); gotoxy(51,3); delay(60000); gotoxy(1,1); rc; clrscr; halt; end; procedure walk(diff:integer); var t:Point; food:boolean; i:integer; begin if hit then die; t:=snake[1]; if direct=0 then t.y:=t.y-1; if direct=1 then t.x:=t.x+1; if direct=2 then t.y:=t.y+1; if direct=3 then t.x:=t.x-1; if map[t.x,t.y]=1 then food:=true else food:=false; if food then snake[0].x:=snake[0].x+1; if (not food) then outputxy (snake[snake[0].x].x,snake[snake[0].x].y,‘ ‘); for i:=snake[0].x downto 2 do snake[i]:=snake[i-1]; snake[1]:=t; outputxy (t.x,t.y,snakechar); if food then begin map[t.x,t.y]:=0; score:=score+10*diff; gotoxy(65,3); write (score); rc; createfood; end; end; var i,diff,speed:integer; key:char; begin clrscr; window(1,1,80,25); textbackground(black); textcolor(blue); gotoxy(28,2);write(‘######################‘); gotoxy(28,3);write(‘# #‘); gotoxy(28,5);write(‘# #‘); gotoxy(28,6);write(‘######################‘); gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy(22,11); write(‘Please input the difficulty (1-20): ‘); readln(diff); while ((diff<1) or (diff>20)) do begin clrscr; gotoxy(28,2);write(‘######################‘); gotoxy(28,3);write(‘# #‘); gotoxy(28,5);write(‘# #‘); gotoxy(28,6);write(‘######################‘); gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy(22,11); write(‘Please input the difficulty (1-20): ‘); readln(diff); end; speed:=50 div trunc(sqrt(diff*2)); foodnum:=1;{22-diff;} wallnum:=diff*4; randomize; drawscreen(diff); initgame(foodnum,wallnum); while (true) do begin if keypressed then key:=readkey; if ord(key)=0 then key:=readkey; delay (speed*1000); if ((key=‘K‘) and (direct<>1)) then direct:=3; if ((key=‘P‘) and (direct<>0)) then direct:=2; if ((key=‘H‘) and (direct<>2)) then direct:=0; if ((key=‘M‘) and (direct<>3)) then direct:=1; if (key=‘p‘) then while (not keypressed) do; if (ord(key)=27) then begin clrscr; halt; end; walk(diff); end; end.