for rex

Dependencies:   FatFileSystem mbed

Fork of SnakeGame by Pujun Bhatnagar

Committer:
superlova
Date:
Sat Aug 08 09:32:56 2015 +0000
Revision:
1:4222a8f9ca88
Parent:
0:8b08136c5edd
for rexry

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
superlova 1:4222a8f9ca88 35 int p1=10; //x-coordinate for food
superlova 1:4222a8f9ca88 36 int p2=20; //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;
superlova 1:4222a8f9ca88 73
superlova 1:4222a8f9ca88 74 while(1)
superlova 1:4222a8f9ca88 75 {
superlova 1:4222a8f9ca88 76 // CONDITIONS TO CHECK FOR JOYSTICK INPUT
pbhatnagar3 0:8b08136c5edd 77 leds=joy;
superlova 1:4222a8f9ca88 78 lcd.locate(i,j);
superlova 1:4222a8f9ca88 79 lcd.printf("~+");
superlova 1:4222a8f9ca88 80 // move backwards
superlova 1:4222a8f9ca88 81 if (joy==0x4)
superlova 1:4222a8f9ca88 82 {
superlova 1:4222a8f9ca88 83 i=i-cst;
superlova 1:4222a8f9ca88 84 back_flag=1;
superlova 1:4222a8f9ca88 85 do{
superlova 1:4222a8f9ca88 86 i--;
superlova 1:4222a8f9ca88 87 lcd.locate(i,j);
superlova 1:4222a8f9ca88 88 lcd.printf("~+");
superlova 1:4222a8f9ca88 89 wait(0.2);
superlova 1:4222a8f9ca88 90 lcd.cls();
superlova 1:4222a8f9ca88 91 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 92 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 93 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 94 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 95 lcd.printf(".");
superlova 1:4222a8f9ca88 96 if(i<0){
superlova 1:4222a8f9ca88 97 end_flag=1;
superlova 1:4222a8f9ca88 98 r=1.0-pot2;
superlova 1:4222a8f9ca88 99 do{
superlova 1:4222a8f9ca88 100 lcd.cls();
superlova 1:4222a8f9ca88 101 lcd.locate(25,10);
superlova 1:4222a8f9ca88 102 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 103 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 104 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 105 fclose(fp1);
superlova 1:4222a8f9ca88 106 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 107 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 108 fclose(fp);
superlova 1:4222a8f9ca88 109 wait(5);
superlova 1:4222a8f9ca88 110 }while(end_flag==1);}
superlova 1:4222a8f9ca88 111 if(joy==0x8||joy==0x1||joy==0x2){
superlova 1:4222a8f9ca88 112 back_flag=0;
superlova 1:4222a8f9ca88 113 break;}
superlova 1:4222a8f9ca88 114 }while(back_flag==1);
superlova 1:4222a8f9ca88 115 }
superlova 1:4222a8f9ca88 116 //move forward
superlova 1:4222a8f9ca88 117 if (joy==0x8)
superlova 1:4222a8f9ca88 118 {
superlova 1:4222a8f9ca88 119 i=i+cst;
superlova 1:4222a8f9ca88 120 forward_flag=1;
superlova 1:4222a8f9ca88 121 do{
superlova 1:4222a8f9ca88 122 i++;
superlova 1:4222a8f9ca88 123 lcd.locate(i,j);
superlova 1:4222a8f9ca88 124 lcd.printf("~+");
superlova 1:4222a8f9ca88 125 wait(0.2);
superlova 1:4222a8f9ca88 126 lcd.cls();
superlova 1:4222a8f9ca88 127 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 128 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 129 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 130 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 131 lcd.printf(".");
superlova 1:4222a8f9ca88 132 if(i>100){
superlova 1:4222a8f9ca88 133 end_flag=1;
superlova 1:4222a8f9ca88 134 r=1.0-pot2;
superlova 1:4222a8f9ca88 135 do{
superlova 1:4222a8f9ca88 136 lcd.cls();
superlova 1:4222a8f9ca88 137 lcd.locate(25,10);
superlova 1:4222a8f9ca88 138 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 139 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 140 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 141 fclose(fp1);
superlova 1:4222a8f9ca88 142 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 143 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 144 fclose(fp);
superlova 1:4222a8f9ca88 145 wait(5);
superlova 1:4222a8f9ca88 146 }while(end_flag==1);}
superlova 1:4222a8f9ca88 147 if(joy==0x4||joy==0x1||joy==0x2){
superlova 1:4222a8f9ca88 148 forward_flag=0;
superlova 1:4222a8f9ca88 149 break;}
superlova 1:4222a8f9ca88 150 }while(forward_flag==1);
pbhatnagar3 0:8b08136c5edd 151 }
superlova 1:4222a8f9ca88 152 //move up
superlova 1:4222a8f9ca88 153 if (joy==0x1)
superlova 1:4222a8f9ca88 154 {
superlova 1:4222a8f9ca88 155 j=j-cst;
superlova 1:4222a8f9ca88 156 up_flag=1;
superlova 1:4222a8f9ca88 157 do{
superlova 1:4222a8f9ca88 158 j--;
superlova 1:4222a8f9ca88 159 lcd.locate(i,j);
superlova 1:4222a8f9ca88 160 lcd.printf("~+");
superlova 1:4222a8f9ca88 161 wait(0.2);
superlova 1:4222a8f9ca88 162 lcd.cls();
superlova 1:4222a8f9ca88 163 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 164 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 165 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 166 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 167 lcd.printf(".");
superlova 1:4222a8f9ca88 168 if(j<0){
superlova 1:4222a8f9ca88 169 end_flag=1;
superlova 1:4222a8f9ca88 170 r=1.0-pot2;
superlova 1:4222a8f9ca88 171 do{
superlova 1:4222a8f9ca88 172 lcd.cls();
superlova 1:4222a8f9ca88 173 lcd.locate(25,10);
superlova 1:4222a8f9ca88 174 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 175 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 176 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 177 fclose(fp1);
superlova 1:4222a8f9ca88 178 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 179 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 180 fclose(fp);
superlova 1:4222a8f9ca88 181 wait(5);
superlova 1:4222a8f9ca88 182 }while(end_flag==1);}
superlova 1:4222a8f9ca88 183 if(joy==0x4||joy==0x8||joy==0x2){
superlova 1:4222a8f9ca88 184 up_flag=0;
superlova 1:4222a8f9ca88 185 break;}
superlova 1:4222a8f9ca88 186 }while(up_flag==1);
superlova 1:4222a8f9ca88 187 }
superlova 1:4222a8f9ca88 188 //move down
superlova 1:4222a8f9ca88 189 if (joy==0x2)
superlova 1:4222a8f9ca88 190 {
superlova 1:4222a8f9ca88 191 j=j+cst;
superlova 1:4222a8f9ca88 192 down_flag=1;
superlova 1:4222a8f9ca88 193 do{
superlova 1:4222a8f9ca88 194 j++;
superlova 1:4222a8f9ca88 195 lcd.locate(i,j);
superlova 1:4222a8f9ca88 196 lcd.printf("~+");
superlova 1:4222a8f9ca88 197 wait(0.2);
superlova 1:4222a8f9ca88 198 lcd.cls();
superlova 1:4222a8f9ca88 199 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 200 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 201 pc.printf("snake location %d %d\n \r", i, j);
superlova 1:4222a8f9ca88 202 lcd.locate(p1, p2);
superlova 1:4222a8f9ca88 203 lcd.printf(".");
superlova 1:4222a8f9ca88 204 if(j>26){
superlova 1:4222a8f9ca88 205 end_flag=1;
superlova 1:4222a8f9ca88 206 r=1.0-pot2;
superlova 1:4222a8f9ca88 207 do{
superlova 1:4222a8f9ca88 208 lcd.cls();
superlova 1:4222a8f9ca88 209 lcd.locate(25,10);
superlova 1:4222a8f9ca88 210 lcd.printf("GAME OVER!!");
superlova 1:4222a8f9ca88 211 FILE *fp1 = fopen("/msc/Score.txt", "w"); // Open "Result.txt" on the msc file system for writing(pendrive)
superlova 1:4222a8f9ca88 212 fprintf(fp1,"score %d\n\r",score);
superlova 1:4222a8f9ca88 213 fclose(fp1);
superlova 1:4222a8f9ca88 214 FILE *fp = fopen("/local/Score.txt", "w"); // Open "Result.txt" on the local file system for writing
superlova 1:4222a8f9ca88 215 fprintf(fp,"score %d\n\r",score);
superlova 1:4222a8f9ca88 216 fclose(fp);
superlova 1:4222a8f9ca88 217 wait(5);
superlova 1:4222a8f9ca88 218 }while(end_flag==1);}
superlova 1:4222a8f9ca88 219 if(joy==0x4||joy==0x8||joy==0x1){
superlova 1:4222a8f9ca88 220 down_flag=0;
superlova 1:4222a8f9ca88 221 break;}
superlova 1:4222a8f9ca88 222 }while(down_flag==1);
superlova 1:4222a8f9ca88 223 }
superlova 1:4222a8f9ca88 224
superlova 1:4222a8f9ca88 225 if (flag == 0)
superlova 1:4222a8f9ca88 226 {
superlova 1:4222a8f9ca88 227 //print score on the LCD
superlova 1:4222a8f9ca88 228 lcd.locate(110, 22);
superlova 1:4222a8f9ca88 229 lcd.printf("%d", score);
superlova 1:4222a8f9ca88 230 //print food on the LCD
superlova 1:4222a8f9ca88 231 lcd.locate(p1, p2);
pbhatnagar3 0:8b08136c5edd 232 lcd.printf(".");
superlova 1:4222a8f9ca88 233 // CONDITION FOR CHECKING THE SNAKE FOOD COLLISION
superlova 1:4222a8f9ca88 234 if (abs(i + 8 - p1) <=cst && abs(j - p2) <=cst+1)
superlova 1:4222a8f9ca88 235 {
superlova 1:4222a8f9ca88 236 score = score + 1;
superlova 1:4222a8f9ca88 237 pc.printf("score %d\n\r", score);
superlova 1:4222a8f9ca88 238 g = 1.0 - pot2; //RGB LED green
superlova 1:4222a8f9ca88 239 wait(0.3);
superlova 1:4222a8f9ca88 240 b = 1.0 - pot2;
superlova 1:4222a8f9ca88 241 //finding a new random location for food
superlova 1:4222a8f9ca88 242 rad=rand();
superlova 1:4222a8f9ca88 243 p1= rad%fDimX;
superlova 1:4222a8f9ca88 244 p2= rad%fDimY;
superlova 1:4222a8f9ca88 245 if (p1 < 30)
superlova 1:4222a8f9ca88 246 p1 = 30;
superlova 1:4222a8f9ca88 247 if (p2 < 10)
superlova 1:4222a8f9ca88 248 p2 = 10;
superlova 1:4222a8f9ca88 249 lcd.printf(".");
superlova 1:4222a8f9ca88 250 }
pbhatnagar3 0:8b08136c5edd 251 wait(0.3);
pbhatnagar3 0:8b08136c5edd 252 }
superlova 1:4222a8f9ca88 253 }//while(1)
superlova 1:4222a8f9ca88 254 }//(int main)