complete

Dependencies:   FatFileSystem mbed

Fork of SnakeSnakeGame by Tho Jian Xiang

Committer:
Rexry7878
Date:
Tue Aug 11 05:25:56 2015 +0000
Revision:
2:ac02c7ff905b
Parent:
1:4222a8f9ca88
complete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbhatnagar3 0:8b08136c5edd 1 //HEADER FILES
pbhatnagar3 0:8b08136c5edd 2 #include "mbed.h"
pbhatnagar3 0:8b08136c5edd 3 #include "C12832.h"
pbhatnagar3 0:8b08136c5edd 4 #include "MMA7660.h"
pbhatnagar3 0:8b08136c5edd 5 #include <time.h>
pbhatnagar3 0:8b08136c5edd 6 #include <stdlib.h>
superlova 1:4222a8f9ca88 7 #include "MSCFileSystem.h"
superlova 1:4222a8f9ca88 8 #include "SongPlayer.h"
pbhatnagar3 0:8b08136c5edd 9
pbhatnagar3 0:8b08136c5edd 10 //IMPORTANT INITIALIZATIONS
pbhatnagar3 0:8b08136c5edd 11 Serial pc(USBTX, USBRX);
pbhatnagar3 0:8b08136c5edd 12 C12832 lcd(p5, p7, p6, p8, p11);
pbhatnagar3 0:8b08136c5edd 13 BusIn joy(p15,p12,p13,p16);
pbhatnagar3 0:8b08136c5edd 14 DigitalIn fire(p14);
pbhatnagar3 0:8b08136c5edd 15 BusOut leds(LED1,LED2,LED3,LED4);
superlova 1:4222a8f9ca88 16 MMA7660 MMA(p28, p27); //I2C Accelerometer
superlova 1:4222a8f9ca88 17 DigitalOut connectionLed(LED1); //Accel OK LED
superlova 1:4222a8f9ca88 18 LocalFileSystem local("local"); // Create the local filesystem
superlova 1:4222a8f9ca88 19 MSCFileSystem msc("msc");
superlova 1:4222a8f9ca88 20 PwmOut r(p23); //RGB LED with 3 PWM outputs for dimmer control
superlova 1:4222a8f9ca88 21 PwmOut g(p24);
superlova 1:4222a8f9ca88 22 PwmOut b(p25);
superlova 1:4222a8f9ca88 23 SongPlayer speaker(p26); //Speaker with PWM driver
superlova 1:4222a8f9ca88 24 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
superlova 1:4222a8f9ca88 25 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
superlova 1:4222a8f9ca88 26 };
superlova 1:4222a8f9ca88 27 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
superlova 1:4222a8f9ca88 28 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
superlova 1:4222a8f9ca88 29 };
superlova 1:4222a8f9ca88 30 AnalogIn pot1(p19); //Reads Pot 1 - near LCD
superlova 1:4222a8f9ca88 31 AnalogIn pot2(p20); //Reads Pot 2 - near RGB LED
pbhatnagar3 0:8b08136c5edd 32
pbhatnagar3 0:8b08136c5edd 33
superlova 1:4222a8f9ca88 34 int rad; //variable for random number
Rexry7878 2:ac02c7ff905b 35 int p1=50; //x-coordinate for food
Rexry7878 2:ac02c7ff905b 36 int p2=15; //y-coordinate for food
superlova 1:4222a8f9ca88 37 int fDimX;
superlova 1:4222a8f9ca88 38 int fDimY;
superlova 1:4222a8f9ca88 39
pbhatnagar3 0:8b08136c5edd 40 int main()
pbhatnagar3 0:8b08136c5edd 41 {
pbhatnagar3 0:8b08136c5edd 42 //IMPORTANT VARIABLE DECLARATIONS
pbhatnagar3 0:8b08136c5edd 43 srand(time(NULL));
superlova 1:4222a8f9ca88 44
superlova 1:4222a8f9ca88 45 int i=0; //variable for x-axis
superlova 1:4222a8f9ca88 46 int dimX = 100; //space for snake can move(x-axis)
superlova 1:4222a8f9ca88 47 int dimY = 26; //space for snake can move(y-axis)
superlova 1:4222a8f9ca88 48
superlova 1:4222a8f9ca88 49 speaker.PlaySong(note,duration);
pbhatnagar3 0:8b08136c5edd 50 lcd.cls();
superlova 1:4222a8f9ca88 51 lcd.locate(5,10);
superlova 1:4222a8f9ca88 52 lcd.printf("welcome to snake game");
superlova 1:4222a8f9ca88 53 wait(2);
superlova 1:4222a8f9ca88 54 lcd.locate(5,10);
superlova 1:4222a8f9ca88 55 lcd.printf("Please do not hit the wall");
pbhatnagar3 0:8b08136c5edd 56 wait(2);
pbhatnagar3 0:8b08136c5edd 57 lcd.cls();
superlova 1:4222a8f9ca88 58
superlova 1:4222a8f9ca88 59 int j = 0; //variable for y-axis
superlova 1:4222a8f9ca88 60 int cst = 4; //cast size
superlova 1:4222a8f9ca88 61 fDimY = dimY - cst;
superlova 1:4222a8f9ca88 62 fDimX = dimX - cst;
superlova 1:4222a8f9ca88 63 int score = 0; //used to store score
superlova 1:4222a8f9ca88 64 int flag = 0; //used as flag
superlova 1:4222a8f9ca88 65 int back_flag=0; //used as flag
superlova 1:4222a8f9ca88 66 int forward_flag=0; //used as flag
superlova 1:4222a8f9ca88 67 int up_flag=0; //used as flag
superlova 1:4222a8f9ca88 68 int down_flag=0; //used as flag
superlova 1:4222a8f9ca88 69 int end_flag=0; //used as flag
superlova 1:4222a8f9ca88 70 r=1.0; //RGB LED off - PWM 100% duty cycle
superlova 1:4222a8f9ca88 71 g=1.0;
superlova 1:4222a8f9ca88 72 b=1.0;
Rexry7878 2:ac02c7ff905b 73 int x=0, y=0;
Rexry7878 2:ac02c7ff905b 74 int bonus = 0;
superlova 1:4222a8f9ca88 75
superlova 1:4222a8f9ca88 76 while(1)
superlova 1:4222a8f9ca88 77 {
Rexry7878 2:ac02c7ff905b 78 //CONDITION FOR CHECKING FOR THE FIREBALL
Rexry7878 2:ac02c7ff905b 79 if (fire && score>=5) {
Rexry7878 2:ac02c7ff905b 80 leds=0xf;
Rexry7878 2:ac02c7ff905b 81 flag = 100;
Rexry7878 2:ac02c7ff905b 82 if(bonus == 0){
Rexry7878 2:ac02c7ff905b 83 score -= 2;
Rexry7878 2:ac02c7ff905b 84 bonus = 1;
Rexry7878 2:ac02c7ff905b 85 }
Rexry7878 2:ac02c7ff905b 86 }
superlova 1:4222a8f9ca88 87 // CONDITIONS TO CHECK FOR JOYSTICK INPUT
pbhatnagar3 0:8b08136c5edd 88 leds=joy;
superlova 1:4222a8f9ca88 89 lcd.locate(i,j);
superlova 1:4222a8f9ca88 90 lcd.printf("~+");
superlova 1:4222a8f9ca88 91 // move backwards
superlova 1:4222a8f9ca88 92 if (joy==0x4)
superlova 1:4222a8f9ca88 93 {
superlova 1:4222a8f9ca88 94 i=i-cst;
superlova 1:4222a8f9ca88 95 back_flag=1;
superlova 1:4222a8f9ca88 96 do{
Rexry7878 2:ac02c7ff905b 97 b = 1.0- pot2;
superlova 1:4222a8f9ca88 98 i--;
superlova 1:4222a8f9ca88 99 lcd.locate(i,j);
superlova 1:4222a8f9ca88 100 lcd.printf("~+");
superlova 1:4222a8f9ca88 101 wait(0.2);
superlova 1:4222a8f9ca88 102 lcd.cls();
superlova 1:4222a8f9ca88 103 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 104 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 105 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 106 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 107 lcd.printf(".");
superlova 1:4222a8f9ca88 108 if(i<0){
superlova 1:4222a8f9ca88 109 end_flag=1;
superlova 1:4222a8f9ca88 110 do{
Rexry7878 2:ac02c7ff905b 111 r=1.0-pot2;
superlova 1:4222a8f9ca88 112 lcd.cls();
superlova 1:4222a8f9ca88 113 lcd.locate(25,10);
superlova 1:4222a8f9ca88 114 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 115 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 116 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 117 fclose(fp1);
superlova 1:4222a8f9ca88 118 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 119 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 120 fclose(fp);
superlova 1:4222a8f9ca88 121 wait(5);
superlova 1:4222a8f9ca88 122 }while(end_flag==1);}
superlova 1:4222a8f9ca88 123 if(joy==0x8||joy==0x1||joy==0x2){
superlova 1:4222a8f9ca88 124 back_flag=0;
superlova 1:4222a8f9ca88 125 break;}
superlova 1:4222a8f9ca88 126 }while(back_flag==1);
superlova 1:4222a8f9ca88 127 }
superlova 1:4222a8f9ca88 128 //move forward
superlova 1:4222a8f9ca88 129 if (joy==0x8)
superlova 1:4222a8f9ca88 130 {
superlova 1:4222a8f9ca88 131 i=i+cst;
superlova 1:4222a8f9ca88 132 forward_flag=1;
superlova 1:4222a8f9ca88 133 do{
Rexry7878 2:ac02c7ff905b 134 b = 1.0- pot2;
superlova 1:4222a8f9ca88 135 i++;
superlova 1:4222a8f9ca88 136 lcd.locate(i,j);
superlova 1:4222a8f9ca88 137 lcd.printf("~+");
superlova 1:4222a8f9ca88 138 wait(0.2);
superlova 1:4222a8f9ca88 139 lcd.cls();
superlova 1:4222a8f9ca88 140 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 141 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 142 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 143 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 144 lcd.printf(".");
superlova 1:4222a8f9ca88 145 if(i>100){
superlova 1:4222a8f9ca88 146 end_flag=1;
superlova 1:4222a8f9ca88 147 do{
Rexry7878 2:ac02c7ff905b 148 r=1.0-pot2;
superlova 1:4222a8f9ca88 149 lcd.cls();
superlova 1:4222a8f9ca88 150 lcd.locate(25,10);
superlova 1:4222a8f9ca88 151 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 152 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 153 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 154 fclose(fp1);
superlova 1:4222a8f9ca88 155 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 156 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 157 fclose(fp);
superlova 1:4222a8f9ca88 158 wait(5);
superlova 1:4222a8f9ca88 159 }while(end_flag==1);}
superlova 1:4222a8f9ca88 160 if(joy==0x4||joy==0x1||joy==0x2){
superlova 1:4222a8f9ca88 161 forward_flag=0;
superlova 1:4222a8f9ca88 162 break;}
superlova 1:4222a8f9ca88 163 }while(forward_flag==1);
pbhatnagar3 0:8b08136c5edd 164 }
superlova 1:4222a8f9ca88 165 //move up
superlova 1:4222a8f9ca88 166 if (joy==0x1)
superlova 1:4222a8f9ca88 167 {
superlova 1:4222a8f9ca88 168 j=j-cst;
superlova 1:4222a8f9ca88 169 up_flag=1;
superlova 1:4222a8f9ca88 170 do{
Rexry7878 2:ac02c7ff905b 171 b = 1.0- pot2;
superlova 1:4222a8f9ca88 172 j--;
superlova 1:4222a8f9ca88 173 lcd.locate(i,j);
superlova 1:4222a8f9ca88 174 lcd.printf("~+");
superlova 1:4222a8f9ca88 175 wait(0.2);
superlova 1:4222a8f9ca88 176 lcd.cls();
superlova 1:4222a8f9ca88 177 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 178 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 179 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 180 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 181 lcd.printf(".");
superlova 1:4222a8f9ca88 182 if(j<0){
superlova 1:4222a8f9ca88 183 end_flag=1;
superlova 1:4222a8f9ca88 184 do{
Rexry7878 2:ac02c7ff905b 185 r=1.0-pot2;
superlova 1:4222a8f9ca88 186 lcd.cls();
superlova 1:4222a8f9ca88 187 lcd.locate(25,10);
superlova 1:4222a8f9ca88 188 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 189 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 190 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 191 fclose(fp1);
superlova 1:4222a8f9ca88 192 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 193 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 194 fclose(fp);
superlova 1:4222a8f9ca88 195 wait(5);
superlova 1:4222a8f9ca88 196 }while(end_flag==1);}
superlova 1:4222a8f9ca88 197 if(joy==0x4||joy==0x8||joy==0x2){
superlova 1:4222a8f9ca88 198 up_flag=0;
superlova 1:4222a8f9ca88 199 break;}
superlova 1:4222a8f9ca88 200 }while(up_flag==1);
superlova 1:4222a8f9ca88 201 }
superlova 1:4222a8f9ca88 202 //move down
superlova 1:4222a8f9ca88 203 if (joy==0x2)
superlova 1:4222a8f9ca88 204 {
superlova 1:4222a8f9ca88 205 j=j+cst;
superlova 1:4222a8f9ca88 206 down_flag=1;
superlova 1:4222a8f9ca88 207 do{
Rexry7878 2:ac02c7ff905b 208 b = 1.0- pot2;
superlova 1:4222a8f9ca88 209 j++;
superlova 1:4222a8f9ca88 210 lcd.locate(i,j);
superlova 1:4222a8f9ca88 211 lcd.printf("~+");
superlova 1:4222a8f9ca88 212 wait(0.2);
superlova 1:4222a8f9ca88 213 lcd.cls();
superlova 1:4222a8f9ca88 214 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 215 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 216 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 217 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 218 lcd.printf(".");
superlova 1:4222a8f9ca88 219 if(j>26){
superlova 1:4222a8f9ca88 220 end_flag=1;
superlova 1:4222a8f9ca88 221 do{
Rexry7878 2:ac02c7ff905b 222 r=1.0-pot2;
superlova 1:4222a8f9ca88 223 lcd.cls();
superlova 1:4222a8f9ca88 224 lcd.locate(25,10);
superlova 1:4222a8f9ca88 225 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 226 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 227 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 228 fclose(fp1);
superlova 1:4222a8f9ca88 229 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 230 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 231 fclose(fp);
superlova 1:4222a8f9ca88 232 wait(5);
superlova 1:4222a8f9ca88 233 }while(end_flag==1);}
superlova 1:4222a8f9ca88 234 if(joy==0x4||joy==0x8||joy==0x1){
superlova 1:4222a8f9ca88 235 down_flag=0;
superlova 1:4222a8f9ca88 236 break;}
superlova 1:4222a8f9ca88 237 }while(down_flag==1);
superlova 1:4222a8f9ca88 238 }
Rexry7878 2:ac02c7ff905b 239
Rexry7878 2:ac02c7ff905b 240 // LOGIC FOR THE FIREBALL
Rexry7878 2:ac02c7ff905b 241 if (flag>=1){
Rexry7878 2:ac02c7ff905b 242 x = (x + MMA.x() * 32.0)/1.5;
Rexry7878 2:ac02c7ff905b 243 y = (y -(MMA.y() * 16.0))/1.5;
Rexry7878 2:ac02c7ff905b 244 lcd.fillcircle(x+63, y+15, 3, 1); //draw bubble
Rexry7878 2:ac02c7ff905b 245 //lcd.circle(63, 15, 8, 1);
Rexry7878 2:ac02c7ff905b 246 wait(.1); //time delay
Rexry7878 2:ac02c7ff905b 247 flag -=1;
Rexry7878 2:ac02c7ff905b 248 if (abs(x + 63 - p1) <=cst && abs(y + 15 - p2) <=cst+1){
Rexry7878 2:ac02c7ff905b 249 score +=15;
Rexry7878 2:ac02c7ff905b 250 flag = -1;
Rexry7878 2:ac02c7ff905b 251 pc.printf(" score %d\n\r", score);
Rexry7878 2:ac02c7ff905b 252 g = 1.0-pot2;
Rexry7878 2:ac02c7ff905b 253 wait(0.3);
Rexry7878 2:ac02c7ff905b 254 g = 1.0;
Rexry7878 2:ac02c7ff905b 255 b = 1.0 - pot2;
Rexry7878 2:ac02c7ff905b 256 }
Rexry7878 2:ac02c7ff905b 257
Rexry7878 2:ac02c7ff905b 258 if (flag < 0)
Rexry7878 2:ac02c7ff905b 259 flag = 0;
Rexry7878 2:ac02c7ff905b 260 lcd.fillcircle(x+63, y+15, 3, 0); //erase bubble
Rexry7878 2:ac02c7ff905b 261 //finding a new random location for food
Rexry7878 2:ac02c7ff905b 262 rad=rand();
Rexry7878 2:ac02c7ff905b 263 p1= rad%fDimX;
Rexry7878 2:ac02c7ff905b 264 p2= rad%fDimY;
Rexry7878 2:ac02c7ff905b 265 if (p1 < 30)
Rexry7878 2:ac02c7ff905b 266 p1 = 30;
Rexry7878 2:ac02c7ff905b 267 if (p2 < 10)
Rexry7878 2:ac02c7ff905b 268 p2 = 10;
Rexry7878 2:ac02c7ff905b 269 lcd.printf(".");
Rexry7878 2:ac02c7ff905b 270 }
Rexry7878 2:ac02c7ff905b 271
superlova 1:4222a8f9ca88 272
superlova 1:4222a8f9ca88 273 if (flag == 0)
superlova 1:4222a8f9ca88 274 {
Rexry7878 2:ac02c7ff905b 275 lcd.cls();
Rexry7878 2:ac02c7ff905b 276 lcd.locate(i,j);
Rexry7878 2:ac02c7ff905b 277 lcd.printf("~+");
superlova 1:4222a8f9ca88 278 //print score on the LCD
superlova 1:4222a8f9ca88 279 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 280 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 281 //print food on the LCD
superlova 1:4222a8f9ca88 282 lcd.locate(p1, p2);
pbhatnagar3 0:8b08136c5edd 283 lcd.printf(".");
superlova 1:4222a8f9ca88 284 // CONDITION FOR CHECKING THE SNAKE FOOD COLLISION
superlova 1:4222a8f9ca88 285 if (abs(i + 8 - p1) <=cst && abs(j - p2) <=cst+1)
Rexry7878 2:ac02c7ff905b 286 {
superlova 1:4222a8f9ca88 287 score = score + 1;
superlova 1:4222a8f9ca88 288 pc.printf("score %d\n\r", score);
superlova 1:4222a8f9ca88 289 g = 1.0 - pot2; //RGB LED green
Rexry7878 2:ac02c7ff905b 290 wait(0.5);
Rexry7878 2:ac02c7ff905b 291 g = 1.0;
superlova 1:4222a8f9ca88 292 b = 1.0 - pot2;
superlova 1:4222a8f9ca88 293 //finding a new random location for food
superlova 1:4222a8f9ca88 294 rad=rand();
superlova 1:4222a8f9ca88 295 p1= rad%fDimX;
superlova 1:4222a8f9ca88 296 p2= rad%fDimY;
superlova 1:4222a8f9ca88 297 if (p1 < 30)
superlova 1:4222a8f9ca88 298 p1 = 30;
superlova 1:4222a8f9ca88 299 if (p2 < 10)
superlova 1:4222a8f9ca88 300 p2 = 10;
superlova 1:4222a8f9ca88 301 lcd.printf(".");
superlova 1:4222a8f9ca88 302 }
pbhatnagar3 0:8b08136c5edd 303 wait(0.3);
pbhatnagar3 0:8b08136c5edd 304 }
superlova 1:4222a8f9ca88 305 }//while(1)
superlova 1:4222a8f9ca88 306 }//(int main)