Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
9:cbb9cfb1f6c5
Parent:
8:fcc333a8f9e1
Child:
10:e18685911e84
--- a/main.cpp	Sat Nov 20 03:57:56 2021 +0000
+++ b/main.cpp	Tue Nov 30 00:48:48 2021 +0000
@@ -6,6 +6,7 @@
 #include "speech.h"
 #include "spells.h"
 #include "actions.h"
+#include "SDFileSystem.h"
 
 // Functions in this file
 int get_action (GameInputs inputs);
@@ -14,6 +15,9 @@
 void init_main_map ();
 int main ();
 
+
+FILE *wave_file;
+
 /**
  * The main game state. Must include Player locations and previous locations for
  * drawing to work properly. Other items can be added as needed.
@@ -25,9 +29,12 @@
     int spell;
     int god;
     int won;
+    int HP,MP;
     // You can add other properties for the player here
 } Player;
 
+
+
 /**
  * Given the game inputs, determine what kind of update needs to happen.
  * Possbile return values are defined below.
@@ -83,9 +90,7 @@
  * draw_game should not optimize drawing and should draw every tile, even if
  * the player has not moved.
  */
-// Gmae Life and Magic Points
-#define HP 100
-#define MP 100
+// Game Life and Magic Points
 
 
 
@@ -97,7 +102,8 @@
     // Save player previous location before updating
     Player.px = Player.x;
     Player.py = Player.y;
-
+    
+    
 
 
     // Do different things based on the each action.
@@ -109,6 +115,7 @@
             MapItem* north = get_north(Player.x,Player.y);
             if (north->walkable && north == NULL || north->type == (PORTAL || PLANT) && Player.y != 0 || Player.god ) {
                 Player.y--;
+                
             }
             break;
         }
@@ -139,207 +146,231 @@
             MapItem* north = get_north(Player.x,Player.y);
             MapItem* east = get_east(Player.x,Player.y);
             MapItem* curr =  get_here(Player.x,Player.y);
-            
-            if((east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC)&& Player.has_key == 1){
+
+            if((east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC)&& Player.has_key == 1) {
                 npc_speech2();
-                }
-                
-            if(east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC && Player.has_key == 0 ){
+            }
+
+            if(east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC && Player.has_key == 0 ) {
                 npc_speech1();
-                }
-            
-            if(north->type == PORTAL||south->type == PORTAL||east->type == PORTAL||west->type == PORTAL){
+            }
+
+            if(north->type == PORTAL||south->type == PORTAL||east->type == PORTAL||west->type == PORTAL) {
                 init_dung();
                 Player.x = Player.y = 7;
-                }
-                
-            if((north->type == DRAGON||south->type == DRAGON||east->type == DRAGON||west->type == DRAGON )&& Player.spell == WATER){
+            }
+
+            if((north->type == DRAGON||south->type == DRAGON||east->type == DRAGON||west->type == DRAGON )&& Player.spell == WATER) {
                 slay_dragon();
                 Player.has_key = 1;
-                }
-                
-            if(north->type == PORTAl||south->type == PORTAl||east->type == PORTAl||west->type == PORTAl){
-                init_main_map();
-                }
-            
-            if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 1){
-                Player.won = door_open();
-                }
-                
-        }
-        break;
-        case MENU_BUTTON: {
-            init_spells();
-            Player.spell = spell();
-        }
-        break;
-        case GOD_MODE: {
-
-            Player.god = !Player.god;
-        }
-        break;
-
-        default:
-
-            break;
-    }
-    return NO_RESULT;
-}
+            }
 
-/**
- * Entry point for frame drawing. This should be called once per iteration of
- * the game loop. This draws all tiles on the screen, followed by the status
- * bars. Unless init is nonzero, this function will optimize drawing by only
- * drawing tiles that have changed from the previous frame.
- */
-void draw_game(int init)
-{
-    // Draw game border first
-    if(init) draw_border();
-
-    // Iterate over all visible map tiles
-    for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
-        for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
-            // Here, we have a given (i,j)
-
-            // Compute the current map (x,y) of this tile
-            int x = i + Player.x;
-            int y = j + Player.y;
+            if(north->type == PORTAl||south->type == PORTAl||east->type == PORTAl||west->type == PORTAl) {
+                init_main_map();
+            }
 
-            // Compute the previous map (px, py) of this tile
-            int px = i + Player.px;
-            int py = j + Player.py;
-
-            // Compute u,v coordinates for drawing
-            int u = (i+5)*11 + 3;
-            int v = (j+4)*11 + 15;
-
-            // Figure out what to draw
-            DrawFunc draw = NULL;
-            if (init && i == 0 && j == 0) { // Only draw the player on init
-                draw_player(u, v, Player.has_key);
-                continue;
-            } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
-                MapItem* curr_item = get_here(x, y);
-                MapItem* prev_item = get_here(px, py);
-                if (init || curr_item != prev_item) { // Only draw if they're different
-                    if (curr_item) { // There's something here! Draw it
-                        draw = curr_item->draw;
-                    } else { // There used to be something, but now there isn't
-                        draw = draw_nothing;
-                    }
-                }
-            } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls.
-                draw = draw_wall;
+            if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 1) {
+                Player.won = door_open();
             }
 
-            // Actually draw the tile
-            if (draw) draw(u, v);
-        }
+            if((north->type == PLANT||south->type == PLANT||east->type == PLANT||west->type == PLANT||curr->type == PLANT)&&
+                    (north->data == FRUIT||south->data == FRUIT||east->data == FRUIT||west->data == FRUIT||curr->data == FRUIT)){
+                if(Player.HP < 100) {
+                    fruit();
+                    Player.HP += 5;
+                    north->data = NULL;
+                    south->data = NULL;
+                    east->data = NULL;
+                    west->data = NULL;
+                    curr->data = NULL;
+                } else 
+                    no_fruit();
+                  wave_file = fopen("/sd/rzelda-1s.wav","r");
+                  waver.play(wave_file);
+                  fclose(wave_file);
+                  
+                    
+                    
+                
+            }
+            }
+            break;
+            case MENU_BUTTON: {
+                init_spells();
+                Player.spell = spell();
+            }
+            break;
+            case GOD_MODE: {
+
+                Player.god = !Player.god;
+                if (Player.god) {
+                    god_modeOn();
+                } else
+                    god_modeOff();
+                    
+            }
+            break;
+
+            default:
+
+                break;
+            }
+            return NO_RESULT;
     }
 
-    // Draw status bars
-    draw_upper_status(Player.x,Player.y);
-    draw_lower_status(HP,MP);
-}
+    /**
+     * Entry point for frame drawing. This should be called once per iteration of
+     * the game loop. This draws all tiles on the screen, followed by the status
+     * bars. Unless init is nonzero, this function will optimize drawing by only
+     * drawing tiles that have changed from the previous frame.
+     */
+    void draw_game(int init) {
+        // Draw game border first
+        if(init) draw_border();
+
+        // Iterate over all visible map tiles
+        for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
+            for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
+                // Here, we have a given (i,j)
+
+                // Compute the current map (x,y) of this tile
+                int x = i + Player.x;
+                int y = j + Player.y;
+
+                // Compute the previous map (px, py) of this tile
+                int px = i + Player.px;
+                int py = j + Player.py;
+
+                // Compute u,v coordinates for drawing
+                int u = (i+5)*11 + 3;
+                int v = (j+4)*11 + 15;
+
+                // Figure out what to draw
+                DrawFunc draw = NULL;
+                if (init && i == 0 && j == 0) { // Only draw the player on init
+                    draw_player(u, v, Player.has_key);
+                    continue;
+                } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
+                    MapItem* curr_item = get_here(x, y);
+                    MapItem* prev_item = get_here(px, py);
+                    if (init || curr_item != prev_item) { // Only draw if they're different
+                        if (curr_item) { // There's something here! Draw it
+                            draw = curr_item->draw;
+                        } else { // There used to be something, but now there isn't
+                            draw = draw_nothing;
+                        }
+                    }
+                } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls.
+                    draw = draw_wall;
+                }
+
+                // Actually draw the tile
+                if (draw) draw(u, v);
+            }
+        }
+
+        // Draw status bars
+        draw_upper_status(Player.x,Player.y);
+        draw_lower_status(Player.HP,Player.MP);
+    }
 
 
-/**
- * Initialize the main world map. Add walls around the edges, interior chambers,
- * and plants in the background so you can see motion. Note: using the similar
- * procedure you can init the secondary map(s).
- */
-void init_main_map()
-{
-    // "Random" plants
-    Map* map = set_active_map(0);
-    for(int i = map_width() + 3; i < map_area(); i += 39) {
-        add_plant(i % map_width(), i / map_width());
-    }
-    pc.printf("plants\r\n");
+    /**
+     * Initialize the main world map. Add walls around the edges, interior chambers,
+     * and plants in the background so you can see motion. Note: using the similar
+     * procedure you can init the secondary map(s).
+     */
+    void init_main_map() {
+        // "Random" plants
+        Map* map = set_active_map(0);
+        for(int i = map_width() + 3; i < map_area(); i += 39) {
+            add_plant(i % map_width(), i / map_width());
+        }
+        pc.printf("plants\r\n");
 
-    pc.printf("Adding walls!\r\n");
-    add_wall(0,              0,              HORIZONTAL, map_width());
-    add_wall(0,              map_height()-1, HORIZONTAL, map_width());
-    add_wall(0,              0,              VERTICAL,   map_height());
-    add_wall(map_width()-1,  0,              VERTICAL,   map_height());
-    pc.printf("Walls done!\r\n");
-    
-    
-    
-    for(int i = 7; i < 13;i++)
-    {
-        for (int j = 23; j <31;j++)
-        {
-            add_kindom(j,i);
-    }
-    }
-    
-    add_npc(9,9);
-
-    //add_chest(10,10);
-
-    add_door(26,12);
-    
-    add_portal(20,35);
-
-    print_map();
-}
+        pc.printf("Adding walls!\r\n");
+        add_wall(0,              0,              HORIZONTAL, map_width());
+        add_wall(0,              map_height()-1, HORIZONTAL, map_width());
+        add_wall(0,              0,              VERTICAL,   map_height());
+        add_wall(map_width()-1,  0,              VERTICAL,   map_height());
+        pc.printf("Walls done!\r\n");
 
 
-/**
- * Program entry point! This is where it all begins.
- * This function orchestrates all the parts of the game. Most of your
- * implementation should be elsewhere - this holds the game loop, and should
- * read like a road map for the rest of the code.
- */
-int main()
-{
-    
-    
-   // MapItem* north = get_north(Player.x,Player.y);
+
+        for(int i = 7; i < 13; i++) {
+            for (int j = 23; j <31; j++) {
+                add_kindom(j,i);
+            }
+        }
     
-    
-    // First things first: initialize hardware
-    ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
+     
+     
+        set_npc(9,9);
+
+        //add_chest(10,10);
+
+        add_door(26,12);
+
+        add_portal(20,35);
+
+        print_map();
+    }
+
 
-    // Initialize the maps
-    maps_init();
-    init_main_map();
+    /**
+     * Program entry point! This is where it all begins.
+     * This function orchestrates all the parts of the game. Most of your
+     * implementation should be elsewhere - this holds the game loop, and should
+     * read like a road map for the rest of the code.
+     */
+    int main() {
 
-    // Initialize game state
-    set_active_map(0);
-    Player.x = Player.y = 5;
+        Player.HP = 100;
+        Player.MP = 100;
+
+        // MapItem* north = get_north(Player.x,Player.y);
+
+
+        // First things first: initialize hardware
+        ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
 
-    // Initial drawing
-    draw_game(true);
-    // Main game loop
-    while(1) {
-        // Timer to measure game update speed
-        Timer t;
-        t.start();
+        // Initialize the maps
+        maps_init();
+        init_main_map();
+
+        // Initialize game state
+        set_active_map(0);
+        Player.x = Player.y = 5;
+
+        // Initial drawing
+        draw_game(true);
+        // Main game loop
+        while(1) {
+            // Timer to measure game update speed
+            Timer t;
+            t.start();
 
-        // Actually do the game update:
-        // 1. Read inputs
-        GameInputs input = read_inputs();
-        // 2. Determine action (get_action)
-        int action = get_action(input);
-        // 3. Update game (update_game)
-        update_game(action);
-        // 3b. Check for game over
-        if(Player.won == 1){
-            draw_gameover();
-            break;
+            // Actually do the game update:
+            // 1. Read inputs
+            GameInputs input = read_inputs();
+            // 2. Determine action (get_action)
+            int action = get_action(input);
+            // 3. Update game (update_game)
+            update_game(action);
+            // 3b. Check for game over
+            if(Player.won == 1) {
+                draw_gameover();
+                break;
             }
-        // 4. Draw frame (draw_game)
-        draw_game(true);
+            // 4. Draw frame (draw_game)
+            draw_game(true);
 
-        // 5. Frame delay
-        t.stop();
-        int dt = t.read_ms();
-        if (dt < 100) wait_ms(100 - dt);
+            // 5. Frame delay
+            t.stop();
+            int dt = t.read_ms();
+            if (dt < 100) wait_ms(100 - dt);
+        }
+        //
+
+
     }
-    //
-    
-
-}