Version of Robotron arcade game using LPC1768, a Gameduino shield, a serial EEPROM (for high scores), two microswitch joysticks and two buttons plus a box to put it in. 20 levels of mayhem.
Dependencies: 25LCxxx_SPI CommonTypes Gameduino mbed
GameRobotRic.cpp@0:5fa232ee5fdf, 2013-06-04 (annotated)
- Committer:
- RichardE
- Date:
- Tue Jun 04 20:16:33 2013 +0000
- Revision:
- 0:5fa232ee5fdf
- Child:
- 1:dfd5eaaf96a3
Started conversion from Maple version of game. So far Gameduino seems to have been initialised OK and just displays a sign on message. Lots of commented out code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RichardE | 0:5fa232ee5fdf | 1 | /* |
RichardE | 0:5fa232ee5fdf | 2 | * SOURCE FILE : GameRobotRic.cpp |
RichardE | 0:5fa232ee5fdf | 3 | * |
RichardE | 0:5fa232ee5fdf | 4 | * The RobotRic game class. |
RichardE | 0:5fa232ee5fdf | 5 | * |
RichardE | 0:5fa232ee5fdf | 6 | */ |
RichardE | 0:5fa232ee5fdf | 7 | |
RichardE | 0:5fa232ee5fdf | 8 | #include "GameRobotRic.h" // this module's prototypes |
RichardE | 0:5fa232ee5fdf | 9 | #include "Types.h" // various integer types etc. |
RichardE | 0:5fa232ee5fdf | 10 | #include "LevelCollection.h" // all the levels |
RichardE | 0:5fa232ee5fdf | 11 | #if 0 |
RichardE | 0:5fa232ee5fdf | 12 | #include "RobotRicCharacterSet.h" // character set used in this game |
RichardE | 0:5fa232ee5fdf | 13 | #include "GDExtra.h" // extra Gameduino functions |
RichardE | 0:5fa232ee5fdf | 14 | #include "GDConst.h" // a few more Gameduino constants |
RichardE | 0:5fa232ee5fdf | 15 | #include "ArenaConst.h" // gameplay arena constants |
RichardE | 0:5fa232ee5fdf | 16 | #include "I2CEEPROM.h" // for access to serial EEPROM |
RichardE | 0:5fa232ee5fdf | 17 | #include "HighScoreEntry.h" // for getting player's name for high score table |
RichardE | 0:5fa232ee5fdf | 18 | #endif |
RichardE | 0:5fa232ee5fdf | 19 | |
RichardE | 0:5fa232ee5fdf | 20 | // Number of lives player starts with. |
RichardE | 0:5fa232ee5fdf | 21 | #define START_LIVES 5 |
RichardE | 0:5fa232ee5fdf | 22 | |
RichardE | 0:5fa232ee5fdf | 23 | // Pin allocations for I2C communications link. |
RichardE | 0:5fa232ee5fdf | 24 | #define EEPROM_SCL 5 |
RichardE | 0:5fa232ee5fdf | 25 | #define EEPROM_SDA 7 |
RichardE | 0:5fa232ee5fdf | 26 | #define EEPROM_WP 8 |
RichardE | 0:5fa232ee5fdf | 27 | |
RichardE | 0:5fa232ee5fdf | 28 | // Address of EEPROM set using A0, A1 and A2 pins. |
RichardE | 0:5fa232ee5fdf | 29 | #define ADDRESS_OF_EEPROM 0 |
RichardE | 0:5fa232ee5fdf | 30 | |
RichardE | 0:5fa232ee5fdf | 31 | /**************************/ |
RichardE | 0:5fa232ee5fdf | 32 | /* CHECK FOR A HIGH SCORE */ |
RichardE | 0:5fa232ee5fdf | 33 | /**************************/ |
RichardE | 0:5fa232ee5fdf | 34 | // Pass pointer to high score table in highScores. |
RichardE | 0:5fa232ee5fdf | 35 | // Pass score that was achieved in score. |
RichardE | 0:5fa232ee5fdf | 36 | void GameRobotRic::CheckForHighScore( HighScoreTable *highScores, UInt32 score ) { |
RichardE | 0:5fa232ee5fdf | 37 | #if 0 |
RichardE | 0:5fa232ee5fdf | 38 | UInt8 tablePos; |
RichardE | 0:5fa232ee5fdf | 39 | // Enter name into high score table if score is high enough. |
RichardE | 0:5fa232ee5fdf | 40 | if( ( tablePos = highScores->GetPositionInTable( player.Score ) ) < highScores->GetCapacity() ) { |
RichardE | 0:5fa232ee5fdf | 41 | // Player has made it onto the high score table. |
RichardE | 0:5fa232ee5fdf | 42 | // Get player to input name. |
RichardE | 0:5fa232ee5fdf | 43 | HighScoreEntry nameGetter; |
RichardE | 0:5fa232ee5fdf | 44 | PlayerName name; |
RichardE | 0:5fa232ee5fdf | 45 | nameGetter.GetName( &name, &controls ); |
RichardE | 0:5fa232ee5fdf | 46 | // Add name and score to table. |
RichardE | 0:5fa232ee5fdf | 47 | highScores->Add( tablePos, &name, score ); |
RichardE | 0:5fa232ee5fdf | 48 | } |
RichardE | 0:5fa232ee5fdf | 49 | #endif |
RichardE | 0:5fa232ee5fdf | 50 | } |
RichardE | 0:5fa232ee5fdf | 51 | |
RichardE | 0:5fa232ee5fdf | 52 | /*****************/ |
RichardE | 0:5fa232ee5fdf | 53 | /* PLAY THE GAME */ |
RichardE | 0:5fa232ee5fdf | 54 | /*****************/ |
RichardE | 0:5fa232ee5fdf | 55 | // This NEVER exits. |
RichardE | 0:5fa232ee5fdf | 56 | void GameRobotRic::Play( void ) { |
RichardE | 0:5fa232ee5fdf | 57 | // Make a digital output for use with Gameduino. |
RichardE | 0:5fa232ee5fdf | 58 | DigitalOut cs( p8 ); |
RichardE | 0:5fa232ee5fdf | 59 | // Initialise an SPI link for communications with Gameduino. |
RichardE | 0:5fa232ee5fdf | 60 | // Use pin 5 for MOSI. |
RichardE | 0:5fa232ee5fdf | 61 | // Use pin 6 for MISO. |
RichardE | 0:5fa232ee5fdf | 62 | // Use pin 7 for SCK. |
RichardE | 0:5fa232ee5fdf | 63 | SPI spi( p5, p6, p7 ); |
RichardE | 0:5fa232ee5fdf | 64 | // 8MHz clock should be OK. |
RichardE | 0:5fa232ee5fdf | 65 | spi.frequency( 8000000 ); |
RichardE | 0:5fa232ee5fdf | 66 | // Set SPI format to use. |
RichardE | 0:5fa232ee5fdf | 67 | // Use 8 bits per SPI frame. |
RichardE | 0:5fa232ee5fdf | 68 | // Use SPI mode 0. |
RichardE | 0:5fa232ee5fdf | 69 | spi.format( 8, 0 ); |
RichardE | 0:5fa232ee5fdf | 70 | // Make a Gameduino and pass SPI link and digital output for chip select. |
RichardE | 0:5fa232ee5fdf | 71 | Gameduino gd( &spi, &cs ); |
RichardE | 0:5fa232ee5fdf | 72 | // Reset the Gameduino. |
RichardE | 0:5fa232ee5fdf | 73 | gd.begin(); |
RichardE | 0:5fa232ee5fdf | 74 | // Lets have a default ASCII character set. |
RichardE | 0:5fa232ee5fdf | 75 | gd.ascii(); |
RichardE | 0:5fa232ee5fdf | 76 | // Display sign on message. |
RichardE | 0:5fa232ee5fdf | 77 | gd.putstr( 3, 10, "RobotRic is up and running!" ); |
RichardE | 0:5fa232ee5fdf | 78 | #if 0 |
RichardE | 0:5fa232ee5fdf | 79 | // Initialise I2C communications (to talk to serial EEPROM). |
RichardE | 0:5fa232ee5fdf | 80 | Wire.begin( EEPROM_SDA, EEPROM_SCL ); |
RichardE | 0:5fa232ee5fdf | 81 | // Initialise serial EEPROM object with EEPROM address of zero. |
RichardE | 0:5fa232ee5fdf | 82 | I2CEEPROM eeprom; |
RichardE | 0:5fa232ee5fdf | 83 | eeprom.Open( &Wire, ADDRESS_OF_EEPROM, EEPROM_WP ); |
RichardE | 0:5fa232ee5fdf | 84 | // Create a high score table that uses the EEPROM. |
RichardE | 0:5fa232ee5fdf | 85 | HighScoreTable highScores( &eeprom ); |
RichardE | 0:5fa232ee5fdf | 86 | #endif |
RichardE | 0:5fa232ee5fdf | 87 | // Start on the attract level. |
RichardE | 0:5fa232ee5fdf | 88 | UInt8 levelNumber = LevelCollection::AttractLevel; |
RichardE | 0:5fa232ee5fdf | 89 | #if 0 |
RichardE | 0:5fa232ee5fdf | 90 | player.Lives = START_LIVES; |
RichardE | 0:5fa232ee5fdf | 91 | #endif |
RichardE | 0:5fa232ee5fdf | 92 | LevelCollection levels; |
RichardE | 0:5fa232ee5fdf | 93 | Level *level; |
RichardE | 0:5fa232ee5fdf | 94 | Level::LevelExitCode exitCode; |
RichardE | 0:5fa232ee5fdf | 95 | #if 0 |
RichardE | 0:5fa232ee5fdf | 96 | // Initialise panel controls. |
RichardE | 0:5fa232ee5fdf | 97 | controls.InitialisePins(); |
RichardE | 0:5fa232ee5fdf | 98 | // Specify controls player should use. |
RichardE | 0:5fa232ee5fdf | 99 | player.SetControls( &controls ); |
RichardE | 0:5fa232ee5fdf | 100 | // Restrict players movement. |
RichardE | 0:5fa232ee5fdf | 101 | player.MovementRestricted = true; |
RichardE | 0:5fa232ee5fdf | 102 | player.Bounds = &ArenaRectangle; |
RichardE | 0:5fa232ee5fdf | 103 | #endif |
RichardE | 0:5fa232ee5fdf | 104 | // Repeat forever. |
RichardE | 0:5fa232ee5fdf | 105 | while( true ) { |
RichardE | 0:5fa232ee5fdf | 106 | #if 0 |
RichardE | 0:5fa232ee5fdf | 107 | // Get level specified by level number. |
RichardE | 0:5fa232ee5fdf | 108 | level = levels.GetLevel( levelNumber ); |
RichardE | 0:5fa232ee5fdf | 109 | // If failed to get level with that number go back to first normal level. |
RichardE | 0:5fa232ee5fdf | 110 | // This will happen if player completes last level. |
RichardE | 0:5fa232ee5fdf | 111 | if( level == NULL ) { |
RichardE | 0:5fa232ee5fdf | 112 | levelNumber = LevelCollection::FirstNormalLevel; |
RichardE | 0:5fa232ee5fdf | 113 | // Refetch level. |
RichardE | 0:5fa232ee5fdf | 114 | level = levels.GetLevel( levelNumber ); |
RichardE | 0:5fa232ee5fdf | 115 | } |
RichardE | 0:5fa232ee5fdf | 116 | // Pass reference to high score table. |
RichardE | 0:5fa232ee5fdf | 117 | level->SetHighScores( &highScores ); |
RichardE | 0:5fa232ee5fdf | 118 | // Set player that is playing the level. |
RichardE | 0:5fa232ee5fdf | 119 | level->SetPlayer( &player ); |
RichardE | 0:5fa232ee5fdf | 120 | // Play the level. |
RichardE | 0:5fa232ee5fdf | 121 | exitCode = level->Play(); |
RichardE | 0:5fa232ee5fdf | 122 | // If player was killed then decrement lives otherwise |
RichardE | 0:5fa232ee5fdf | 123 | // advance to next level. |
RichardE | 0:5fa232ee5fdf | 124 | switch( exitCode ) { |
RichardE | 0:5fa232ee5fdf | 125 | case Level::GameOver : |
RichardE | 0:5fa232ee5fdf | 126 | // TODO : Do some sort of game over fuss. |
RichardE | 0:5fa232ee5fdf | 127 | CheckForHighScore( &highScores, player.Score ); |
RichardE | 0:5fa232ee5fdf | 128 | // Go back to attract level and reset player lives and score. |
RichardE | 0:5fa232ee5fdf | 129 | levelNumber = LevelCollection::AttractLevel; |
RichardE | 0:5fa232ee5fdf | 130 | player.Lives = START_LIVES; |
RichardE | 0:5fa232ee5fdf | 131 | player.Score = 0; |
RichardE | 0:5fa232ee5fdf | 132 | break; |
RichardE | 0:5fa232ee5fdf | 133 | case Level::Completed : |
RichardE | 0:5fa232ee5fdf | 134 | levelNumber++; |
RichardE | 0:5fa232ee5fdf | 135 | break; |
RichardE | 0:5fa232ee5fdf | 136 | } |
RichardE | 0:5fa232ee5fdf | 137 | #endif |
RichardE | 0:5fa232ee5fdf | 138 | // Finished with Gameduino. |
RichardE | 0:5fa232ee5fdf | 139 | gd.end(); |
RichardE | 0:5fa232ee5fdf | 140 | } |
RichardE | 0:5fa232ee5fdf | 141 | } |
RichardE | 0:5fa232ee5fdf | 142 |