Kern Fowler / Mbed 2 deprecated Donkey_Kong_Game

Dependencies:   mbed

Revision:
10:28575a6eaa13
Parent:
9:e6832bf222b7
Child:
11:b288d01533cc
--- a/main.cpp	Tue May 07 17:40:29 2019 +0000
+++ b/main.cpp	Tue May 07 22:49:13 2019 +0000
@@ -10,224 +10,94 @@
 Version: 1.2
 
 */
-#include "main.h"
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Donkey.h"
+#include "Options.h"
+#include "Barrel.h" 
+#include "Banana.h"
+#include "GameEngine.h"
 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad; 
+Donkey dky;
+Barrel barrel;
+Banana banana;
+GameEngine eng;
 Options opt;
-Donkey dky;
-GameEngine eng;
+
+
+void init();
+void print_menu();
+void welcome();
+void arrow_location();
+void arrow_select();
 
 int fps = 24;
 int direction;
 int menu_option_pos = 0;
 int arrow_pos = 0;
 
-
-// Donkey -----------
-int donkeykong_x = 0;
-int donkeykong_y = 32;
-int donkey_kong_speed = 10;
-
-Donkey::Donkey()
-{
-
-}
-Donkey::~Donkey()
-{
-
-}
+int menu_arrow[7][7] = { // Arrow pointer used in main menu
+{0,0,0,0,0,0,0,},
+{0,0,0,0,1,0,0,},
+{0,0,0,0,1,1,0,},
+{1,1,1,1,1,1,1,},
+{0,0,0,0,1,1,0,},
+{0,0,0,0,1,0,0,},
+{0,0,0,0,0,0,0,},
+};
 
-void Donkey::donkeykong_movement(Gamepad &pad, N5110 &lcd) {
-    direction = pad.get_direction();
-        if (direction == NE || direction == E || direction == SE) {
-            donkeykong_x = donkeykong_x + 1;
-            lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_right_1);
-        } 
-        else if (direction == NW || direction == W || direction == SW) {
-            donkeykong_x = donkeykong_x - 1;
-            lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_left_1);
-        } 
-        else {
-            lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_stationary);
-        }
-        wait_ms(donkey_kong_speed);
-        lcd.refresh();
-        if (donkeykong_x > 68) {
-            donkeykong_x = 68;
-        }
-        if (donkeykong_x < 0) {
-            donkeykong_x = 0;
-        }
-}
-// Barrel -----------
-int barrel_x = 30;
-int barrel_y = 0;
-int barrel_speed = 0;
-int barrel_min = 0;
-int barrel_max = 76;
-float barrel_time = 0;
-
-Barrel::Barrel()
-{
-
-}
-
-Barrel::~Barrel()
-{
-
-}
-
-void Barrel::barrel_drop(Gamepad &pad, N5110 &lcd) {
-    lcd.drawSprite(barrel_x,barrel_y,4,8,(int *)game_barrel);
-    lcd.refresh();
-    wait_ms(50);
-    barrel_y = barrel_y + 1 + barrel_time;
-    if (barrel_y > 44) {
-        barrel_y = 0;
-        barrel_x = rand() % (barrel_max + 1 - barrel_min) + barrel_min;
-        barrel_time = barrel_time + 0.1;
-    }
-}
-
-
-// Banana -----------
+int menu_dk_face[36][34] =   { // donkey kong face for menu
+    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,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1,1,1,1,0,0,0,0,0,0,0,0,0 },
+    { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
+    };
+    
+    // Controls -------------------------
+void controls_run();
+// Instructions ---------------------
+void instructions_run();
+// Options --------------------------
 
 
-// Game State--------------------------------------------------------------
 
-GameEngine::GameEngine()
-{
-
-}
-
-GameEngine::~GameEngine()
-{
-
-}
-
-void GameEngine::gameengine_run(Gamepad &pad, N5110 &lcd, Barrel &barrel) {
-    wait_ms(250);
-
-    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
-        //printf("Game State");
-        lcd.clear();
-        lcd.printString("Game",0,0);
-        dky.donkeykong_movement(pad, lcd);
-        barrel.barrel_drop(pad, lcd);
-        if ((barrel_y >= 34) & ((barrel_x + 7) >= donkeykong_x) & (barrel_x <= (donkeykong_x + 15))) {
-            lcd.clear();
-            lcd.printString("OVER",0,0);
-            wait_ms(1000);
-        }
-        wait_ms(1.0f/fps);
-    }
-}
-
-// Controls State----------------------------------------------------------
-void controls_run() {
-    wait_ms(250);
-
-    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
-        //printf("Control State");
-        lcd.clear();
-        lcd.printString("Controls",19,0);
-        lcd.refresh();
-        wait_ms(1.0f/fps);
-    }
-}
-
-// Instructions State------------------------------------------------------
-void instructions_run() {
-    wait_ms(250);
-
-    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
-        //printf("Instructions State");
-        lcd.clear();
-        lcd.printString("Instructions",7,0);
-        lcd.refresh();
-        wait_ms(1.0f/fps);
-    }
-}
-
-// Options State-----------------------------------------------------------
-float opt_brightness = 0.5;
-float opt_contrast = 0.396;
-int opt_volume = 1;
-
-Options::Options()
-{
-
-}
-
-Options::~Options()
-{
-
-}
-
-void Options::options_run(Gamepad &pad, N5110 &lcd) {
-    wait_ms(250);
-    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
-        //printf("Options State");
-        lcd.clear();
-        lcd.printString("Options",21,0);
-        options_brightness(pad, lcd);
-        options_contrast(pad, lcd);
-        options_volume(pad, lcd);
-        lcd.refresh();
-        wait_ms(1.0f/fps);
-    }
-}
-
-void Options::options_brightness(Gamepad &pad, N5110 &lcd) {
-    if (pad.check_event(Gamepad::B_PRESSED) == true) {
-        opt_brightness = 0;
-    }
-    if (pad.check_event(Gamepad::A_PRESSED) == true) {
-        opt_brightness = 0.5;
-    }
-    lcd.setBrightness(opt_brightness);
-    lcd.printString("A/B = BackLite",0,2);
-}
-
-void Options::options_contrast(Gamepad &pad, N5110 &lcd) {
-    opt_contrast = pad.read_pot();
-    lcd.setContrast(opt_contrast);
-    lcd.printString("Pot = Contrast",0,3);
-    printf("Contrast = %f", opt_contrast);
-}
-
-void Options::options_volume(Gamepad &pad, N5110 &lcd) {
-    if (pad.check_event(Gamepad::Y_PRESSED) == true) {
-        opt_volume = 0;
-    }
-    if (pad.check_event(Gamepad::X_PRESSED) == true) {
-        opt_volume = 1;
-        pad.tone(2400, 0.2);
-        wait_ms(200);
-        pad.tone(2400, 0.2);
-        wait_ms(200);
-        pad.tone(2400, 0.2);
-    }
-    lcd.printString("X/Y = Volume",0,4);
-    if (opt_volume == 0) {
-        lcd.printString("Off",36,5);
-    } else {
-        lcd.printString("On",36,5);
-    }
-    
-}
-
-// High Score State--------------------------------------------------------
-void high_scores_run() {
-    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
-        //printf("High Score State");
-        lcd.clear();
-        lcd.printString("High Scores",12,0);
-        lcd.refresh();
-        wait_ms(1.0f/fps);
-    }
-}
+// High Scores-----------------------
+void high_scores_run();
 
 // Main Menu --------------------------------------------------------------
 int main() {
@@ -247,6 +117,10 @@
     lcd.init();
     // initialise Gamepad
     pad.init();
+    opt.options_init();
+    dky.donkey_init();
+    barrel.barrel_init();
+    banana.banana_init();
     wait(2.5);
 }
 
@@ -276,7 +150,7 @@
     if (pad.check_event(Gamepad::A_PRESSED) == true) {
         if (menu_option_pos == 0) {
             // printf("GameEngine");
-            eng.gameengine_run(pad, lcd, barrel);
+            eng.gameengine_run(pad, lcd, barrel, banana, dky);
         }
         if (menu_option_pos == 1) {
             // printf("Controls");
@@ -324,3 +198,62 @@
     lcd.refresh();
     wait(1.0);
 }
+
+
+// Donkey -----------
+
+// Barrel -----------
+
+
+
+// Banana -----------
+
+
+
+// Game State--------------------------------------------------------------
+
+
+
+
+
+// Controls State----------------------------------------------------------
+void controls_run() {
+    wait_ms(250);
+
+    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
+        //printf("Control State");
+        lcd.clear();
+        lcd.printString("Controls",19,0);
+        lcd.refresh();
+        wait_ms(1.0f/fps);
+    }
+}
+
+// Instructions State------------------------------------------------------
+void instructions_run() {
+    wait_ms(250);
+
+    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
+        //printf("Instructions State");
+        lcd.clear();
+        lcd.printString("Instructions",7,0);
+        lcd.refresh();
+        wait_ms(1.0f/fps);
+    }
+}
+
+// Options State-----------------------------------------------------------
+
+
+
+// High Score State--------------------------------------------------------
+void high_scores_run() {
+    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
+        //printf("High Score State");
+        lcd.clear();
+        lcd.printString("High Scores",12,0);
+        lcd.refresh();
+        wait_ms(1.0f/fps);
+    }
+}
+