Snake a la Nokia

Previous topic - Next topic

Ian Price

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.

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



[EDIT] Removed initial value from "speed"
[EDIT 2 by Schranz0r] Set Syntax highlighting for your post   :-[
I came. I saw. I played.

fuzzy70

Nice one Ian, I have not played that in an absolute age.

Reminds me of when I used to commute to London on the train & when I liked Nokia's  :D

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

I wrote it for a guy on the Pandora forums, deliberately omitting some features so that he could build on it to make the game his own. He had a semi-working buggy version, but his source was substantial - I knew that it could be done in much less code, so I did it. I've since had a brief look around the net and found lots of source for this game, but all was much larger than I expected and much larger than the one above.
I came. I saw. I played.

bigsofty

Short but still easy to read, well done Ian. :)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

erico

slick code! <3
Even I can understand it :good:

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard