Jeffery Hurley
/
Asteroids
Simple starter skeleton for asteroids video game.
main.cpp@3:98aa3db6a48f, 2021-04-01 (annotated)
- Committer:
- jhurley31
- Date:
- Thu Apr 01 19:10:58 2021 +0000
- Revision:
- 3:98aa3db6a48f
- Parent:
- 2:30020ddfccf6
Initial commit.
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 | 3:98aa3db6a48f | 3 | #include <ctime> |
jhurley31 | 3:98aa3db6a48f | 4 | #include <cstdlib> |
jhurley31 | 0:0c450cb95a1e | 5 | #include "Speaker.h" |
jhurley31 | 0:0c450cb95a1e | 6 | #include "PinDetect.h" |
jhurley31 | 1:a6872783beca | 7 | #include "uLCD_4DGL.h" |
jhurley31 | 3:98aa3db6a48f | 8 | #include "CommandShip.h" |
jhurley31 | 3:98aa3db6a48f | 9 | #include "Asteroid.h" |
jhurley31 | 3:98aa3db6a48f | 10 | |
jhurley31 | 3:98aa3db6a48f | 11 | |
jhurley31 | 3:98aa3db6a48f | 12 | using namespace std; |
jhurley31 | 3:98aa3db6a48f | 13 | #define GAME_PAUSED 0 |
jhurley31 | 3:98aa3db6a48f | 14 | #define GAME_RUNNING 1 |
jhurley31 | 3:98aa3db6a48f | 15 | #define GAME_OVER 2 |
jhurley31 | 3:98aa3db6a48f | 16 | |
jhurley31 | 3:98aa3db6a48f | 17 | #define NUM_ASTEROIDS 8 |
jhurley31 | 3:98aa3db6a48f | 18 | |
jhurley31 | 1:a6872783beca | 19 | //////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 20 | // Setup instance of LCD display |
jhurley31 | 2:30020ddfccf6 | 21 | uLCD_4DGL guLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
jhurley31 | 1:a6872783beca | 22 | //////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 23 | // Setup instances of push button pins |
jhurley31 | 2:30020ddfccf6 | 24 | PinDetect gPB_left(p16); |
jhurley31 | 2:30020ddfccf6 | 25 | PinDetect gPB_right(p17); |
jhurley31 | 3:98aa3db6a48f | 26 | PinDetect gPB_fire(p18); |
jhurley31 | 3:98aa3db6a48f | 27 | // Create Ship and Asteriods |
jhurley31 | 3:98aa3db6a48f | 28 | CommandShip gShip; |
jhurley31 | 3:98aa3db6a48f | 29 | Asteroid gAsteroids[NUM_ASTEROIDS]; |
jhurley31 | 2:30020ddfccf6 | 30 | |
jhurley31 | 3:98aa3db6a48f | 31 | // Variable indicates if game is paused or running |
jhurley31 | 3:98aa3db6a48f | 32 | int gGameState = GAME_PAUSED; |
jhurley31 | 3:98aa3db6a48f | 33 | // Declare and initialize the speaker |
jhurley31 | 2:30020ddfccf6 | 34 | Speaker gSpeakerOut(p21); |
jhurley31 | 2:30020ddfccf6 | 35 | |
jhurley31 | 3:98aa3db6a48f | 36 | // declare the gTimeStep |
jhurley31 | 2:30020ddfccf6 | 37 | |
jhurley31 | 3:98aa3db6a48f | 38 | double gTimeStep = 0.03; |
jhurley31 | 3:98aa3db6a48f | 39 | int gNumLives = 3; |
jhurley31 | 3:98aa3db6a48f | 40 | |
jhurley31 | 3:98aa3db6a48f | 41 | double gOriginX = 63.0; |
jhurley31 | 3:98aa3db6a48f | 42 | double gOriginY = 63.0; |
jhurley31 | 1:a6872783beca | 43 | ////////////////////////////////////////////////////////////////////// |
jhurley31 | 1:a6872783beca | 44 | // Interrupt routine |
jhurley31 | 1:a6872783beca | 45 | // used to output next analog sample whenever a timer interrupt occurs |
jhurley31 | 1:a6872783beca | 46 | void Sample_timer_interrupt(void) |
jhurley31 | 1:a6872783beca | 47 | { |
jhurley31 | 3:98aa3db6a48f | 48 | // Call speaker function to play next value |
jhurley31 | 2:30020ddfccf6 | 49 | gSpeakerOut.PlayNextValue(); |
jhurley31 | 1:a6872783beca | 50 | } |
jhurley31 | 0:0c450cb95a1e | 51 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 0:0c450cb95a1e | 52 | // Callback routine is interrupt activated by a debounced pb_left hit |
jhurley31 | 0:0c450cb95a1e | 53 | void pb_left_hit_callback (void) |
jhurley31 | 0:0c450cb95a1e | 54 | { |
jhurley31 | 3:98aa3db6a48f | 55 | // Update game state and tell ship to rotate to the left |
jhurley31 | 3:98aa3db6a48f | 56 | if (gGameState == GAME_RUNNING) |
jhurley31 | 3:98aa3db6a48f | 57 | { |
jhurley31 | 3:98aa3db6a48f | 58 | gShip.rotateLeft(); |
jhurley31 | 3:98aa3db6a48f | 59 | } |
jhurley31 | 3:98aa3db6a48f | 60 | |
jhurley31 | 3:98aa3db6a48f | 61 | gGameState = GAME_RUNNING; |
jhurley31 | 3:98aa3db6a48f | 62 | |
jhurley31 | 0:0c450cb95a1e | 63 | } |
jhurley31 | 0:0c450cb95a1e | 64 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 0:0c450cb95a1e | 65 | // Callback routine is interrupt activated by a debounced pb_right hit |
jhurley31 | 0:0c450cb95a1e | 66 | void pb_right_hit_callback (void) |
jhurley31 | 0:0c450cb95a1e | 67 | { |
jhurley31 | 3:98aa3db6a48f | 68 | // Update game state and tell ship to rotate to the left |
jhurley31 | 3:98aa3db6a48f | 69 | if (gGameState == GAME_RUNNING) |
jhurley31 | 3:98aa3db6a48f | 70 | { |
jhurley31 | 3:98aa3db6a48f | 71 | |
jhurley31 | 3:98aa3db6a48f | 72 | gShip.rotateRight(); |
jhurley31 | 3:98aa3db6a48f | 73 | } |
jhurley31 | 3:98aa3db6a48f | 74 | |
jhurley31 | 3:98aa3db6a48f | 75 | gGameState = GAME_RUNNING; |
jhurley31 | 3:98aa3db6a48f | 76 | |
jhurley31 | 1:a6872783beca | 77 | } |
jhurley31 | 1:a6872783beca | 78 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 3:98aa3db6a48f | 79 | // Callback routine is interrupt activated by a debounced pb_fire hit |
jhurley31 | 3:98aa3db6a48f | 80 | void pb_fire_hit_callback (void) |
jhurley31 | 1:a6872783beca | 81 | { |
jhurley31 | 3:98aa3db6a48f | 82 | // Update game state and tell ship to fire |
jhurley31 | 3:98aa3db6a48f | 83 | if (gGameState == GAME_RUNNING) |
jhurley31 | 3:98aa3db6a48f | 84 | { |
jhurley31 | 3:98aa3db6a48f | 85 | gShip.fire(); |
jhurley31 | 3:98aa3db6a48f | 86 | } |
jhurley31 | 3:98aa3db6a48f | 87 | gGameState = GAME_RUNNING; |
jhurley31 | 0:0c450cb95a1e | 88 | } |
jhurley31 | 3:98aa3db6a48f | 89 | |
jhurley31 | 0:0c450cb95a1e | 90 | //--------------------------------------------------------------------------------------------------- |
jhurley31 | 3:98aa3db6a48f | 91 | |
jhurley31 | 0:0c450cb95a1e | 92 | int main() |
jhurley31 | 0:0c450cb95a1e | 93 | { |
jhurley31 | 3:98aa3db6a48f | 94 | |
jhurley31 | 3:98aa3db6a48f | 95 | srand(static_cast<unsigned int>(time(0))); |
jhurley31 | 3:98aa3db6a48f | 96 | |
jhurley31 | 2:30020ddfccf6 | 97 | // Setup push buttons |
jhurley31 | 2:30020ddfccf6 | 98 | gPB_left.mode(PullUp); |
jhurley31 | 2:30020ddfccf6 | 99 | gPB_right.mode(PullUp); |
jhurley31 | 3:98aa3db6a48f | 100 | gPB_fire.mode(PullUp); |
jhurley31 | 1:a6872783beca | 101 | // Delay for initial pullup to take effect |
jhurley31 | 1:a6872783beca | 102 | wait(.01); |
jhurley31 | 1:a6872783beca | 103 | // Setup Interrupt callback functions for a pb hit |
jhurley31 | 2:30020ddfccf6 | 104 | gPB_left.attach_deasserted(&pb_left_hit_callback); |
jhurley31 | 2:30020ddfccf6 | 105 | gPB_right.attach_deasserted(&pb_right_hit_callback); |
jhurley31 | 3:98aa3db6a48f | 106 | gPB_fire.attach_deasserted(&pb_fire_hit_callback); |
jhurley31 | 3:98aa3db6a48f | 107 | |
jhurley31 | 1:a6872783beca | 108 | // Setup speaker |
jhurley31 | 3:98aa3db6a48f | 109 | //gSpeakerOut.period(1.0/200000.0); |
jhurley31 | 3:98aa3db6a48f | 110 | gSpeakerOut.period(1.0/100000.0); |
jhurley31 | 2:30020ddfccf6 | 111 | // set up a timer to be used for sample rate interrupts |
jhurley31 | 2:30020ddfccf6 | 112 | Ticker Sample_Period; |
jhurley31 | 2:30020ddfccf6 | 113 | Sample_Period.attach(&Sample_timer_interrupt, 1.0/(20000.0)); |
jhurley31 | 1:a6872783beca | 114 | |
jhurley31 | 1:a6872783beca | 115 | //Setup LCD display |
jhurley31 | 3:98aa3db6a48f | 116 | guLCD.display_control(LANDSCAPE); |
jhurley31 | 2:30020ddfccf6 | 117 | guLCD.background_color(BLACK); |
jhurley31 | 2:30020ddfccf6 | 118 | guLCD.cls(); |
jhurley31 | 2:30020ddfccf6 | 119 | guLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display |
jhurley31 | 1:a6872783beca | 120 | wait(1.0); |
jhurley31 | 2:30020ddfccf6 | 121 | |
jhurley31 | 1:a6872783beca | 122 | // Start sampling pb inputs using interrupts |
jhurley31 | 2:30020ddfccf6 | 123 | gPB_left.setSampleFrequency(); |
jhurley31 | 2:30020ddfccf6 | 124 | gPB_right.setSampleFrequency(); |
jhurley31 | 3:98aa3db6a48f | 125 | gPB_fire.setSampleFrequency(); |
jhurley31 | 3:98aa3db6a48f | 126 | |
jhurley31 | 2:30020ddfccf6 | 127 | ////////////////////////////////////// |
jhurley31 | 2:30020ddfccf6 | 128 | // Everything should be ready to start playing the game. |
jhurley31 | 1:a6872783beca | 129 | while(1) |
jhurley31 | 1:a6872783beca | 130 | { |
jhurley31 | 3:98aa3db6a48f | 131 | guLCD.cls(); |
jhurley31 | 2:30020ddfccf6 | 132 | // Ask the user if they would like to play a game. |
jhurley31 | 3:98aa3db6a48f | 133 | guLCD.printf("Would you like to play a game?\n\n Press Any Key to Start"); |
jhurley31 | 2:30020ddfccf6 | 134 | |
jhurley31 | 3:98aa3db6a48f | 135 | wait(.01); |
jhurley31 | 2:30020ddfccf6 | 136 | // Wait for a button to be pressed |
jhurley31 | 3:98aa3db6a48f | 137 | gGameState = GAME_PAUSED; |
jhurley31 | 3:98aa3db6a48f | 138 | |
jhurley31 | 2:30020ddfccf6 | 139 | while (gGameState == GAME_PAUSED) |
jhurley31 | 2:30020ddfccf6 | 140 | { |
jhurley31 | 3:98aa3db6a48f | 141 | |
jhurley31 | 3:98aa3db6a48f | 142 | wait(0.1); |
jhurley31 | 3:98aa3db6a48f | 143 | } |
jhurley31 | 3:98aa3db6a48f | 144 | |
jhurley31 | 3:98aa3db6a48f | 145 | guLCD.cls(); |
jhurley31 | 3:98aa3db6a48f | 146 | |
jhurley31 | 3:98aa3db6a48f | 147 | // Start up new game |
jhurley31 | 3:98aa3db6a48f | 148 | gSpeakerOut.SwitchSound(Speaker::BEATS1); |
jhurley31 | 3:98aa3db6a48f | 149 | // Create Initial Asteriods |
jhurley31 | 3:98aa3db6a48f | 150 | |
jhurley31 | 3:98aa3db6a48f | 151 | // Start Game loop |
jhurley31 | 3:98aa3db6a48f | 152 | |
jhurley31 | 3:98aa3db6a48f | 153 | while (gNumLives > 0) |
jhurley31 | 3:98aa3db6a48f | 154 | { |
jhurley31 | 3:98aa3db6a48f | 155 | // Move the ship and the asteriods |
jhurley31 | 3:98aa3db6a48f | 156 | gShip.move(); |
jhurley31 | 3:98aa3db6a48f | 157 | |
jhurley31 | 3:98aa3db6a48f | 158 | for (int ii = 0 ; ii < NUM_ASTEROIDS ; ++ii) |
jhurley31 | 2:30020ddfccf6 | 159 | { |
jhurley31 | 3:98aa3db6a48f | 160 | if (gAsteroids[ii].isValid()) |
jhurley31 | 3:98aa3db6a48f | 161 | { |
jhurley31 | 3:98aa3db6a48f | 162 | gAsteroids[ii].move(); |
jhurley31 | 3:98aa3db6a48f | 163 | } |
jhurley31 | 2:30020ddfccf6 | 164 | } |
jhurley31 | 3:98aa3db6a48f | 165 | |
jhurley31 | 3:98aa3db6a48f | 166 | // Check if all asteriods are invalid and exit game if that is the case |
jhurley31 | 3:98aa3db6a48f | 167 | wait(gTimeStep); |
jhurley31 | 2:30020ddfccf6 | 168 | } |
jhurley31 | 2:30020ddfccf6 | 169 | |
jhurley31 | 2:30020ddfccf6 | 170 | gGameState = GAME_PAUSED; |
jhurley31 | 1:a6872783beca | 171 | } |
jhurley31 | 2:30020ddfccf6 | 172 | |
jhurley31 | 3:98aa3db6a48f | 173 | } //end main |
jhurley31 | 3:98aa3db6a48f | 174 |