Simple starter skeleton for asteroids video game.

Dependencies:   PinDetect

Revision:
2:30020ddfccf6
Parent:
1:a6872783beca
Child:
3:98aa3db6a48f
--- a/main.cpp	Sat Feb 23 21:38:45 2019 +0000
+++ b/main.cpp	Sat Mar 02 16:11:43 2019 +0000
@@ -1,27 +1,45 @@
 #include "mbed.h"
+#include <stdio.h>
 #include "Speaker.h"
 #include "PinDetect.h"
 #include "BuzzyGraphics.h"
 #include "uLCD_4DGL.h"
-
+//#include "Sprite.h"
+#include "Buzzy.h"
+#include "Ghosts.h"
 ////////////////////////////////////////
 // Setup instance of LCD display
-uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
+uLCD_4DGL guLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
 ////////////////////////////////////////
 // Setup instances of push button pins
-PinDetect pb_left(p16); 
-PinDetect pb_right(p17); 
-PinDetect pb_up(p18);
-PinDetect pb_down(p19);
+PinDetect gPB_left(p16); 
+PinDetect gPB_right(p17); 
+PinDetect gPB_up(p18);
+PinDetect gPB_down(p19);
+
+Buzzy gBuzzy;
+Sprite gGhosts[NUM_GHOSTS];
+
+
 
-Speaker SpeakerOut(p21);
+int gGameState = GAME_PAUSED;
+
+Speaker gSpeakerOut(p21);
+////////////////////////////////////////////////////////
+// This is the maze that changes as the game is played
+char gDynaMaze[MAZE_NUM_ROW][MAZE_NUM_COL]; 
+
+//////////////////////////
+// Prototype functions
+void DrawMaze();
+
 //////////////////////////////////////////////////////////////////////
 // Interrupt routine
 // used to output next analog sample whenever a timer interrupt occurs
 void Sample_timer_interrupt(void)
 {
     // send next analog sample out to D to A
-    SpeakerOut.PlayNextValue();
+    gSpeakerOut.PlayNextValue();
 
 }
 //---------------------------------------------------------------------------------------------------
@@ -29,74 +47,117 @@
 void pb_left_hit_callback (void)
 {
     // Tell Buzzy to go left
-    //*************
-    // Fill in needed Code here
-    //*************
-
+    gBuzzy.SetDesiredDirectionToMove(Sprite::LEFT_DIR);
 }
 //---------------------------------------------------------------------------------------------------
 // Callback routine is interrupt activated by a debounced pb_right hit
 void pb_right_hit_callback (void)
 {
     // Tell Buzzy to go right
-    //*************
-    // Fill in needed Code here
-    //*************
-
+    gBuzzy.SetDesiredDirectionToMove(Sprite::RIGHT_DIR);
 }
 //---------------------------------------------------------------------------------------------------
 // Callback routine is interrupt activated by a debounced pb_up hit
 void pb_up_hit_callback (void)
 {
     // Tell Buzzy to go up
-    //*************
-    // Fill in needed Code here
-    //*************
-
+    gBuzzy.SetDesiredDirectionToMove(Sprite::UP_DIR);
 }
 //---------------------------------------------------------------------------------------------------
 // Callback routine is interrupt activated by a debounced pb_down hit
 void pb_down_hit_callback (void)
 {
     // Tell Buzzy to go down
-    //*************
-    // Fill in needed Code here
-    //*************
-
+    gBuzzy.SetDesiredDirectionToMove(Sprite::DOWN_DIR);
 }
 //---------------------------------------------------------------------------------------------------
 int main()
 {
-    //setup push buttons
-    pb_left.mode(PullUp);
-    pb_right.mode(PullUp);
-    pb_up.mode(PullUp);
-    pb_down.mode(PullUp);
+    int ii;
+    unsigned char NumLivesRemaining = 3;
+         
+    // Zero out the dynamic 2D maze array
+    memset(&gDynaMaze[0][0], MAZE_NUM_ROW*MAZE_NUM_COL, 0);
+    // Setup push buttons
+    gPB_left.mode(PullUp);
+    gPB_right.mode(PullUp);
+    gPB_up.mode(PullUp);
+    gPB_down.mode(PullUp);
     // Delay for initial pullup to take effect
     wait(.01);
     // Setup Interrupt callback functions for a pb hit
-    pb_left.attach_deasserted(&pb_left_hit_callback);
-    pb_right.attach_deasserted(&pb_right_hit_callback);
-    pb_up.attach_deasserted(&pb_up_hit_callback);
-    pb_down.attach_deasserted(&pb_down_hit_callback);
+    gPB_left.attach_deasserted(&pb_left_hit_callback);
+    gPB_right.attach_deasserted(&pb_right_hit_callback);
+    gPB_up.attach_deasserted(&pb_up_hit_callback);
+    gPB_down.attach_deasserted(&pb_down_hit_callback);
     // Setup speaker
-    SpeakerOut.period(1.0/200000.0);    
+    gSpeakerOut.period(1.0/200000.0);  
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;      
+    Sample_Period.attach(&Sample_timer_interrupt, 1.0/(20000.0));
 
     //Setup LCD display
-    uLCD.display_control(PORTRAIT);
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-    uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
+    guLCD.display_control(PORTRAIT);
+    guLCD.background_color(BLACK);
+    guLCD.cls();
+    guLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
     wait(1.0);
- //   DrawMaze();    
+  
     // Start sampling pb inputs using interrupts
-    pb_left.setSampleFrequency();
-    pb_right.setSampleFrequency();
-    pb_up.setSampleFrequency();
-    pb_down.setSampleFrequency();
-    // pushbuttons now setup and running
+    gPB_left.setSampleFrequency();
+    gPB_right.setSampleFrequency();
+    gPB_up.setSampleFrequency();
+    gPB_down.setSampleFrequency();
+    //////////////////////////////////////
+    // Everything should be ready to start playing the game.
     while(1)
     {
+        // Ask the user if they would like to play a game. 
+        guLCD.locate(10, 10);
+        guLCD.puts("If you would you like to play a game,");
+        guLCD.locate(10, 35);
+        guLCD.puts("please press a button.");
+        
+        // Wait for a button to be pressed
+        while (gGameState == GAME_PAUSED){};
+        // Reset the Ghosts and Buzzy
+
+        // Start up new name
+        // Play introduction sounds while drawing the Maze
+        gSpeakerOut.SwitchSound(Speaker::BEGIN);
+        DrawMaze();  
+        // Start Game loop
+        while (gGameState == GAME_PAUSED)
+        {
+            gSpeakerOut.SwitchSound(Speaker::DEATH);
+            // Move Buzzy and any active ghosts
+            gBuzzy.Move();
+            for (ii = 0 ; ii < NUM_GHOSTS ; ii++)
+            {
+                gGhosts[ii].Move();
  
+            }
+           // Check to see if a Ghost got Buzzy
+/*            if (gBuzzy.DidGhostGetBuzzy())
+            {                    
+                // Check to see if Game is over
+                if (--NumLivesRemaining == 0)
+                {
+                    NumLivesRemaining = 3;
+                    gGameState = GAME_OVER;
+                    break;
+                }
+                // Play Death sound
+                gSpeakerOut.SwitchSound(Speaker::DEATH);
+                                        
+                wait(2.0);
+                
+                // Put Buzzy back at starting location
+            }            
+ */           // Check to see if Buzzy has eaten all the honey drops
+        }
+
+        gGameState = GAME_PAUSED;
     }
+
 } //end main
\ No newline at end of file