Please do not post the same thing in multiple places.
http://forum.thegamecreators.com/?m=forum_view&t=38627&b=6
You can use code tags in your post. Like this but without the spaces.
[ code ]
some code
[ /code ]
eg
sync rate 0
sync on
hide mouse
` our snake sprite
ink rgb(255,255,255),0
box 1,1,15,15
get image 1,0,0,16,16
` our fruit
ink rgb(255,0,0),0
box 1,1,15,15
get image 2,0,0,16,16
` our border
ink rgb(0,0,255),0
box 1,1,15,15
get image 3,0,0,16,16
` "where is the snake?" array
` because the snake is 16x16 in size the game grid is 40x30
dim grid(40,30)
` add the border to the array
for a=0 to 40
grid(a,0) = 1201
grid(a,29) = 1201
next a
for a=0 to 30
grid(0,a) = 1201
grid(39,a) = 1201
next a
` initial variables
` d = direction (1=up, 2=right,3=down,4=left)
` x and y are starting location for the snake (must be offset of 16)
` eaten is the fruit value (1 = onscreen, 0 = eaten)
` speed is the delay between the snake moving in ms, decrese for speed!
d = 2
x = 320
y = 240
eaten = 1
score = 0
speed = 75
length = 2
placed = 0
t = timer() + speed
set text opaque
ink rgb(255,255,255),0
create bitmap 1,640,480
` the main loop
do
cls 0
if upkey() and d<>3 then d=1
if downkey() and d<>1 then d=3
if leftkey() and d<>2 then d=4
if rightkey() and d<>4 then d=2
` if it's time to redraw the snake then do so here
if timer()>t
if d=1 then dec y,16
if d=2 then inc x,16
if d=3 then inc y,16
if d=4 then dec x,16
if grid(x/16,y/16) > 0 and grid(x/16,y/16) < 1500
goto dead
endif
` have we hit some fruit? If so increase length and score
if grid(x/16,y/16) = 1500
inc length
grid(x/16,y/16) = length
eaten = 1
inc score,25
else
grid(x/16,y/16) = length
endif
t = timer() + speed
` length cascade
for a=0 to 40
for b=0 to 30
if grid(a,b) > 0 and grid(a,b) < 1201
temp = grid(a,b)
dec temp
grid(a,b) = temp
endif
next b
next a
endif
` new fruit (make sure it's not placed on an already occupied location)
if eaten = 1
repeat
fx = 2 + rnd(37)
fy = 2 + rnd(27)
if grid(fx,fy) = 0 then placed = 1
until placed = 1
grid(fx,fy) = 1500
eaten = 0
placed = 0
endif
` draw the game grid - this draws the game screen each sync
for a=0 to 40
for b=0 to 30
tempg = grid(a,b)
if tempg > 0
if tempg = 1500 then paste image 2,a*16,b*16
if tempg = 1201 then paste image 3,a*16,b*16
if tempg < 1201 then paste image 1,a*16,b*16
endif
next b
next a
` un-comment this line to see the debug information instead of the score
` t$ = "X: " + str$(x/16) + " Y: " + str$(y/16) + " FPS: " + str$(screen fps()) + " Length: " + str$(length) + " "
t$ = "SCORE: " + str$(score) + " "
text 0,0,t$
copy bitmap 1,0
sync
loop
dead:
blur bitmap 0,2
set current bitmap 0
repeat
center text 320,240,"haha u lose!"
sync
until spacekey()=1
end
One improvement would be to stop the snake from reversing into its self.
For instance when the snake is traveling to the right and quickly press the up key then the left key you die.

it's cool to hate