home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows CE - The Ultimate Companion
/
ROMMAN_CE.iso
/
Files
/
Programming
/
Basice
/
Digger
/
DIGGER09.BAS
next >
Wrap
BASIC Source File
|
1997-12-10
|
19KB
|
781 lines
call clrscr
wait 0.2
dim board%[256] ! the whole 16x16 board status flag
hiscore%=0
no_file%=0
call read_hi
if hiscore%<100 or no_file%=1 then
hiscore%=100
endif
!********** Possible board%[n] **********!
! 0 digged (empty)
! 1 (undigged) sand
! 2 undigged sand with diamond
call init
quit% = 0
fail% = 0
call show_face
loop
call init_level
call init_diamond
call init_magic
call draw_frame
call draw_base
call score
call draw_digger
call draw_predator
loop
k$=key$
k%=asc%(k$,1)
change% = 0
! escape the program !
if k%=27 then
quit% = 1
endif
! move the digger around !
if ((k%=37) and (xx%>1)) then
xx% = xx%-1
change% = 1
endif
if ((k%=38) and (yy%>1)) then
yy% = yy%-1
change% = 1
endif
if ((k%=39) and (xx%<16)) then
xx% = xx%+1
change% = 1
endif
if ((k%=40) and (yy%<16)) then
yy% = yy%+1
change% = 1
endif
if (change%=1) then
moveleft% = moveleft%-1
! if scored and check if this level is finished !
if (board%[(yy%-1)*16+xx%]=2) then
moveleft% = 0
dstep% = 1
score% = score%+20
ate% = ate%+1
call score
if (ate%=dnum%) then
nextlevel% = 1
endif
endif
! digger is qualified for additional move ? !
if (board%[(yy%-1)*16+xx%]=0) then
dstep% = 3
endif
if (board%[(yy%-1)*16+xx%]=1) then
dstep% = 2
endif
! if digger got magic !
if (board%[(yy%-1)*16+xx%]=3) and (magic%=0) then
magic% = 1
magicstep% = 0
endif
! check if magic runs out !
if (magic%=1) then
magicstep% = magicstep%+1
reststep% = 16-level%-magicstep%
if (reststep%>0) then
call curpos(52,17)
print reststep%-1" magic left";
else
magic% = 0
call curpos(52,17)
print " ";
endif
endif
! see if the predator should be moved !
if (moveleft%<1) then
call move_predator
call draw_predator
moveleft% = dstep%
endif
! see if ghost should appear !
if (ghoston%=0) then
! first see sand and diamond left to set probability !
space% = 0
loop for i%=1 to 256
space% = space%+board%[i%]
endloop
call random
if (mod%(rand%, 256+dnum%)>space%) then
ghoston% = 1
ghoststep% = 0
call set_ghost
if (ghoston%=1) then
call draw_ghost
call curpos (43,17)
print "GHOST!";
endif
endif
endif
! move the ghost, check if it should be off !
if (ghoston%=1) then
if (ghoststep%>(dnum%+dnum%/2)) then
! or (xg%<1) or (yg%<1) or (xg%>16) or (yg%>16)) then
ghoston% = 0
call die_ghost
call curpos (43,17)
print " ";
else
call move_ghost
call draw_ghost
ghoststep% = ghoststep%+1
endif
endif
! move the digger !
board%[(yy%-1)*16+xx%]=0
call draw_digger
call curpos (25,17)
print moveleft%" more step(s)";
! check if failed (being caught by predator) !
if ((abs(xc%-xx%)+abs(yc%-yy%))<2) and (magic%=0) then
fail% = 1
endif
! check if got magic bite !
if ((abs(xc%-xx%)+abs(yc%-yy%))<1) and (magic%=1) then
score%=score%+50
call score
magic%=0
call curpos(52,17)
print " ";
oldxc% = 8
oldyc% = 8
xc% = 8
yc% = 8
endif
! check if being caught by ghost !
if (ghoston%=1) and ((abs(xg%-xx%)+abs(yg%-yy%))<2) then
moveleft% = 1
dstep% = 1
score% = score%-10
call score
endif
endif
wait 0.01
while (fail%=0) and (quit%=0) and (nextlevel%=0)
endloop
while (quit%=0) and (fail%=0) and (level%<maxlevel%)
endloop
if (fail%=1) then
call explode(xx%, yy%)
wait 0.5
call draw_grid(xx%, yy%)
wait 0.5
call explode(xx%, yy%)
wait 1.0
endif
print "Q"+chr$(32);
call fillrect(160,40,320,120)
print "Q"+chr$(35);
call line(160,40,160,120)
call line(320,40,320,120)
call line(160,40,320,40)
call line(160,120,320,120)
if (score%>hiscore%) then
call curpos(30, 7)
print "NEW HI-SCORE: "score%;
hiscore%=score%
call save_hi
else
call curpos(32, 7)
print "SCORE:"score%;
endif
call curpos(30, 9)
print "press any key to exit"
loop
k$ = key$
wait 0.1
while len%(k$)=0
endloop
call clrscr
call curpos(0,2)
print"Thanks for playing digger"
print"email: lrong@watfast.uwaterloo.ca"
print""
! end of program !
!----- PROCEDURES -------
procedure show_face
call curpos(0,0)
print "DIGGER V0.9";
call curpos(0, 1)
print " L. Rong ";
call curpos(0,2)
print " 1997";
call curpos(4,4)
print "digger";
call curpos(4,6)
print "predator";
call curpos(4,8)
print "ghost";
call curpos(4,10)
print "diamond";
call curpos(4,12)
print "magic";
call shape_digger(0, 40)
call shape_predator(0,59)
call shape_ghost(0,78)
call shape_diamond(0,97)
call shape_magic(0, 116)
endproc
procedure random
rand% = rand%*25173+13849
rand% = mod%(rand%,65536)
endproc
! move the predator close to digger
procedure move_predator
dx% = xx%-xc%
dy% = yy%-yc%
adx% = abs(dx%)
ady% = abs(dy%)
if (magic%=0) then
if (adx%>ady%) then
step% = dx%/adx%
xc% = xc%+step%
else
step% = dy%/ady%
yc% = yc%+step%
endif
else
if (xc%>xx%) and (xc%<16) then
xc% = xc%+1
else
if (yc%>yy%) and (yc%<16) then
yc% = yc%+1
else
if (xc%<xx%) and (xc%>1) then
xc% = xc%-1
else
if (yc%<yy%) and (yc%>1) then
yc% = yc%-1
else
if (adx%>ady%) then
step% = dx%/adx%
xc% = xc%+step%
else
if (ady%=0) then
step% = 0
else
step% = dy%/ady%
endif
yc% = yc%+step%
endif
endif
endif
endif
endif
endif
endproc
! find the next move for the ghost
procedure move_ghost
mleft% = 0
mright% = 0
mtop% = 0
mdown% = 0
! check left space for digged place !
if (xg%>1) then
if (board%[(yg%-1)*16+xg%-1]=0) then
mleft% = mleft%+20
endif
endif
! check right space for digged place !
if (xg%<16) then
if (board%[(yg%-1)*16+xg%+1]=0) then
mright% = mright%+20
endif
endif
! check top space for digged place !
if (yg%>1) then
if (board%[(yg%-2)*16+xg%]=0) then
mtop% = mtop%+20
endif
endif
! check down space for digged place !
if (yg%<16) then
if (board%[yg%*16+xg%]=0) then
mdown% = mdown%+20
endif
endif
! check ghost's relative position to the digger !
if (xx%>xg%) then
mright% = mright%+(xx%-xg%)
else
mleft% = mleft%+(xg%-xx%)
endif
if (yy%>yg%) then
mdown% = mdown%+(yy%-yg%)
else
mtop% = mtop%+(yg%-yy%)
endif
! make sure ghost not move out of board
if (xg%=1) then mleft% = -400 endif
if (xg%=16) then mright% = -400 endif
if (yg%=1) then mtop% = -400 endif
if (yg%=16) then mdown% = -400 endif
! now select the direction with highest score
oldxg% = xg%
oldyg% = yg%
if (mleft%>=mright%) and (mleft%>=mtop%) and (mleft%>=mdown%) then
xg% = xg%-1
endif
if (mright%>=mleft%) and (mright%>=mtop%) and (mright%>=mdown%) then
xg% = xg%+1
endif
if (mtop%>=mleft%) and (mtop%>=mright%) and (mtop%>=mdown%) then
yg% = yg%-1
endif
if (mdown%>=mleft%) and (mdown%>=mright%) and (mdown%>=mtop%) then
yg% = yg%+1
endif
endproc
! find an initial coordinate of ghost !
procedure set_ghost
call random
m%=mod%(rand%, 256)
pxg% = mod%(m%, 16)+1
pyg% = m%/16+1
if (board%[m%+1]=0 and ((abs(pxg%-xx%)+abs(pyg%-yy%))>1)) then
xg% = pxg%
yg% = pyg%
else
! search for lower empty places !
loop for i%=1 to m%+1
pxg% = mod%(i%-1, 16)+1
pyg% = (i%-1)/16+1
if ((board%[i%]=0) and ((abs(pxg%-xx%)+abs(pyg%-yy%))>1)) then
m1% = i%-1
break
endif
endloop
! search for higher empty places !
loop for i%=m%+1 to 256
pxg% = mod%(i%-1, 16)+1
pyg% = (i%-1)/16+1
if ((board%[i%]=0) and ((abs(pxg%-xx%)+abs(pyg%-yy%))>1)) then
m2% = i%-1
break
endif
endloop
! select one of place
xg1% = mod%(m1%, 16)+1
yg1% = m1%/16+1
xg2% = mod%(m2%, 16)+1
yg2% = m2%/16+1
dis1% = abs(xg1%-xx%)+abs(yg1%-yy%)
dis2% = abs(xg2%-xx%)+abs(yg2%-yy%)
if (dis1%>dis2%) and (board%[m1%+1]=0) and (dis1%>2) then
xg% = xg1%
yg% = yg1%
else
if (dis1%<=dis2%) and (board%[m2%+1]=0) and (dis2%>2) then
xg% = xg2%
yg% = yg2%
else
ghoston% = 0
endif
endif
endif
endproc
procedure draw_frame
print "Q"+chr$(32);
call fillrect(75,4,402,166)
print "Q"+chr$(35);
call line(75,166,402,166)
call line(75,4,402,4)
call line(75,4,75,166)
call line(402,4,402,166)
endproc
procedure draw_base
loop for j%=1 to 16
loop for i%=1 to 16
call draw_grid(i%, j%)
endloop
endloop
endproc
procedure draw_grid(ii%, jj%)
! Draw grid (ii, jj) !
xs% = (ii%-1)*20+80
ys% = (jj%-1)*10+5
! empty places
if (board%[(jj%-1)*16+ii%]=0) then
print "Q"+chr$(32);
call fillrect(xs%,ys%,xs%+20,ys%+10)
endif
! draw sand
if (board%[(jj%-1)*16+ii%]>0) then
print "Q"+chr$(35);
call plot(xs%, ys%+2)
call plot(xs%+5, ys%+2)
call plot(xs%+10, ys%+2)
call plot(xs%+15, ys%+2)
call plot(xs%, ys%+4)
call plot(xs%+5, ys%+7)
call plot(xs%+10, ys%+7)
call plot(xs%+15, ys%+7)
endif
! draw diamond
if (board%[(jj%-1)*16+ii%]=2) then
call draw_diamond(ii%, jj%)
endif
! draw magic
if (board%[(jj%-1)*16+ii%]=3) then
call draw_magic(ii%, jj%)
endif
endproc
! draw diamond at (id, jd) !
procedure draw_diamond(id%, jd%)
xs% = (id%-1)*20+80
ys% = (jd%-1)*10+5
call shape_diamond(xs%, ys%)
endproc
! draw magic at (id, jd) !
procedure draw_magic(id%, jd%)
xs% = (id%-1)*20+80
ys% = (jd%-1)*10+5
call shape_magic(xs%, ys%)
endproc
procedure shape_diamond(xs%, ys%)
print "Q"+chr$(35);
call line(xs%+5, ys%+5, xs%+10, ys%+10)
call line(xs%+15, ys%+5, xs%+10, ys%+10)
call line(xs%+5, ys%+5, xs%+10, ys%)
call line(xs%+15, ys%+5, xs%+10, ys%)
call line(xs%+5, ys%+5, xs%+15, ys%+5)
endproc
procedure shape_magic(xs%, ys%)
print "Q"+chr$(35);
call line(xs%+7, ys%, xs%+13, ys%)
call line(xs%+13, ys%, xs%+11, ys%+3)
call line(xs%+11, ys%+3, xs%+16, ys%+9)
call line(xs%+16, ys%+9, xs%+4, ys%+9)
call line(xs%+4, ys%+9, xs%+9, ys%+3)
call line(xs%+9, ys%+3, xs%+7, ys%)
endproc
procedure draw_ghost
! first let old ghost place filled with sand !
print "Q"+chr$(32);
xs% = (oldxg%-1)*20+80
ys% = (oldyg%-1)*10+5
call fillrect(xs%, ys%, xs%+20, ys%+10)
if board%[(oldyg%-1)*16+oldxg%]=0 then
board%[(oldyg%-1)*16+oldxg%]=1
endif
call draw_grid(oldxg%, oldyg%)
! now at new place draw the ghost
xs% = (xg%-1)*20+80
ys% = (yg%-1)*10+5
call shape_ghost(xs%,ys%)
endproc
procedure shape_ghost(xs%, ys%)
print "Q"+chr$(35);
call line(xs%+7, ys%, xs%+13, ys%)
call line(xs%+13, ys%, xs%+10, ys%+3)
call line(xs%+10, ys%+3, xs%+7, ys%)
call line(xs%+3, ys%+4, xs%+17, ys%+4)
call line(xs%+7, ys%+10, xs%+10, ys%+5)
call line(xs%+13, ys%+10, xs%+10, ys%+5)
endproc
procedure die_ghost
! draw the "white" ghost
print "Q"+chr$(32);
xs% = (xg%-1)*20+80
ys% = (yg%-1)*10+5
call line(xs%+7, ys%, xs%+13, ys%)
call line(xs%+13, ys%, xs%+10, ys%+3)
call line(xs%+10, ys%+3, xs%+7, ys%)
call line(xs%+3, ys%+4, xs%+17, ys%+4)
call line(xs%+7, ys%+10, xs%+10, ys%+5)
call line(xs%+13, ys%+10, xs%+10, ys%+5)
endproc
procedure draw_digger
! first erase old digger at (oldxx, oldyy) !
print "Q"+chr$(32);
xs% = (oldxx%-1)*20+80
ys% = (oldyy%-1)*10+5
call fillrect(xs%, ys%, xs%+20, ys%+10)
! clear (newly digged) the new place at (xx, yy) !
print "Q"+chr$(32);
xs% = (xx%-1)*20+80
ys% = (yy%-1)*10+5
call fillrect(xs%, ys%, xs%+20, ys%+10)
! Draw the digger at (xx, yy) !
xs% = (xx%-1)*20+80
ys% = (yy%-1)*10+5
call shape_digger(xs%,ys%)
! update the digger's "old" coordinate !
oldxx% = xx%
oldyy% = yy%
endproc
procedure shape_digger(xs%, ys%)
print "Q"+chr$(35);
call fillrect(xs%+5, ys%, xs%+15, ys%+5)
call fillrect(xs%, ys%+5, xs%+20, ys%+10)
endproc
procedure draw_predator
! first erase old predator at (oldxc, oldyc) !
xs% = (oldxc%-1)*20+80
ys% = (oldyc%-1)*10+5
print "Q"+chr$(32);
call fillrect(xs%,ys%,xs%+20,ys%+10)
call draw_grid(oldxc%, oldyc%)
! Draw the predator at (xc, yc) !
xs% = (xc%-1)*20+80
ys% = (yc%-1)*10+5
call shape_predator(xs%,ys%)
oldxc% = xc%
oldyc% = yc%
endproc
procedure shape_predator(xs%,ys%)
print "Q"+chr$(35);
call line(xs%+1, ys%+2, xs%+10, ys%+10)
call line(xs%+19, ys%+2, xs%+10, ys%+10)
call line(xs%+1, ys%+7, xs%+10, ys%)
call line(xs%+19, ys%+7, xs%+10, ys%)
call line(xs%+1, ys%+2, xs%+1, ys%+7)
call line(xs%+19, ys%+2, xs%+19, ys%+7)
endproc
procedure explode(hx%, hy%)
xs% = (hx%-1)*20+80
ys% = (hy%-1)*10+5
print "Q"+chr$(32);
call fillrect(xs%,ys%,xs%+20,ys%+10)
print "Q"+chr$(35);
call line(xs%,ys%+5,xs%+8,ys%+5)
call line(xs%,ys%,xs%+8,ys%+4)
call line(xs%+10,ys%,xs%+10,ys%+4)
call line(xs%+20,ys%,xs%+12,ys%+4)
call line(xs%+20,ys%+5,xs%+12,ys%+5)
call line(xs%+20,ys%+10,xs%+12,ys%+6)
call line(xs%+10,ys%+10,xs%+10,ys%+6)
call line(xs%,ys%+10,xs%+8,ys%+6)
endproc
procedure score
print "Q"+chr$(35);
call curpos(2,17)
print" ";
call curpos(2,17)
print"SCORE:"score%;
call curpos (15,17)
print"LEVEL:"level%;
call curpos(68,17)
print"HIGH:"hiscore%;
endproc
procedure init
maxlevel%=10
score%=0
level%=0
dnum%=10
endproc
procedure init_level
dnum%=dnum%+(level%*2)
level%=level%+1
! pseudo random seed by timer
curtime$ = dtime$(0)
rand%=asc%(mid$(curtime$, 19, 1), 1)+level%
! fill the board with sand !
loop for i%=1 to 256
board%[i%] = 1
endloop
! position the digger !
xx% = 16
yy% = 16
oldxx% = 16
oldyy% = 16
! position the predator !
xc% = 8
yc% = 8
oldxc% = 8
oldyc% = 8
! position the ghost !
xg% = 1
yg% = 1
oldxg% = 1
oldyg% = 1
! some initial values !
dstep% = 2
moveleft% = dstep%
nextlevel% = 0
ate% = 0
ghoston% = 0
ghoststep% = 0
magic% = 0
magicstep% = 0
call curpos (43,17)
print " ";
call curpos(52,17)
print " ";
endproc
procedure init_diamond
n%=0
loop
call random
m%=mod%(rand%, 256)+1
if ((board%[m%]=1) and (m%<>120)) then
board%[m%]=2
n%=n%+1
endif
while n%<dnum%
endloop
endproc
procedure init_magic
mnum% = 3-level%/3
if mnum%<1 then
mnum% = 1
endif
n%=0
loop
call random
m%=mod%(rand%, 256)+1
if ((board%[m%]=1) and (m%<>120)) then
board%[m%]=3
n%=n%+1
endif
while n%<mnum%
endloop
endproc
procedure plot(px%,py%) ! Clone of POINT !
print "P"+chr$(shift%(px%,-6)+1)+chr$((px% and 63)+1)+\
chr$(shift%(py%,-6)+1)+chr$((py% and 63)+1);
endproc
procedure read_hi
done%=0
ON "ERROR" CALL ERR_HANDLER
OPEN "\\digger.hsc",1
if(done%) then return endif
INPUT #1,dt#
hiscore%=dt#
close 1
endproc
procedure save_hi
done%=0
ON "ERROR" CALL ERR_HANDLER
CREATE "\\digger.hsc",1
if(done%) then return endif
dt#=hiscore%
print #1,dt#
print #1,"degger hi-score file"
close 1
endproc
PROCEDURE ERR_HANDLER
no_file%=1
done%=1
ENDPROC
! BasiCE library files already included here. Just in case !
procedure clrscr
print "C";
endproc
procedure line( x1%,y1%,x2%,y2%)
print "L";chr$(shift%(x1%,-6)+1);chr$((x1% and 63)+1);\
chr$(shift%(y1%,-6)+1);chr$((y1% and 63)+1);
print chr$(shift%(x2%,-6)+1);chr$((x2% and 63)+1);\
chr$(shift%(y2%,-6)+1);chr$((y2% and 63)+1);
endproc
procedure fillrect( x1%,y1%,x2%,y2%)
print "R";chr$(shift%(x1%,-6)+1);chr$((x1% and 63)+1);\
chr$(shift%(y1%,-6)+1);chr$((y1% and 63)+1);
print chr$(shift%(x2%,-6)+1);chr$((x2% and 63)+1);\
chr$(shift%(y2%,-6)+1);chr$((y2% and 63)+1);
endproc
procedure curpos(c%,r%)
print "G";chr$(c%+1);chr$(r%+1);
endproc