Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of el17z2q by
main.cpp@2:6dc7bc55c1cb, 2018-05-07 (annotated)
- Committer:
- yzjdxl
- Date:
- Mon May 07 22:11:43 2018 +0000
- Branch:
- GameEngine
- Revision:
- 2:6dc7bc55c1cb
- Parent:
- 1:00a4ea97c4cd
publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yzjdxl | 1:00a4ea97c4cd | 1 | /* |
yzjdxl | 1:00a4ea97c4cd | 2 | ELEC2645 Embedded Systems Project |
yzjdxl | 1:00a4ea97c4cd | 3 | School of Electronic & Electrical Engineering |
yzjdxl | 1:00a4ea97c4cd | 4 | University of Leeds |
yzjdxl | 1:00a4ea97c4cd | 5 | Name:Zikang Qian |
yzjdxl | 1:00a4ea97c4cd | 6 | Username: yzjdxl |
yzjdxl | 1:00a4ea97c4cd | 7 | Student ID Number: 201189213 |
yzjdxl | 1:00a4ea97c4cd | 8 | Date: 2/5/2018 |
yzjdxl | 1:00a4ea97c4cd | 9 | */ |
yzjdxl | 1:00a4ea97c4cd | 10 | |
yzjdxl | 1:00a4ea97c4cd | 11 | ///////// pre-processor directives //////// |
yzjdxl | 0:f707ce1010fa | 12 | #include "mbed.h" |
yzjdxl | 1:00a4ea97c4cd | 13 | #include "Gamepad.h" |
yzjdxl | 1:00a4ea97c4cd | 14 | #include "N5110.h" |
yzjdxl | 1:00a4ea97c4cd | 15 | #include "GameEngine.h" |
yzjdxl | 1:00a4ea97c4cd | 16 | |
yzjdxl | 1:00a4ea97c4cd | 17 | #ifdef WITH_TESTING |
yzjdxl | 1:00a4ea97c4cd | 18 | #include "tests.h" |
yzjdxl | 1:00a4ea97c4cd | 19 | #endif |
yzjdxl | 0:f707ce1010fa | 20 | |
yzjdxl | 2:6dc7bc55c1cb | 21 | #define BAG_WIDTH 15 |
yzjdxl | 1:00a4ea97c4cd | 22 | #define BAG_HEIGHT 2 |
yzjdxl | 2:6dc7bc55c1cb | 23 | #define COIN_SIZE 2 |
yzjdxl | 1:00a4ea97c4cd | 24 | #define COIN_SPEED 3 |
yzjdxl | 1:00a4ea97c4cd | 25 | /////////////// structs ///////////////// |
yzjdxl | 1:00a4ea97c4cd | 26 | struct UserInput { |
yzjdxl | 1:00a4ea97c4cd | 27 | Direction d; |
yzjdxl | 1:00a4ea97c4cd | 28 | float mag; |
yzjdxl | 1:00a4ea97c4cd | 29 | }; |
yzjdxl | 1:00a4ea97c4cd | 30 | /////////////// objects /////////////// |
yzjdxl | 1:00a4ea97c4cd | 31 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
yzjdxl | 1:00a4ea97c4cd | 32 | Gamepad pad; |
yzjdxl | 1:00a4ea97c4cd | 33 | GameEngine game; |
yzjdxl | 0:f707ce1010fa | 34 | |
yzjdxl | 1:00a4ea97c4cd | 35 | ///////////// prototypes /////////////// |
yzjdxl | 1:00a4ea97c4cd | 36 | void init(); |
yzjdxl | 1:00a4ea97c4cd | 37 | void update_game(UserInput input); |
yzjdxl | 1:00a4ea97c4cd | 38 | void render(); |
yzjdxl | 1:00a4ea97c4cd | 39 | void begin_text(); |
yzjdxl | 1:00a4ea97c4cd | 40 | void rule_text(); |
yzjdxl | 1:00a4ea97c4cd | 41 | void gameover_text(); |
yzjdxl | 1:00a4ea97c4cd | 42 | |
yzjdxl | 1:00a4ea97c4cd | 43 | ///////////// functions //////////////// |
yzjdxl | 0:f707ce1010fa | 44 | int main() |
yzjdxl | 0:f707ce1010fa | 45 | { |
yzjdxl | 1:00a4ea97c4cd | 46 | |
yzjdxl | 1:00a4ea97c4cd | 47 | #ifdef WITH_TESTING |
yzjdxl | 1:00a4ea97c4cd | 48 | int number_of_failures = run_all_tests(); |
yzjdxl | 1:00a4ea97c4cd | 49 | |
yzjdxl | 1:00a4ea97c4cd | 50 | if(number_of_failures > 0) return number_of_failures; |
yzjdxl | 1:00a4ea97c4cd | 51 | #endif |
yzjdxl | 1:00a4ea97c4cd | 52 | |
yzjdxl | 1:00a4ea97c4cd | 53 | int fps = 10; // frames per second |
yzjdxl | 1:00a4ea97c4cd | 54 | int nodead; |
yzjdxl | 1:00a4ea97c4cd | 55 | |
yzjdxl | 1:00a4ea97c4cd | 56 | do{ |
yzjdxl | 1:00a4ea97c4cd | 57 | init(); // initialise and then display start screen |
yzjdxl | 1:00a4ea97c4cd | 58 | begin_text(); // waiting for the user to start |
yzjdxl | 1:00a4ea97c4cd | 59 | lcd.clear(); |
yzjdxl | 1:00a4ea97c4cd | 60 | |
yzjdxl | 1:00a4ea97c4cd | 61 | init(); // initialise and then display rule screen |
yzjdxl | 1:00a4ea97c4cd | 62 | rule_text(); // waiting for the user to read the text |
yzjdxl | 1:00a4ea97c4cd | 63 | |
yzjdxl | 1:00a4ea97c4cd | 64 | render(); // first draw the initial frame |
yzjdxl | 1:00a4ea97c4cd | 65 | wait(1.0f/fps); // and wait for one frame period |
yzjdxl | 1:00a4ea97c4cd | 66 | |
yzjdxl | 1:00a4ea97c4cd | 67 | // game loop - read input, update the game state and render the display |
yzjdxl | 1:00a4ea97c4cd | 68 | do{ |
yzjdxl | 1:00a4ea97c4cd | 69 | game.read_input(pad); |
yzjdxl | 1:00a4ea97c4cd | 70 | nodead = game.update(pad); |
yzjdxl | 1:00a4ea97c4cd | 71 | render(); |
yzjdxl | 1:00a4ea97c4cd | 72 | wait(1.0f/fps); |
yzjdxl | 1:00a4ea97c4cd | 73 | }while(nodead); |
yzjdxl | 1:00a4ea97c4cd | 74 | |
yzjdxl | 1:00a4ea97c4cd | 75 | // escape the loop when missing a coin |
yzjdxl | 1:00a4ea97c4cd | 76 | lcd.clear(); |
yzjdxl | 1:00a4ea97c4cd | 77 | init(); // initialise and then display end screen |
yzjdxl | 1:00a4ea97c4cd | 78 | gameover_text(); // waiting for the user to restart |
yzjdxl | 1:00a4ea97c4cd | 79 | |
yzjdxl | 1:00a4ea97c4cd | 80 | }while(1); |
yzjdxl | 1:00a4ea97c4cd | 81 | } |
yzjdxl | 1:00a4ea97c4cd | 82 | |
yzjdxl | 1:00a4ea97c4cd | 83 | void render() |
yzjdxl | 1:00a4ea97c4cd | 84 | { |
yzjdxl | 1:00a4ea97c4cd | 85 | // clear screen, re-draw and refresh |
yzjdxl | 1:00a4ea97c4cd | 86 | lcd.clear(); |
yzjdxl | 1:00a4ea97c4cd | 87 | game.draw(lcd); |
yzjdxl | 1:00a4ea97c4cd | 88 | lcd.refresh(); |
yzjdxl | 1:00a4ea97c4cd | 89 | } |
yzjdxl | 1:00a4ea97c4cd | 90 | |
yzjdxl | 1:00a4ea97c4cd | 91 | // initialies all classes and libraries |
yzjdxl | 1:00a4ea97c4cd | 92 | void init() |
yzjdxl | 1:00a4ea97c4cd | 93 | { |
yzjdxl | 1:00a4ea97c4cd | 94 | // need to initialise LCD and Gamepad |
yzjdxl | 1:00a4ea97c4cd | 95 | lcd.init(); |
yzjdxl | 1:00a4ea97c4cd | 96 | pad.init(); |
yzjdxl | 1:00a4ea97c4cd | 97 | // initialise the game with correct coin and bag sizes |
yzjdxl | 1:00a4ea97c4cd | 98 | game.init(BAG_WIDTH,BAG_HEIGHT,COIN_SIZE,COIN_SPEED); |
yzjdxl | 1:00a4ea97c4cd | 99 | } |
yzjdxl | 1:00a4ea97c4cd | 100 | |
yzjdxl | 1:00a4ea97c4cd | 101 | // simple splash screen displayed on start-up |
yzjdxl | 1:00a4ea97c4cd | 102 | void begin_text() { |
yzjdxl | 1:00a4ea97c4cd | 103 | |
yzjdxl | 1:00a4ea97c4cd | 104 | lcd.printString("Monopoly",17,2); |
yzjdxl | 1:00a4ea97c4cd | 105 | lcd.printString("Press START",10,5); |
yzjdxl | 1:00a4ea97c4cd | 106 | lcd.refresh(); |
yzjdxl | 1:00a4ea97c4cd | 107 | |
yzjdxl | 1:00a4ea97c4cd | 108 | // wait flashing LEDs until start button is pressed |
yzjdxl | 1:00a4ea97c4cd | 109 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
yzjdxl | 1:00a4ea97c4cd | 110 | pad.leds_on(); |
yzjdxl | 1:00a4ea97c4cd | 111 | wait(0.1); |
yzjdxl | 1:00a4ea97c4cd | 112 | pad.leds_off(); |
yzjdxl | 1:00a4ea97c4cd | 113 | wait(0.1); |
yzjdxl | 1:00a4ea97c4cd | 114 | } |
yzjdxl | 1:00a4ea97c4cd | 115 | } |
yzjdxl | 1:00a4ea97c4cd | 116 | |
yzjdxl | 1:00a4ea97c4cd | 117 | void rule_text() { |
yzjdxl | 1:00a4ea97c4cd | 118 | |
yzjdxl | 1:00a4ea97c4cd | 119 | lcd.printString("Take the coins",0,1); |
yzjdxl | 1:00a4ea97c4cd | 120 | lcd.printString("as many as",0,2); |
yzjdxl | 1:00a4ea97c4cd | 121 | lcd.printString("possible!",0,3); |
yzjdxl | 1:00a4ea97c4cd | 122 | lcd.printString("Press START",10,5); |
yzjdxl | 1:00a4ea97c4cd | 123 | lcd.refresh(); |
yzjdxl | 1:00a4ea97c4cd | 124 | |
yzjdxl | 1:00a4ea97c4cd | 125 | // wait flashing LEDs until start button is pressed |
yzjdxl | 1:00a4ea97c4cd | 126 | while (pad.check_event(Gamepad::START_PRESSED) == false); |
yzjdxl | 1:00a4ea97c4cd | 127 | } |
yzjdxl | 1:00a4ea97c4cd | 128 | // simple splash screen displayed on game-over |
yzjdxl | 1:00a4ea97c4cd | 129 | void gameover_text() { |
yzjdxl | 1:00a4ea97c4cd | 130 | |
yzjdxl | 1:00a4ea97c4cd | 131 | lcd.printString("Game Over!",13,2); |
yzjdxl | 1:00a4ea97c4cd | 132 | lcd.printString("Press BACK",10,5); |
yzjdxl | 1:00a4ea97c4cd | 133 | lcd.refresh(); |
yzjdxl | 1:00a4ea97c4cd | 134 | |
yzjdxl | 1:00a4ea97c4cd | 135 | // wait flashing LEDs until back button is pressed |
yzjdxl | 1:00a4ea97c4cd | 136 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false) { |
yzjdxl | 1:00a4ea97c4cd | 137 | pad.leds_on(); |
yzjdxl | 1:00a4ea97c4cd | 138 | wait(0.1); |
yzjdxl | 1:00a4ea97c4cd | 139 | pad.leds_off(); |
yzjdxl | 1:00a4ea97c4cd | 140 | wait(0.1); |
yzjdxl | 0:f707ce1010fa | 141 | } |
yzjdxl | 0:f707ce1010fa | 142 | } |