`***************************
` Title: 3D Snake
` Author: Joseph Mohan
` Date: 5/11/2002
`***************************
`THINGS TO DO: 1 make snake grow when if eats food (DONE)
` 2 make sure food is not randomly generated where the snake is (DONE)
` 3 add difficulty option (DONE)
` 4 add some media, music, textures ...
` 5 maybe add an intro screen(DONE) and some other features
` 6 maybe add two player option, AI(maybe)
`GOES TO THE CORRESPONDING SUBROUTINE
gosub intro
gosub start_up
gosub constant
gosub make_objects
`***********************************************************
` MAIN LOOP
`***********************************************************
do
`THIS IS TO CONTROL THE SPEED AND HENCE THE DIFFICULTY
if timer() > start_time + (100 - difficulty)
gosub generate_food
gosub info
gosub camera
gosub move_snake
gosub check_gameover
sync
if gameover = 1 then goto game_end
start_time = timer()
endif
loop
`***********************************************************
` SUB-ROUTINES
`***********************************************************
`INTRO
intro:
hide mouse
set text size 14
center text 320,210,"Welcome to 3-D Snake"
center text 320,225,"by Joseph Mohan"
center text 320,240,"Enjoy!"
wait 3000
cls
re_enter:
text 0,25,"(10 -> Hardest, 1 -> Easiest)"
input "Please Pick a Difficulty Level Between 1 and 10 > ",difficulty
if (difficulty > 10) or (difficulty < 1)
cls
goto re_enter
endif
difficulty = difficulty*10
cls
center text 320,240,"Good Luck!"
center text 320,260,"Press Any Key to Begin..."
suspend for key
cls
return
`START UP THE GAME (sync rate, hide mouse, ...)
start_up:
sync on
sync rate 40
randomize timer()
hide mouse
set global collision on
return
`DECLARE CONSTANTS TO BE USED THROUGHOUT THE GAME
constant:
gameover = 0
`START THE SNAKE MOVING IN THE POSITIVE X DIRECTION (right)
xstep = 5
zstep = 0
food = 7000
snake_segments = 10
score = 0
start_time = timer()
`CREATE ARRAY TO HELP WITH MOVING THE SNAKES BODY
dim oldx#(10)
dim oldz#(10)
return
`MAKE ALL OBJECT FOR USE IN THE GAME
make_objects:
`THIS IS JUST A GRID TO HELP WITH COORDINATES, IT WILL BE DELETED IN THE FINAL VERSION
make matrix 1,300,200,60,40
position matrix 1,0,-2.5,0 : `MATRIX Y VALUE IS SET TO -2.5 SO THAT ALL OTHER OBJECT CAN BE SET TO zero
`MAKE HEAD SLIGHTLY SMALLER TO AVOID COLLISION DETECTION PROBLEMS (as soon as you turn the game would end)
make object cube 1,4.99
position object 1,47.5,0,2.5
color object 1,RGB(0,128,0)
`MAKE BODY
for i = 2 to 10
make object cube i,5
position object i,42.5-(i-1)*5,0,2.5
color object i,RGB(0,128,0)
next i
`BOTTOM WALL
make object box 6000,300,5,5
position object 6000,150,0,-2.51
`TOP WALL
make object box 6001,300,5,5
position object 6001,150,0,202.51
`LEFT WALL
make object box 6002,5,5,210
position object 6002,-2.51,0,100
`RIGHT WALL
make object box 6003,5,5,210
position object 6003,302.51,0,100
`COLOR WALLS BLACK
for i = 1 to 4
color object 5999 + i,RGB(0,0,0)
next i
`FOOD
make object sphere food,5
position object food,152.5,0,102.5
color object food,RGB(232,0,0)
return
`HANDLE THE FOOD
generate_food:
if (object position x(1) = object position x(food)) and (object position z(1) = object position z(food))
score = score + difficulty
regen:
x#=rnd(59)
z#=rnd(39)
`CHECK THAT FOOD IS NOT GOING TO BE PLACED WHERE THE SNAKE IS
for i = 1 to snake_segments
if (x# = object position x(i)) and (z# = object position z(i))
goto regen
endif
next i
`IF FOOD POSITION IS OK THEN PLACE NEW FOOD AT x#,0,y#
position object food,(x#*5)+2.5,0,(z#*5)+2.5
`MAKE THE NEXT SNAKE SEGMENT
snake_segments = snake_segments + 1
make object cube snake_segments,5
position object snake_segments, object position x(snake_segments - 1), 0, object position z(snake_segments - 1)
color object snake_segments,RGB(0,128,0)
`MAKE A SPARE ARRAY.....
dim spare_x#(snake_segments)
dim spare_z#(snake_segments)
`......PUT ALL OLD x z POSITIONS INTO THIS ARRAY....
for i = 1 to (snake_segments-1)
spare_x#(i) = oldx#(i)
spare_z#(i) = oldz#(i)
next i
`...FILL IN THE LAST ONE MANUALLY...
spare_x# = object position x(snake_segments)
spare_z# = object position z(snake_segments)
`...DELETE THE TWO PREVIOUS "old" ARRAYS...
undim oldx#(snake_segments - 1)
undim oldz#(snake_segments - 1)
`...CREATE TWO NEW ONES
dim oldx#(snake_segments)
dim oldz#(snake_segments)
`GET RID OF THIS ARRAY BECAUSE WE'LL HAVE TO USE IT AGAIN WHEN THE NEXT FOOD IS EATEN
undim spare_x#(snake_segments)
undim spare_z#(snake_segments)
endif
return
`ANY INFO I MAY WANT TO DISPLAY FOR DEBUGGING PURPOSES
info:
set cursor 0,0
print "score : ",score
s = screen fps()
print s
return
`POSITION CAMERA
camera:
position camera 150,200,100
point camera 150,0,100
return
`MOVE HEAD AND BODY
move_snake:
headx# = object position x(1)
headz# = object position z(1)
if upkey() = 1 and zstep <> -5
zstep = 5
xstep = 0
endif
if downkey() = 1 and zstep <> 5
zstep = -5
xstep = 0
endif
if leftkey() =1 and xstep <> 5
xstep = -5
zstep = 0
endif
if rightkey() = 1 and xstep <> -5
xstep = 5
zstep = 0
endif
`THE NEXT POSITION OF EACH SEGMENT IS THE OLD POSITION OF THE SEGMENT IN FRONT (riddle me this, riddle me that)
for i = 1 to snake_segments
oldx#(i) = object position x(i)
oldz#(i) = object position z(i)
next i
`HEAD HAS TO BE MOVED HERE.....
position object 1,headx#+xstep,0,headz#+zstep
`.....BECAUSE OF THIS LOOP
for i = 2 to snake_segments
position object i,oldx#(i-1),0,oldz#(i-1)
next i
return
`CHECK FOR END OF GAME
check_gameover:
`Has snake hit anything?
hit = object collision(1,0)
if (hit > 0) and (hit < food) then gameover = 1
return
`END GAME
game_end:
do
set text size 20
ink 0,0
center text 320,220,"GAME OVER!"
center text 320,240,"Press Any Key to End"
sync
if scancode() <> 0 then end
loop