Jeffery Hurley
/
Asteroids
Simple starter skeleton for asteroids video game.
main.cpp@2:30020ddfccf6, 2019-03-02 (annotated)
- Committer:
- jhurley31
- Date:
- Sat Mar 02 16:11:43 2019 +0000
- Revision:
- 2:30020ddfccf6
- Parent:
- 1:a6872783beca
- Child:
- 3:98aa3db6a48f
There is an issue with inheritance
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhurley31 | 0:0c450cb95a1e | 1 | #include "mbed.h" |
jhurley31 | 2:30020ddfccf6 | 2 | #include <stdio.h> |
jhurley31 | 0:0c450cb95a1e | 3 | #include "Speaker.h" |
jhurley31 | 0:0c450cb95a1e | 4 | #include "PinDetect.h" |
jhurley31 | 1:a6872783beca | 5 | #include "BuzzyGraphics.h" |
jhurley31 | 1:a6872783beca | 6 | #include "uLCD_4DGL.h" |
jhurley31 | 2:30020ddfccf6 | 7 | //#include "Sprite.h" |
jhurley31 | 2:30020ddfccf6 | 8 | #include "Buzzy.h" |
jhurley31 | 2:30020ddfccf6 | 9 | #include "Ghosts.h" |
jhurley31 | 1:a6872783beca | 10 | //////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 11 | // Setup instance of LCD display |
jhurley31 | 2:30020ddfccf6 | 12 | uLCD_4DGL guLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
jhurley31 | 1:a6872783beca | 13 | //////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 14 | // Setup instances of push button pins |
jhurley31 | 2:30020ddfccf6 | 15 | PinDetect gPB_left(p16); |
jhurley31 | 2:30020ddfccf6 | 16 | PinDetect gPB_right(p17); |
jhurley31 | 2:30020ddfccf6 | 17 | PinDetect gPB_up(p18); |
jhurley31 | 2:30020ddfccf6 | 18 | PinDetect gPB_down(p19); |
jhurley31 | 2:30020ddfccf6 | 19 | |
jhurley31 | 2:30020ddfccf6 | 20 | Buzzy gBuzzy; |
jhurley31 | 2:30020ddfccf6 | 21 | Sprite gGhosts[NUM_GHOSTS]; |
jhurley31 | 2:30020ddfccf6 | 22 | |
jhurley31 | 2:30020ddfccf6 | 23 | |
jhurley31 | 1:a6872783beca | 24 | |
jhurley31 | 2:30020ddfccf6 | 25 | int gGameState = GAME_PAUSED; |
jhurley31 | 2:30020ddfccf6 | 26 | |
jhurley31 | 2:30020ddfccf6 | 27 | Speaker gSpeakerOut(p21); |
jhurley31 | 2:30020ddfccf6 | 28 | //////////////////////////////////////////////////////// |
jhurley31 | 2:30020ddfccf6 | 29 | // This is the maze that changes as the game is played |
jhurley31 | 2:30020ddfccf6 | 30 | char gDynaMaze[MAZE_NUM_ROW][MAZE_NUM_COL]; |
jhurley31 | 2:30020ddfccf6 | 31 | |
jhurley31 | 2:30020ddfccf6 | 32 | ////////////////////////// |
jhurley31 | 2:30020ddfccf6 | 33 | // Prototype functions |
jhurley31 | 2:30020ddfccf6 | 34 | void DrawMaze(); |
jhurley31 | 2:30020ddfccf6 | 35 | |
jhurley31 | 1:a6872783beca | 36 | ////////////////////////////////////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 37 | // Interrupt routine |
jhurley31 | 1:a6872783beca | 38 | // used to output next analog sample whenever a timer interrupt occurs |
jhurley31 | 1:a6872783beca | 39 | void Sample_timer_interrupt(void) |
jhurley31 | 1:a6872783beca | 40 | { |
jhurley31 | 1:a6872783beca | 41 | // send next analog sample out to D to A |
jhurley31 | 2:30020ddfccf6 | 42 | gSpeakerOut.PlayNextValue(); |
jhurley31 | 1:a6872783beca | 43 | |
jhurley31 | 1:a6872783beca | 44 | } |
jhurley31 | 0:0c450cb95a1e | 45 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 0:0c450cb95a1e | 46 | // Callback routine is interrupt activated by a debounced pb_left hit |
jhurley31 | 0:0c450cb95a1e | 47 | void pb_left_hit_callback (void) |
jhurley31 | 0:0c450cb95a1e | 48 | { |
jhurley31 | 1:a6872783beca | 49 | // Tell Buzzy to go left |
jhurley31 | 2:30020ddfccf6 | 50 | gBuzzy.SetDesiredDirectionToMove(Sprite::LEFT_DIR); |
jhurley31 | 0:0c450cb95a1e | 51 | } |
jhurley31 | 0:0c450cb95a1e | 52 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 0:0c450cb95a1e | 53 | // Callback routine is interrupt activated by a debounced pb_right hit |
jhurley31 | 0:0c450cb95a1e | 54 | void pb_right_hit_callback (void) |
jhurley31 | 0:0c450cb95a1e | 55 | { |
jhurley31 | 1:a6872783beca | 56 | // Tell Buzzy to go right |
jhurley31 | 2:30020ddfccf6 | 57 | gBuzzy.SetDesiredDirectionToMove(Sprite::RIGHT_DIR); |
jhurley31 | 1:a6872783beca | 58 | } |
jhurley31 | 1:a6872783beca | 59 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 1:a6872783beca | 60 | // Callback routine is interrupt activated by a debounced pb_up hit |
jhurley31 | 1:a6872783beca | 61 | void pb_up_hit_callback (void) |
jhurley31 | 1:a6872783beca | 62 | { |
jhurley31 | 1:a6872783beca | 63 | // Tell Buzzy to go up |
jhurley31 | 2:30020ddfccf6 | 64 | gBuzzy.SetDesiredDirectionToMove(Sprite::UP_DIR); |
jhurley31 | 1:a6872783beca | 65 | } |
jhurley31 | 1:a6872783beca | 66 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 1:a6872783beca | 67 | // Callback routine is interrupt activated by a debounced pb_down hit |
jhurley31 | 1:a6872783beca | 68 | void pb_down_hit_callback (void) |
jhurley31 | 1:a6872783beca | 69 | { |
jhurley31 | 1:a6872783beca | 70 | // Tell Buzzy to go down |
jhurley31 | 2:30020ddfccf6 | 71 | gBuzzy.SetDesiredDirectionToMove(Sprite::DOWN_DIR); |
jhurley31 | 0:0c450cb95a1e | 72 | } |
jhurley31 | 0:0c450cb95a1e | 73 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 0:0c450cb95a1e | 74 | int main() |
jhurley31 | 0:0c450cb95a1e | 75 | { |
jhurley31 | 2:30020ddfccf6 | 76 | int ii; |
jhurley31 | 2:30020ddfccf6 | 77 | unsigned char NumLivesRemaining = 3; |
jhurley31 | 2:30020ddfccf6 | 78 | |
jhurley31 | 2:30020ddfccf6 | 79 | // Zero out the dynamic 2D maze array |
jhurley31 | 2:30020ddfccf6 | 80 | memset(&gDynaMaze[0][0], MAZE_NUM_ROW*MAZE_NUM_COL, 0); |
jhurley31 | 2:30020ddfccf6 | 81 | // Setup push buttons |
jhurley31 | 2:30020ddfccf6 | 82 | gPB_left.mode(PullUp); |
jhurley31 | 2:30020ddfccf6 | 83 | gPB_right.mode(PullUp); |
jhurley31 | 2:30020ddfccf6 | 84 | gPB_up.mode(PullUp); |
jhurley31 | 2:30020ddfccf6 | 85 | gPB_down.mode(PullUp); |
jhurley31 | 1:a6872783beca | 86 | // Delay for initial pullup to take effect |
jhurley31 | 1:a6872783beca | 87 | wait(.01); |
jhurley31 | 1:a6872783beca | 88 | // Setup Interrupt callback functions for a pb hit |
jhurley31 | 2:30020ddfccf6 | 89 | gPB_left.attach_deasserted(&pb_left_hit_callback); |
jhurley31 | 2:30020ddfccf6 | 90 | gPB_right.attach_deasserted(&pb_right_hit_callback); |
jhurley31 | 2:30020ddfccf6 | 91 | gPB_up.attach_deasserted(&pb_up_hit_callback); |
jhurley31 | 2:30020ddfccf6 | 92 | gPB_down.attach_deasserted(&pb_down_hit_callback); |
jhurley31 | 1:a6872783beca | 93 | // Setup speaker |
jhurley31 | 2:30020ddfccf6 | 94 | gSpeakerOut.period(1.0/200000.0); |
jhurley31 | 2:30020ddfccf6 | 95 | // set up a timer to be used for sample rate interrupts |
jhurley31 | 2:30020ddfccf6 | 96 | Ticker Sample_Period; |
jhurley31 | 2:30020ddfccf6 | 97 | Sample_Period.attach(&Sample_timer_interrupt, 1.0/(20000.0)); |
jhurley31 | 1:a6872783beca | 98 | |
jhurley31 | 1:a6872783beca | 99 | //Setup LCD display |
jhurley31 | 2:30020ddfccf6 | 100 | guLCD.display_control(PORTRAIT); |
jhurley31 | 2:30020ddfccf6 | 101 | guLCD.background_color(BLACK); |
jhurley31 | 2:30020ddfccf6 | 102 | guLCD.cls(); |
jhurley31 | 2:30020ddfccf6 | 103 | guLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display |
jhurley31 | 1:a6872783beca | 104 | wait(1.0); |
jhurley31 | 2:30020ddfccf6 | 105 | |
jhurley31 | 1:a6872783beca | 106 | // Start sampling pb inputs using interrupts |
jhurley31 | 2:30020ddfccf6 | 107 | gPB_left.setSampleFrequency(); |
jhurley31 | 2:30020ddfccf6 | 108 | gPB_right.setSampleFrequency(); |
jhurley31 | 2:30020ddfccf6 | 109 | gPB_up.setSampleFrequency(); |
jhurley31 | 2:30020ddfccf6 | 110 | gPB_down.setSampleFrequency(); |
jhurley31 | 2:30020ddfccf6 | 111 | ////////////////////////////////////// |
jhurley31 | 2:30020ddfccf6 | 112 | // Everything should be ready to start playing the game. |
jhurley31 | 1:a6872783beca | 113 | while(1) |
jhurley31 | 1:a6872783beca | 114 | { |
jhurley31 | 2:30020ddfccf6 | 115 | // Ask the user if they would like to play a game. |
jhurley31 | 2:30020ddfccf6 | 116 | guLCD.locate(10, 10); |
jhurley31 | 2:30020ddfccf6 | 117 | guLCD.puts("If you would you like to play a game,"); |
jhurley31 | 2:30020ddfccf6 | 118 | guLCD.locate(10, 35); |
jhurley31 | 2:30020ddfccf6 | 119 | guLCD.puts("please press a button."); |
jhurley31 | 2:30020ddfccf6 | 120 | |
jhurley31 | 2:30020ddfccf6 | 121 | // Wait for a button to be pressed |
jhurley31 | 2:30020ddfccf6 | 122 | while (gGameState == GAME_PAUSED){}; |
jhurley31 | 2:30020ddfccf6 | 123 | // Reset the Ghosts and Buzzy |
jhurley31 | 2:30020ddfccf6 | 124 | |
jhurley31 | 2:30020ddfccf6 | 125 | // Start up new name |
jhurley31 | 2:30020ddfccf6 | 126 | // Play introduction sounds while drawing the Maze |
jhurley31 | 2:30020ddfccf6 | 127 | gSpeakerOut.SwitchSound(Speaker::BEGIN); |
jhurley31 | 2:30020ddfccf6 | 128 | DrawMaze(); |
jhurley31 | 2:30020ddfccf6 | 129 | // Start Game loop |
jhurley31 | 2:30020ddfccf6 | 130 | while (gGameState == GAME_PAUSED) |
jhurley31 | 2:30020ddfccf6 | 131 | { |
jhurley31 | 2:30020ddfccf6 | 132 | gSpeakerOut.SwitchSound(Speaker::DEATH); |
jhurley31 | 2:30020ddfccf6 | 133 | // Move Buzzy and any active ghosts |
jhurley31 | 2:30020ddfccf6 | 134 | gBuzzy.Move(); |
jhurley31 | 2:30020ddfccf6 | 135 | for (ii = 0 ; ii < NUM_GHOSTS ; ii++) |
jhurley31 | 2:30020ddfccf6 | 136 | { |
jhurley31 | 2:30020ddfccf6 | 137 | gGhosts[ii].Move(); |
jhurley31 | 1:a6872783beca | 138 | |
jhurley31 | 2:30020ddfccf6 | 139 | } |
jhurley31 | 2:30020ddfccf6 | 140 | // Check to see if a Ghost got Buzzy |
jhurley31 | 2:30020ddfccf6 | 141 | /* if (gBuzzy.DidGhostGetBuzzy()) |
jhurley31 | 2:30020ddfccf6 | 142 | { |
jhurley31 | 2:30020ddfccf6 | 143 | // Check to see if Game is over |
jhurley31 | 2:30020ddfccf6 | 144 | if (--NumLivesRemaining == 0) |
jhurley31 | 2:30020ddfccf6 | 145 | { |
jhurley31 | 2:30020ddfccf6 | 146 | NumLivesRemaining = 3; |
jhurley31 | 2:30020ddfccf6 | 147 | gGameState = GAME_OVER; |
jhurley31 | 2:30020ddfccf6 | 148 | break; |
jhurley31 | 2:30020ddfccf6 | 149 | } |
jhurley31 | 2:30020ddfccf6 | 150 | // Play Death sound |
jhurley31 | 2:30020ddfccf6 | 151 | gSpeakerOut.SwitchSound(Speaker::DEATH); |
jhurley31 | 2:30020ddfccf6 | 152 | |
jhurley31 | 2:30020ddfccf6 | 153 | wait(2.0); |
jhurley31 | 2:30020ddfccf6 | 154 | |
jhurley31 | 2:30020ddfccf6 | 155 | // Put Buzzy back at starting location |
jhurley31 | 2:30020ddfccf6 | 156 | } |
jhurley31 | 2:30020ddfccf6 | 157 | */ // Check to see if Buzzy has eaten all the honey drops |
jhurley31 | 2:30020ddfccf6 | 158 | } |
jhurley31 | 2:30020ddfccf6 | 159 | |
jhurley31 | 2:30020ddfccf6 | 160 | gGameState = GAME_PAUSED; |
jhurley31 | 1:a6872783beca | 161 | } |
jhurley31 | 2:30020ddfccf6 | 162 | |
jhurley31 | 0:0c450cb95a1e | 163 | } //end main |