Samples of how to use the micro:bit DAL with mbed. This is a hg clone of the real git repo that can be found here: https://github.com/lancaster-university/microbit-samples
Dependencies: microbit
Fork of microbit-samples by
Snake.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #include "MicroBit.h" 00027 #include "MicroBitSamples.h" 00028 00029 #ifdef MICROBIT_SAMPLE_SNAKE 00030 00031 #define SNAKE_EMPTY 0 00032 #define SNAKE_UP 1 00033 #define SNAKE_LEFT 2 00034 #define SNAKE_RIGHT 3 00035 #define SNAKE_DOWN 4 00036 00037 #define SNAKE_FRAME_DELAY 350 00038 #define GROWTH_SPEED 3 00039 00040 struct Point 00041 { 00042 int x; 00043 int y; 00044 }; 00045 00046 MicroBit uBit; 00047 Point head; // Location of the head of our snake. 00048 Point tail; // Location of the tail of our snake. 00049 Point food; // Location of food. 00050 MicroBitImage map(5,5); 00051 00052 void place_food() 00053 { 00054 int r = uBit.random(24); 00055 int x = 0; int y = 0; 00056 00057 while (r > 0) 00058 { 00059 x = (x+1) % 5; 00060 if (x == 0) 00061 y = (y+1) % 5; 00062 00063 if(map.getPixelValue(x,y) == SNAKE_EMPTY) 00064 r--; 00065 } 00066 00067 food.x = x; 00068 food.y = y; 00069 } 00070 00071 void snake() 00072 { 00073 Point newHead; // Calculated placement of new head position based on user input. 00074 int hdirection; // Head's direction of travel 00075 int tdirection; // Tail's direction of travel 00076 int snakeLength; // number of segments in the snake. 00077 int growing; // boolean state indicating if we've just eaten some food. 00078 int score; 00079 00080 // Start in the middle of the screen. 00081 tail.x = tail.y = 2; 00082 head.x = head.y = 2; 00083 snakeLength = 1; 00084 growing = 0; 00085 score = 0; 00086 map.clear(); 00087 00088 uBit.display.image.setPixelValue(head.x, head.y, 255); 00089 00090 // Add some random food. 00091 place_food(); 00092 00093 while (1) 00094 { 00095 // Flash the food is necessary; 00096 uBit.display.image.setPixelValue(food.x, food.y, uBit.systemTime() % 1000 < 500 ? 0 : 255); 00097 00098 int dx = uBit.accelerometer.getX(); 00099 int dy = uBit.accelerometer.getY(); 00100 00101 newHead.x = head.x; 00102 newHead.y = head.y; 00103 00104 if (abs(dx) > abs(dy)) 00105 { 00106 if(dx < 0) 00107 { 00108 hdirection = SNAKE_LEFT; 00109 newHead.x = newHead.x == 0 ? 4 : newHead.x-1; 00110 } 00111 else 00112 { 00113 hdirection = SNAKE_RIGHT; 00114 newHead.x = newHead.x == 4 ? 0 : newHead.x+1; 00115 } 00116 } 00117 else 00118 { 00119 if(dy < 0) 00120 { 00121 hdirection = SNAKE_UP; 00122 newHead.y = newHead.y == 0 ? 4 : newHead.y-1; 00123 } 00124 else 00125 { 00126 hdirection = SNAKE_DOWN; 00127 newHead.y = newHead.y == 4 ? 0 : newHead.y+1; 00128 } 00129 } 00130 00131 int status = map.getPixelValue(newHead.x, newHead.y); 00132 if (status == SNAKE_UP || status == SNAKE_DOWN || status == SNAKE_LEFT || status == SNAKE_RIGHT) 00133 { 00134 uBit.display.scroll("GAME OVER! SCORE: "); 00135 uBit.display.scroll(score); 00136 00137 return; 00138 } 00139 00140 // move the head. 00141 map.setPixelValue(head.x, head.y, hdirection); 00142 uBit.display.image.setPixelValue(newHead.x, newHead.y, 255); 00143 00144 if (growing == GROWTH_SPEED) 00145 { 00146 growing = 0; 00147 snakeLength++; 00148 } 00149 else 00150 { 00151 // move the tail. 00152 tdirection = map.getPixelValue(tail.x,tail.y); 00153 map.setPixelValue(tail.x, tail.y, SNAKE_EMPTY); 00154 uBit.display.image.setPixelValue(tail.x, tail.y, 0); 00155 00156 // Move our record of the tail's location. 00157 if (snakeLength == 1) 00158 { 00159 tail.x = newHead.x; 00160 tail.y = newHead.y; 00161 } 00162 else 00163 { 00164 if (tdirection == SNAKE_UP) 00165 tail.y = tail.y == 0 ? 4 : tail.y-1; 00166 00167 if (tdirection == SNAKE_DOWN) 00168 tail.y = tail.y == 4 ? 0 : tail.y+1; 00169 00170 if (tdirection == SNAKE_LEFT) 00171 tail.x = tail.x == 0 ? 4 : tail.x-1; 00172 00173 if (tdirection == SNAKE_RIGHT) 00174 tail.x = tail.x == 4 ? 0 : tail.x+1; 00175 } 00176 } 00177 00178 // Update our record of the head location and away we go! 00179 head.x = newHead.x; 00180 head.y = newHead.y; 00181 00182 // if we've eaten some food, replace the food and grow ourselves! 00183 if (head.x == food.x && head.y == food.y) 00184 { 00185 growing++; 00186 score++; 00187 place_food(); 00188 } 00189 00190 uBit.sleep(SNAKE_FRAME_DELAY); 00191 } 00192 } 00193 00194 int main() 00195 { 00196 // Initialise the micro:bit runtime. 00197 uBit.init(); 00198 00199 // Insert your code here! 00200 uBit.display.scroll("SNAKE v1.0"); 00201 00202 while(1) 00203 snake(); 00204 } 00205 00206 #endif
Generated on Thu Jul 14 2022 08:55:58 by
