主题:贪食蛇
一不小心,学编程已经快一年了,唉,老了,思绪万千。
回想起这个下面“这条蛇”,记得当时,好像刚学了编程三节课左右,刚学到条件语句,我就便出了这个东西,同学的羡慕眼光,一个字:爽。。。嘻嘻。不过当时真的很爽,不仅为了同学的羡慕,还有,自己两个多小时的绞尽脑汁终于使这条小蛇会动了,呵呵!!!
============================================================================
好了,废话说多了,进入正题,[b][color=FF0000]希望大家能把自己学编程过程中比较难忘的程序、事件说一说,与大家分享一下。[/color] [/b]
============================================================================
刚学编程时所编写的那“条”贪食蛇:
program L_snack_011;
uses crt;
type
d=array [1..12,1..28] of char;
e=array [1..150] of integer;
var
a:d;
b,c:e;
mark,l1,l2,l3,l4,l5,r1,r2,i,j,f,k,l,m,n:integer;
cc,ch:char;
procedure print(a:d;
i,j:integer);
begin
clrscr;
for i:=1 to 12 do
begin
for j:=1 to 28 do
write(a[i,j]);
writeln;
end;
end;
begin
for i:=2 to 11 do
begin
a[i,1]:='I';
a[i,22]:='I';
a[i,28]:='I';
end;
for j:=1 to 28 do
begin
a[1,j]:='=';
a[12,j]:='=';
end;
for i:=23 to 27 do
a[5,i]:='=';
l:=0;
f:=7;
for j:=8 downto 2 do
begin
l:=l+1;
b[l]:=2;
c[l]:=j;
a[b[l],c[l]]:='*';
end;
randomize;
repeat
r1:=random(19)+2;
r2:=random(9)+2;
for i:=1 to f do
if (r1=c[i]) and (r2=b[i]) then
begin
cc:='c';
break;
end
else cc:='s';
until cc='s';
a[r2,r1]:='$';
print(a,i,j);
a[b[1],c[1]]:=' ';
repeat
until keypressed;
while (a[b[1],c[1]]<>'=') and (a[b[1],c[1]]<>'I') and (a[b[1],c[1]]<>'*') do
begin
for i:=1 to f do
a[b[i],c[i]]:='*';
m:=b[1]-b[2];
n:=c[1]-c[2];
mark:=(f-7)*10;
print(a,i,j);
gotoxy(23,4);
writeln(mark:4);
gotoxy(28,13);
a[b[f],c[f]]:=' ';
b[f+1]:=b[f];
c[f+1]:=c[f];
for i:=f downto 2 do
begin
b[i]:=b[i-1];
c[i]:=c[i-1];
end;
delay(200);
if keypressed then
ch:=readkey;
case ord(ch) of
$48:b[1]:=b[1]-1;
$50:b[1]:=b[1]+1;
$4b:c[1]:=c[1]-1;
$4d:c[1]:=c[1]+1;
else
begin
if m<>0 then if m>0 then b[1]:=b[1]+1
else b[1]:=b[1]-1
else if n>0 then c[1]:=c[1]+1
else c[1]:=c[1]-1;
end;
end;
if (b[1]=b[3]) and (c[1]=c[3]) then
begin
b[1]:=b[2];
c[1]:=c[2];
if m<>0 then if m>0 then b[1]:=b[1]+1
else b[1]:=b[1]-1
else if n>0 then c[1]:=c[1]+1
else c[1]:=c[1]-1;
end;
if a[b[1],c[1]]='$' then
begin
Sound(220); { Beep }
Delay(200); { For 200 ms }
NoSound; { Relief! }
f:=f+1;
repeat
r1:=random(19)+2;
r2:=random(9)+2;
for i:=1 to f do
if (r1=c[i]) and (r2=b[i]) then
begin
cc:='c';
break;
end
else cc:='s';
until cc='s';
a[r2,r1]:='$';
end;
end;
for i:=1 to 3 do
begin
Sound(440); { Beep }
Delay(300); { For 200 ms }
NoSound; { Relief! }
Sound(190); { Beep }
Delay(300); { For 200 ms }
NoSound; { Relief! }
end;
end.
回想起这个下面“这条蛇”,记得当时,好像刚学了编程三节课左右,刚学到条件语句,我就便出了这个东西,同学的羡慕眼光,一个字:爽。。。嘻嘻。不过当时真的很爽,不仅为了同学的羡慕,还有,自己两个多小时的绞尽脑汁终于使这条小蛇会动了,呵呵!!!
============================================================================
好了,废话说多了,进入正题,[b][color=FF0000]希望大家能把自己学编程过程中比较难忘的程序、事件说一说,与大家分享一下。[/color] [/b]
============================================================================
刚学编程时所编写的那“条”贪食蛇:
program L_snack_011;
uses crt;
type
d=array [1..12,1..28] of char;
e=array [1..150] of integer;
var
a:d;
b,c:e;
mark,l1,l2,l3,l4,l5,r1,r2,i,j,f,k,l,m,n:integer;
cc,ch:char;
procedure print(a:d;
i,j:integer);
begin
clrscr;
for i:=1 to 12 do
begin
for j:=1 to 28 do
write(a[i,j]);
writeln;
end;
end;
begin
for i:=2 to 11 do
begin
a[i,1]:='I';
a[i,22]:='I';
a[i,28]:='I';
end;
for j:=1 to 28 do
begin
a[1,j]:='=';
a[12,j]:='=';
end;
for i:=23 to 27 do
a[5,i]:='=';
l:=0;
f:=7;
for j:=8 downto 2 do
begin
l:=l+1;
b[l]:=2;
c[l]:=j;
a[b[l],c[l]]:='*';
end;
randomize;
repeat
r1:=random(19)+2;
r2:=random(9)+2;
for i:=1 to f do
if (r1=c[i]) and (r2=b[i]) then
begin
cc:='c';
break;
end
else cc:='s';
until cc='s';
a[r2,r1]:='$';
print(a,i,j);
a[b[1],c[1]]:=' ';
repeat
until keypressed;
while (a[b[1],c[1]]<>'=') and (a[b[1],c[1]]<>'I') and (a[b[1],c[1]]<>'*') do
begin
for i:=1 to f do
a[b[i],c[i]]:='*';
m:=b[1]-b[2];
n:=c[1]-c[2];
mark:=(f-7)*10;
print(a,i,j);
gotoxy(23,4);
writeln(mark:4);
gotoxy(28,13);
a[b[f],c[f]]:=' ';
b[f+1]:=b[f];
c[f+1]:=c[f];
for i:=f downto 2 do
begin
b[i]:=b[i-1];
c[i]:=c[i-1];
end;
delay(200);
if keypressed then
ch:=readkey;
case ord(ch) of
$48:b[1]:=b[1]-1;
$50:b[1]:=b[1]+1;
$4b:c[1]:=c[1]-1;
$4d:c[1]:=c[1]+1;
else
begin
if m<>0 then if m>0 then b[1]:=b[1]+1
else b[1]:=b[1]-1
else if n>0 then c[1]:=c[1]+1
else c[1]:=c[1]-1;
end;
end;
if (b[1]=b[3]) and (c[1]=c[3]) then
begin
b[1]:=b[2];
c[1]:=c[2];
if m<>0 then if m>0 then b[1]:=b[1]+1
else b[1]:=b[1]-1
else if n>0 then c[1]:=c[1]+1
else c[1]:=c[1]-1;
end;
if a[b[1],c[1]]='$' then
begin
Sound(220); { Beep }
Delay(200); { For 200 ms }
NoSound; { Relief! }
f:=f+1;
repeat
r1:=random(19)+2;
r2:=random(9)+2;
for i:=1 to f do
if (r1=c[i]) and (r2=b[i]) then
begin
cc:='c';
break;
end
else cc:='s';
until cc='s';
a[r2,r1]:='$';
end;
end;
for i:=1 to 3 do
begin
Sound(440); { Beep }
Delay(300); { For 200 ms }
NoSound; { Relief! }
Sound(190); { Beep }
Delay(300); { For 200 ms }
NoSound; { Relief! }
end;
end.