This is a snakes game example, but I have problem, when the snake eats the food, the snake grows, but each segment is like 1/4 of the first segment, and its the same image, it overlaps with the original sprite image, how can I make it so that each additional segment ends up at the end of the previous segment? Here is my code:
Set Display Mode 640,480,16
Sync On
Sync Rate 30
`****************************************
`Variables
`****************************************
xPos as integer
yPos as integer
score as integer
segments=1
#constant screen_width=640
#constant screen_height=480
#constant maxsize=35
dim snakelength#(maxsize,2)
`****************************************
`Images
`****************************************
`Make Food Image
load image "imagespill.bmp",2
`Make Snake Image
load image "imagessnake.bmp",1
`Make Background
load image "imagesbackground.bmp",5000
`****************************************
`Main Loop
`****************************************
Sprite 600,rnd(screen_width),rnd(screen_height),2
do
`Paste Background To Avoid Bluescreen
paste image 5000,0,0
`Controls
if upkey()=1
up=1
down=0
right=0
left=0
endif
if downkey()=1
up=0
down=1
right=0
left=0
endif
if rightkey()=1
up=0
down=0
right=1
left=0
endif
if leftkey()=1
up=0
down=0
right=0
left=1
endif
if up=1
snakelength#(1,2)=snakelength#(1,2)-2
else
endif
if down=1
snakelength#(1,2)=snakelength#(1,2)+2
else
endif
if right=1
snakelength#(1,1)=snakelength#(1,1)+2
else
endif
if left=1
snakelength#(1,1)=snakelength#(1,1)-2
else
endif
Sprite 1,snakelength#(1,1),snakelength#(1,2),1
`Add More Segments As Needed
For i=segments to 2 step -1
snakelength#(i,1)=snakelength#(i-1,1)
snakelength#(i,2)=snakelength#(i-1,2)
Sprite i+1,snakelength#(i,1),snakelength#(i,2),1
Next i
`Pill Collision & Placement
if sprite collision(1,600)=1 and segments<=maxsize
score=score+1
segments=segments+1
replace=1
hide sprite 600
endif
replace_P(replace)
replace=0
Sync
loop
function replace_P(replace)
if replace=1
sprite 600,rnd(screen_width),rnd(screen_height),2
show sprite 600
endif
endfunction
