Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created January 18, 2017 13:20
Show Gist options
  • Save will-moore/1ec768c468ab76d6acd5bbb9b5a1f12f to your computer and use it in GitHub Desktop.
Save will-moore/1ec768c468ab76d6acd5bbb9b5a1f12f to your computer and use it in GitHub Desktop.
"Worst snake ever" game from bbc microbit, created on the Code Kingdoms JavaScript editor.
// When the BBC micro:bit runs.
function onStart( ) {
microbit.draw(Pattern("01000.01100.01110.01100.01000"));
wait(200);
}
function onPressA( ) {
/* create variables to keep track of
the player's score, called score (should start at 1000)
the player's x-position, called posX (should start at 0)
the player's y-position, called posY (should start at 4)
the apple's x-position, called appleX (should start at 4)
the apple's y-position, called appleY (should start at 3)
the current level of the game, called level (should start at 0)
*/
globals.score = 1000;
globals.appleX = 4;
globals.appleY = 4;
globals.posX = 0;
globals.posY = 0;
globals.level = 0;
microbit.draw(Pattern("11111.10001.10101.10001.11111"));
wait(300);
microbit.clear();
// create a while loop which checks whether the current level of the game is less than 10
while (globals.level < 5) {
// make the player's dot flash by turning it on, off and on again (with small pauses inbetween)
// (remember the x and y coordinate of the player's dot is stored in posX and posY
// and you can turn a single LED off by using microbit.on and microbit.off!)
microbit.on(globals.posX, globals.posY);
wait(200);
microbit.off(globals.posX, globals.posY);
wait(200);
microbit.on(globals.posX, globals.posY);
// display the apple on the screen (remember the apple's position is stored in appleX and appleY)
// globals.appX = Random.number(1, 2);
// microbit.on(globals.posX, globals.posY);
microbit.on(globals.appleX, globals.appleY);
/* create an if chunk that checks whether the player's X position (stored in posX)
is equal to the apple's X position (appleX) and the same for their Y positions.
You can check if two things are equal inside a condition by using '=='
*/
if (( globals.posX == globals.appleX ) && ( globals.posY == globals.appleY )) {
// if the condition is true, that means the player has collected the apple,
// increase the game level by one
globals.level = globals.level + 1;
microbit.say(globals.level);
wait(300);
// set the apple's x- and y-position to a random number between 0 and 4
globals.appleX = Random.number(0, 4);
globals.appleY = Random.number(0, 4);
// clear the screen
microbit.clear();
// microbit.on(globals.appleX, globals.appleY);
// wait(1000);
}
/* create an if chunk to see if the micro:bit is being tilted to the right
AND that the player's dot is at x-position 1 or more (i.e., not at the edge)
*/
if (( microbit.tiltX > 2 ) && ( globals.posX > 0 )) {
microbit.off(globals.posX, globals.posY);
globals.posX = globals.posX - 1;
}
if (( microbit.tiltX < 2 ) && ( globals.posX < 4 )) {
microbit.off(globals.posX, globals.posY);
globals.posX = globals.posX + 1;
}
if (( microbit.tiltY > 2 ) && ( globals.posY > 0 )) {
microbit.off(globals.posX, globals.posY);
globals.posY = globals.posY - 1;
}
if (( microbit.tiltY < 2 ) && ( globals.posY < 4 )) {
microbit.off(globals.posX, globals.posY);
globals.posY = globals.posY + 1;
}
globals.score = globals.score - 1;
}
// out of the loop so the game is over! display the player's score to the screen
microbit.say(globals.score);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment