Faizan Khan / Mbed 2 deprecated 4180-lab4

Dependencies:   4DGL-uLCD-SE mbed SDFileSystem wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 //#include "SongPlayer.h"
00004 #include "SDFileSystem.h"
00005 #include "wave_player.h"
00006 #include "NavSwitch.h"
00007 #include "food.h"
00008 #include "stack.h"
00009 #include <ctime>
00010 #include <cstdlib>
00011 #include <cmath>  
00012 #include <vector>
00013 
00014 #define BREAD 1
00015 #define LETTUCE 2
00016 #define CHEESE 3
00017 #define TOMATO 4
00018 #define BADLETTUCE 5
00019 #define BADCHEESE 6
00020 #define BADTOMATO 7
00021 
00022 #define MAROON 0x8b0000
00023 #define DARKBROWN 0x654321
00024 #define YELLOW 0xffff00
00025 #define PURPLE 0x551a8b
00026 #define BROWN 0xf4a460
00027 
00028 uLCD_4DGL lcd(p9,p10,p11);
00029 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00030 AnalogOut DACout(p18);
00031 wave_player waver(&DACout);
00032 Nav_Switch joystick(p20, p19, p17, p16, p15); // pin order on Sparkfun breakout
00033 DigitalIn speed(p21);
00034 DigitalIn pause(p22);
00035 
00036 vector<Food> foods;
00037 Food * collided;
00038 
00039 using namespace std;
00040 Stack sandwich(64,&lcd);
00041 int score, lives;
00042 float clk, clk2;
00043 Timer t;
00044 int indexToRemove;
00045 
00046 bool collisionCheck();
00047 void updateScore(int);
00048 void drawLives();
00049 
00050 int main() {
00051     speed.mode(PullUp);
00052     pause.mode(PullUp);
00053     
00054     // splash sequence
00055     lcd.filled_rectangle(34, 18, 44, 19, BROWN); 
00056     lcd.filled_rectangle(30, 22, 50, 20, BROWN); // top bun
00057     lcd.filled_rectangle(34, 38, 44, 42, YELLOW); // cheese
00058     lcd.filled_rectangle(34, 56, 44, 60, GREEN); // lettuce
00059     lcd.filled_rectangle(34, 74, 44, 78, RED); // tomato
00060     lcd.filled_rectangle(30, 94, 50, 96, BROWN); // bottom bun
00061     lcd.filled_rectangle(34, 98, 44, 97, BROWN); 
00062     
00063     FILE *wave_file;
00064     wave_file=fopen("/sd/start.wav","r");
00065     waver.play(wave_file);
00066     fclose(wave_file);
00067     
00068     lcd.text_string("bun", 11, 3, FONT_7X8, WHITE);
00069     wait(1);
00070     lcd.text_string("cheese", 11, 5, FONT_7X8, WHITE);
00071     wait(1);
00072     lcd.text_string("lettuce", 11, 7, FONT_7X8, WHITE);
00073     wait(1);
00074     lcd.text_string("tomato", 11, 9, FONT_7X8, WHITE);
00075     wait(1);
00076     lcd.text_string("bun", 11, 11, FONT_7X8, WHITE);
00077     wait(4);
00078     lcd.cls();
00079     
00080     lcd.filled_rectangle(34, 38, 44, 42, MAROON); // bad foods
00081     lcd.filled_rectangle(34, 56, 44, 60, DARKBROWN);
00082     lcd.filled_rectangle(34, 74, 44, 78, PURPLE);
00083     wait(2);
00084     lcd.text_string("GROSS!", 11, 7, FONT_7X8, WHITE);
00085     wait(2);
00086     lcd.cls();
00087     
00088     score = 0;
00089     lives = 3;
00090     t.start();
00091     srand(time(NULL));
00092     clk = clk2 = 0;
00093     sandwich.draw();
00094     
00095     lcd.line(108, 1, 108, 128, WHITE);
00096     
00097     // inital lives
00098     lcd.filled_circle(118, 80, 3, YELLOW); // x, y, radius, color
00099     lcd.filled_circle(118, 90, 3, YELLOW);
00100     lcd.filled_circle(118, 100, 3, YELLOW);
00101     
00102     updateScore(0);
00103     
00104     while(lives) {
00105         if (!pause) { // pause game
00106             lcd.text_string("PAUSED", 6, 7, FONT_7X8, WHITE);
00107             int paused = 1;
00108             while (paused) {
00109                 wait(.2);
00110                 paused = pause;
00111             }
00112             lcd.text_string("PAUSED", 6, 7, FONT_7X8, BLACK);
00113         }
00114         
00115         if (joystick.up()) {
00116             sandwich.move(-1);
00117         } else if (joystick.down()) {
00118             sandwich.move(+1);
00119         }
00120         if (collisionCheck()) {
00121             collided->erase();
00122             if (collided->isBad) {
00123                 lives--;
00124                 drawLives();
00125                 // make a noise!1111
00126             } else if (collided->typeOfFood == BREAD) {
00127                 // updateScore(sandwich.size()); // add the points to the overall score
00128                 sandwich.clear(); // should redraw
00129                 // make a noise!!11
00130             } else {
00131                 // remove from falling object list, erase
00132                 foods.erase(foods.begin() + indexToRemove);
00133                 sandwich.add(collided);
00134             }
00135         }
00136         // every 3 seconds add new food! use timer.
00137         float curr = t.read();
00138         if (curr - clk >= 5.0) {
00139             clk = curr;
00140             // lcd.printf(" new ");
00141             int x = rand()%90+1;
00142             int type = rand()%7+1;
00143             Food item(type, x, &lcd);
00144             foods.push_back(item);
00145             // add new food
00146         }
00147         // every .2 second each food should fall a lil bit! use timer.
00148         if (curr - clk2 >= 0.1) {
00149             clk2 = curr;
00150             // lcd.printf(" fall ");
00151             for (int i = 0; i < foods.size(); i++) {
00152                 Food * f = &foods[i];
00153                 f->fall();
00154             }
00155             // each food should fall a lil bit!
00156         }
00157     }
00158     
00159     lcd.cls();
00160     
00161     char message[20];
00162     sprintf(message, "Your score? %d", score);
00163     while (true) { // end game
00164         lcd.text_string(message, 1, 7, FONT_7X8, WHITE);
00165         wait(0.7);
00166         lcd.text_string(message, 1, 7, FONT_7X8, BLACK);
00167         wait(0.4);
00168     }
00169 }
00170 
00171 bool collisionCheck() {
00172     if (!foods.empty()) {
00173         Food * f = &foods.front();
00174         if (f->y > 128) foods.erase(foods.begin()); // removed if offscreen!
00175     }
00176     for (int i = 0; i < foods.size(); i++) {
00177         if (foods[i].y + 6 == sandwich.top) {
00178             if (abs(foods[i].x - sandwich.x) < 8) {
00179                 collided = &foods[i];
00180                 indexToRemove = i;
00181                 return true;
00182             } else break;
00183         }
00184     }
00185     return false;
00186     // find foods that have an (x,y) that is within the range of the top of the stack
00187     // if nothing found w/i the range
00188         // return false
00189     // if something found
00190         // collidedFood = that food item
00191         // return true
00192 }
00193 
00194 void updateScore(int sandwichSize) {
00195     char scoreString[2];   // max 2-digit score
00196 
00197     FILE *wave_file;
00198     wave_file=fopen("/sd/score.wav","r");
00199     waver.play(wave_file);
00200     fclose(wave_file);
00201 
00202     score += sandwichSize;
00203     score = (score > 99) ? score - 100 : score; // score rollover
00204     sprintf(scoreString, "%d", score); // keep score as a char string
00205     lcd.text_string(scoreString, 16, 1, FONT_7X8, WHITE); // 16 chars over, 1 down
00206 }
00207 
00208 void drawLives() {
00209     
00210     FILE *wave_file;
00211     wave_file=fopen("/sd/life.wav","r");
00212     waver.play(wave_file);
00213     fclose(wave_file);
00214 
00215     if (lives == 2) {
00216         lcd.filled_circle(118, 80, 3, BLACK); // erase the life symbol
00217     }
00218     else if (lives == 1) {
00219         lcd.filled_circle(118, 90, 3, BLACK);
00220     }
00221     else if (lives == 0) {
00222         lcd.filled_circle(118, 100, 3, BLACK);
00223     }   
00224 }