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.

Newcomers DBPro Corner / Free 3D Snake Code

Author
Message
jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 7th Nov 2002 22:13
cead mile failte...
here is some code for a 3D Snake Game
ive posted it in the showcase forum but im gonna post
it here aswell so newcomers can use it (im a newcomer to DB myself)
its heavily remarked and (i think)quite well structured so i hope it will be of help

if anyone wants to take the code and improve it or just jazz it up id love to see it, so post it here.


slan agus tabhaire aire duit fein
jomo

jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 7th Nov 2002 22:17
jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 7th Nov 2002 22:19
DONT use the first code snippet
large bits got left out
good job i checked or it would REALLY have confused the hell out of any newcomers...

slan
jomo
anoon
21
Years of Service
User Offline
Joined: 30th Oct 2002
Location: United States
Posted: 8th Nov 2002 12:00
When I try to run it, I get:

"Command out of place in line 156"

jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 8th Nov 2002 21:54
are you using DB Pro or DB? cause i made it in DB
i dont have DB Pro but i understand that DB code will work in DB Pro

i copied and pasted the code from the forum and it checked out ok so i dont know what went wrong...sorry bout that
if anyone else has the same problem, or better still has fixed it, let us know

seems like my attempt to help others failed miserably
lets this be a lesson to everyone DONT HELP ANYINE!

slan
jomo
TGWDNGHN
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 8th Nov 2002 23:45
dang boy you really messed up your code...many stupid accidents you made! you forgot to use the mathamatical operators and so...you really gotta fix the code, unless the forum messed it up (it has happend before)
TGWDNGHN
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 8th Nov 2002 23:48
Oh yea and you forgot to declare arrays and such...I hava fealling you did a good job and all but the frum messed it up...try posting your code (copy and past )again without the [c o d e] [/ c o d e] thingy
jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 9th Nov 2002 16:50
i didnt mess up the code...im new but im not that bad,the code does work... i wouldnt post something that didnt

anyway thank for your advice, here it goes (last try)...

`Start of code
`***************************
` 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
`end of code
TGWDNGHN
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 10th Nov 2002 01:30
don't work...I canfix it....maybe


My DBE has a problem with the intro: label...I don't see anything wrong.

DB, DBE, and DBP have a tendancy to say that there is a problem here but it is acctually over there...



odd...
jomo
21
Years of Service
User Offline
Joined: 31st Oct 2002
Location: Ireland
Posted: 12th Nov 2002 10:34
ok if anyone really wants the code ill email it to you...
just post your address here or if your reluctant to give your out email address you can email me at joseph.mohan2@mail.dcu.ie
and ill send it to you.
thats the best i can do
very sorry about the cock up

slan
jomo
TGWDNGHN
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 13th Nov 2002 00:27
omega478@hotmail.com, your sending the source code AND resources right?
polgames
21
Years of Service
User Offline
Joined: 19th Jan 2003
Location: Italy
Posted: 19th Jan 2003 12:23
ciao ho dato una piccola modifica in Italiano al code!!!!!


aw2004
21
Years of Service
User Offline
Joined: 19th Jan 2003
Location:
Posted: 19th Jan 2003 13:51
input "Please Pick a Difficulty Level Between 1 and 10
> ",difficulty

it doesnt work because you left the carriage return in here. Take it out and it runs fine! very good but you can run into yourself when you press up and down in the same loop. Controls are generally too slow. well done.

Login to post a reply

Server time is: 2024-05-17 09:53:26
Your offset time is: 2024-05-17 09:53:26