ECE 2035 Summer 2015

Dependencies:   4DGL-uLCD-SE MMA8452 SDFileSystem mbed wave_player

Dependents:   RoboFrogTemplate

Fork of ECE2035_FroggerGame_SUM1025 by Le Tran

Files at this revision

API Documentation at this revision

Comitter:
ece2035ta
Date:
Wed Jul 08 15:45:22 2015 +0000
Parent:
7:b9b305de0fbb
Commit message:
Added car files

Changed in this revision

cars/cars.cpp Show annotated file Show diff for this revision Revisions of this file
cars/cars.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cars/cars.cpp	Wed Jul 08 15:45:22 2015 +0000
@@ -0,0 +1,118 @@
+#include "cars.h"
+//#include "mbed.h"
+//#include "globals.h"
+//#include "map_public.h"
+
+
+//=======================================================================
+//Private Functions
+void collision() {
+     uLCD.cls();
+    uLCD.locate(6,8);
+    uLCD.printf("GAME OVER");
+    
+            while(1) { }
+    }
+void car_up(car_t * g)
+{
+    //Need if statement to loop around
+    if (g->car_blk_y == 0)
+    {
+        car_move(g,g->car_blk_x, 13);
+        return;
+    } 
+
+    car_move(g, g->car_blk_x, g->car_blk_y-1);
+    return;
+}
+
+//Function to choose car direction
+//This can probably be merged with drive function
+void car_down(car_t * g)
+{
+
+    car_move(g, g->car_blk_x, g->car_blk_y+1);
+    return;
+}
+
+void clean_blk(unsigned int blk_x, unsigned int blk_y)
+{
+    GRID grid_info = map_get_grid_status(blk_x,blk_y);
+    uLCD.filled_rectangle(grid_info.x, grid_info.y, grid_info.x+GRID_SIZE-1, grid_info.y+GRID_SIZE+1, BACKGROUND_COLOR);
+    return;
+}
+
+void drawCar(car_t * g, int grid_x, int grid_y)
+{
+    GRID grid_info = map_get_grid_status(grid_x,grid_y);
+    int pos_x = grid_info.x + GRID_RADIUS;
+    int pos_y = grid_info.y + GRID_RADIUS;
+    uLCD.filled_rectangle(pos_x-GRID_RADIUS, pos_y, pos_x+GRID_RADIUS-2,pos_y+GRID_RADIUS+2,g->car_color);
+    return;
+    
+}
+//This moves the car from tile to tile
+
+void car_move(car_t * g, unsigned int new_blk_x, unsigned int new_blk_y)
+{
+    // clean up ghost at old position
+    clean_blk(g->car_blk_x, g->car_blk_y);
+    // clean the block at new position
+    clean_blk(new_blk_x, new_blk_y);
+    // draw the ghost at new position
+    drawCar(g, new_blk_x, new_blk_y);
+
+    // recover map component
+    map_draw_grid(g->car_blk_x, g->car_blk_y);
+
+    g->car_blk_x = new_blk_x;
+    g->car_blk_y = new_blk_y;
+    return;
+}
+
+
+//==============================================================================
+//Public Functions
+
+
+void car_init(car_t * g) {
+   if(g->lane == 1) 
+   {    g->car_blk_x = 3;
+        g->car_blk_y = 0;    
+        drawCar(g, 3, 0);
+        return;
+    }
+    if (g->lane == 2)
+    {
+        g->car_blk_x = 7;
+        g->car_blk_y = 0;
+        drawCar(g, 7, 0);
+        return;
+    }
+        
+    if (g->lane == 3)
+    {
+        g->car_blk_x = 10;
+        g->car_blk_y = 0;
+        drawCar(g, 10, 0);
+        return;
+       }
+
+return;
+}
+
+void drive(car_t * g)
+{
+    
+    switch (g->car_motion) {
+        case CAR_UP:
+            car_up(g);
+            break;
+        case CAR_DOWN:
+            car_down(g);
+            break;
+        default:
+            break;
+    }
+    return;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cars/cars.h	Wed Jul 08 15:45:22 2015 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+//#include "cars.h"
+#include "map_public.h"
+#include "globals.h"
+
+
+//void clearCar(int lane);
+//void moveCar(int whichOne);         // 1 -> lane 1. 2 -> lane 2 etc
+
+
+
+/// The enum defines the motion of the car
+typedef enum {
+    CAR_UP=0, ///< move up
+    CAR_DOWN, ///< move down
+} CAR_MOTION;
+
+typedef struct
+{
+    unsigned int car_blk_x;   ///< horizontal position in the grid
+    unsigned int car_blk_y;   ///< vertical position in the grid
+    unsigned int car_color;   ///< color of the car
+    unsigned int lane;         //car lane
+    CAR_MOTION car_motion;  ///< the motion of the car  
+} car_t;
+
+void car_move(car_t * g, unsigned int new_blk_x, unsigned int new_blk_y); //A function to move the car
+//void drawCar(car_t * g, int grid_x, int grid_y);   // @param lane @param color I don't think this function needs to be public
+void car_init(car_t * g); // Initialize a car
+void drive(car_t*);
+void collision();