Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "MicroBit.h" 00002 int pLocationX; 00003 int pLocationY; 00004 int xPos = 2; 00005 bool gameOver = false; 00006 int y; 00007 int obstacleX; 00008 int score = 0; 00009 int playX; 00010 int direction = 0; 00011 MicroBit uBit; 00012 00013 //Gets X value from microbit 00014 void getXPos(){ 00015 playX = uBit.accelerometer.getX(); 00016 if(playX > 250){ 00017 xPos++; 00018 direction = 1; 00019 }else if (playX < -250) { 00020 xPos--; 00021 direction = 0; 00022 } 00023 00024 } 00025 00026 //Compares player location with obstacle location to check collision. 00027 void checkCollision(){ 00028 if (xPos == obstacleX || xPos == obstacleX - 1 && y == 4){ 00029 gameOver = true; 00030 }else{ 00031 00032 gameOver = false; 00033 } 00034 00035 } 00036 00037 //Draws the player on screen, and ensures it cannot go off the side. Also clears lEDs as it moves. 00038 void displayPlayerLocation(){ 00039 getXPos(); 00040 if (xPos < 0){ 00041 xPos = 0; 00042 } else if(xPos > 4) { 00043 xPos = 4; 00044 } 00045 if(direction == 1) { 00046 uBit.display.image.setPixelValue(xPos - 1, 4, 0); 00047 } else if(direction == 0) { 00048 uBit.display.image.setPixelValue(xPos + 1, 4, 0); 00049 } 00050 00051 checkCollision(); 00052 uBit.display.image.setPixelValue(xPos, 4, 255); 00053 } 00054 00055 //Displays the falling leds, and also clears them afterwards 00056 void displayObs(){ 00057 uBit.display.image.setPixelValue(obstacleX, y, 0); 00058 obstacleX = uBit.random(5); 00059 00060 for (y = 0; y < 5; y++){ // descends the obstacles based on y value 00061 uBit.display.image.setPixelValue(obstacleX, y, 255); 00062 if (obstacleX > 0) 00063 { 00064 uBit.display.image.setPixelValue(obstacleX - 1, y, 255); 00065 } 00066 if (y > 0) 00067 { 00068 uBit.display.image.setPixelValue(obstacleX, y - 1, 0); 00069 } 00070 if (obstacleX > 0 && y > 0) 00071 { 00072 uBit.display.image.setPixelValue(obstacleX - 1, y - 1, 0); 00073 } 00074 if (score > 0) 00075 { 00076 uBit.sleep(500 - score); 00077 }else if (score >= 500) 00078 { 00079 uBit.sleep(0); 00080 }else{ 00081 uBit.sleep(500); 00082 } 00083 checkCollision(); 00084 displayPlayerLocation(); 00085 } 00086 y = 4; 00087 uBit.display.image.setPixelValue(obstacleX, y, 0); 00088 uBit.display.image.setPixelValue(obstacleX - 1, y, 0); 00089 score += 5; 00090 } 00091 00092 00093 00094 00095 int main(){ 00096 uBit.init(); 00097 while(gameOver == false){ 00098 displayObs(); 00099 } 00100 uBit.display.scroll("Game Over, Score: "); 00101 uBit.display.scroll(score); 00102 } 00103 00104 00105
Generated on Sat Aug 27 2022 06:24:34 by
1.7.2