How to start a snake game?

Blitz3D Forums/Blitz3D Beginners Area/How to start a snake game?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

A default game frameworks is a sutch
Graphics 800,600,32,2
SetBuffer BackBuffer()

While Not KeyHit(1)

	Flip

Wend
End


inside you could generate a "meal" by using X=Rnd(10,600) and Y=Rnd(10,400) and then drawing a rect X,Y,10,10

If SERVED = 0
		Z=Rnd(GraphicsWidth()-10)
		C=Rnd(GraphicsHeight()-10)
		SERVED = 1
	EndIf
	
	Rect Z,C,10,10


thats how you generate meals.

Now On to player movement.

first you determine how the snake moves....ther are 4 directions so we assign 4 keys to set 4 directions

If KeyHit(208)
		DIR=1
	ElseIf KeyHit(200)
		DIR=2
	ElseIf KeyHit(205)
		DIR=3
	ElseIf KeyHit(203)
		DIR=4
	EndIf
	
	If DIR=1
		X=X+0
		Y=Y+5
		Rect X,Y,10,10
	ElseIf DIR=2
		X=X+0
		Y=Y-5
		Rect X,Y,10,10
	ElseIf DIR=3
		X=X+5
		Y=Y+0
		Rect X,Y,10,10
	ElseIf DIR=4
		X=X-5
		Y=Y+0
		Rect X,Y,10,10
	EndIf


now we set up collisions & score

If RectsOverlap(X,Y,10,10,Z,C,10,10)
		SERVED=0
		SCORE = SCORE + 10
	EndIf
	
	Text 20,20,SCORE



My Samples entire code :

Graphics 800,600,32,2
SetBuffer BackBuffer()

LENG = 10
DIR  = 1

While Not KeyHit(1)
	Cls
	
	If SERVED = 0
		Z=Rnd(GraphicsWidth()-10)
		C=Rnd(GraphicsHeight()-10)
		SERVED = 1
	EndIf
	
	Rect Z,C,10,10
	
	If KeyHit(208)
		DIR=1
	ElseIf KeyHit(200)
		DIR=2
	ElseIf KeyHit(205)
		DIR=3
	ElseIf KeyHit(203)
		DIR=4
	EndIf
	
	If DIR=1
		X=X+0
		Y=Y+5
		Rect X,Y,10,10
	ElseIf DIR=2
		X=X+0
		Y=Y-5
		Rect X,Y,10,10
	ElseIf DIR=3
		X=X+5
		Y=Y+0
		Rect X,Y,10,10
	ElseIf DIR=4
		X=X-5
		Y=Y+0
		Rect X,Y,10,10
	EndIf
	
	If RectsOverlap(X,Y,10,10,Z,C,10,10)
		SERVED=0
		SCORE = SCORE + 10
	EndIf
	
	Text 20,20,SCORE
	Flip

Wend
End