snake game from https://github.com/lancaster-university/microbit-samples/tree/master/source/examples/snake

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "MicroBit.h"
00002 
00003 #define SNAKE_EMPTY 0
00004 #define SNAKE_UP    1
00005 #define SNAKE_LEFT  2
00006 #define SNAKE_RIGHT 3
00007 #define SNAKE_DOWN  4
00008 
00009 #define SNAKE_FRAME_DELAY   350
00010 #define GROWTH_SPEED        3
00011 
00012 struct Point
00013 {
00014     int     x;
00015     int     y;
00016 };
00017 
00018 MicroBit        uBit;
00019 Point           head;                 // Location of the head of our snake.
00020 Point           tail;                 // Location of the tail of our snake.
00021 Point           food;                 // Location of food.
00022 MicroBitImage   map(5,5);  
00023 
00024 void place_food()
00025 {
00026     int r = uBit.random(24);
00027     int x = 0; int y = 0;
00028     
00029     while (r > 0)
00030     {
00031         x = (x+1) % 5;
00032         if (x == 0)
00033             y = (y+1) % 5;
00034             
00035         if(map.getPixelValue(x,y) == SNAKE_EMPTY)
00036             r--;
00037     }
00038     
00039     food.x = x;
00040     food.y = y;
00041 }
00042 
00043 void snake()
00044 {   
00045     Point newHead;              // Calculated placement of new head position based on user input.    
00046     int hdirection;             // Head's direction of travel
00047     int tdirection;             // Tail's direction of travel
00048     int snakeLength;            // number of segments in the snake.
00049     int growing;                // boolean state indicating if we've just eaten some food.
00050     int score;
00051     
00052     // Start in the middle of the screen.
00053     tail.x = tail.y = 2;    
00054     head.x = head.y = 2;
00055     snakeLength = 1;
00056     growing = 0;
00057     score = 0;
00058     map.clear();
00059         
00060     uBit.display.image.setPixelValue(head.x, head.y, 255);
00061         
00062     // Add some random food.    
00063     place_food();
00064         
00065     while (1)
00066     {    
00067         // Flash the food is necessary;       
00068         uBit.display.image.setPixelValue(food.x, food.y, uBit.systemTime() % 1000 < 500 ? 0 : 255);
00069           
00070         int dx = uBit.accelerometer.getX();
00071         int dy = uBit.accelerometer.getY();
00072         
00073         newHead.x = head.x;
00074         newHead.y = head.y;
00075         
00076         if (abs(dx) > abs(dy))
00077         {
00078             if(dx < 0)
00079             {
00080                 hdirection = SNAKE_LEFT;
00081                 newHead.x = newHead.x == 0 ? 4 : newHead.x-1;
00082             }
00083             else
00084             {
00085                 hdirection = SNAKE_RIGHT;
00086                 newHead.x = newHead.x == 4 ? 0 : newHead.x+1;
00087             }            
00088         }
00089         else    
00090         {
00091             if(dy < 0)
00092             {
00093                 hdirection = SNAKE_UP;
00094                 newHead.y = newHead.y == 0 ? 4 : newHead.y-1;
00095             }
00096             else
00097             {
00098                 hdirection = SNAKE_DOWN;
00099                 newHead.y = newHead.y == 4 ? 0 : newHead.y+1;
00100             }
00101         }           
00102         
00103         int status = map.getPixelValue(newHead.x, newHead.y);
00104         if (status == SNAKE_UP || status == SNAKE_DOWN || status == SNAKE_LEFT || status == SNAKE_RIGHT)
00105         {
00106             uBit.display.scroll("GAME OVER! SCORE: ");
00107             uBit.display.scroll(score);
00108             
00109             return;            
00110         }
00111                                           
00112         // move the head.       
00113         map.setPixelValue(head.x, head.y, hdirection);
00114         uBit.display.image.setPixelValue(newHead.x, newHead.y, 255);
00115 
00116         if (growing == GROWTH_SPEED)
00117         {
00118             growing = 0;
00119             snakeLength++;
00120         }
00121         else
00122         {        
00123             // move the tail.
00124             tdirection = map.getPixelValue(tail.x,tail.y);     
00125             map.setPixelValue(tail.x, tail.y, SNAKE_EMPTY);         
00126             uBit.display.image.setPixelValue(tail.x, tail.y, 0);
00127     
00128             // Move our record of the tail's location.        
00129             if (snakeLength == 1)
00130             {
00131                 tail.x = newHead.x;
00132                 tail.y = newHead.y;
00133             }
00134             else
00135             {
00136                 if (tdirection == SNAKE_UP)
00137                     tail.y = tail.y == 0 ? 4 : tail.y-1;
00138                 
00139                 if (tdirection == SNAKE_DOWN)
00140                     tail.y = tail.y == 4 ? 0 : tail.y+1;
00141             
00142                 if (tdirection == SNAKE_LEFT)
00143                     tail.x = tail.x == 0 ? 4 : tail.x-1;
00144                 
00145                 if (tdirection == SNAKE_RIGHT)
00146                     tail.x = tail.x == 4 ? 0 : tail.x+1;
00147             }
00148         }
00149 
00150         // Update our record of the head location and away we go!
00151         head.x = newHead.x;
00152         head.y = newHead.y;
00153       
00154         // if we've eaten some food, replace the food and grow ourselves!
00155         if (head.x == food.x && head.y == food.y)
00156         {
00157             growing++;
00158             score++;
00159             place_food();
00160         }
00161       
00162         uBit.sleep(SNAKE_FRAME_DELAY);   
00163     }   
00164 }
00165 
00166 int main()
00167 {
00168     // Initialise the micro:bit runtime.
00169     uBit.init();
00170 
00171     // Insert your code here!
00172     uBit.display.scroll("SNAKE v1.0");
00173 
00174     while(1)
00175         snake();
00176 }
00177