Simple starter skeleton for asteroids video game.

Dependencies:   PinDetect

main.cpp

Committer:
jhurley31
Date:
2019-03-02
Revision:
2:30020ddfccf6
Parent:
1:a6872783beca
Child:
3:98aa3db6a48f

File content as of revision 2:30020ddfccf6:

#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 guLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
////////////////////////////////////////
// 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;

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
    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);
}
//---------------------------------------------------------------------------------------------------
// 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);
}
//---------------------------------------------------------------------------------------------------
// Callback routine is interrupt activated by a debounced pb_down hit
void pb_down_hit_callback (void)
{
    // Tell Buzzy to go down
    gBuzzy.SetDesiredDirectionToMove(Sprite::DOWN_DIR);
}
//---------------------------------------------------------------------------------------------------
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);
    // 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
    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
    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
    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);
  
    // Start sampling pb inputs using interrupts
    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