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.
Dependencies: mbed MotionSensor
Diff: main.cpp
- Revision:
- 48:f7d9ae3e554d
- Parent:
- 47:6e31b195ce3c
- Child:
- 49:3f83ed62d123
--- a/main.cpp Thu May 09 01:57:49 2019 +0000
+++ b/main.cpp Thu May 09 02:46:44 2019 +0000
@@ -25,17 +25,7 @@
room_engine = new RoomEngine(global_contrast);
game_loop();
- game_over();
- for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
- for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
- if (valid_rooms[j][j]){
- delete rooms[j][i];
- valid_rooms[i][j] = false;
- }
- }
- }
- delete room_engine;
- delete player;
+ game_unload();
}
}
@@ -57,42 +47,38 @@
boss_room_number = 5 + rand() % 4; // Boss room appears after travelling 5-8 rooms
boss_room_counter = 0;
while(1) { // Room Loop
- update_room_coords();
- if (!valid_rooms[room_y][room_x]){
- generate_room();
- }
- room_engine->load(player, rooms[room_y][room_x]);
- room_engine->entrance_scene(lcd, gamepad);
- while(room_engine->check_player_room_position() == INSIDE) { // Room actions
+ room_entrance();
+ while(room_engine->check_player_room_position() == INSIDE) { // actions inside the Room
room_engine->read_input(gamepad);
room_engine->update(number_of_enemies_killed);
room_engine->render(lcd, gamepad);
minimap_detection();
total_time++;
- if (player->get_hp() <= 0) {
- goto gameover;
- }
- if ((rooms[room_y][room_x]->get_room_type() == 10) && !(rooms[room_y][room_x]->enemies_exist())){
- goto winner;
- }
+ if (player->get_hp() <= 0) {goto gameover;} // gameover if player health depleted
+ if ((rooms[room_y][room_x]->get_room_type() == 10) && !(rooms[room_y][room_x]->enemies_exist())){goto winner;} // wins game if room is a boss room, and the boss is dead
}
- room_engine->exit_scene(lcd, gamepad);
- rooms[room_y][room_x]->unload();
- player->delete_bullets();
- room_engine->update_current_room();
+ room_exit();
}
}
- gameover : {
- game_over();
- goto displaystats;
- }
- winner : {
- win();
- goto displaystats;
- }
- displaystats : {
- display_stats();
- }
+ gameover : { game_over(); goto displaystats;}
+ winner : { win(); goto displaystats;}
+ displaystats : { display_stats();}
+}
+
+void room_entrance()
+{
+ update_room_coords();
+ if (!valid_rooms[room_y][room_x]){generate_room();} // generate a new room if player enters a nonexistent room
+ room_engine->load(player, rooms[room_y][room_x]);
+ room_engine->entrance_scene(lcd, gamepad);
+}
+
+void room_exit()
+{
+ room_engine->exit_scene(lcd, gamepad);
+ rooms[room_y][room_x]->unload();
+ player->delete_bullets();
+ room_engine->update_current_room();
}
void generate_room()
@@ -129,6 +115,124 @@
room_x = room_engine->get_room_x();
}
+void minimap_detection()
+{
+ while(gamepad.check_event(Gamepad::BACK_PRESSED)) {
+ lcd.clear();
+ for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+ for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
+ if (valid_rooms[j][i]) {
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]);
+ if (rooms[j][i]->get_room_type() == 10) {
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]);
+ } else if (rooms[j][i]->enemies_exist()) {
+ lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]);
+ }
+ if (rooms[j][i]->get_doorway(0)) {
+ lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(1)) {
+ lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(2)) {
+ lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
+ }
+ if (rooms[j][i]->get_doorway(3)) {
+ lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+ }
+ }
+ }
+ }
+ lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]);
+ lcd.refresh();
+ wait_ms(1000/40);
+ };
+}
+
+void game_over()
+{
+ lcd.clear();
+ lcd.setContrast(global_contrast);
+ lcd.printString("Game Over", 0, 0);
+ lcd.printString("Retry?", 0, 1);
+}
+
+void win()
+{
+ lcd.clear();
+ lcd.setContrast(global_contrast);
+ lcd.printString("You won!", 0, 0);
+}
+
+void display_stats()
+{
+ lcd.printString("Enemies Killed:", 0, 2);
+ char kills[10];
+ sprintf(kills, "%d enemies", number_of_enemies_killed);
+ lcd.printString(kills, 0, 3);
+ lcd.printString("Time:", 0, 4);
+ char total_duration[10];
+ sprintf(total_duration, "%d seconds", (total_time/40));
+ lcd.printString(total_duration, 0, 5);
+ lcd.refresh();
+ while(gamepad.check_event(Gamepad::A_PRESSED)) {
+ }
+ wait(0.05);
+ while(!gamepad.check_event(Gamepad::A_PRESSED)) {
+ }
+ wait(0.05);
+ while(gamepad.check_event(Gamepad::A_PRESSED)) {
+ }
+}
+
+int available_boss_room()
+{
+ if (!valid_rooms[room_y - 1][room_x]){
+ rooms[room_y][room_x]->set_doorway(0, true);
+ valid_rooms[room_y - 1][room_x] = true;
+ rooms[room_y - 1][room_x] = new Room(0, 10);
+ rooms[room_y - 1][room_x]->set_doorway(2, true);
+ return 0;
+ } else if (!valid_rooms[room_y][room_x + 1]){
+ rooms[room_y][room_x]->set_doorway(1, true);
+ valid_rooms[room_y][room_x + 1] = true;
+ rooms[room_y][room_x + 1] = new Room(0, 10);
+ rooms[room_y][room_x + 1]->set_doorway(3, true);
+ return 1;
+ } else if (!valid_rooms[room_y + 1][room_x]){
+ rooms[room_y][room_x]->set_doorway(2, true);
+ valid_rooms[room_y + 1][room_x] = true;
+ rooms[room_y + 1][room_x] = new Room(0, 10);
+ rooms[room_y + 1][room_x]->set_doorway(0, true);
+ return 2;
+ } else if (!valid_rooms[room_y][room_x - 1]){
+ rooms[room_y][room_x]->set_doorway(3, true);
+ valid_rooms[room_y][room_x - 1] = true;
+ rooms[room_y][room_x - 1] = new Room(0, 10);
+ rooms[room_y][room_x - 1]->set_doorway(1, true);
+ return 3;
+ }
+ delete rooms[room_y - 1][room_x];
+ rooms[room_y - 1][room_x] = new Room(0, 10);
+ rooms[room_y - 1][room_x]->set_doorway(2, true);
+ return 0;
+}
+
+void game_unload()
+{
+ for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
+ for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+ if (valid_rooms[j][j]){
+ delete rooms[j][i];
+ valid_rooms[i][j] = false;
+ }
+ }
+ }
+ delete room_engine;
+ delete player;
+}
+
+// Functions
int opposite(int value)
{
if (value <= 1) {
@@ -218,107 +322,4 @@
have_to[3] = false;
cannot[3] = false;
}
-}
-
-int available_boss_room()
-{
- if (!valid_rooms[room_y - 1][room_x]){
- rooms[room_y][room_x]->set_doorway(0, true);
- valid_rooms[room_y - 1][room_x] = true;
- rooms[room_y - 1][room_x] = new Room(0, 10);
- rooms[room_y - 1][room_x]->set_doorway(2, true);
- return 0;
- } else if (!valid_rooms[room_y][room_x + 1]){
- rooms[room_y][room_x]->set_doorway(1, true);
- valid_rooms[room_y][room_x + 1] = true;
- rooms[room_y][room_x + 1] = new Room(0, 10);
- rooms[room_y][room_x + 1]->set_doorway(3, true);
- return 1;
- } else if (!valid_rooms[room_y + 1][room_x]){
- rooms[room_y][room_x]->set_doorway(2, true);
- valid_rooms[room_y + 1][room_x] = true;
- rooms[room_y + 1][room_x] = new Room(0, 10);
- rooms[room_y + 1][room_x]->set_doorway(0, true);
- return 2;
- } else if (!valid_rooms[room_y][room_x - 1]){
- rooms[room_y][room_x]->set_doorway(3, true);
- valid_rooms[room_y][room_x - 1] = true;
- rooms[room_y][room_x - 1] = new Room(0, 10);
- rooms[room_y][room_x - 1]->set_doorway(1, true);
- return 3;
- }
- delete rooms[room_y - 1][room_x];
- rooms[room_y - 1][room_x] = new Room(0, 10);
- rooms[room_y - 1][room_x]->set_doorway(2, true);
- return 0;
-}
-
-void minimap_detection()
-{
- while(gamepad.check_event(Gamepad::BACK_PRESSED)) {
- lcd.clear();
- for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
- for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
- if (valid_rooms[j][i]) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]);
- if (rooms[j][i]->get_room_type() == 10) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]);
- } else if (rooms[j][i]->enemies_exist()) {
- lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]);
- }
- if (rooms[j][i]->get_doorway(0)) {
- lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(1)) {
- lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(2)) {
- lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
- }
- if (rooms[j][i]->get_doorway(3)) {
- lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
- }
- }
- }
- }
- lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]);
- lcd.refresh();
- wait_ms(1000/40);
- };
-}
-
-void game_over()
-{
- lcd.clear();
- lcd.setContrast(global_contrast);
- lcd.printString("Game Over", 0, 0);
- lcd.printString("Retry?", 0, 1);
-}
-
-void win()
-{
- lcd.clear();
- lcd.setContrast(global_contrast);
- lcd.printString("You won!", 0, 0);
-}
-
-void display_stats()
-{
- lcd.printString("Enemies Killed:", 0, 2);
- char kills[10];
- sprintf(kills, "%d enemies", number_of_enemies_killed);
- lcd.printString(kills, 0, 3);
- lcd.printString("Time:", 0, 4);
- char total_duration[10];
- sprintf(total_duration, "%d seconds", (total_time/40));
- lcd.printString(total_duration, 0, 5);
- lcd.refresh();
- while(gamepad.check_event(Gamepad::A_PRESSED)) {
- }
- wait(0.05);
- while(!gamepad.check_event(Gamepad::A_PRESSED)) {
- }
- wait(0.05);
- while(gamepad.check_event(Gamepad::A_PRESSED)) {
- }
-}
+}
\ No newline at end of file