First game

Dependencies:   microbit

Committer:
bvnoake
Date:
Mon Feb 11 12:40:14 2019 +0000
Revision:
0:a4158fb0ba8a
Game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bvnoake 0:a4158fb0ba8a 1 //Including the microbit library
bvnoake 0:a4158fb0ba8a 2 #include "MicroBit.h"
bvnoake 0:a4158fb0ba8a 3
bvnoake 0:a4158fb0ba8a 4 //defining points for the game to set how long runs for
bvnoake 0:a4158fb0ba8a 5 int points = 0;
bvnoake 0:a4158fb0ba8a 6 //defining co-ordinates for pixels
bvnoake 0:a4158fb0ba8a 7 int obsy = 0;
bvnoake 0:a4158fb0ba8a 8 int x = 4;
bvnoake 0:a4158fb0ba8a 9 int y = 4;
bvnoake 0:a4158fb0ba8a 10 //sets whether game is running or not
bvnoake 0:a4158fb0ba8a 11 bool gamerunning = true;
bvnoake 0:a4158fb0ba8a 12 //users lives before game ends
bvnoake 0:a4158fb0ba8a 13 int lives = 3;
bvnoake 0:a4158fb0ba8a 14
bvnoake 0:a4158fb0ba8a 15 //
bvnoake 0:a4158fb0ba8a 16 MicroBit uBit;
bvnoake 0:a4158fb0ba8a 17 //creates random x co-ordinate
bvnoake 0:a4158fb0ba8a 18 int x2 = uBit.random(5);
bvnoake 0:a4158fb0ba8a 19
bvnoake 0:a4158fb0ba8a 20 //collision test function
bvnoake 0:a4158fb0ba8a 21 void pointscollide()
bvnoake 0:a4158fb0ba8a 22 {
bvnoake 0:a4158fb0ba8a 23 //if two x co-ordinates of the bottom pixels are the same
bvnoake 0:a4158fb0ba8a 24 if (x == x2)
bvnoake 0:a4158fb0ba8a 25 {
bvnoake 0:a4158fb0ba8a 26 /*if points is equal to 8 then this ends the game,
bvnoake 0:a4158fb0ba8a 27 sets game running to be false and scrolls across the screen game over
bvnoake 0:a4158fb0ba8a 28 you win and then their points */
bvnoake 0:a4158fb0ba8a 29 if (points == 8)
bvnoake 0:a4158fb0ba8a 30 {
bvnoake 0:a4158fb0ba8a 31 gamerunning = false;
bvnoake 0:a4158fb0ba8a 32 uBit.display.scroll("Game Over, you win");
bvnoake 0:a4158fb0ba8a 33 uBit.display.scroll(points);
bvnoake 0:a4158fb0ba8a 34 }
bvnoake 0:a4158fb0ba8a 35 /* if points isn't 8 then points just adds by 1 then a new pixel is
bvnoake 0:a4158fb0ba8a 36 created in a new random location again */
bvnoake 0:a4158fb0ba8a 37 else
bvnoake 0:a4158fb0ba8a 38 {
bvnoake 0:a4158fb0ba8a 39 points = points + 1;
bvnoake 0:a4158fb0ba8a 40 x2 = uBit.random(5);
bvnoake 0:a4158fb0ba8a 41 uBit.display.image.setPixelValue(x2, 4, 255);
bvnoake 0:a4158fb0ba8a 42 }
bvnoake 0:a4158fb0ba8a 43
bvnoake 0:a4158fb0ba8a 44 }
bvnoake 0:a4158fb0ba8a 45
bvnoake 0:a4158fb0ba8a 46
bvnoake 0:a4158fb0ba8a 47
bvnoake 0:a4158fb0ba8a 48 }
bvnoake 0:a4158fb0ba8a 49 //function for button pressing
bvnoake 0:a4158fb0ba8a 50 void onButton(MicroBitEvent e)
bvnoake 0:a4158fb0ba8a 51 {
bvnoake 0:a4158fb0ba8a 52 //if button A is pressed
bvnoake 0:a4158fb0ba8a 53 if (e.source == MICROBIT_ID_BUTTON_A)
bvnoake 0:a4158fb0ba8a 54 {
bvnoake 0:a4158fb0ba8a 55 /* shifts over pixel one place to the left whilst
bvnoake 0:a4158fb0ba8a 56 getting rid of pixels original place */
bvnoake 0:a4158fb0ba8a 57 uBit.display.image.setPixelValue(x = x - 1, y, 255);
bvnoake 0:a4158fb0ba8a 58 if (x < 4)
bvnoake 0:a4158fb0ba8a 59 {
bvnoake 0:a4158fb0ba8a 60 uBit.display.image.setPixelValue(x + 1, y, 0);
bvnoake 0:a4158fb0ba8a 61 }
bvnoake 0:a4158fb0ba8a 62 //calls collide function to check whether collides with other pixel
bvnoake 0:a4158fb0ba8a 63 pointscollide();
bvnoake 0:a4158fb0ba8a 64
bvnoake 0:a4158fb0ba8a 65
bvnoake 0:a4158fb0ba8a 66 }
bvnoake 0:a4158fb0ba8a 67 //if button B is pressed
bvnoake 0:a4158fb0ba8a 68 if (e.source == MICROBIT_ID_BUTTON_B)
bvnoake 0:a4158fb0ba8a 69 {
bvnoake 0:a4158fb0ba8a 70 /* shifts over pixel one place to the right whilst
bvnoake 0:a4158fb0ba8a 71 getting rid of pixels original place */
bvnoake 0:a4158fb0ba8a 72 uBit.display.image.setPixelValue(x = x + 1, y, 255);
bvnoake 0:a4158fb0ba8a 73 if (x > 0 )
bvnoake 0:a4158fb0ba8a 74 {
bvnoake 0:a4158fb0ba8a 75 uBit.display.image.setPixelValue(x - 1, y, 0);
bvnoake 0:a4158fb0ba8a 76 }
bvnoake 0:a4158fb0ba8a 77 //calls collide function to check whether collides with other pixel
bvnoake 0:a4158fb0ba8a 78 pointscollide();
bvnoake 0:a4158fb0ba8a 79
bvnoake 0:a4158fb0ba8a 80
bvnoake 0:a4158fb0ba8a 81 }
bvnoake 0:a4158fb0ba8a 82 }
bvnoake 0:a4158fb0ba8a 83
bvnoake 0:a4158fb0ba8a 84
bvnoake 0:a4158fb0ba8a 85
bvnoake 0:a4158fb0ba8a 86
bvnoake 0:a4158fb0ba8a 87 //running game function - for pixels moving down screen
bvnoake 0:a4158fb0ba8a 88 void play()
bvnoake 0:a4158fb0ba8a 89 {
bvnoake 0:a4158fb0ba8a 90 //creating random x co-ordinate
bvnoake 0:a4158fb0ba8a 91 int obsx = uBit.random(5);
bvnoake 0:a4158fb0ba8a 92 /* loop for the size of the y axis on led
bvnoake 0:a4158fb0ba8a 93 matrix adding one each time so that the pixel
bvnoake 0:a4158fb0ba8a 94 can move down the screen one line at a time */
bvnoake 0:a4158fb0ba8a 95 for (obsy = 0; obsy < 5; obsy++){
bvnoake 0:a4158fb0ba8a 96 /* sets pixels on the screen for each y co-ordinate as it
bvnoake 0:a4158fb0ba8a 97 increases by 1, sets pixels with x co-ordinates and then
bvnoake 0:a4158fb0ba8a 98 deletes pixels above */
bvnoake 0:a4158fb0ba8a 99 uBit.display.image.setPixelValue(obsx, obsy, 255);
bvnoake 0:a4158fb0ba8a 100 if (obsx > 0)
bvnoake 0:a4158fb0ba8a 101 {
bvnoake 0:a4158fb0ba8a 102 uBit.display.image.setPixelValue(obsx - 1, obsy, 255);
bvnoake 0:a4158fb0ba8a 103 }
bvnoake 0:a4158fb0ba8a 104 if (obsy > 0)
bvnoake 0:a4158fb0ba8a 105 {
bvnoake 0:a4158fb0ba8a 106 uBit.display.image.setPixelValue(obsx, obsy - 1, 0);
bvnoake 0:a4158fb0ba8a 107 }
bvnoake 0:a4158fb0ba8a 108 if (obsx > 0 && obsy > 0)
bvnoake 0:a4158fb0ba8a 109 {
bvnoake 0:a4158fb0ba8a 110 uBit.display.image.setPixelValue(obsx - 1, obsy - 1, 0);
bvnoake 0:a4158fb0ba8a 111 }
bvnoake 0:a4158fb0ba8a 112 /* sets the speed depending on the points that
bvnoake 0:a4158fb0ba8a 113 the user holds to show the pixels going down each line */
bvnoake 0:a4158fb0ba8a 114 if (points > 4)
bvnoake 0:a4158fb0ba8a 115 {
bvnoake 0:a4158fb0ba8a 116 uBit.sleep(300);
bvnoake 0:a4158fb0ba8a 117 }
bvnoake 0:a4158fb0ba8a 118 else if (points > 6)
bvnoake 0:a4158fb0ba8a 119 {
bvnoake 0:a4158fb0ba8a 120 uBit.sleep(200);
bvnoake 0:a4158fb0ba8a 121 }
bvnoake 0:a4158fb0ba8a 122 else
bvnoake 0:a4158fb0ba8a 123 {
bvnoake 0:a4158fb0ba8a 124 uBit.sleep(600);
bvnoake 0:a4158fb0ba8a 125 }
bvnoake 0:a4158fb0ba8a 126
bvnoake 0:a4158fb0ba8a 127
bvnoake 0:a4158fb0ba8a 128 }
bvnoake 0:a4158fb0ba8a 129 /*tests whether this pixel moving down the screen hits
bvnoake 0:a4158fb0ba8a 130 another so it doesn't delete the points receiving pixel */
bvnoake 0:a4158fb0ba8a 131 if (obsx == x2 || obsx - 1 == x2)
bvnoake 0:a4158fb0ba8a 132 {
bvnoake 0:a4158fb0ba8a 133 uBit.display.image.setPixelValue(obsx, 4, 255);
bvnoake 0:a4158fb0ba8a 134 }
bvnoake 0:a4158fb0ba8a 135 else
bvnoake 0:a4158fb0ba8a 136 {
bvnoake 0:a4158fb0ba8a 137 uBit.display.image.setPixelValue(obsx, 4, 0);
bvnoake 0:a4158fb0ba8a 138 }
bvnoake 0:a4158fb0ba8a 139 if (obsx < 4){
bvnoake 0:a4158fb0ba8a 140 uBit.display.image.setPixelValue(obsx - 1, 4, 0);
bvnoake 0:a4158fb0ba8a 141 }
bvnoake 0:a4158fb0ba8a 142 /* If the pixel hits the pixel manually moved by the
bvnoake 0:a4158fb0ba8a 143 buttons then lives will be decreased by 1 but if they're at 0
bvnoake 0:a4158fb0ba8a 144 then game running set at false and displayed on screen that user
bvnoake 0:a4158fb0ba8a 145 loses and displays points */
bvnoake 0:a4158fb0ba8a 146 if (obsx == x || obsx - 1 == x)
bvnoake 0:a4158fb0ba8a 147 {
bvnoake 0:a4158fb0ba8a 148 if (lives == 0)
bvnoake 0:a4158fb0ba8a 149 {
bvnoake 0:a4158fb0ba8a 150 gamerunning = false;
bvnoake 0:a4158fb0ba8a 151 uBit.display.scroll("Game over, you lose");
bvnoake 0:a4158fb0ba8a 152 uBit.display.scroll(points);
bvnoake 0:a4158fb0ba8a 153 }
bvnoake 0:a4158fb0ba8a 154 else
bvnoake 0:a4158fb0ba8a 155 {
bvnoake 0:a4158fb0ba8a 156 lives--;
bvnoake 0:a4158fb0ba8a 157 }
bvnoake 0:a4158fb0ba8a 158 }
bvnoake 0:a4158fb0ba8a 159
bvnoake 0:a4158fb0ba8a 160 //sets obsy back to 0 so can go through the loop again setting back to start
bvnoake 0:a4158fb0ba8a 161 obsy = 0;
bvnoake 0:a4158fb0ba8a 162 /* recalls the function again to go through the system
bvnoake 0:a4158fb0ba8a 163 again with pixel starting at top and moving down again */
bvnoake 0:a4158fb0ba8a 164 play();
bvnoake 0:a4158fb0ba8a 165 }
bvnoake 0:a4158fb0ba8a 166
bvnoake 0:a4158fb0ba8a 167 //Main function
bvnoake 0:a4158fb0ba8a 168 int main()
bvnoake 0:a4158fb0ba8a 169 {
bvnoake 0:a4158fb0ba8a 170 // Initialise the micro:bit runtime.
bvnoake 0:a4158fb0ba8a 171 uBit.init();
bvnoake 0:a4158fb0ba8a 172 //sets two bottom pixels, one to be manually moved by buttons and one for the points system
bvnoake 0:a4158fb0ba8a 173 uBit.display.image.setPixelValue(x, y, 255);
bvnoake 0:a4158fb0ba8a 174 uBit.display.image.setPixelValue(x2, 4, 255);
bvnoake 0:a4158fb0ba8a 175 //To connect with the buttons so it's acknowledged when one is pressed
bvnoake 0:a4158fb0ba8a 176 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButton);
bvnoake 0:a4158fb0ba8a 177 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton);
bvnoake 0:a4158fb0ba8a 178 //plays game when game running set as true but if not displays game over
bvnoake 0:a4158fb0ba8a 179 if (gamerunning == true)
bvnoake 0:a4158fb0ba8a 180 {
bvnoake 0:a4158fb0ba8a 181 play();
bvnoake 0:a4158fb0ba8a 182 }
bvnoake 0:a4158fb0ba8a 183 else if (gamerunning == false)
bvnoake 0:a4158fb0ba8a 184 {
bvnoake 0:a4158fb0ba8a 185 uBit.display.scroll("Game Over");
bvnoake 0:a4158fb0ba8a 186 uBit.display.scroll(points);
bvnoake 0:a4158fb0ba8a 187
bvnoake 0:a4158fb0ba8a 188 }
bvnoake 0:a4158fb0ba8a 189 // If main exits, there may still be other fibers running or
bvnoake 0:a4158fb0ba8a 190 // registered event handlers etc.
bvnoake 0:a4158fb0ba8a 191 // Simply release this fiber, which will mean we enter the
bvnoake 0:a4158fb0ba8a 192 // scheduler. Worse case, we then
bvnoake 0:a4158fb0ba8a 193 // sit in the idle task forever, in a power efficient sleep.
bvnoake 0:a4158fb0ba8a 194 release_fiber();
bvnoake 0:a4158fb0ba8a 195 }