ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

Committer:
el17arm
Date:
Tue May 07 23:51:54 2019 +0000
Revision:
60:3df033345059
Parent:
58:4a826093d9e9
Child:
63:ec95e155fb30
Hopefully final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17arm 42:d81c008b0436 1 /*
el17arm 42:d81c008b0436 2 ELEC2645 Embedded Systems Project
el17arm 42:d81c008b0436 3 School of Electronic & Electrical Engineering
el17arm 42:d81c008b0436 4 University of Leeds
el17arm 42:d81c008b0436 5 Name: Andrew Milner
el17arm 42:d81c008b0436 6 Username: el17arm
el17arm 42:d81c008b0436 7 Student ID Number: 201162219
el17arm 42:d81c008b0436 8 Date: 18/03/19
el17arm 42:d81c008b0436 9 */
el17arm 42:d81c008b0436 10
el17arm 45:bad704c546d4 11 ///////// pre-processor directives ////////
el17arm 42:d81c008b0436 12
el17arm 0:fe19852199d2 13 #include "mbed.h"
el17arm 0:fe19852199d2 14 #include "N5110.h"
el17arm 0:fe19852199d2 15 #include "Gamepad.h"
el17arm 29:d85886364643 16 #include "Gameengine.h"
el17arm 0:fe19852199d2 17
el17arm 45:bad704c546d4 18 /////////////// objects ///////////////
el17arm 45:bad704c546d4 19
el17arm 29:d85886364643 20 Gameengine game;
el17arm 0:fe19852199d2 21 Gamepad pad;
el17arm 0:fe19852199d2 22 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el17arm 0:fe19852199d2 23 AnalogIn pot0(PTB2);
el17arm 41:0cf320f73424 24 BusOut oxygen(PTA1, PTA2, PTC2);
el17arm 41:0cf320f73424 25 BusOut lives(PTC3, PTC4, PTD3);
el17arm 41:0cf320f73424 26
el17arm 45:bad704c546d4 27 ///////////// prototypes ///////////////
el17arm 45:bad704c546d4 28
el17arm 58:4a826093d9e9 29 /**
el17arm 45:bad704c546d4 30 * @brief sets the contrast of lcd screen
el17arm 45:bad704c546d4 31 * @details contrast is adjust with analog input PTB2
el17arm 45:bad704c546d4 32 */
el17arm 0:fe19852199d2 33 void contrast();
el17arm 56:8c827d1cae3b 34 /** Initialises all classes and libraries and default game settings
el17arm 45:bad704c546d4 35 * @details initialises LCD and gamepad library and sets screen brightness
el17arm 45:bad704c546d4 36 */
el17arm 0:fe19852199d2 37 void init();
el17arm 56:8c827d1cae3b 38 /** Displays opening screen
el17arm 45:bad704c546d4 39 * @details Screen displays title of game and instructs player to press start to begin
el17arm 45:bad704c546d4 40 */
el17arm 0:fe19852199d2 41 void start_screen();
el17arm 56:8c827d1cae3b 42 /** Controls LEDs on front panel.
el17arm 45:bad704c546d4 43 * @details Every time a player dies a RHS LED is turned off, as time reduces
el17arm 56:8c827d1cae3b 44 * LEDs turn off on LHS.
el17arm 45:bad704c546d4 45 */
el17arm 41:0cf320f73424 46 void leds();
el17arm 56:8c827d1cae3b 47 /** Initialises and renders all level objects
el17arm 45:bad704c546d4 48 * @details Contains functions for all levels and will display next level objects
el17arm 45:bad704c546d4 49 * once player has completed each level
el17arm 45:bad704c546d4 50 */
el17arm 15:2bda80896a84 51 void render();
el17arm 56:8c827d1cae3b 52 /** Displays screen if game over or game complete
el17arm 56:8c827d1cae3b 53 * @details Screen will display player's final score and display game over or game complete
el17arm 45:bad704c546d4 54 */
el17arm 19:4789cb4ca550 55 void restart();
el17arm 58:4a826093d9e9 56 /** Displays options screen
el17arm 58:4a826093d9e9 57 * @details gives player controls and displays credits
el17arm 58:4a826093d9e9 58 */
el17arm 60:3df033345059 59 void option_screen();
el17arm 60:3df033345059 60
el17arm 60:3df033345059 61 bool options = false;
el17arm 45:bad704c546d4 62
el17arm 45:bad704c546d4 63 ///////////// functions ////////////////
el17arm 45:bad704c546d4 64
el17arm 0:fe19852199d2 65 int main()
el17arm 0:fe19852199d2 66 {
el17arm 45:bad704c546d4 67 init(); // initialises screen and default game settings
el17arm 45:bad704c546d4 68 start_screen(); // waits for user to press start to begin
el17arm 58:4a826093d9e9 69 option_screen(); // displays options screen
el17arm 58:4a826093d9e9 70
el17arm 45:bad704c546d4 71 // game loop, reads input and updates game accordingly
el17arm 0:fe19852199d2 72 while (1) {
el17arm 41:0cf320f73424 73
el17arm 45:bad704c546d4 74 contrast(); // adjusts contrast of screen
el17arm 45:bad704c546d4 75 render(); // draws all level objects on to screen
el17arm 45:bad704c546d4 76 game.update(lcd, pad); // updates movement, collected keys, lives, time left
el17arm 45:bad704c546d4 77 restart(); // restart screen if game over or end screen if game complete displays final score
el17arm 45:bad704c546d4 78 wait(0.1); // wait function to control fps
el17arm 45:bad704c546d4 79 }
el17arm 45:bad704c546d4 80 }
el17arm 48:bd1f31fbfee3 81
el17arm 41:0cf320f73424 82
el17arm 45:bad704c546d4 83 //initialises all classes and libraries and default game settings
el17arm 0:fe19852199d2 84 void init()
el17arm 0:fe19852199d2 85 {
el17arm 0:fe19852199d2 86 lcd.init();
el17arm 0:fe19852199d2 87 lcd.normalMode(); // normal colour mode
el17arm 0:fe19852199d2 88 lcd.refresh();
el17arm 41:0cf320f73424 89 lcd.setBrightness(1.0);
el17arm 0:fe19852199d2 90 pad.init();
el17arm 29:d85886364643 91 game.game_init();
el17arm 15:2bda80896a84 92 }
el17arm 2:725c213b2396 93
el17arm 45:bad704c546d4 94 // detects which level is being played and draws the level objects with collision settings
el17arm 15:2bda80896a84 95 void render()
el17arm 15:2bda80896a84 96 {
el17arm 58:4a826093d9e9 97 game.draw_l1(lcd, pad); // level 1
el17arm 45:bad704c546d4 98 game.draw_l2(lcd, pad); // level 2
el17arm 45:bad704c546d4 99 game.draw_l3(lcd, pad); // level 3
el17arm 58:4a826093d9e9 100 game.draw_l4(lcd, pad); // level 4
el17arm 45:bad704c546d4 101 leds(); // function to update leds to reflect time and lives left
el17arm 0:fe19852199d2 102 }
el17arm 0:fe19852199d2 103
el17arm 45:bad704c546d4 104 // controls contrast of the screen
el17arm 0:fe19852199d2 105 void contrast()
el17arm 0:fe19852199d2 106 {
el17arm 0:fe19852199d2 107 lcd.refresh();
el17arm 0:fe19852199d2 108 float con = pot0.read();
el17arm 0:fe19852199d2 109 lcd.setContrast(con);
el17arm 1:813ba5341985 110 lcd.clear();
el17arm 0:fe19852199d2 111 }
el17arm 0:fe19852199d2 112
el17arm 45:bad704c546d4 113 // displays start screen and waits for user to press start
el17arm 0:fe19852199d2 114 void start_screen()
el17arm 58:4a826093d9e9 115 {
el17arm 60:3df033345059 116 while (pad.check_event(Gamepad::START_PRESSED) == false && options == false) {
el17arm 60:3df033345059 117 lcd.clear();
el17arm 60:3df033345059 118 lcd.drawSprite(2,20,8,3,(int *)miner_right);
el17arm 60:3df033345059 119 lcd.drawSprite(79,20,8,3,(int *)miner_left);
el17arm 60:3df033345059 120 lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
el17arm 60:3df033345059 121 lcd.printString("MANIC MILNER",7,1);
el17arm 60:3df033345059 122 lcd.printString("Press start!",8,2);
el17arm 60:3df033345059 123 lcd.printString("A = Controls",6,4);
el17arm 60:3df033345059 124 contrast();
el17arm 60:3df033345059 125 //printf("start screen = %d \n",options);
el17arm 60:3df033345059 126 if (pad.check_event(Gamepad::A_PRESSED) == true) {
el17arm 60:3df033345059 127 options = true;
el17arm 60:3df033345059 128 //printf("options = %d \n",options);
el17arm 60:3df033345059 129 }
el17arm 0:fe19852199d2 130 }
el17arm 19:4789cb4ca550 131 }
el17arm 19:4789cb4ca550 132
el17arm 45:bad704c546d4 133 // displays appropriate screen with final score for game over and game complete
el17arm 19:4789cb4ca550 134 void restart()
el17arm 19:4789cb4ca550 135 {
el17arm 29:d85886364643 136 if (game.game_over() == true) {
el17arm 19:4789cb4ca550 137 lcd.clear();
el17arm 41:0cf320f73424 138 game.get_score(lcd);
el17arm 45:bad704c546d4 139 lcd.drawRect(0,0,84,48, FILL_TRANSPARENT);
el17arm 45:bad704c546d4 140 lcd.printString("Game Over! ",14,1);
el17arm 45:bad704c546d4 141 lcd.printString("Score ",18,2);
el17arm 60:3df033345059 142 lcd.printString("Back = Replay",3,4);
el17arm 44:e29458976114 143 }
el17arm 44:e29458976114 144 if (game.game_complete(lcd) == true) {
el17arm 44:e29458976114 145 lcd.clear();
el17arm 44:e29458976114 146 game.get_score(lcd);
el17arm 45:bad704c546d4 147 lcd.printString("YOU WIN!!! ",12,1);
el17arm 45:bad704c546d4 148 lcd.printString("Score ",18,2);
el17arm 45:bad704c546d4 149 lcd.printString("Well done!!!",8,4);
el17arm 19:4789cb4ca550 150 }
el17arm 60:3df033345059 151 if(pad.check_event(Gamepad::BACK_PRESSED) == true) {
el17arm 60:3df033345059 152 init();
el17arm 60:3df033345059 153 }
el17arm 60:3df033345059 154 }
el17arm 60:3df033345059 155
el17arm 60:3df033345059 156 void option_screen()
el17arm 60:3df033345059 157 {
el17arm 60:3df033345059 158 while (options == true) {
el17arm 60:3df033345059 159 lcd.clear();
el17arm 60:3df033345059 160 lcd.printString("Stick = L/R",0,0);
el17arm 60:3df033345059 161 lcd.printString("A = Jump ",0,1);
el17arm 60:3df033345059 162 lcd.printString("Get keys and",0,2);
el17arm 60:3df033345059 163 lcd.printString("return to exit",0,3);
el17arm 60:3df033345059 164 lcd.printString("Led=time/lives",0,4);
el17arm 60:3df033345059 165 lcd.printString("<< Press back",0,5);
el17arm 60:3df033345059 166 //printf("options screen = %d \n",options);
el17arm 60:3df033345059 167 lcd.refresh();
el17arm 60:3df033345059 168 if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
el17arm 60:3df033345059 169 options = false;
el17arm 60:3df033345059 170 start_screen();
el17arm 60:3df033345059 171 }
el17arm 60:3df033345059 172 }
el17arm 41:0cf320f73424 173 }
el17arm 41:0cf320f73424 174
el17arm 45:bad704c546d4 175 // leds indicate how many lives and time left
el17arm 41:0cf320f73424 176 void leds()
el17arm 58:4a826093d9e9 177 {
el17arm 45:bad704c546d4 178 // fsm to display lives left
el17arm 41:0cf320f73424 179 int l_leds[4] = {0b111,0b110,0b100,0b000};
el17arm 45:bad704c546d4 180 //fsm to display how much time (oxygen) left
el17arm 41:0cf320f73424 181 int r_leds[4] = {0b111,0b110,0b100,0b000};
el17arm 58:4a826093d9e9 182
el17arm 45:bad704c546d4 183 // function keeps track of time elapsed and changes leds accordingly
el17arm 41:0cf320f73424 184 oxygen = l_leds[game.oxygen_leds()];
el17arm 45:bad704c546d4 185 // function keeps track of lives left and changes leds accordingly
el17arm 41:0cf320f73424 186 lives = r_leds[game.lives_leds()];
el17arm 37:4d525a37d5d2 187 }