To the main page
Version 2.1
- Fixed a bug: If you held down an arrow key you couldn't turn immediately after you released it
//--- Flash MX Snake Game 1Kb by Strille. Version 2.1, 750 bytes
//--- Paste this code on frame 1 and set scene size to 512x280 and Frame Rate to 16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 1, 1, 255, 511, 32); // create a text field to write score and instructions
t.text = "Snake Game - Press SPACE"; // show start text
beginFill(0xeeeeee); lineStyle(1); lineTo(511, 0); lineTo(511, 256); lineTo(0, 256); endFill(); // draw background with border
Key.addListener(t); // use an existing object as key listener (we don't waste bytes by creating a new object)
t.onKeyDown = function() { // define an anonymous method to execute when a key is pressed
c = Key.getCode()-37; // get key code (c is a variable used "locally" several times)
if (!(c>>2)) { // arrow keys pressed (c = 0, 1, 2 or 3)
if (c != q[0]) // only add to the queue if it is a new direction
q.unshift(c);
return; // save the turn in the queue and exit method
}
// SPACE or another key other than an arrow key has been pressed
x = 32*8 + 32*8*65; // snake start pos (left and right side of + can be viewed as x and y coord
q = []; // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
m = []; // create an array to store food pos and snake
createEmptyMovieClip("s", w=0); // create MC to store the snake and the food MC and reset snake counter(w)
e = 2*(m[x-65*8] = 2*(r=1)); // set erase counter (e) to 4, set current direction (r) to up (1) and set food on the position the snake will be over the first time to place food
onEnterFrame = function () { // MAIN function
c = q.pop(); // ...pick the next turn in the queue (may be undefined if queue is empty)...
if (c != undefined and 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 += [-1, -65, 1, 65][r]*8; // move the snake to a new x position (-1 = left, -65 = up, 1 = right, 65 = down)
if (m[x] == 1 or !(x%520) or !(int(x/(65*8)) % 33)) { // GAME OVER if it is a snake block or outside the map on the next position
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!"; return; // type game over text and exit main
}
with(s.createEmptyMovieClip(w, w)) { // place a snake block (or food block the first loop)
beginFill(255<<16); // red food color first time...
if (w++) // ...blue snake color the other times
beginFill(0x555588);
_x = x%520; _y = int(x/(65*8))*8; // set snake block position
lineTo(-7, 0); lineTo(-7, -7); lineTo(0, -7); endFill(); // draw a square
}
m[x] += 1; // set current pos as "occupied" by a snake block
if (m[x] == 3) { // check if there is a food block on the new pos (2 = food, but we just added 1 above)
t.text = "Score: " +(w-(e-=5)-2)*2; // delay erase counter with 5 (the snake will grow 5 blocks each time), calculate and type score (+10p for a food block)
do {} while (m[c = (s[0]._x = 8+random(64)*8)+(s[0]._y = 8+random(32)*8)*65]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to 2
}
if (e) { // if not food MC (s[0]) then erase last snake MC and entry in array m
c = s[e]; // get last MC
delete m[c._x+65*c._y]; removeMovieClip(c); // delete the value in the array m and delete the MC
}
e++; // increase erase snake counter
}
}
Version 2.0
Major new version! This 2.0 version is 51 bytes smaller than the previous one. It's amazing what a couple of weeks away from the code can do! There are many small improvements, but here are the major changes:
- Using the text field as a key listener instead of the key object
- Removed the test if SPACE is pressed. You can now start the game by pressing any key (except arrow keys). This is a bit "cheating", but the text "Press SPACE to Start" is still true
- Only using 1 variable (called x) to keep track of current snake position instead of x and y. This reduces the direction arrays from 2 to 1, and a more efficient test if the snake is outside the game area. This saved quite a few bytes!
- Removed the eat counter variable (called e). Instead, "eating" is performed by decreasing the snake erase counter (now called e instead of d)
- Removed the score variable p. The score was a function of the snake length anyway, so it can be calculated at any time. I've tried to remove it before, but I never could make the score both a multiple of 10 and show 0 the first time. Because of the change above it now works.
//--- Flash MX Snake Game 1Kb by Strille. Version 2.0, 750 bytes
//--- Paste this code on frame 1 and set scene size to 512x280 and Frame Rate to 16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 1, 1, 255, 511, 32); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - Press SPACE to Start"; // show start text
beginFill(0xeeeeee); lineStyle(1); lineTo(511, 0); lineTo(511, 256); lineTo(0, 256); endFill(); // draw background with border
Key.addListener(t); // use an existing object as key listener (we don't waste bytes by creating a new object)
t.onKeyDown = function() { // define an anonymous method to execute when a key is pressed
c = Key.getCode()-37; // get key code (c is a variable used "locally" several times)
if (!(c>>2)) { // arrow keys pressed (c = 0, 1, 2 or 3)
q.unshift(c); return; // save the turn in the queue and exit method
}
// SPACE or another key other than an arrow key has been pressed
q = []; // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
m = []; // create an array to store food pos and snake
x = 32*8 + 32*8*65; // snake start pos (left and right side of + can be viewed as x and y coord
createEmptyMovieClip("s", w=0); // create MC to store the snake and the food MC and reset snake counter(w)
e = 2*(m[x-65*8] = 2*(r=1)); // set erase counter (e) to 4, set current direction (r) to up (1) and set food on the position the snake will be over the first time to place food
onEnterFrame = function () { // MAIN function
c = q.pop(); // ...pick the next turn in the queue (may be undefined if queue is empty)...
if (c != undefined and 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 += [-1, -65, 1, 65][r]*8; // move the snake to a new x position (-1 = left, -65 = up, 1 = right, 65 = down)
if (m[x] == 1 or !(x%520) or !(int(x/(65*8)) % 33)) { // GAME OVER if it is a snake block or outside the map on the next position
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!"; return; // type game over text and exit main
}
with(s.createEmptyMovieClip(w, w)) { // place a snake block (or food block the first loop)
beginFill(255<<16); // red food color first time...
if (w++) // ...blue snake color the other times
beginFill(0x555588);
_x = x%520; _y = int(x/(65*8))*8; // set snake block position
lineTo(-7, 0); lineTo(-7, -7); lineTo(0, -7); endFill(); // draw a square
}
m[x] += 1; // set current pos as "occupied" by a snake block
if (m[x] == 3) { // check if there is a food block on the new pos (2 = food, but we just added 1 above)
t.text = "Score: " +(w-(e-=5)-2)*2; // delay erase counter with 5 (the snake will grow 5 blocks each time), calculate and type score (+10p for a food block)
do {} while (m[c = (s[0]._x = 8+random(64)*8)+(s[0]._y = 8+random(32)*8)*65]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to 2
}
if (e) { // if not food MC (s[0]) then erase last snake MC and entry in array m
c = s[e]; // get last MC
delete m[c._x+65*c._y]; removeMovieClip(c); // delete the value in the array m and delete the MC
}
e++; // increase erase snake counter
}
}
Version 1.9
- Removed the last else-statement in the code. Saved 2 bytes.
- Changed the border thickness from 2 to 1. Saved 1 byte.
//--- Flash MX Snake Game 1Kb by Strille Version 1.9
//--- Paste this code on frame 1 and set scene size to 512x280 and frame rate to 16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 1, 1, 256, 512, 32); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - Press SPACE to Start"; // show start text
beginFill(0xeeeeee); lineStyle(1); moveTo(1, 1); lineTo(511, 1); lineTo(511, 255); lineTo(1, 255); endFill(); // draw background box with border
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key code (c is a variable used "locally" several times)
if (int((c-1)>>2) == 9) // arrow keys pressed . Arrow keys are values 37-40. 36-39 (c-1) are the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
if (c == 32) { // space pressed - init game
q = []; // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
m = []; // create an array to store food pos and snake
x = y = c*8; // start coordinate (32, 32). All coords are multiples of 8 to match gfx coords
createEmptyMovieClip("s", p = e = w = d = 0); // create MC to store the snake and the food MC and reset point, eat , snake and snake erase counters
r = m[x+(y-8)*64] = 2; // set dir to up (1=left, 2=up, 3=right, 4=down) and set food on the pos snake will be over the first time in order for the food to be placed
onEnterFrame = function () { // this is the main function
c = q.pop(); // ...pick the next turn in the queue (may be undefined if queue is empty)...
if (c && 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 += [-8, null, 8, null][r-1]; // move the snake to a new x position
y += [null, -8, null, 8][r-1]; // move the snake to a new y position
if (m[x+y*64] == 1 or x < 0 or x > 63*8 or y < 0 or y > 31*8) { // check if it is something else (a snake block or outside the map) - GAME OVER
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!"; // type game over text
return; // end main function
}
with(s.createEmptyMovieClip(w, w)) { // place a snake block (or food block the first loop)
beginFill(255<<16); // red food color first time...
if (w++) // ...blue snake color the other times
beginFill(0x555588);
_x = x; _y = y;
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8); endFill(); // draw a square
}
m[x+y*64] += 1; // set current pos as "occupied" by a snake block
if (m[x+y*64] == 3) { // check if there is a food block on the new pos
t.text = "Score: " + p; p+=10; // write score in the text field and then add 10 points
do {} while (m[c = (s[0]._x = random(64)*8)+(s[0]._y = random(32)*8)*64]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to 2
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
}
if (e < 4) { // if not "eating" set map pos to empty and remove last MC
c = s[++d]; // get tail MC
delete m[c._x+64*c._y]; removeMovieClip(c); return;
}
e--; // if eating, decrease eat counter and do nothing more
}
}
}
Key.addListener(Key); // add the key listener
Version 1.8
- Removed the function B() that draws a block and included that code in the main loop. Made some small changes to make that work
- Changed the erase counter variable from ww to d. Thanks to Flashkit user random10122 for the tip
- Changed
beginFill(w-1 ? 0x555588 : 240 << 16);
to 2 beginfills and 1 if. This actually saves 5 bytes. Thanks to Flashkit user BlinkOk for the tip
- Changed
10*p++;
to p; p+=10;
//--- Flash MX Snake Game 1Kb by Strille Version 1.8
//--- Paste this code on frame 1 and set scene size to 512x280 and FPS to 16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 1, 1, 256, 512, 32); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - Press SPACE to Start"; // show start text
beginFill(0xeeeeee); lineStyle(2); moveTo(1, 1); lineTo(511, 1); lineTo(511, 255); lineTo(1, 255); endFill(); // draw background box with border
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key code (c is a variable used "locally" several times)
if (int((c-1)>>2) == 9) // arrow keys pressed . Arrow keys are values 37-40. 36-39 (c-1) are the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
if (c == 32) { // space pressed - init game
q = []; // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
m = []; // create an array to store food pos and snake
x = y = c*8; // start coordinate (32, 32). All coords are multiples of 8 to match gfx coords
createEmptyMovieClip("s", p = e = w = d = 0); // create MC to store the snake and the food MC and reset point, eat , snake and snake erase counters
r = m[x+(y-8)*64] = 2; // set dir to up (1=left, 2=up, 3=right, 4=down) and set food on the pos snake will be over the first time in order for the food to be placed
onEnterFrame = function () { // this is the main function
c = q.pop(); // ...pick the next turn in the queue (may be undefined if queue is empty)...
if (c && 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 += [-8, null, 8, null][r-1]; // move the snake to a new x position
y += [null, -8, null, 8][r-1]; // move the snake to a new y position
if (m[x+y*64] == 1 or x < 0 or x > 63*8 or y < 0 or y > 31*8) { // check if it is something else (a snake block or outside the map) - GAME OVER
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!"; // type game over text
return; // end main function
}
with(s.createEmptyMovieClip(w, w)) { // place a snake block (or food block the first loop)
beginFill(255<<16); // red food color first time...
if (w++) // ...blue snake color the other times
beginFill(0x555588);
_x = x; _y = y;
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8); endFill(); // draw a square
}
m[x+y*64] += 1; // set current pos as "occupied" by a snake block
if (m[x+y*64] == 3) { // check if there is a food block on the new pos
t.text = "Score: " + p; p+=10; // write score in the text field and then add 10 points
do {} while (m[c = (s[0]._x = random(64)*8)+(s[0]._y = random(32)*8)*64]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to 2
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
}
if (e > 3) { // if eating, decrease eat counter and do nothing more
e--;
} else { // ...else set map pos to empty and remove last MC
c = s[++d]; // get tail MC
delete m[c._x+64*c._y]; removeMovieClip(c);
}
}
}
}
Key.addListener(Key); // add the key listener
Version 1.7
- Changed the movie size from 400x260 to 512 to 280. The game area is now 512x256, or 64x32 blocks. This compresses better than 50x30 blocks
- Changed textfield width from 400 to 512, and textfield height from 20 to 16
//--- Flash MX Snake Game 1Kb by Strille Version 1.7
//--- Paste this code on frame 1 and set scene size to 512x280 and FPS to ~16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 1, 1, 256, 512, 16); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - Press SPACE to start";
beginFill(0xeeeeee); lineStyle(2); moveTo(1, 1); lineTo(511, 1); lineTo(511, 255); lineTo(1, 255); endFill(); // draw background box with border
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key code (c is a variable used "locally" serveral times)
if (int((c-1)>>2) == 9) { // arrow keys pressed . Arrow keys is values 37-40. 36-39 (c-1) is the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
} // actually saves 3 bytes to not have an "else" here
if (c == 32) { // space pressed - init game
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)
m = []; // create an array to store food pos and snake
x = y = c*8; // start coordinate (25, 25). All coords are multiples of 8 to match gfx coords
createEmptyMovieClip("s", p = e = w = ww = 0); // create MC to store the snake and the food MC and reset points, eat counter, snake counter, snake erase counter
B(); // create a red food MC (first time B() is called the block is drawn red)
B(); // create a blue snake MC
r = m[x+(y-8)*64] = 2; // set current direction to up (1=left, 2=up, 3=right, 4=down) and set food on pos snake will be over the first time in order for the food to be placed
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 && 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 += [-8, null, 8, null][r-1]; // move the snake to a new x position
y += [null, -8, null, 8][r-1]; // move the snake to a new y position
if (m[x+y*64] > 1) { // check if there is a food block on the new pos
t.text = "Score: "+10*p++; // add 10 points to the score and write it in the text field
do {} while (m[c = (s[0]._x = random(64)*8)+(s[0]._y = random(32)*8)*64]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to "2"
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} // actually saves 3 bytes to not have an "else" here (the if above is only true when food is eaten so it doesn't matter really
if (m[x+y*64] == 1 or x < 0 or x == 64*8 or y < 0 or y == 32*8) { // check if it is something else (a snake block or outside the map) - GAME OVER
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!";
} else {
m[x+y*64] = 1; // set current pos as "occupied" by a snake block
B(); // create a new block
if (e>5) { // if eating, decrease eat counter and do nothing more
e--;
} else { // ...else set map pos to empty and remove last MC
c = s[++ww]; // get tail MC
delete m[c._x+64*c._y];
removeMovieClip(c);
}
}
}
}
}
Key.addListener(Key); // add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 240 << 16); // red color first time, then blue
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8); endFill(); // draw a square
_x = x; _y = y;
}
}
Version 1.6
- Changed
int((c-1)/4
to int((c-1)>>2
and " GAME OVER!"
to "\tGAME OVER!"
. 2 glorious bytes saved. Thanks to Dentoid for pointing these out
- Changed
m[c._x+50*c._y] = 0;
to delete m[c._x+50*c._y];
. 1 byte saved
- Changed "or" syntax to Flash 4 style (
||
to or
). 4 bytes saved
- Used ActionScript Highlighter to make the code on this page more readable
//--- Flash MX Snake Game 1Kb by Strille Version 1.6
//--- Paste this code on frame 1 and set scene size to 400x260 and FPS to ~16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 10, 1, 240, 400, 20); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - Press SPACE to start";
beginFill(0xeeeeee); lineStyle(2); moveTo(1, 1); lineTo(399, 1); lineTo(399, 239); lineTo(1, 239); endFill(); // draw background box with border
v = [8, null, -8, null, 8]; // create a weird direction array (pos 0-3 is the direction in x (with wrong sign) and pos 1-4 is the dir in y (with wrong sign) when moving left, up, right, down
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key code (c is a variable used "locally" serveral times)
if (int((c-1)>>2) == 9) { // arrow keys pressed . Arrow keys is values 37-40. 36-39 (c-1) is the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
} // actually saves 3 bytes to not have an "else" here
if (c == 32) { // space pressed - init game
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)
m = []; // create an array to store food pos and snake
x = y = 25*8; // start coordinate (25, 25). All coords are multiples of 8 to match gfx coords
createEmptyMovieClip("s", p = e = w = ww = 0); // create MC to store the snake and the food MC and reset points, eat counter, snake counter, snake erase counter
B(); // create a red food MC (first time B() is called the block is drawn red)
B(); // create a blue snake MC
r = m[x+(y-8)*50] = 2; // set current direction to up (1=left, 2=up, 3=right, 4=down) and set food on pos snake will be over the first time in order for the food to be placed
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 && 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-1]; // move the snake to a new x position
y += v[r]; // move the snake to a new y position
if (m[x+y*50] > 1) { // check if there is a food block on the new pos
t.text = "Score: "+10*p++; // add 10 points to the score and write it in the text field
do {} while (m[c = (s[0]._x = random(50)*8)+(s[0]._y = random(30)*8)*50]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to "2"
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} // actually saves 3 bytes to not have an "else" here (the if above is only true when food is eaten so it doesn't matter really
if (m[x+y*50] == 1 or x < 0 or x == 50*8 or y < 0 or y == 30*8) { // check if it is something else (a snake block or outside the map) - GAME OVER
delete onEnterFrame; // quit looping main function
t.text += "\tGAME OVER!";
} else {
m[x+y*50] = 1; // set current pos as "occupied" by a snake block
B(); // create a new block
if (e>5) { // if eating, decrease eat counter and do nothing more
e--;
} else { // ...else set map pos to empty and remove last MC
c = s[++ww]; // get tail MC
delete m[c._x+50*c._y];
removeMovieClip(c);
}
}
}
}
}
Key.addListener(Key); // add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 240 << 16); // red color first time, then blue
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8); // draw a square
endFill();
_x = x;
_y = y;
}
}
Version 1.5
- Changed the zeros in array v to
null
- Moved
p = e = w = ww = 0
to the depth argument in createEmptyMovieClip("s", 0);
- More efficient do - while loop to place the food
- Changed
onEnterFrame = null;
to delete onEnterFrame;
- Removed
return;
in the "game over" code and added an else part to that if-statement
- The red color is defined using
240 << 16
rather than 0xdd0000;
- Replaced variable j, k (used as food coords) and z (reference to a Movie Clip to be deleted) with variable c
//--- Flash MX Snake Game 1Kb by Strille Version 1.5
//--- Paste this code on frame 1 and set scene size to 400x260 and FPS to ~16
//--- The code is not written with speed in mind, only small file size. Not that it is slow :-)
createTextField("t", 10, 1, 240, 400, 20); // create a text field to write score and instructions
t.text = "Snake Game 1Kb - 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 = [8, null, -8, null, 8]; // create a weird direction array (pos 0-3 is the direction in x (with wrong sign) and pos 1-4 is the dir in y (with wrong sign) when moving left, up, right, down
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key code (c is a variable used "locally" serveral times)
if (int((c-1)/4) == 9) { // arrow keys pressed . Arrow keys is values 37-40. 36-39 (c-1) is the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
} // actually saves 3 bytes to not have an "else" here
if (c == 32) { // space pressed - init game
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)
m = []; // create an array to store food pos and snake
x = y = 25*8; // start coordinate (25, 25). All coords are multiples of 8 to match gfx coords
createEmptyMovieClip("s", p = e = w = ww = 0); // create MC to store the snake and the food MC and reset points, eat counter, snake counter, snake erase counter
B(); // create a red food MC (first time B() is called the block is drawn red)
B(); // create a blue snake MC
r = m[x+(y-8)*50] = 2; // set current direction to up (1=left, 2=up, 3=right, 4=down) and set food on pos snake will be over the first time in order for the food to be placed
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 && 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-1]; // move the snake to a new x position
y += v[r]; // move the snake to a new y position
if (m[x+y*50] > 1) { // check if there is a food block on the new pos
t.text = "Score: "+10*p++; // add 10 points to the score and write it in the text field
do {} while (m[c = (s[0]._x = random(50)*8)+(s[0]._y = random(30)*8)*50]); // pick a free spot to place the food, save that number, place the food MC
m[c] = 2; // set the position picked on the line above to "2"
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} // actually saves 3 bytes to not have an "else" here (the if above is only true when food is eaten so it doesn't matter really
if (m[x+y*50] == 1 || x < 0 || x == 50*8 || y < 0 || y == 30*8) { // check if it is something else (a snake block or outside the map) - GAME OVER
delete onEnterFrame; // quit looping main function
t.text += " GAME OVER!";
} else {
m[x+y*50] = 1; // set current pos as "occupied" by a snake block
B();
if (e>5) { // if eating, decrease eat counter and do nothing more
e--;
} else { // ...else set map pos to empty and remove last MC
c = s[++ww]; // get tail MC
m[c._x+50*c._y] = 0;
removeMovieClip(c);
}
}
}
}
}
Key.addListener(Key); // Add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 240 << 16); // red color first time, then blue
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8);
endFill();
_x = x;
_y = y;
}
}
Version 1.4
- Changed
z.removeMovieClip();
to removeMovieClip(z);
. Dot-syntax takes a few bytes more...
- Removed the object used as key listener, using the existing Key object instead.
//----- Flash MX Snake Game 1Kb by Strille Version 1.4
//----- 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 1Kb - 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 = [8, 0, -8, 0, 8]; // create a weird direction array (pos 0-3 is the direction in x (with wrong sign) and pos 1-4 is the dir in y (with wrong sign) when moving left, up, right, down
Key.onKeyDown = function() { // use an existing object as a key listener (we don't waste bytes by creating a new object...)
c = Key.getCode(); // get pressed key
if (int((c-1)/4) == 9) { // arrow keys pressed . Arrow keys is values 37-40. 36-39 (c-1) is the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
} // actually saves 3 bytes to not have an "else" here
if (c == 32) { // space pressed - init game
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
m = []; // create a 50x30=1500 1d array to store food pos and snake
p = e = w = ww = 0; // Reset points, eat counter, snake counter, snake erase counter
x = y = 25*8; // start coordinate (25, 25)
createEmptyMovieClip("s", 0); // create MC to store the snake and the food MC
B(); // create a red food MC
B(); // create a snake of length 1
r = m[x+(y-8)*50] = 2; // set current direction to up (1=left, 2=up, 3=right, 4=down) and set food on pos snake will be over the first time in order for the food to be placed
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 && 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-1]; // move the snake to a new x position
y += V[r]; // move the snake to a new y position
if (m[x+y*50] > 1) { // check if there is a food block on the new pos
t.text = "Score: "+10*p++; // add 10 points to the score and write it in the text field
do {
s[0]._x = j = random(50)*8;
s[0]._y = k = random(30)*8;
} while (m[j+50*k]);
m[j+50*k] = 2;
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} // actually saves 3 bytes to not have an "else" here (the if above is only true when food is eaten so it doesn't matter really
if (m[x+y*50] == 1 || x < 0 || x == 50*8 || y<0 || y == 30*8) { // 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();
if (e>5) { // if eating, wait...
e--;
} else { // ...else set map pos to empty and remove last MC
z = s[++ww]; // get tail MC
m[z._x+50*z._y] = 0;
removeMovieClip(z);
}
}
}
}
Key.addListener(Key); // Add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 0xdd0000);
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8);
endFill();
_x = x;
_y = y;
}
}
Version 1.3
- Rewritten the if-statement that checks if an arrow key has been pressed:
if (c>0 && c<5)
to if (int((c-1)/4) == 9)
. This actually takes up a few bytes less. In order for this to work -36
was moved from c = Key.getCode()-36;
to q.unshift(c);
. This does not affect file size, but it also changes if (c == -4)
to if (c == 32)
, which actually takes 2 bytes less! (very strange, but negative numbers appear to take a bit more space)
- The discovery that negative values take up more space made me change
V = [-8, 0, 8, 0, -8];
to V = [8, 0, -8, 0, 8];
. The only effect this had was that I had to switch sign when adding/subtracting V. Saved 4 bytes!
- Changed if - else statements to two if statements (saves 1 byte)
- Removed the function F() and incorporated the code in main. This function placed the food on an empty position. F() was called once before a game started and then every time a food block was eaten. Now this code is only called when food is eaten (food is always eaten at the start of a new game in order to place the food) thus it no longer needed to be a function
//----- Flash MX Snake Game 1Kb by Strille Version 1.3
//----- 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 1Kb - 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 = [8, 0, -8, 0, 8]; // create a weird direction array (pos 0-3 is the direction in x (with wrong sign) and pos 1-4 is the dir in y (with wrong sign) 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(); // get pressed key
if (int((c-1)/4) == 9) { // arrow keys pressed . Arrow keys is values 37-40. 36-39 (c-1) is the only values that equals 9 when divided with 4 and rounded down
q.unshift(c-36); // save the turn in the queue
} // actually saves 3 bytes to not have an "else" here
if (c == 32) { // space pressed - init game
m = []; // create a 50x30=1500 1d array to store food pos and snake
p = e = w = ww = 0; // Reset points, eat counter, snake counter
x = y = 25*8; // 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(); // create a red food MC
B(); // create a snake of length 1
r = m[x+(y-8)*50] = 2; // set current direction to 2 (1=left, 2=up, 3=right, 4=down) and set food on pos snake will be over the first time in order for the food to be placed
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 && 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-1]; // move the snake to a new x position
y += V[r]; // 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
t.text = "Score: "+10*p++; // add 10 points to the score and write it in the text field
do {
s[0]._x = j = random(50)*8;
s[0]._y = k = random(30)*8;
} while (m[j+50*k]);
m[j+50*k] = 2;
e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
} // actually saves 3 bytes to not have an "else" here (the if above is only true when food is eaten so it doesn't matter really
if (m[x+y*50] == 1 || x<0 || x==50*8 || y<0 || y==30*8) { // 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(); // create a new snake block
if (e>5) { // if eating, wait...
e--;
} else { // ...else set map pos to empty and remove last MC
z = s[++ww]; // get tail MC
m[z._x+50*z._y] = 0;
z.removeMovieClip();
}
}
}
}
Key.addListener(K); // Add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 0xdd0000);
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8);
endFill();
_x = x;
_y = y;
}
}
Version 1.2
- Updated functions B() and F()
- Rewritten
if(var != undefined)
to if(var)
- Snake coords are now multiples of 8 to match movie clip coords
- Directions are now stored as integers 1-4 instead of 0-3 in order to be able to change
if (c != undefined && c%2 != r%2)
to if (c && c%2 != r%2)
//----- Flash MX Snake Game 1Kb by Strille Version 1.2
//----- 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 1Kb - 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 = [-8, 0, 8, 0, -8]; // 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()-36; // get pressed key
if (c>0 && c<5) { // arrow keys pressed
q.unshift(c); // save the key press in the queue
} else if (c == -4) { // space pressed - init game
m = []; // create a 50x30=1500 1d array to store food pos and snake
t.text = ""; // clear the text field
p = e = w = 0; // Reset points, eat counter, snake counter
ww = 1; // reset snake erase counter
x = y = 25*8; // 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
r = 2; // current direction (1=left, 2=up, 3=right, 4=down) and
createEmptyMovieClip("s", 0); // create MC to store the snake and the food MC
B(); // create a red food MC
F(); // place food
B(); // 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 && 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-1]; // move the snake to a new x position
y -= V[r]; // 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
p += 10; // add 10 points to the score
t.text = "Score: "+p; // 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] || x<0 || x==50*8 || y<0 || y==30*8) { // 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();
if (e>0) { // if eating, wait...
e--;
} else { // ...else set map pos to empty and remove last MC
z = s[ww++];
m[z._x+50*z._y] = undefined;
z.removeMovieClip();
}
}
}
}
Key.addListener(K); // Add the key listener
function B() { // create a block
with(s.createEmptyMovieClip(w++, w)) {
beginFill(w-1 ? 0x555588 : 0xdd0000);
moveTo(1, 1); lineTo(8, 1); lineTo(8, 8); lineTo(1, 8);
endFill();
_x = x;
_y = y;
}
}
function F() { // place the food on an empty space
do {
s[0]._x = j = random(50)*8;
s[0]._y = k = random(30)*8;
} while (m[j+50*k]);
m[j+50*k] = 2;
}
Version 1.1
//----- Flash MX Snake Game 1Kb by Strille Version 1.1
//----- 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 1Kb - 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;
}
To the main page