Rony Stephan / Mbed 2 deprecated 2035_rpg_game_fall2021

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Files at this revision

API Documentation at this revision

Comitter:
ranroun3
Date:
Wed Dec 01 21:21:12 2021 +0000
Parent:
20:762297435411
Child:
22:397601b1cdb4
Commit message:
added 1up, hammer, and health potion logic

Changed in this revision

graphics.cpp Show annotated file Show diff for this revision Revisions of this file
graphics.h Show annotated file Show diff for this revision Revisions of this file
hardware.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/graphics.cpp	Wed Dec 01 19:44:43 2021 +0000
+++ b/graphics.cpp	Wed Dec 01 21:21:12 2021 +0000
@@ -272,19 +272,26 @@
     // Add other status info drawing code here
 }
 
-void draw_lower_status(int item, int potion1, int potion2)
+void draw_lower_status(int item, int potion1, int potion2, bool firstDraw)
 {
-    if(item){
-        draw_hammer(1, 118);
-        }
-        if(potion1) {
-            draw_healthPotion(13, 118);
-            }
-        if(potion2) {
-            draw_heart(25, 118);
-        }
+    if(firstDraw){
     // Draw top border of status bar
     uLCD.line(0, 118, 127, 118, GREEN);
+        }
+    if(item) {
+        draw_hammer(1, 118);
+//        uLCD.line(12, 118, 12, 127, 0xFFFFFF);
+    }
+    
+    if(potion1) {
+        draw_healthPotion(13, 118);
+//        uLCD.line(24, 118, 24, 127, 0xFFFFFF);
+    }
+    if(potion2) {
+        draw_heart(25, 118);
+    }
+    
+
 
     // Add other status info drawing code here
 }
--- a/graphics.h	Wed Dec 01 19:44:43 2021 +0000
+++ b/graphics.h	Wed Dec 01 21:21:12 2021 +0000
@@ -55,7 +55,7 @@
 /**
  * Draw the lower status bar.
  */ 
-void draw_lower_status(int item, int potion1, int potion2);
+void draw_lower_status(int item, int potion1, int potion2, bool firstDraw);
 
 /**
  * Draw the border for the map.
--- a/hardware.cpp	Wed Dec 01 19:44:43 2021 +0000
+++ b/hardware.cpp	Wed Dec 01 21:21:12 2021 +0000
@@ -15,9 +15,9 @@
 MMA8452 acc(p28, p27, 100000);        // Accelerometer (sda, sdc, rate)
 DigitalIn button1(p21);                 // Pushbuttons (pin)
 DigitalIn button2(p22);
-DigitalIn button3(p23);
+DigitalIn button3(p25);
 DigitalIn button4(p24);
-DigitalIn button5(p25);
+DigitalIn button5(p23);
 AnalogOut DACout(p18);                  // Speaker (pin)
 PwmOut speaker(p26);
 wave_player waver(&DACout);
@@ -35,6 +35,8 @@
     button1.mode(PullUp); 
     button2.mode(PullUp);
     button3.mode(PullUp);
+    button4.mode(PullUp);
+    button5.mode(PullUp);
     
     acc.activate();
     
--- a/main.cpp	Wed Dec 01 19:44:43 2021 +0000
+++ b/main.cpp	Wed Dec 01 21:21:12 2021 +0000
@@ -30,6 +30,8 @@
 void youLoseScreen();
 void decreaseLife();
 void conditionals();
+void increaseLife();
+void increaseHealth();
 int main ();
 
 
@@ -53,6 +55,7 @@
     // You can add other properties for the player here
 } Player;
 GameInputs in;
+bool firstDraw;
 /**
  * Given the game inputs, determine what kind of update needs to happen.
  * Possbile return values are defined below.
@@ -427,24 +430,8 @@
                 } else if (right->type == DOOR) {
                     door1 = (Door *)right->data;
                 }
-                if(Player.item1) {
-                    char using_hammer[] = "Using hammer!";
-                    speech(using_hammer, NULL);
-                    Player.item1 = 0;
-                    int startX = door1->startX;
-                    int startY = door1->startY;
-                    int endX = door1->endX;
-                    int endY = door1->endY;
-                    for (int i = startX; i <= endX; i++) {
-                        map_erase(i, startY);
-                        map_erase(i, endY);
-                    }
-                    for (int i = startY; i <= endY; i++) {
-                        map_erase(startX, i);
-                        map_erase(endX, i);
-                    }
-                    return FULL_DRAW;
-                }
+
+
 
                 if (Player.has_key) {
                     char unlocked_door_0[] = "Congratulations!";
@@ -504,19 +491,100 @@
             return FULL_DRAW;
         }
         case ITEM_1: {
-            break;
+            if(Player.item1) {
+                MapItem *above = get_north(Player.x, Player.y);
+                MapItem *left = get_west(Player.x, Player.y);
+                MapItem *right = get_east(Player.x, Player.y);
+                MapItem *below = get_south(Player.x, Player.y);
+                char using_hammer[] = "Using hammer!";
+                speech(using_hammer, NULL);
+                if ((above->type == DOOR) || (below->type == DOOR) ||
+                        (left->type == DOOR) || (right->type == DOOR)) {
+
+                    Door *door1 = NULL;
+                    //determine which one is the correct npc location
+                    if (above->type == DOOR) {
+                        door1 = (Door *)above->data;
+                    } else if (below->type == DOOR) {
+                        door1 = (Door *)below->data;
+                    } else if (left->type == DOOR) {
+                        door1 = (Door *)left->data;
+                    } else if (right->type == DOOR) {
+                        door1 = (Door *)right->data;
+                    }
+                    draw_nothing(1, 118);
+                    Player.item1 = 0;
+                    int startX = door1->startX;
+                    int startY = door1->startY;
+                    int endX = door1->endX;
+                    int endY = door1->endY;
+                    for (int i = startX; i <= endX; i++) {
+                        map_erase(i, startY);
+                        map_erase(i, endY);
+                    }
+                    for (int i = startY; i <= endY; i++) {
+                        map_erase(startX, i);
+                        map_erase(endX, i);
+                    }
+                    return FULL_DRAW;
+                } else {
+                    char noWallNearby[] = "No wall nearby!";
+                    speech(noWallNearby, NULL);
+                    draw_nothing(1, 118);
+                    Player.item1 = 0;
+                }
+            }
+
+            else {
+                char noItemHere[] = "No item here!";
+                speech(noItemHere, NULL);
+            }
         }
         case ITEM_2: {
-            break;
-        }
-        case ITEM_3: {
-            break;
+            if(Player.potion1) {
+                increaseHealth();
+                return FULL_DRAW;
+            } else {
+                char noItemHere[] = "No item here!";
+                speech(noItemHere, NULL);
+                break;
+            }
+            case ITEM_3: {
+                if(Player.potion2) {
+                    increaseLife();
+                    return FULL_DRAW;
+                } else {
+                    char noItemHere[] = "No item here!";
+                    speech(noItemHere, NULL);
+                    break;
+                }
+
+            }
+
+          
+
         }
-
     }
+    return NO_RESULT;
+}
 
-    return NO_RESULT;
-
+void increaseLife()
+{
+    Player.lives++;
+    if(Player.lives>9){
+        Player.lives = 9;
+        }
+    draw_nothing(25, 118);
+    Player.potion2 = 0;
+}
+void increaseHealth()
+{
+    Player.health +=30;
+    if(Player.health>99) {
+        Player.health = 99;
+    }
+    draw_nothing(13, 118);
+    Player.potion1 = 0;
 }
 
 
@@ -576,7 +644,7 @@
     // Draw status bars
 
     draw_upper_status(Player.x, Player.y, Player.health, Player.lives);
-    draw_lower_status(Player.item1, Player.potion1, Player.potion2);
+    draw_lower_status(Player.item1, Player.potion1, Player.potion2, firstDraw);
 }
 
 
@@ -775,10 +843,11 @@
     Player.health = 99;
     Player.lives = 3;
     Player.item1 = Player.potion1 = Player.potion2 = 0;
-
+    firstDraw = true;
 
     // Initial drawing
     draw_game(true);
+    firstDraw = false;
 
     // Main game loop
     int doesGameContinue = 1;