Simple starter skeleton for asteroids video game.

Dependencies:   PinDetect

Revision:
3:98aa3db6a48f
Parent:
2:30020ddfccf6
--- a/main.cpp	Sat Mar 02 16:11:43 2019 +0000
+++ b/main.cpp	Thu Apr 01 19:10:58 2021 +0000
@@ -1,12 +1,21 @@
 #include "mbed.h"
 #include <stdio.h>
+#include <ctime>
+#include <cstdlib>
 #include "Speaker.h"
 #include "PinDetect.h"
-#include "BuzzyGraphics.h"
 #include "uLCD_4DGL.h"
-//#include "Sprite.h"
-#include "Buzzy.h"
-#include "Ghosts.h"
+#include "CommandShip.h"
+#include "Asteroid.h"
+
+
+using namespace std;
+#define GAME_PAUSED 0
+#define GAME_RUNNING 1
+#define GAME_OVER 2
+
+#define NUM_ASTEROIDS 8
+
 ////////////////////////////////////////
 // Setup instance of LCD display
 uLCD_4DGL guLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
@@ -14,90 +23,97 @@
 // Setup instances of push button pins
 PinDetect gPB_left(p16); 
 PinDetect gPB_right(p17); 
-PinDetect gPB_up(p18);
-PinDetect gPB_down(p19);
-
-Buzzy gBuzzy;
-Sprite gGhosts[NUM_GHOSTS];
-
-
-
-int gGameState = GAME_PAUSED;
+PinDetect gPB_fire(p18);
+// Create Ship and Asteriods
+CommandShip gShip;
+Asteroid gAsteroids[NUM_ASTEROIDS];
 
+// Variable indicates if game is paused or running
+int gGameState = GAME_PAUSED;
+// Declare and initialize the speaker
 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();
+// declare the gTimeStep
 
+double gTimeStep = 0.03;
+int gNumLives = 3;
+
+double gOriginX = 63.0;
+double gOriginY = 63.0;
 //////////////////////////////////////////////////////////////////////
 // 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
+    // Call speaker function to play next value
     gSpeakerOut.PlayNextValue();
-
 }
 //---------------------------------------------------------------------------------------------------
 // Callback routine is interrupt activated by a debounced pb_left hit
 void pb_left_hit_callback (void)
 {
-    // Tell Buzzy to go left
-    gBuzzy.SetDesiredDirectionToMove(Sprite::LEFT_DIR);
+    // Update game state and tell ship to rotate to the left
+    if (gGameState == GAME_RUNNING)
+    {
+        gShip.rotateLeft();
+    }
+    
+    gGameState = GAME_RUNNING;
+ 
 }
 //---------------------------------------------------------------------------------------------------
 // Callback routine is interrupt activated by a debounced pb_right hit
 void pb_right_hit_callback (void)
 {
-    // Tell Buzzy to go right
-    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
-    gBuzzy.SetDesiredDirectionToMove(Sprite::UP_DIR);
+    // Update game state and tell ship to rotate to the left
+    if (gGameState == GAME_RUNNING)
+    {
+ 
+        gShip.rotateRight();
+    }
+    
+    gGameState = GAME_RUNNING;
+
 }
 //---------------------------------------------------------------------------------------------------
-// Callback routine is interrupt activated by a debounced pb_down hit
-void pb_down_hit_callback (void)
+// Callback routine is interrupt activated by a debounced pb_fire hit
+void pb_fire_hit_callback (void)
 {
-    // Tell Buzzy to go down
-    gBuzzy.SetDesiredDirectionToMove(Sprite::DOWN_DIR);
+    // Update game state and tell ship to fire
+    if (gGameState == GAME_RUNNING)
+    {
+        gShip.fire();
+    }
+    gGameState = GAME_RUNNING;
 }
+
 //---------------------------------------------------------------------------------------------------
+
 int main()
 {
-    int ii;
-    unsigned char NumLivesRemaining = 3;
-         
-    // Zero out the dynamic 2D maze array
-    memset(&gDynaMaze[0][0], MAZE_NUM_ROW*MAZE_NUM_COL, 0);
+    
+    srand(static_cast<unsigned int>(time(0)));
+
     // Setup push buttons
     gPB_left.mode(PullUp);
     gPB_right.mode(PullUp);
-    gPB_up.mode(PullUp);
-    gPB_down.mode(PullUp);
+    gPB_fire.mode(PullUp);
     // Delay for initial pullup to take effect
     wait(.01);
     // Setup Interrupt callback functions for a pb hit
     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);
+    gPB_fire.attach_deasserted(&pb_fire_hit_callback);
+
     // Setup speaker
-    gSpeakerOut.period(1.0/200000.0);  
+    //gSpeakerOut.period(1.0/200000.0);  
+    gSpeakerOut.period(1.0/100000.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
-    guLCD.display_control(PORTRAIT);
+    guLCD.display_control(LANDSCAPE);
     guLCD.background_color(BLACK);
     guLCD.cls();
     guLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
@@ -106,58 +122,53 @@
     // Start sampling pb inputs using interrupts
     gPB_left.setSampleFrequency();
     gPB_right.setSampleFrequency();
-    gPB_up.setSampleFrequency();
-    gPB_down.setSampleFrequency();
+    gPB_fire.setSampleFrequency();
+
     //////////////////////////////////////
     // Everything should be ready to start playing the game.
     while(1)
     {
+        guLCD.cls();
         // 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.");
+        guLCD.printf("Would you like to play a game?\n\n Press Any Key to Start");
         
+        wait(.01);
         // 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
+        gGameState = GAME_PAUSED;
+              
         while (gGameState == GAME_PAUSED)
         {
-            gSpeakerOut.SwitchSound(Speaker::DEATH);
-            // Move Buzzy and any active ghosts
-            gBuzzy.Move();
-            for (ii = 0 ; ii < NUM_GHOSTS ; ii++)
+
+            wait(0.1);
+        }
+
+        guLCD.cls();
+        
+        // Start up new game
+        gSpeakerOut.SwitchSound(Speaker::BEATS1);
+        // Create Initial Asteriods
+        
+        // Start Game loop
+
+        while (gNumLives > 0)
+        {
+            // Move the ship and the asteriods
+           gShip.move();
+
+            for (int ii = 0 ; ii < NUM_ASTEROIDS ; ++ii)
             {
-                gGhosts[ii].Move();
- 
+                if (gAsteroids[ii].isValid())
+                {
+                    gAsteroids[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
+            
+            // Check if all asteriods are invalid and exit game if that is the case
+            wait(gTimeStep);
         }
 
         gGameState = GAME_PAUSED;
     }
 
-} //end main
\ No newline at end of file
+} //end main
+