Cup and Ball game. Catch the ball at the top of the screen in the cup at the bottom of the screen.
main.cpp@0:f81fa18f9e85, 2016-08-14 (annotated)
- Committer:
- MikeDabb
- Date:
- Sun Aug 14 20:33:51 2016 +0000
- Revision:
- 0:f81fa18f9e85
- Child:
- 1:0d85663db186
First program on the Micro:Bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikeDabb | 0:f81fa18f9e85 | 1 | /* |
MikeDabb | 0:f81fa18f9e85 | 2 | The MIT License (MIT) |
MikeDabb | 0:f81fa18f9e85 | 3 | |
MikeDabb | 0:f81fa18f9e85 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
MikeDabb | 0:f81fa18f9e85 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
MikeDabb | 0:f81fa18f9e85 | 6 | |
MikeDabb | 0:f81fa18f9e85 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
MikeDabb | 0:f81fa18f9e85 | 8 | copy of this software and associated documentation files (the "Software"), |
MikeDabb | 0:f81fa18f9e85 | 9 | to deal in the Software without restriction, including without limitation |
MikeDabb | 0:f81fa18f9e85 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MikeDabb | 0:f81fa18f9e85 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
MikeDabb | 0:f81fa18f9e85 | 12 | Software is furnished to do so, subject to the following conditions: |
MikeDabb | 0:f81fa18f9e85 | 13 | |
MikeDabb | 0:f81fa18f9e85 | 14 | The above copyright notice and this permission notice shall be included in |
MikeDabb | 0:f81fa18f9e85 | 15 | all copies or substantial portions of the Software. |
MikeDabb | 0:f81fa18f9e85 | 16 | |
MikeDabb | 0:f81fa18f9e85 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MikeDabb | 0:f81fa18f9e85 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MikeDabb | 0:f81fa18f9e85 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
MikeDabb | 0:f81fa18f9e85 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MikeDabb | 0:f81fa18f9e85 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
MikeDabb | 0:f81fa18f9e85 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
MikeDabb | 0:f81fa18f9e85 | 23 | DEALINGS IN THE SOFTWARE. |
MikeDabb | 0:f81fa18f9e85 | 24 | */ |
MikeDabb | 0:f81fa18f9e85 | 25 | |
MikeDabb | 0:f81fa18f9e85 | 26 | //****** Cup and Ball game ****** |
MikeDabb | 0:f81fa18f9e85 | 27 | //the object of the game is to catch the ball in the cup. |
MikeDabb | 0:f81fa18f9e85 | 28 | //the cup is at the bottom of the screen and the ball appears |
MikeDabb | 0:f81fa18f9e85 | 29 | //at the top of the screen. The player has three lives before |
MikeDabb | 0:f81fa18f9e85 | 30 | //the game will end dslpaying the score. |
MikeDabb | 0:f81fa18f9e85 | 31 | |
MikeDabb | 0:f81fa18f9e85 | 32 | #include "MicroBit.h" |
MikeDabb | 0:f81fa18f9e85 | 33 | |
MikeDabb | 0:f81fa18f9e85 | 34 | MicroBit uBit; |
MikeDabb | 0:f81fa18f9e85 | 35 | |
MikeDabb | 0:f81fa18f9e85 | 36 | int ball_x = 0; |
MikeDabb | 0:f81fa18f9e85 | 37 | int ball_y = 0; |
MikeDabb | 0:f81fa18f9e85 | 38 | int loop = 0; |
MikeDabb | 0:f81fa18f9e85 | 39 | int before_x = 0; |
MikeDabb | 0:f81fa18f9e85 | 40 | int now_x = 0; |
MikeDabb | 0:f81fa18f9e85 | 41 | int cup_x = 0; |
MikeDabb | 0:f81fa18f9e85 | 42 | int score = 0; |
MikeDabb | 0:f81fa18f9e85 | 43 | int lives = 3; |
MikeDabb | 0:f81fa18f9e85 | 44 | int falling_speed = 500; |
MikeDabb | 0:f81fa18f9e85 | 45 | |
MikeDabb | 0:f81fa18f9e85 | 46 | const int cup_y = 4; |
MikeDabb | 0:f81fa18f9e85 | 47 | |
MikeDabb | 0:f81fa18f9e85 | 48 | bool game_running = true; |
MikeDabb | 0:f81fa18f9e85 | 49 | bool pause = false; |
MikeDabb | 0:f81fa18f9e85 | 50 | |
MikeDabb | 0:f81fa18f9e85 | 51 | // show your smiley face on the screen... |
MikeDabb | 0:f81fa18f9e85 | 52 | MicroBitImage smiley("0,0,0,0, 0\n0,255,0,255,0\n0,0,0,0,0\n255,0,0,0,255\n0,255,255,255,0\n"); |
MikeDabb | 0:f81fa18f9e85 | 53 | |
MikeDabb | 0:f81fa18f9e85 | 54 | // show your sad face on the screen... |
MikeDabb | 0:f81fa18f9e85 | 55 | MicroBitImage sad("0,0,0,0, 0\n0,255,0,255,0\n0,0,0,0,0\n0,255,255,255,0\n255,0,0,0,255\n"); |
MikeDabb | 0:f81fa18f9e85 | 56 | |
MikeDabb | 0:f81fa18f9e85 | 57 | |
MikeDabb | 0:f81fa18f9e85 | 58 | void onButtonA(MicroBitEvent e) |
MikeDabb | 0:f81fa18f9e85 | 59 | { |
MikeDabb | 0:f81fa18f9e85 | 60 | if(pause == false) |
MikeDabb | 0:f81fa18f9e85 | 61 | { |
MikeDabb | 0:f81fa18f9e85 | 62 | if(cup_x > 0) |
MikeDabb | 0:f81fa18f9e85 | 63 | { |
MikeDabb | 0:f81fa18f9e85 | 64 | uBit.display.image.setPixelValue(cup_x,cup_y,0); |
MikeDabb | 0:f81fa18f9e85 | 65 | cup_x = cup_x - 1; |
MikeDabb | 0:f81fa18f9e85 | 66 | uBit.display.image.setPixelValue(cup_x,cup_y,255); |
MikeDabb | 0:f81fa18f9e85 | 67 | } |
MikeDabb | 0:f81fa18f9e85 | 68 | } |
MikeDabb | 0:f81fa18f9e85 | 69 | } |
MikeDabb | 0:f81fa18f9e85 | 70 | |
MikeDabb | 0:f81fa18f9e85 | 71 | void onButtonB(MicroBitEvent e) |
MikeDabb | 0:f81fa18f9e85 | 72 | { |
MikeDabb | 0:f81fa18f9e85 | 73 | if(pause == false) |
MikeDabb | 0:f81fa18f9e85 | 74 | { |
MikeDabb | 0:f81fa18f9e85 | 75 | if(cup_x < 4) |
MikeDabb | 0:f81fa18f9e85 | 76 | { |
MikeDabb | 0:f81fa18f9e85 | 77 | uBit.display.image.setPixelValue(cup_x,cup_y,0); |
MikeDabb | 0:f81fa18f9e85 | 78 | cup_x = cup_x + 1; |
MikeDabb | 0:f81fa18f9e85 | 79 | uBit.display.image.setPixelValue(cup_x,cup_y,255); |
MikeDabb | 0:f81fa18f9e85 | 80 | } |
MikeDabb | 0:f81fa18f9e85 | 81 | } |
MikeDabb | 0:f81fa18f9e85 | 82 | } |
MikeDabb | 0:f81fa18f9e85 | 83 | |
MikeDabb | 0:f81fa18f9e85 | 84 | void ball_catch() |
MikeDabb | 0:f81fa18f9e85 | 85 | { |
MikeDabb | 0:f81fa18f9e85 | 86 | //disable the buttons |
MikeDabb | 0:f81fa18f9e85 | 87 | pause = true; |
MikeDabb | 0:f81fa18f9e85 | 88 | |
MikeDabb | 0:f81fa18f9e85 | 89 | score = score + 1; |
MikeDabb | 0:f81fa18f9e85 | 90 | uBit.display.clear(); |
MikeDabb | 0:f81fa18f9e85 | 91 | |
MikeDabb | 0:f81fa18f9e85 | 92 | for (int y=4; y >= 0; y--) |
MikeDabb | 0:f81fa18f9e85 | 93 | { |
MikeDabb | 0:f81fa18f9e85 | 94 | uBit.display.image.paste(smiley,0,y); |
MikeDabb | 0:f81fa18f9e85 | 95 | uBit.sleep(100); |
MikeDabb | 0:f81fa18f9e85 | 96 | } |
MikeDabb | 0:f81fa18f9e85 | 97 | |
MikeDabb | 0:f81fa18f9e85 | 98 | uBit.sleep(1000); |
MikeDabb | 0:f81fa18f9e85 | 99 | uBit.display.scroll(score); |
MikeDabb | 0:f81fa18f9e85 | 100 | uBit.sleep(1000); |
MikeDabb | 0:f81fa18f9e85 | 101 | uBit.display.clear(); |
MikeDabb | 0:f81fa18f9e85 | 102 | |
MikeDabb | 0:f81fa18f9e85 | 103 | //increase the speed of the ball by subtracting each time |
MikeDabb | 0:f81fa18f9e85 | 104 | falling_speed = (falling_speed - 30); |
MikeDabb | 0:f81fa18f9e85 | 105 | |
MikeDabb | 0:f81fa18f9e85 | 106 | //enable the buttons |
MikeDabb | 0:f81fa18f9e85 | 107 | pause = false; |
MikeDabb | 0:f81fa18f9e85 | 108 | } |
MikeDabb | 0:f81fa18f9e85 | 109 | |
MikeDabb | 0:f81fa18f9e85 | 110 | void ball_missed() |
MikeDabb | 0:f81fa18f9e85 | 111 | { |
MikeDabb | 0:f81fa18f9e85 | 112 | uBit.display.clear(); |
MikeDabb | 0:f81fa18f9e85 | 113 | lives = lives - 1; |
MikeDabb | 0:f81fa18f9e85 | 114 | |
MikeDabb | 0:f81fa18f9e85 | 115 | if(lives == 0) |
MikeDabb | 0:f81fa18f9e85 | 116 | { |
MikeDabb | 0:f81fa18f9e85 | 117 | game_running = false; |
MikeDabb | 0:f81fa18f9e85 | 118 | uBit.display.clear(); |
MikeDabb | 0:f81fa18f9e85 | 119 | uBit.display.scroll("GAME OVER! SCORE:"); |
MikeDabb | 0:f81fa18f9e85 | 120 | uBit.display.scroll(score); |
MikeDabb | 0:f81fa18f9e85 | 121 | } |
MikeDabb | 0:f81fa18f9e85 | 122 | else |
MikeDabb | 0:f81fa18f9e85 | 123 | { |
MikeDabb | 0:f81fa18f9e85 | 124 | for (int y=4; y >= 0; y--) |
MikeDabb | 0:f81fa18f9e85 | 125 | { |
MikeDabb | 0:f81fa18f9e85 | 126 | uBit.display.image.paste(sad,0,y); |
MikeDabb | 0:f81fa18f9e85 | 127 | uBit.sleep(100); |
MikeDabb | 0:f81fa18f9e85 | 128 | } |
MikeDabb | 0:f81fa18f9e85 | 129 | uBit.sleep(1000); |
MikeDabb | 0:f81fa18f9e85 | 130 | uBit.display.clear(); |
MikeDabb | 0:f81fa18f9e85 | 131 | } |
MikeDabb | 0:f81fa18f9e85 | 132 | } |
MikeDabb | 0:f81fa18f9e85 | 133 | |
MikeDabb | 0:f81fa18f9e85 | 134 | void welcome() |
MikeDabb | 0:f81fa18f9e85 | 135 | { |
MikeDabb | 0:f81fa18f9e85 | 136 | uBit.display.scroll("Ball Catch"); |
MikeDabb | 0:f81fa18f9e85 | 137 | } |
MikeDabb | 0:f81fa18f9e85 | 138 | |
MikeDabb | 0:f81fa18f9e85 | 139 | int main() |
MikeDabb | 0:f81fa18f9e85 | 140 | { |
MikeDabb | 0:f81fa18f9e85 | 141 | //display the welcome message |
MikeDabb | 0:f81fa18f9e85 | 142 | welcome(); |
MikeDabb | 0:f81fa18f9e85 | 143 | |
MikeDabb | 0:f81fa18f9e85 | 144 | // Initialise the micro:bit runtime. |
MikeDabb | 0:f81fa18f9e85 | 145 | uBit.init(); |
MikeDabb | 0:f81fa18f9e85 | 146 | |
MikeDabb | 0:f81fa18f9e85 | 147 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOWN, onButtonA); |
MikeDabb | 0:f81fa18f9e85 | 148 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_DOWN, onButtonB); |
MikeDabb | 0:f81fa18f9e85 | 149 | |
MikeDabb | 0:f81fa18f9e85 | 150 | while(game_running) |
MikeDabb | 0:f81fa18f9e85 | 151 | { |
MikeDabb | 0:f81fa18f9e85 | 152 | |
MikeDabb | 0:f81fa18f9e85 | 153 | //get random starting point for ball to drop from |
MikeDabb | 0:f81fa18f9e85 | 154 | do |
MikeDabb | 0:f81fa18f9e85 | 155 | {now_x = uBit.random(5); |
MikeDabb | 0:f81fa18f9e85 | 156 | }while (now_x == before_x); |
MikeDabb | 0:f81fa18f9e85 | 157 | |
MikeDabb | 0:f81fa18f9e85 | 158 | before_x = now_x; |
MikeDabb | 0:f81fa18f9e85 | 159 | ball_x = now_x; |
MikeDabb | 0:f81fa18f9e85 | 160 | ball_y = 0; |
MikeDabb | 0:f81fa18f9e85 | 161 | |
MikeDabb | 0:f81fa18f9e85 | 162 | |
MikeDabb | 0:f81fa18f9e85 | 163 | //get random starting point for cup at bottom screen |
MikeDabb | 0:f81fa18f9e85 | 164 | //cup_x = uBit.random(5); |
MikeDabb | 0:f81fa18f9e85 | 165 | do |
MikeDabb | 0:f81fa18f9e85 | 166 | {cup_x = uBit.random(5); |
MikeDabb | 0:f81fa18f9e85 | 167 | }while (cup_x == ball_x); |
MikeDabb | 0:f81fa18f9e85 | 168 | //check to make sure the starting point of the ball is not the same os the cup |
MikeDabb | 0:f81fa18f9e85 | 169 | //otherwise it would score a point even if not buttones were pressed |
MikeDabb | 0:f81fa18f9e85 | 170 | |
MikeDabb | 0:f81fa18f9e85 | 171 | |
MikeDabb | 0:f81fa18f9e85 | 172 | //display the cup on the screen at the bottom at a random location |
MikeDabb | 0:f81fa18f9e85 | 173 | uBit.display.image.setPixelValue(cup_x,cup_y,255); |
MikeDabb | 0:f81fa18f9e85 | 174 | |
MikeDabb | 0:f81fa18f9e85 | 175 | //wait a small amount of time after showing the cup |
MikeDabb | 0:f81fa18f9e85 | 176 | //and before showing the ball at the top of the screen |
MikeDabb | 0:f81fa18f9e85 | 177 | uBit.sleep(100); |
MikeDabb | 0:f81fa18f9e85 | 178 | |
MikeDabb | 0:f81fa18f9e85 | 179 | //game loop |
MikeDabb | 0:f81fa18f9e85 | 180 | for ( loop = 0; loop < 5; loop++ ) { |
MikeDabb | 0:f81fa18f9e85 | 181 | uBit.display.image.setPixelValue(ball_x,ball_y,255); |
MikeDabb | 0:f81fa18f9e85 | 182 | uBit.sleep(falling_speed); |
MikeDabb | 0:f81fa18f9e85 | 183 | uBit.display.image.setPixelValue(ball_x,ball_y,0); |
MikeDabb | 0:f81fa18f9e85 | 184 | ball_y = ball_y + 1; |
MikeDabb | 0:f81fa18f9e85 | 185 | |
MikeDabb | 0:f81fa18f9e85 | 186 | if(ball_y == 5) |
MikeDabb | 0:f81fa18f9e85 | 187 | { |
MikeDabb | 0:f81fa18f9e85 | 188 | //check to see if the cup has caught the ball |
MikeDabb | 0:f81fa18f9e85 | 189 | if((ball_x == cup_x) and ((ball_y - 1) == cup_y)) |
MikeDabb | 0:f81fa18f9e85 | 190 | { |
MikeDabb | 0:f81fa18f9e85 | 191 | ball_catch(); |
MikeDabb | 0:f81fa18f9e85 | 192 | } |
MikeDabb | 0:f81fa18f9e85 | 193 | else |
MikeDabb | 0:f81fa18f9e85 | 194 | { |
MikeDabb | 0:f81fa18f9e85 | 195 | ball_missed(); |
MikeDabb | 0:f81fa18f9e85 | 196 | } |
MikeDabb | 0:f81fa18f9e85 | 197 | } |
MikeDabb | 0:f81fa18f9e85 | 198 | } |
MikeDabb | 0:f81fa18f9e85 | 199 | |
MikeDabb | 0:f81fa18f9e85 | 200 | uBit.sleep(100); |
MikeDabb | 0:f81fa18f9e85 | 201 | } |
MikeDabb | 0:f81fa18f9e85 | 202 | } |
MikeDabb | 0:f81fa18f9e85 | 203 |