Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / 2D Snake game - using blocks not images or sprites

Author
Message
mmouse
1
Years of Service
User Offline
Joined: 6th Nov 2015
Location:
Posted: 29th Dec 2015 21:24
Rem Project: block_eater : Created: Tuesday, December 29, 2015
set display mode 800,600,32 : sync on : set window off : sync rate 40 : randomize timer()
highscore = 0 : score = 0 : maxx = 20 :maxy = 20 : maxsnake = maxx*maxy :highlevel = 0
newsnake = 1 : playgame = 0 :level = 0 : time = 0 : maxtime = 8 : facing = 1 `1 = up , 2 = down, 3 =left, 4 = right
white = rgb(255,255,255) : red = rgb(255,0,0) : green = rgb(0,255,0)
type snakepart
x as integer
y as integer
endtype
dim snake(400) as snakepart
food as snakepart
REM INTRO SCREEN UNTIL 'S' is pressed
while escapekey() = 0
while playgame = 1
if newsnake = 1 then gosub _new_snake
cls
REM DRAW GRIS GROUND SQUARES
ink white,white
for xx = 0 to 21 : line (xx*20),0,(xx*20),420 : next
for yy = 0 to 21 : line 0,yy*20,420,yy*20 : next
ink green,green :box (food.x*20)+2,(food.y*20)+2,((food.x+1)*20)-1,((food.y+1)*20)-1 : REM DRAW FOOD
REM DRAW SNAKE
for i = 0 to level
ink white,white
if i = 0 then ink red,red
box (snake(i).x*20)+2,(snake(i).y*20)+2,((snake(i).x+1)*20)-1,((snake(i).y+1)*20)-1
next
rem move snake foward at given rate
inc time
if time > maxtime
for i = level to 1 step -1 : snake(i).x = snake(i-1).x : snake(i).y = snake(i-1).y : next
select facing
case 1: y = -1 : endcase
case 2: y = 1 : endcase
case 3: x = -1: endcase
case 4: x = 1 : endcase
endselect
time = 0 : snake(0).x = snake(0).x+x : snake(0).y = snake(0).y+y
REM check if head east body (crossing)
for i = 1 to level
if snake(0).x = snake(i).x and snake(0).y = snake(i).y then gosub _new_snake
next
endif
REM Check if head eats food
if snake(0).x+x = food.x and snake(0).y+y = food.y+y
inc level,1
for i =level to 1 step -1
snake(i).x = snake(i-1).x : snake(i).y = snake(i-1).y
next
snake(0).x = food.x : snake(0).y = food.y
gosub _food
inc score, level*(10-maxtime)
if level > highlevel then highlevel = level
if score > highscore then highscore = score
endif
rem reset move forwards to 0
x=0 : y=0
REM MOVE SNAKE / speed
if upkey()=1 and md = 0 and facing <> 2 then facing = 1
if downkey()=1 and md = 0 and facing <> 1 then facing = 2
if leftkey()=1 and md = 0 and facing <> 4 then facing = 3
if rightkey()=1 and md = 0 and facing <> 3 then facing = 4
for i = 1 to 9
if inkey$() = str$(i) then maxtime = i
next
md = UPKEY()+downkey()+leftkey()+rightkey() : inc time
REM SNAKE OFF SCREEN
if snake(0).x < -1 or snake(0).x > 21 or snake(0).y < -1 or snake(0).y > 21 then gosub _new_snake
REM SCREEN OUTPUTS
ink white,white : set text size 32
text 490,0, "HighScore:"+str$(highscore) : text 490,32, "High Level:"+str$(highlevel)
text 490,64,"Score :"+str$(score) : text 490,128,"Next Food :"+str$((level+1)*(10-maxtime))
ink white,white : text 480,256,"Current Speed:"+str$(maxtime)
set text size 14 : text 505,285,"Use numbers to set level" : text 505,305,"Fast<-------------->Slow"
for i = 1 to 9 : ink white,white
if maxtime = i then ink red,red
text 522+(I*15),320,str$(i)
next
sync : endwhile
REM INTRO SCREEN
set text size 64 : ink white,white : text 0,70,"Use arrow keys to control"
text 50,140,"Eat the green block" : text 0,210,"Don't eat your own tail"
text 50,280,"Press 's' to play" : if inkey$() ="s" or inkey$()="S" then playgame = 1
sync
endwhile

REM GOSUBS
_new_snake:
if highscore > 0
ink red,red
set text SIZE 128
text 50,100,"GAME OVER"
sync
WAIT 1000
endif
for i = 0 to 400 : snake(i).x = 0 : snake(i).y = 0 :next
level = 0 : newsnake = 0 : score =0 : snake(0).x = 10 : snake(0).y = 20 :facing = 1
GOSUB _food
return

_food:
food.x = rnd(19)+1 : food.y = rnd(19)+1
for i = 1 to level
if food.x = snake(i).x and snake(i).y = food.y
while food.x = snake(i).x and snake(i).y = food.y
food.x = rnd(19)+1 : food.y = rnd(19)+1
endwhile
endif
next
timings = 150
return

Attachments

Login to view attachments
mmouse
1
Years of Service
User Offline
Joined: 6th Nov 2015
Location:
Posted: 29th Dec 2015 22:01
Amendment to line 68- (X and y > 21 allowed off grid)
if snake(0).x < -1 or snake(0).x > 20 or snake(0).y < -1 or snake(0).y > 20 then gosub _new_snake
LBFN
9
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 31st Dec 2015 02:51 Edited at: 8th Jan 2016 01:32
The game plays pretty well. Nice job.



So many games to code.....so little time.

Login to post a reply

Server time is: 2017-03-24 00:05:04
Your offset time is: 2017-03-24 00:05:04