1
Code Snippets / Snake a la Nokia
« on: 2013-May-26 »
Here is a short and simple piece code of that recreates the snake movement in the Nokia Snake mobile phone game.
The code doesn't include collision detection between the snake itself or walls (in fact there aren't any), but it does allow collisions between the snake head and the flashing food. The snake also grows as you eat.
[EDIT] Removed initial value from "speed"
[EDIT 2 by Schranz0r] Set Syntax highlighting for your post
The code doesn't include collision detection between the snake itself or walls (in fact there aren't any), but it does allow collisions between the snake head and the flashing food. The snake also grows as you eat.
Code: GLBasic [Select]
// --------------------------------- //
// Project: snake
// Created by: Ian Price
// Start: Sunday, May 26, 2013
// IDE Version: 10.283
GLOBAL body_x%[], body_y%[], length%=1, fruit_x%, fruit_y, speed%, dir%, score%
DIM body_x[100]
DIM body_y[100]
// Head position
body_x[0]=10
body_y[0]=10
// Tail position
body_x[1]=9
body_y[1]=10
// Create fruit at random position
fruit_x=RND(39)
fruit_y=RND(29)
WHILE TRUE
// Set snake direction
IF KEY(200) THEN dir=4
IF KEY(205) THEN dir=1
IF KEY(208) THEN dir=2
IF KEY(203) THEN dir=3
// Move snake in selected direction
IF dir>0 AND speed=0
// Eat fruit
IF body_x[0]=fruit_x AND body_y[0]=fruit_y
INC score
fruit_x=RND(39)
fruit_y=RND(29)
INC length
ENDIF
// Ensure that all segments follow the head
FOR n=length TO 1 STEP -1
body_x[n]=body_x[n-1]
body_y[n]=body_y[n-1]
NEXT
// Move head segment
IF dir=1 THEN INC body_x[0]
IF dir=2 THEN INC body_y[0]
IF dir=3 THEN DEC body_x[0]
IF dir=4 THEN DEC body_y[0]
ENDIF
// Draw "fruit"
DRAWRECT fruit_x*16,fruit_y*16,16,16,RGB(RND(255),RND(255),RND(255))
// Draw head
DRAWRECT body_x[0]*16,body_y[0]*16,16,16,RGB(255,0,0)
// Draw body segments
FOR n=1 TO length
DRAWRECT body_x[n]*16,body_y[n]*16,15,15,RGB(0,255,0)
NEXT
DEC speed
IF speed<0 THEN speed=5
PRINT "SCORE "+score,10,10
SHOWSCREEN
WEND
// Project: snake
// Created by: Ian Price
// Start: Sunday, May 26, 2013
// IDE Version: 10.283
GLOBAL body_x%[], body_y%[], length%=1, fruit_x%, fruit_y, speed%, dir%, score%
DIM body_x[100]
DIM body_y[100]
// Head position
body_x[0]=10
body_y[0]=10
// Tail position
body_x[1]=9
body_y[1]=10
// Create fruit at random position
fruit_x=RND(39)
fruit_y=RND(29)
WHILE TRUE
// Set snake direction
IF KEY(200) THEN dir=4
IF KEY(205) THEN dir=1
IF KEY(208) THEN dir=2
IF KEY(203) THEN dir=3
// Move snake in selected direction
IF dir>0 AND speed=0
// Eat fruit
IF body_x[0]=fruit_x AND body_y[0]=fruit_y
INC score
fruit_x=RND(39)
fruit_y=RND(29)
INC length
ENDIF
// Ensure that all segments follow the head
FOR n=length TO 1 STEP -1
body_x[n]=body_x[n-1]
body_y[n]=body_y[n-1]
NEXT
// Move head segment
IF dir=1 THEN INC body_x[0]
IF dir=2 THEN INC body_y[0]
IF dir=3 THEN DEC body_x[0]
IF dir=4 THEN DEC body_y[0]
ENDIF
// Draw "fruit"
DRAWRECT fruit_x*16,fruit_y*16,16,16,RGB(RND(255),RND(255),RND(255))
// Draw head
DRAWRECT body_x[0]*16,body_y[0]*16,16,16,RGB(255,0,0)
// Draw body segments
FOR n=1 TO length
DRAWRECT body_x[n]*16,body_y[n]*16,15,15,RGB(0,255,0)
NEXT
DEC speed
IF speed<0 THEN speed=5
PRINT "SCORE "+score,10,10
SHOWSCREEN
WEND
[EDIT] Removed initial value from "speed"
[EDIT 2 by Schranz0r] Set Syntax highlighting for your post






