So, been working on reprogramming a game I did a bit ago. Many of you may know it. It's called SNAKE!!! Looked at a few other codes, seeing how other people do it, and I found an interesting way of doing it, spun it a bit, and wallah! One bug, small one, but one that's bugging me (heh, pun). For some reason if you hold down an aarow key, you can't turn immediatly after releasing it. Here is a commented version of my code...
//----- Paste this code on frame 1 and set scene size to 400x260 and FPS to ~16
createTextField("t", 10, 1, 240, 400, 20); // create a text field to write score and instructions
t.text = "Snake Game - Press SPACE to start";
// Draw background box with border
beginFill(0xeeeeee);
lineStyle(2);
moveTo(1, 1);
lineTo(399, 1);
lineTo(399, 239);
lineTo(1, 239);
endFill();
V = [-1, 0, 1, 0, -1]; // create a weird direction array (pos 0-3 is the direction in x and pos 1-4 is the dir in y when moving left, up, right, down
K = {}; // create an object used as a key listener
K.onKeyDown = function() { // Define a function to run when a key is pressed
c = Key.getCode()-37; // get pressed key
if (c>=0 && c<=3) { // arrow keys pressed
q.unshift(c); // save the key press in the queue
} else if (c == -5) { // space pressed - init game
m = []; // create a 50x30=1500 1d array to store food pos and snake
t.text = ""; // clear the text field
sc = e = w = 0; // Reset score, eat counter, snake counter
r = ww = 1; // current direction (0=left, 1=up, 2=right, 3=down) and erase counter
x = y = 25; // start coordinate
q = []; // a queue to store key presses (so that x number of key presses during one frame will be spread over x number of frames
createEmptyMovieClip("s", 0); // create MC to store the snake and the food MC
B(0xdd0000); // create a red food MC
F(); // place food
B(0x555588); // create a snake of length 1
onEnterFrame = function () { // this is the main function
c = q.pop(); // ...pick the next one in the queue (may be undefined if queue is empty)...
if (c != undefined && c%2 != r%2) { // ...and check that it is not undefined and not a 180 degree turn (annoying to be able to turn into the snake with one key press)
r = c; // change current direction to the new value
}
x += V[r]; // move the snake to a new x position
y -= V[r+1]; // move the snake to a new y position
if (m[x+y*50] == 2) { // check if there is a food block on the new pos
sc += 10; // add 10 to the score
t.text = "Score: "+sc; // write the score in the text field
F(); // place the food on a new pos
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} else if (m[x+y*50] != undefined || x<0 || x==50 || y<0 || y==30) { // check if it is something else (a snake block or outside the map) - GAME OVER
onEnterFrame = null; // quit looping main function
t.text += " GAME OVER!";
return; // exit main function
}
m[x+y*50] = 1; // set current pos as "occupied" by a snake block
B(0x555588);
if (e>0) { // if eating, wait...
e--;
} else { // ...else set map pos to empty and remove last MC
z = s[ww++];
m[z._x/8+50*z._y/8] = undefined;
z.removeMovieClip();
}
}
}
}
Key.addListener(K); // Add the key listener
function B(c) { // create a block with color c
with(s.createEmptyMovieClip(w++, w)) {
beginFill(c);
moveTo(1, 1);
lineTo(8, 1);
lineTo(8, 8 );
lineTo(1, 8 );
endFill();
_x = x*8;
_y = y*8;
}
}
function F() { // place the food on an empty space
do {
j = random(50);
k = random(30);
} while (m[j+50*k] != undefined);
s[0]._x = j*8;
s[0]._y = k*8;
m[j+50*k] = 2;
}