Faizan and Pages fun little game

Dependencies:   4DGL-uLCD-SE mbed SDFileSystem wave_player

main.cpp

Committer:
fkhan39
Date:
2016-10-31
Revision:
4:c00da0b158c4
Parent:
3:058e10b8ecf6
Child:
5:d832d2d67411

File content as of revision 4:c00da0b158c4:

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "food.h"
#include "stack.h"
#include <ctime>
#include <cstdlib>
#include <cmath>  
#include <vector>

#define BREAD 1
#define LETTUCE 2
#define CHEESE 3
#define TOMATO 4
#define BADLETTUCE 5
#define BADCHEESE 6
#define BADTOMATO 7

uLCD_4DGL lcd(p9,p10,p11);
vector<Food> foods;
Food * collided;

using namespace std;
Stack sandwich(64,&lcd);
DigitalIn right(p19);
DigitalIn left(p20);
int score, lives;
float clk, clk2;
Timer t;
int indexToRemove;

bool collisionCheck();

int main() {
    
    score = 0;
    lives = 3;
    left.mode(PullUp);
    right.mode(PullUp);
    t.start();
    srand(time(NULL));
    clk = clk2 = 0;
    sandwich.draw();
    
    lcd.line(120, 1, 120, 128, WHITE);
    
    while(lives) {
        if (!left) {
            sandwich.move(-1);
        } else if (!right) {
            sandwich.move(+1);
        }
        if (collisionCheck()) {
            collided->erase();
            if (collided->isBad) {
                lives--;
                // lifeLost()
                // make a noise!1111
            } else if (collided->typeOfFood == BREAD) {
                // updateScore(sandwich.size()); // add the points to the overall score
                sandwich.clear(); // should redraw
                // make a noise!!11
            } else {
                // remove from falling object list, erase
                foods.erase(foods.begin() + indexToRemove);
                sandwich.add(collided);
            }
        }
        // every 3 seconds add new food! use timer.
        float curr = t.read();
        if (curr - clk >= 3.0) {
            clk = curr;
            // lcd.printf(" new ");
            int x = rand()%110+1;
            int type = rand()%7+1;
            Food item(type, x, &lcd);
            foods.push_back(item);
            // add new food
        }
        // every .2 second each food should fall a lil bit! use timer.
        if (curr - clk2 >= 0.2) {
            clk2 = curr;
            // lcd.printf(" fall ");
            for (int i = 0; i < foods.size(); i++) {
                Food * f = &foods[i];
                f->fall();
            }
            // each food should fall a lil bit!
        }
    }
    
    lcd.printf("Your score is: %i", score);
}

bool collisionCheck() {
    if (!foods.empty()) {
        Food * f = &foods.front();
        if (f->y > 128) foods.erase(foods.begin()); // removed if offscreen!
    }
    for (int i = 0; i < foods.size(); i++) {
        if (foods[i].y + 6 == sandwich.top) {
            if (abs(foods[i].x - sandwich.x) < 8) {
                collided = &foods[i];
                indexToRemove = i;
                return true;
            } else break;
        }
    }
    return false;
    // find foods that have an (x,y) that is within the range of the top of the stack
    // if nothing found w/i the range
        // return false
    // if something found
        // collidedFood = that food item
        // return true
}