Snake (No Media Required) by Richard Davey10th Aug 2004 20:59
|
---|
Summary This is a complete snake game I wrote in DarkBASIC back in 2001. Ideal for beginners to learn from, it doesn't even need any media - just run it and play. Description This is a complete snake game I wrote in DarkBASIC back in 2001. Ideal for beginners to learn from, it doesn't even need any media - just run it and play. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com ` ------------------------------------------------------------------------- ` Snake! DarkForge FAQ 25/1/2001 ` ------------------------------------------------------------------------- ` Coded for a forum request to show how the game "snake" works. Use cursor ` keys to move the snake, eat the fruit to expand your length. See how long ` you can get! 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,"GAME OVER!" sync until spacekey()=1 end |