This is a slight mod of the source code uploaded by Sam Thys - https://os.mbed.com/users/sammekevremde/code/Snake/ Please check out the original project, this fork is only a hack of some working code to fix a few playability issues. See https://youtu.be/mUqJvReB1lg for a listing
Dependencies: C12832_lcd LM75B MMA7660 mbed
Fork of Snake by
main.cpp
00001 // original sources Sam Thys - https://os.mbed.com/users/sammekevremde/code/Snake/ 00002 // modified by: zaphodikus Jul 2018 00003 // uses thermometer and accelerometer on the LPC7760 to generate random 00004 // fruit positions 00005 #include "mbed.h" 00006 #include "C12832_lcd.h" 00007 #include "LM75B.h" 00008 #include "MMA7660.h" 00009 00010 MMA7660 MMA(p28,p27); // accelerometer 00011 C12832_LCD lcd; 00012 Ticker gameticker; 00013 LM75B tmp(p28,p27); // I2C Temperature Sensor 00014 00015 //-- Variable -- 00016 enum direction { up, down, left, right, null}; 00017 enum gamemode { start,stop,run,pause}; 00018 direction currentdirection; 00019 direction lastdirection; 00020 gamemode game; 00021 // -- Classes -- 00022 struct point 00023 { 00024 int x; 00025 int y; 00026 }; 00027 //--------------------- 00028 struct point snakeHead; 00029 struct point fruit; 00030 int countFruit=0; 00031 float board_temp; 00032 00033 point* snakeBody=new point[countFruit+5]; 00034 // -- I/O setting -- 00035 InterruptIn upHandler(p12); 00036 InterruptIn downHandler(p15); 00037 InterruptIn leftHandler(p13); 00038 InterruptIn rightHandler(p16); 00039 InterruptIn centerHandler(p14); 00040 00041 // -- Functions -- 00042 00043 void placeFruit() 00044 { 00045 board_temp = tmp; 00046 00047 int data[3]; 00048 MMA.readData((int*)&data); 00049 srand(data[0]*192 + data[1]*192 + data[2]*192 + board_temp*2048); 00050 fruit.x = rand() % 126 +1; 00051 fruit.y = rand() % 30 +1; 00052 } 00053 00054 void direction_up() 00055 { 00056 if ( currentdirection!=down) 00057 currentdirection=up; 00058 } 00059 void direction_down() 00060 { 00061 if ( currentdirection!=up) 00062 currentdirection=down; 00063 } 00064 void direction_left() 00065 { 00066 if ( currentdirection!=right) 00067 currentdirection=left; 00068 } 00069 void direction_right() 00070 { 00071 if ( currentdirection!=left) 00072 currentdirection=right; 00073 } 00074 00075 void center_button() 00076 { 00077 if (game==pause) 00078 { 00079 currentdirection=lastdirection; 00080 game=run; 00081 } 00082 else if (game==run) 00083 { 00084 lastdirection=currentdirection; 00085 currentdirection=null; 00086 game=pause; 00087 } 00088 else if (game==stop) 00089 { 00090 for (int i=0;i<=4;i++) 00091 { 00092 snakeBody[i].x=(snakeHead.x)-(i+1); 00093 snakeBody[i].y=15; 00094 } 00095 currentdirection=null; 00096 game=run; 00097 } 00098 } 00099 00100 void tickHandler() 00101 { 00102 00103 if (game==run) 00104 { 00105 if (currentdirection!=null) 00106 { 00107 for (int j=(countFruit+4);j>=1;j--) 00108 { 00109 snakeBody[j]=snakeBody[j-1]; 00110 } 00111 snakeBody[0]=snakeHead; 00112 } 00113 00114 switch (currentdirection) 00115 { 00116 case up: 00117 snakeHead.y+=1; 00118 break; 00119 case down: 00120 snakeHead.y-=1; 00121 break; 00122 case left: 00123 snakeHead.x-=1; 00124 break; 00125 case right: 00126 snakeHead.x+=1; 00127 break; 00128 00129 } 00130 00131 if ((snakeHead.y == 0 ||snakeHead.y == 31 ||snakeHead.x == 127 ||snakeHead.x == 0) ) 00132 { 00133 lcd.cls(); 00134 lcd.locate(25,5); 00135 lcd.printf("Game Over!!"); 00136 lcd.locate(25,15); 00137 lcd.printf("Score > %d",countFruit); 00138 currentdirection=null; 00139 snakeHead.x=15; 00140 snakeHead.y=15; 00141 game=stop; 00142 countFruit=0; 00143 } 00144 else if ((snakeHead.y == fruit.y )&& (snakeHead.x == fruit.x) ) 00145 { 00146 countFruit +=1; 00147 placeFruit(); 00148 } 00149 else 00150 { 00151 lcd.cls(); 00152 lcd.pixel(snakeHead.x,snakeHead.y,1); 00153 lcd.copy_to_lcd(); 00154 for(int k=0;k<=(countFruit +4);k++) 00155 { 00156 lcd.pixel(snakeBody[k].x,snakeBody[k].y,1); 00157 } 00158 lcd.pixel(fruit.x-1,fruit.y-1, 1); 00159 lcd.pixel(fruit.x-1,fruit.y, 1); 00160 lcd.pixel(fruit.x-1,fruit.y+1, 1); 00161 lcd.pixel(fruit.x,fruit.y-1, 1); 00162 lcd.pixel(fruit.x,fruit.y, 1); 00163 lcd.pixel(fruit.x,fruit.y+1, 1); 00164 lcd.pixel(fruit.x+1,fruit.y-1, 1); 00165 lcd.pixel(fruit.x+1,fruit.y, 1); 00166 lcd.pixel(fruit.x+1,fruit.y+1, 1); 00167 lcd.rect(0,0, 127, 31, 1); 00168 lcd.copy_to_lcd(); 00169 } 00170 00171 for (int a=0;a<=(countFruit +4);a++) 00172 { 00173 if ( snakeBody[a].x==snakeHead.x && snakeBody[a].y==snakeHead.y) 00174 { 00175 lcd.cls(); 00176 lcd.locate(25,5); 00177 lcd.printf("Game Over!!"); 00178 lcd.locate(25,15); 00179 lcd.printf("U score is %d",countFruit 00180 ); 00181 currentdirection=null; 00182 snakeHead.x=15; 00183 snakeHead.y=15; 00184 game=stop; 00185 countFruit 00186 =0; 00187 00188 } 00189 } 00190 } 00191 else if (game==pause) 00192 { 00193 lcd.cls(); 00194 lcd.locate(30,15); 00195 lcd.printf("pause"); 00196 } 00197 } 00198 00199 00200 //-------------------------------- 00201 int main() 00202 { 00203 game=run; 00204 lcd.rect(0, 0, 127, 31, 1); 00205 lcd.copy_to_lcd(); 00206 currentdirection=null; 00207 snakeHead.x=15; 00208 snakeHead.y=15; 00209 for (int i=0;i<=4;i++) 00210 { 00211 snakeBody[i].x=(snakeHead.x)-(i+1); 00212 snakeBody[i].y=15; 00213 } 00214 placeFruit(); 00215 gameticker.attach(&tickHandler, 0.1); 00216 // set interrupt handlers 00217 upHandler.rise(&direction_up); 00218 downHandler.rise(&direction_down); 00219 leftHandler.rise(&direction_left); 00220 rightHandler.rise(&direction_right); 00221 centerHandler.rise(¢er_button); 00222 00223 while (1) 00224 { 00225 00226 } 00227 }
Generated on Wed Jul 20 2022 19:51:03 by 1.7.2