I made a simple little snake game this afternoon because I was a bit bored. The actual game mechanics seem to work alright, but when you die, the text does not display. Then you press any key to reset the variables and start again, but the box on which the end game message is supposed to display doesn't disappear, and the score box doesn't come back. All in all, everything goes a bit poo.
set display mode 800,600,16
cls
lvlSize=56 : `the size of the level
dim lvlArray(lvlSize,lvlSize,1)
hide mouse
sync on
sync rate 0
randomize timer
gosub setvars
gosub setgraphics
RandomizeItem(sX,sY,lvlSize)
do
if timer()-oldtimer>=tLength
sDX=sTDX : sDY=sTDY
if (sX+sDX)<0 or (sX+sDX)>lvlSize or (sY+sDY)<0 or (sY+sDY)>lvlSize then gosub endvar
if lvlArray((sX+sDX),(sY+sDY),0)=1 then gosub endvar
if lvlArray((sX+sDX),(sY+sDY),0)=2
acqItems=acqItems+1
tlLength=acqItems+2
acqPts=acqPts+lvlArray((sX+sDX),(sY+sDY),1)-3
RandomizeItem(sX,sY,lvlSize)
endif
sX=sX+sDX
sY=sY+sDY
lvlArray(sX,sY,0)=1
lvlArray(sX,sY,1)=timer()
gosub updatepos
oldtimer=timer()
ink rgb(90,0,0),0
box 600,30,749,179
ink rgb(95,0,0),0
box 601,31,748,178
ink rgb(100,0,0),0
box 602,32,747,177
ink rgb(105,0,0),0
box 603,33,746,176
ink rgb(110,0,0),0
box 604,34,745,175
ink rgb(0,0,0),0
set cursor 610,40
print "score: "+STR$(acqPts)
set cursor 610,60
print "captured: "+STR$(acqItems)
set cursor 610,80
print "time (s): "+STR$(INT((timer()-stTime)/1000))
endif
if rightkey()=1 and sDX=0
sTDX=1
sTDY=0
endif
if leftkey()=1 and sDX=0
sTDX=-1
sTDY=0
endif
if upkey()=1 and sDY=0
sTDX=0
sTDY=-1
endif
if downkey()=1 and sDY=0
sTDX=0
sTDY=1
endif
sync
loop
setvars:
tLength=100 : `the time length between each movement
acqItems=0 : `the amount of things you've acquired
acqPts=0 : `your points score
tlLength=2 : `tail length
stTime=timer() : `start time
sX=lvlSize/2
sY=lvlSize/2
sDX=0
sDY=-1
sTDX=0
sTDY=-1
lvlArray(sX,sY,0)=1
lvlArray(sX,sY,1)=timer()
lvlArray(sX,sY+1,0)=1
lvlArray(sX,sY+1,1)=timer()-tLength
lvlArray(sX,sY+2,0)=1
lvlArray(sX,sY+2,1)=timer()-(tLength*2)
return
setgraphics:
create bitmap 1,(lvlsize*10)+31,(lvlsize*10)+31
ink 0,rgb(170,200,80)
cls
box 0,0,10,(lvlsize*10)+30
box 0,0,(lvlsize*10)+30,10
box (lvlsize*10)+20,0,(lvlsize*10)+30,(lvlsize*10)+30
box 0,(lvlsize*10)+20,(lvlsize*10)+30,(lvlsize*10)+30
ink rgb(175,205,85),rgb(170,200,80)
for pos=20 to (lvlsize*10)+10 step 10
line 11,pos,(lvlsize*10)+19,pos
line pos,11,pos,(lvlsize*10)+19
next pos
create bitmap 2,(lvlsize*10)+31,(lvlsize*10)+31
create bitmap 3,9,9
ink rgb(20,20,20),rgb(170,200,80)
box 0,0,8,8
create bitmap 4,9,9
ink rgb(155,0,0),rgb(170,200,80)
box 0,0,8,8
create bitmap 5,9,9
ink rgb(0,155,0),rgb(170,200,80)
box 0,0,8,8
create bitmap 6,9,9
ink rgb(0,0,155),rgb(170,200,80)
box 0,0,8,8
set current bitmap 0
cls rgb(85,0,0)
return
function RandomizeItem(sX,sY,lvlSize)
repeat
itemX=rnd(lvlSize-4)+2
itemY=rnd(lvlSize-4)+2
until ((sX-itemX)^2)+((sY-itemY)^2)>10
lvlArray(itemX,itemY,0)=2
itemColour=rnd(2)+4
lvlArray(itemX,itemY,1)=itemColour
endfunction
updatepos:
copy bitmap 1,2
for x=0 to lvlsize
for y=0 to lvlsize
if lvlArray(x,y,0)=1
if timer()-lvlArray(x,y,1)>=tlLength*tLength then lvlArray(x,y,0)=0 : lvlArray(x,y,1)=0
copy bitmap 3,0,0,8,8,2,x*10+11,y*10+11,x*10+19,y*10+19
endif
if lvlArray(x,y,0)=2
copy bitmap lvlArray(x,y,1),0,0,8,8,2,x*10+11,y*10+11,x*10+19,y*10+19
endif
next y
next x
copy bitmap 2,0
return
endvar:
set current bitmap 0
cls rgb(85,0,0)
MakeRoundedBox(100,100,700,500,60,3,0,0,0,1,170,200,80,0,31,0)
set text size 50
set text font "verdana"
ink rgb(20,20,20),0
center text 400,200,"HAHA"
center text 400,260,"You die"
center text 400,320,"Loser"
set text size 20
center text 400,420,"Press any key to start again"
repeat : sync : until scancode()<>0
cls rgb(85,0,0)
for x=0 to lvlSize
for y=0 to lvlSize
lvlArray(x,y,0)=0
next y
next x
gosub Setvars
RandomizeItem(sX,sY,lvlSize)
cls rgb(85,0,0)
sync on
sync rate 0
sync
return
function MakeRoundedBox(x1,y1,x2,y2,cornrad,lthick,fr,fg,fb,boolbg,br,bg,bb,fbmp,nbmp,tbmp)
lthick#=lthick
bmpwidth=SCREEN WIDTH()
bmpheight=SCREEN HEIGHT()
create bitmap nbmp,bmpwidth,bmpheight
copy bitmap fbmp,nbmp
x1a=x1+cornrad
y1a=y1+cornrad
x2a=x2-cornrad
y2a=y2-cornrad
`background
if boolbg=1
ink rgb(br,bg,bb),0
box x1,y1a,x2,y2a
box x1a,y1,x2a,y2
for x=0 to cornrad
for y=0 to cornrad
if SQRT((x^2)+(y^2))<cornrad
dot x2a+x,y2a+y
dot x1a-x,y1a-y
dot x1a-x,y2a+y
dot x2a+x,y1a-y
endif
next y
next x
endif
`foreground
ink rgb(fr,fg,fb),0
for t=0 to lthick#-1
line x1a,y1+t,x2a,y1+t
line x1+t,y1a,x1+t,y2a
line x1a,y2-t,x2a,y2-t
line x2-t,y1a,x2-t,y2a
next t
for x=0 to cornrad
for y=0 to cornrad
if SQRT((x^2)+(y^2))>cornrad-(lthick#/2) and SQRT((x^2)+(y^2))<cornrad+(lthick#/2)
dot x2a+x,y2a+y
dot x1a-x,y1a-y
dot x1a-x,y2a+y
dot x2a+x,y1a-y
endif
next y
next x
copy bitmap nbmp,tbmp
endfunction
EDIT: By the way I'm pretty sure the problem is in here somewhere:
endvar:
set current bitmap 0
cls rgb(85,0,0)
MakeRoundedBox(100,100,700,500,60,3,0,0,0,1,170,200,80,0,31,0)
set text size 50
set text font "verdana"
ink rgb(20,20,20),0
center text 400,200,"HAHA"
center text 400,260,"You die"
center text 400,320,"Loser"
set text size 20
center text 400,420,"Press any key to start again"
repeat : sync : until scancode()<>0
cls rgb(85,0,0)
for x=0 to lvlSize
for y=0 to lvlSize
lvlArray(x,y,0)=0
next y
next x
gosub Setvars
That would make most sense, seeing as it's where the programme has the problems, and it's also the only bit I really altered before it all started going pearshaped.
EDIT 2: Removed an errant "sync off" command which was from some halbrained earlier attempt to fix this problem. Now the text does display, but it sort of flickers on and off, and the other problems remain.
EDIT 3: Changed the code a little bit more. But I don't think it did any good.
http://jamesmason01.googlepages.com/index.htm
