A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
main.cpp@31:ab24d028ddfd, 2019-05-05 (annotated)
- Committer:
- el17sm
- Date:
- Sun May 05 18:37:23 2019 +0000
- Revision:
- 31:ab24d028ddfd
- Parent:
- 30:ec915d24d3e9
- Child:
- 32:fe6359ef9916
Rearranged main.cpp to be converted into a game_engine class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17sm | 0:8e92b66a0755 | 1 | /* |
el17sm | 0:8e92b66a0755 | 2 | ELEC2645 Embedded Systems Project |
el17sm | 0:8e92b66a0755 | 3 | School of Electronic & Electrical Engineering |
el17sm | 0:8e92b66a0755 | 4 | University of Leeds |
el17sm | 0:8e92b66a0755 | 5 | Name: Steven Mahasin |
el17sm | 0:8e92b66a0755 | 6 | Username: el17sm |
el17sm | 0:8e92b66a0755 | 7 | Student ID Number: 201192939 |
el17sm | 0:8e92b66a0755 | 8 | Date: 11/04/2019 |
el17sm | 1:1fa7ecca8dfb | 9 | */ |
el17sm | 1:1fa7ecca8dfb | 10 | |
el17sm | 27:a1b41626f57c | 11 | // Pre-Processor |
el17sm | 7:4aaa37a711a1 | 12 | #include "mbed.h" |
el17sm | 7:4aaa37a711a1 | 13 | #include "Gamepad.h" |
el17sm | 7:4aaa37a711a1 | 14 | #include "N5110.h" |
el17sm | 7:4aaa37a711a1 | 15 | #include "math.h" |
el17sm | 7:4aaa37a711a1 | 16 | #include "sprites.h" |
el17sm | 7:4aaa37a711a1 | 17 | #include "Entity.h" |
el17sm | 7:4aaa37a711a1 | 18 | #include "Player.h" |
el17sm | 10:1a3499f6b583 | 19 | #include "Headless.h" |
el17sm | 17:99e533f7f2fb | 20 | #include "Snake.h" |
el17sm | 27:a1b41626f57c | 21 | #include "RoomEngine.h" |
el17sm | 30:ec915d24d3e9 | 22 | #include <time.h> |
el17sm | 7:4aaa37a711a1 | 23 | |
el17sm | 28:98848e6a77a2 | 24 | #define INSIDE 4 |
el17sm | 29:6b8411bb040a | 25 | #define MAX_ROOMS_MAP_X 11 |
el17sm | 29:6b8411bb040a | 26 | #define MAX_ROOMS_MAP_Y 11 |
el17sm | 28:98848e6a77a2 | 27 | |
el17sm | 31:ab24d028ddfd | 28 | // Variables |
el17sm | 31:ab24d028ddfd | 29 | int title_count = 0; |
el17sm | 31:ab24d028ddfd | 30 | int cursor_timer = 20; |
el17sm | 31:ab24d028ddfd | 31 | int title_option = 0; |
el17sm | 31:ab24d028ddfd | 32 | float global_contrast = 0.5; |
el17sm | 31:ab24d028ddfd | 33 | |
el17sm | 27:a1b41626f57c | 34 | // Objects |
el17sm | 7:4aaa37a711a1 | 35 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17sm | 7:4aaa37a711a1 | 36 | Gamepad gamepad; |
el17sm | 7:4aaa37a711a1 | 37 | |
el17sm | 27:a1b41626f57c | 38 | // Prototypes |
el17sm | 31:ab24d028ddfd | 39 | void title_screen(); |
el17sm | 31:ab24d028ddfd | 40 | void draw_title_screen(N5110 &lcd); |
el17sm | 31:ab24d028ddfd | 41 | void title_options_joystick(Gamepad &gamepad); |
el17sm | 31:ab24d028ddfd | 42 | |
el17sm | 31:ab24d028ddfd | 43 | void title_option_option(); |
el17sm | 31:ab24d028ddfd | 44 | void title_option_credit(); |
el17sm | 31:ab24d028ddfd | 45 | void title_option_hi_scores(); |
el17sm | 31:ab24d028ddfd | 46 | void game_loop(); |
el17sm | 31:ab24d028ddfd | 47 | void game_over(); |
el17sm | 31:ab24d028ddfd | 48 | Player *player; |
el17sm | 31:ab24d028ddfd | 49 | Room *rooms[MAX_ROOMS_MAP_Y][MAX_ROOMS_MAP_X]; |
el17sm | 31:ab24d028ddfd | 50 | RoomEngine *room_engine; |
el17sm | 10:1a3499f6b583 | 51 | |
el17sm | 27:a1b41626f57c | 52 | // Functions |
el17sm | 1:1fa7ecca8dfb | 53 | int main() |
el17sm | 1:1fa7ecca8dfb | 54 | { |
el17sm | 29:6b8411bb040a | 55 | // Initialize |
el17sm | 1:1fa7ecca8dfb | 56 | lcd.init(); |
el17sm | 30:ec915d24d3e9 | 57 | lcd.setContrast(global_contrast); |
el17sm | 3:359a49bace1b | 58 | gamepad.init(); |
el17sm | 28:98848e6a77a2 | 59 | |
el17sm | 29:6b8411bb040a | 60 | while(1) { // Gameloop |
el17sm | 31:ab24d028ddfd | 61 | |
el17sm | 31:ab24d028ddfd | 62 | player = new Player(5, 36); |
el17sm | 31:ab24d028ddfd | 63 | title_screen(); |
el17sm | 31:ab24d028ddfd | 64 | delete player; |
el17sm | 31:ab24d028ddfd | 65 | |
el17sm | 31:ab24d028ddfd | 66 | srand(title_count); |
el17sm | 31:ab24d028ddfd | 67 | player = new Player(39, 27); |
el17sm | 31:ab24d028ddfd | 68 | room_engine = new RoomEngine(global_contrast); |
el17sm | 31:ab24d028ddfd | 69 | |
el17sm | 31:ab24d028ddfd | 70 | game_loop(); |
el17sm | 31:ab24d028ddfd | 71 | game_over(); |
el17sm | 31:ab24d028ddfd | 72 | delete room_engine; |
el17sm | 31:ab24d028ddfd | 73 | delete player; |
el17sm | 31:ab24d028ddfd | 74 | } |
el17sm | 31:ab24d028ddfd | 75 | } |
el17sm | 31:ab24d028ddfd | 76 | |
el17sm | 31:ab24d028ddfd | 77 | void draw_title_screen(N5110 &lcd) |
el17sm | 31:ab24d028ddfd | 78 | { |
el17sm | 31:ab24d028ddfd | 79 | lcd.drawSprite(11, 4, 15, 44, (int *)title_name_0); |
el17sm | 31:ab24d028ddfd | 80 | lcd.drawSpriteTransparent(19, 14, 17, 53, (int *)title_name_1); |
el17sm | 31:ab24d028ddfd | 81 | lcd.drawCircle(79, 7, 10, FILL_BLACK); |
el17sm | 31:ab24d028ddfd | 82 | lcd.drawCircle(81, 5, 8, FILL_WHITE); |
el17sm | 31:ab24d028ddfd | 83 | lcd.drawSprite(56, 6, 11, 5, (int *)star_sprite[abs(((title_count/20) % 7) - 3)]); |
el17sm | 31:ab24d028ddfd | 84 | lcd.drawSprite(12, 34, 8, 8, (int *)button_A_sprite); |
el17sm | 31:ab24d028ddfd | 85 | lcd.drawSprite(22, 37, 3, 2, (int *)arrow_left_sprite); |
el17sm | 31:ab24d028ddfd | 86 | lcd.drawSprite(59, 37, 3, 2, (int *)arrow_right_sprite); |
el17sm | 31:ab24d028ddfd | 87 | lcd.drawSprite(69, 31, 12, 6, (int *)sprite_player[(title_count/40) % 4][(title_count/10) % 4]); |
el17sm | 31:ab24d028ddfd | 88 | lcd.drawSprite(26, 35, 9, 32, (int *)title_options_sprite[title_option]); |
el17sm | 31:ab24d028ddfd | 89 | } |
el17sm | 31:ab24d028ddfd | 90 | |
el17sm | 31:ab24d028ddfd | 91 | void title_options_joystick(Gamepad &gamepad) |
el17sm | 31:ab24d028ddfd | 92 | { |
el17sm | 31:ab24d028ddfd | 93 | if ((gamepad.get_direction() == 3) && (cursor_timer > 20)) { // Detect Joystick going right |
el17sm | 31:ab24d028ddfd | 94 | cursor_timer = 0; |
el17sm | 31:ab24d028ddfd | 95 | if (title_option >= 3) { |
el17sm | 30:ec915d24d3e9 | 96 | title_option = 0; |
el17sm | 31:ab24d028ddfd | 97 | } else { |
el17sm | 31:ab24d028ddfd | 98 | title_option++; |
el17sm | 31:ab24d028ddfd | 99 | } |
el17sm | 31:ab24d028ddfd | 100 | } else if ((gamepad.get_direction() == 7) && (cursor_timer > 20)) { // Detect Joystick going left |
el17sm | 31:ab24d028ddfd | 101 | cursor_timer = 0; |
el17sm | 31:ab24d028ddfd | 102 | if (title_option <= 0) { |
el17sm | 31:ab24d028ddfd | 103 | title_option = 3; |
el17sm | 31:ab24d028ddfd | 104 | } else { |
el17sm | 31:ab24d028ddfd | 105 | title_option--; |
el17sm | 31:ab24d028ddfd | 106 | } |
el17sm | 31:ab24d028ddfd | 107 | } |
el17sm | 31:ab24d028ddfd | 108 | cursor_timer++; |
el17sm | 31:ab24d028ddfd | 109 | } |
el17sm | 31:ab24d028ddfd | 110 | |
el17sm | 31:ab24d028ddfd | 111 | void title_screen() |
el17sm | 31:ab24d028ddfd | 112 | { |
el17sm | 31:ab24d028ddfd | 113 | while(1){ // Title Screen Loop |
el17sm | 31:ab24d028ddfd | 114 | title_option = 0; |
el17sm | 31:ab24d028ddfd | 115 | while(!gamepad.check_event(Gamepad::A_PRESSED)){ |
el17sm | 31:ab24d028ddfd | 116 | lcd.clear(); |
el17sm | 31:ab24d028ddfd | 117 | draw_title_screen(lcd); |
el17sm | 31:ab24d028ddfd | 118 | lcd.refresh(); |
el17sm | 31:ab24d028ddfd | 119 | |
el17sm | 31:ab24d028ddfd | 120 | title_options_joystick(gamepad); |
el17sm | 31:ab24d028ddfd | 121 | |
el17sm | 31:ab24d028ddfd | 122 | title_count++; |
el17sm | 31:ab24d028ddfd | 123 | wait_ms(1000/40); // 1s/framerate |
el17sm | 31:ab24d028ddfd | 124 | } |
el17sm | 31:ab24d028ddfd | 125 | while(gamepad.check_event(Gamepad::A_PRESSED)){} |
el17sm | 31:ab24d028ddfd | 126 | if(title_option == 0) { |
el17sm | 31:ab24d028ddfd | 127 | break; |
el17sm | 31:ab24d028ddfd | 128 | } else if (title_option == 1) { |
el17sm | 31:ab24d028ddfd | 129 | while(!gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 31:ab24d028ddfd | 130 | title_option_option(); |
el17sm | 30:ec915d24d3e9 | 131 | } |
el17sm | 31:ab24d028ddfd | 132 | wait(0.05); |
el17sm | 31:ab24d028ddfd | 133 | while(gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 31:ab24d028ddfd | 134 | } |
el17sm | 31:ab24d028ddfd | 135 | } else if (title_option == 2) { |
el17sm | 31:ab24d028ddfd | 136 | title_option_credit(); |
el17sm | 31:ab24d028ddfd | 137 | wait(0.05); |
el17sm | 31:ab24d028ddfd | 138 | while(!gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 31:ab24d028ddfd | 139 | } |
el17sm | 31:ab24d028ddfd | 140 | wait(0.05); |
el17sm | 31:ab24d028ddfd | 141 | while(gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 31:ab24d028ddfd | 142 | } |
el17sm | 31:ab24d028ddfd | 143 | } else if (title_option == 3) { |
el17sm | 31:ab24d028ddfd | 144 | |
el17sm | 31:ab24d028ddfd | 145 | } |
el17sm | 31:ab24d028ddfd | 146 | } |
el17sm | 31:ab24d028ddfd | 147 | } |
el17sm | 31:ab24d028ddfd | 148 | |
el17sm | 31:ab24d028ddfd | 149 | void title_option_option() |
el17sm | 31:ab24d028ddfd | 150 | { |
el17sm | 31:ab24d028ddfd | 151 | global_contrast = gamepad.read_pot(); |
el17sm | 31:ab24d028ddfd | 152 | lcd.setContrast(global_contrast); |
el17sm | 31:ab24d028ddfd | 153 | lcd.clear(); |
el17sm | 31:ab24d028ddfd | 154 | lcd.printString("Set contrast", 0, 0); |
el17sm | 31:ab24d028ddfd | 155 | lcd.printString("using the", 0, 1); |
el17sm | 31:ab24d028ddfd | 156 | lcd.printString("potentiometer", 0, 2); |
el17sm | 31:ab24d028ddfd | 157 | player->draw(lcd); |
el17sm | 31:ab24d028ddfd | 158 | lcd.refresh(); |
el17sm | 31:ab24d028ddfd | 159 | player->move(1, 0, (int *)level_map[0][0], (bool *)sprite_transparent_player); |
el17sm | 31:ab24d028ddfd | 160 | player->buttons(false, true, false, false); |
el17sm | 31:ab24d028ddfd | 161 | player->update_bullets((int *)level_map[0][0], (bool *)sprite_transparent_player); |
el17sm | 31:ab24d028ddfd | 162 | wait_ms(1000/40); |
el17sm | 31:ab24d028ddfd | 163 | } |
el17sm | 31:ab24d028ddfd | 164 | |
el17sm | 31:ab24d028ddfd | 165 | void title_option_credit() |
el17sm | 31:ab24d028ddfd | 166 | { |
el17sm | 31:ab24d028ddfd | 167 | lcd.clear(); |
el17sm | 31:ab24d028ddfd | 168 | lcd.setContrast(global_contrast); |
el17sm | 31:ab24d028ddfd | 169 | lcd.printString("Made by:", 0, 0); |
el17sm | 31:ab24d028ddfd | 170 | lcd.printString("Steven Mahasin", 0, 1); |
el17sm | 31:ab24d028ddfd | 171 | lcd.printString("201192939", 0, 2); |
el17sm | 31:ab24d028ddfd | 172 | lcd.refresh(); |
el17sm | 31:ab24d028ddfd | 173 | } |
el17sm | 31:ab24d028ddfd | 174 | void title_option_hi_scores() |
el17sm | 31:ab24d028ddfd | 175 | { |
el17sm | 31:ab24d028ddfd | 176 | |
el17sm | 31:ab24d028ddfd | 177 | } |
el17sm | 31:ab24d028ddfd | 178 | |
el17sm | 31:ab24d028ddfd | 179 | void game_loop() |
el17sm | 31:ab24d028ddfd | 180 | { |
el17sm | 31:ab24d028ddfd | 181 | while(1) { // Floor Loop |
el17sm | 31:ab24d028ddfd | 182 | for (int i = 0; i < MAX_ROOMS_MAP_X; i++) { |
el17sm | 31:ab24d028ddfd | 183 | for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) { |
el17sm | 31:ab24d028ddfd | 184 | rooms[j][i] = new Room(2); |
el17sm | 30:ec915d24d3e9 | 185 | } |
el17sm | 30:ec915d24d3e9 | 186 | } |
el17sm | 31:ab24d028ddfd | 187 | |
el17sm | 31:ab24d028ddfd | 188 | // Rooms Generation |
el17sm | 31:ab24d028ddfd | 189 | while(1) { // Room Loop |
el17sm | 31:ab24d028ddfd | 190 | rooms[room_engine->get_room_y()][room_engine->get_room_x()]->load(); |
el17sm | 31:ab24d028ddfd | 191 | room_engine->load(player, rooms[room_engine->get_room_y()][room_engine->get_room_x()]); |
el17sm | 31:ab24d028ddfd | 192 | |
el17sm | 31:ab24d028ddfd | 193 | room_engine->entrance_scene(lcd, gamepad); |
el17sm | 31:ab24d028ddfd | 194 | while(room_engine->check_player_room_position() == INSIDE) { // Room actions |
el17sm | 31:ab24d028ddfd | 195 | room_engine->read_input(gamepad); |
el17sm | 31:ab24d028ddfd | 196 | room_engine->update(); |
el17sm | 31:ab24d028ddfd | 197 | room_engine->render(lcd, gamepad); |
el17sm | 31:ab24d028ddfd | 198 | if (player->get_hp() <= 0) { |
el17sm | 31:ab24d028ddfd | 199 | goto gameover; |
el17sm | 29:6b8411bb040a | 200 | } |
el17sm | 29:6b8411bb040a | 201 | } |
el17sm | 31:ab24d028ddfd | 202 | room_engine->exit_scene(lcd, gamepad); |
el17sm | 31:ab24d028ddfd | 203 | rooms[room_engine->get_room_y()][room_engine->get_room_x()]->unload(); |
el17sm | 31:ab24d028ddfd | 204 | player->delete_bullets(); |
el17sm | 31:ab24d028ddfd | 205 | room_engine->update_current_room(); |
el17sm | 31:ab24d028ddfd | 206 | } |
el17sm | 31:ab24d028ddfd | 207 | } |
el17sm | 31:ab24d028ddfd | 208 | gameover : { |
el17sm | 31:ab24d028ddfd | 209 | game_over(); |
el17sm | 31:ab24d028ddfd | 210 | } |
el17sm | 31:ab24d028ddfd | 211 | } |
el17sm | 29:6b8411bb040a | 212 | |
el17sm | 31:ab24d028ddfd | 213 | void game_over() |
el17sm | 31:ab24d028ddfd | 214 | { |
el17sm | 31:ab24d028ddfd | 215 | while(1){ // Game Over Screen Loop |
el17sm | 30:ec915d24d3e9 | 216 | lcd.clear(); |
el17sm | 30:ec915d24d3e9 | 217 | lcd.setContrast(global_contrast); |
el17sm | 30:ec915d24d3e9 | 218 | lcd.printString("Game Over", 0, 0); |
el17sm | 30:ec915d24d3e9 | 219 | lcd.printString("Retry?", 0, 1); |
el17sm | 30:ec915d24d3e9 | 220 | lcd.refresh(); |
el17sm | 30:ec915d24d3e9 | 221 | wait(0.05); |
el17sm | 30:ec915d24d3e9 | 222 | while(!gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 30:ec915d24d3e9 | 223 | } |
el17sm | 30:ec915d24d3e9 | 224 | wait(0.05); |
el17sm | 30:ec915d24d3e9 | 225 | while(gamepad.check_event(Gamepad::A_PRESSED)) { |
el17sm | 30:ec915d24d3e9 | 226 | } |
el17sm | 30:ec915d24d3e9 | 227 | break; |
el17sm | 30:ec915d24d3e9 | 228 | } |
el17sm | 10:1a3499f6b583 | 229 | } |