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: N5110 PowerControl mbed
Diff: main.cpp
- Revision:
- 15:05a227f970c2
- Parent:
- 14:55802ce40285
- Child:
- 16:a26d7519830e
--- a/main.cpp Sat May 02 14:28:12 2015 +0000 +++ b/main.cpp Sat May 02 21:58:44 2015 +0000 @@ -79,7 +79,7 @@ {"Huge Rat", 5, 3, 0, 25, 70 }, //0- Huge Rat {"Goblin", 7, 3, 2, 25, 60}, //1- Goblin {"Skeleton", 10, 5, 3, 10, 50}, //2- Skeleton - {"Giant Spider", 6, 5, 0, 40, 60}, //3- Giant Spider + {"Giant Spider", 6, 5, 0, 40, 60}, //3- Giant Spider }; //Variables @@ -142,6 +142,7 @@ //Voids void MainMenu(); +void GameLoop(); void Walls() { @@ -464,6 +465,17 @@ } } +void GameOver() +{ + + lcd.clear(); + lcd.printString("GAME OVER", 12, 2); + lcd.refresh(); + wait(1.0); + Sleep(); + MainMenu(); +} + void Fight() { int m = rand()%level; @@ -522,15 +534,37 @@ if(rand()%100 + 1 > EnemyList[m].EDodge) { //If monster doesn't dodge int damage = ItemList[pw].ItemValue - EnemyList[m].EArmour + rand()%3 - rand()%3; //Calculate damage - if (damage > 0) { - mh = mh - damage; //Apply damage and calculate the monster's health + if (damage < 0) { + damage = 0; } + mh = mh - damage; //Apply damage and calculate the monster's health + + char buffer3[14]; + write = sprintf(buffer3,"for %d damage",damage); // print formatted data to buffer + + lcd.clear(); + lcd.printString("You hit the", 0 , 0); + lcd.printString(EnemyList[m].EName, 0 ,1); + lcd.printString(buffer3, 0, 2); + lcd.refresh(); + wait(1.0); + Sleep(); + + } else { //Monster dodges + lcd.clear(); + lcd.printString(EnemyList[m].EName, 0 ,0); + lcd.printString("dodges your", 0 , 1); + lcd.printString("attack", 0 , 2); + lcd.refresh(); + wait(1.0); + Sleep(); } if(mh <= 0) { lcd.clear(); - lcd.printString("You win!", 0, 0); + lcd.printString("You win!", 18, 2); lcd.refresh(); + wait(1.0); Sleep(); break; } @@ -539,17 +573,33 @@ if(rand()%100 + 1 < EnemyList[m].EHit) { //If monster hits and isn't dead int damage = EnemyList[m].EDamage - ItemList[pa].ItemValue + rand()%3 - rand()%3; //Calculate damage - if (damage > 0) { - ph = ph - damage; //Apply damage and calculate the monster's health + if (damage < 0) { + damage = 0; } + ph = ph - damage; //Apply damage and calculate the monster's health + + char buffer4[14]; + write = sprintf(buffer4,"%d damage",damage); // print formatted data to buffer + + lcd.clear(); + lcd.printString(EnemyList[m].EName, 0 ,0); + lcd.printString("hits you for", 0 , 1); + lcd.printString(buffer4, 0 , 2); + lcd.refresh(); + wait(1.0); + Sleep(); + + } else { //You dodge + lcd.clear(); + lcd.printString("You dodge the", 0 ,0); + lcd.printString("monster's", 0 , 1); + lcd.printString("attack", 0 , 2); + lcd.refresh(); + wait(1.0); + Sleep(); } - if(ph <= 0) { - lcd.clear(); - lcd.printString("GAME OVER", 0, 0); - lcd.refresh(); - Sleep(); - MainMenu(); + GameOver(); } @@ -618,7 +668,7 @@ lcd.printString(". Floor", 0, 2); lcd.printString("= Chest", 0, 3); lcd.printString("/ Open Chest", 0, 4); - + while(1) { Sleep(); @@ -673,8 +723,7 @@ lcd.printString("Continue", 0, 5); lcd.refresh(); Sleep(); - } - else if (menu == 3) { + } else if (menu == 3) { lcd.clear(); lcd.printString(buffer, 0, 0); lcd.printString("Map", 0, 2); @@ -729,64 +778,219 @@ } -void Chest() +void getItem() { + int r = rand()%10; DirFlag = 0; - while(1) { + lcd.clear(); + lcd.printString("Do you want to", 0, 1); + lcd.printString("take the", 0, 2); + lcd.printString(ItemList[r].ItemName, 0, 3); + lcd.printString("-Yes (Action)", 0, 4); + lcd.printString("-No (Other)", 0, 5); + lcd.refresh(); + Sleep(); + + + if(ActFlag) { + + ActFlag = 0; + + if (r > 5) { + pa = r; + } else { + pw = r; + } + + lcd.clear(); + lcd.printString("You take the", 0, 1); + lcd.printString(ItemList[r].ItemName, 0, 2); + lcd.refresh(); + } + if(DirFlag) { + + DirFlag = 0; + lcd.clear(); - lcd.printString("Do you want to", 0, 1); - lcd.printString("take the", 0, 2); - lcd.printString(ItemList[r].ItemName, 0, 3); - lcd.printString("-Yes (Action)", 0, 4); - lcd.printString("-No (Other)", 0, 5); + lcd.printString("You leave the", 0, 1); + lcd.printString(ItemList[r].ItemName, 0, 2); + lcd.refresh(); + } + if(StartFlag) { + + StartFlag = 0; + + lcd.clear(); + lcd.printString("You leave the", 0, 1); + lcd.printString(ItemList[r].ItemName, 0, 2); lcd.refresh(); + } +} + +void BoobyTrap() +{ + + int damage = rand()%5; + + if(damage != 0) { + + ph = ph - damage; + + char buffer[15]; + int write = sprintf(buffer, "You recive %d", damage); + + lcd.clear(); + lcd.printString("The chest is", 0, 0); + lcd.printString("booby trapped!", 0, 2); + lcd.printString(buffer, 0, 3); + lcd.printString("damage", 0, 4); + lcd.refresh(); + wait(1.0); Sleep(); + //Check if player is dead + if(ph < 0) { + GameOver(); + } + } else { - if(ActFlag) { + lcd.clear(); + lcd.printString("The chest is", 0, 0); + lcd.printString("booby trapped", 0, 2); + lcd.printString("but the trap", 0, 3); + lcd.printString("fails", 0, 4); + lcd.refresh(); + wait(1.0); + Sleep(); + + } +} - ActFlag = 0; +void RevealMap() +{ + + for(int i = 0; i < 84; i++) { + for(int j = 0; j < 48; j++) { + + if(map[i][j] == FLOOR) { + map[i][j] = FLOOR_SEEN; + } + } + } - if (r > 5) { - pa = r; - } else { - pw = r; - } + lcd.clear(); + lcd.printString("You find a map", 0, 0); + lcd.printString("of the current", 0, 2); + lcd.printString("level inside", 0, 3); + lcd.printString("the chest", 0, 4); + lcd.refresh(); + wait(1.0); + Sleep(); +} + +void Potion() +{ + + int p = rand()%5; //0- Poison, 1- Teleport, Else- Full heal + + lcd.clear(); + lcd.printString("You find a", 0, 0); + lcd.printString("potion inside", 0, 2); + lcd.printString("the chest", 0, 3); + lcd.printString("Drink (Action)", 0, 4); + lcd.printString("Leave (Other)", 0, 5); + lcd.refresh(); + wait(1.0); + Sleep(); + + if(ActFlag) { //Potion drunk + ActFlag = 0; + + if(p == 0) { //Poison + + int damage = rand()%4 + 1; + + ph = ph - damage; + + char buffer[15]; + int write = sprintf(buffer, "%d damage", damage); lcd.clear(); - lcd.printString("You take the", 0, 1); - lcd.printString(ItemList[r].ItemName, 0, 2); + lcd.printString("You drink the", 0, 0); + lcd.printString("potion and", 0, 1); + lcd.printString("suddenly feel", 0, 2); + lcd.printString("ill.", 0, 3); + lcd.printString("You take", 0, 4); + lcd.printString(buffer, 0, 5); lcd.refresh(); - map[px][py] = CHEST_OPENED; - break; - } - if(DirFlag) { + wait(1.0); + Sleep(); + + //Check if player is dead + if(ph < 0) { + GameOver(); + } + } else if(p == 1) { //Teleport - DirFlag = 0; + lcd.clear(); + lcd.printString("You drink the", 0, 0); + lcd.printString("potion and", 0, 1); + lcd.printString("suddenly feel", 0, 2); + lcd.printString("your body", 0, 3); + lcd.printString("being", 0, 4); + lcd.printString("transported", 0, 5); + lcd.refresh(); + wait(1.0); + Sleep(); + + GameLoop(); + + } else { //Full heal + + ph = 15; lcd.clear(); - lcd.printString("You leave the", 0, 1); - lcd.printString(ItemList[r].ItemName, 0, 2); + lcd.printString("You drink the", 0, 0); + lcd.printString("potion and", 0, 1); + lcd.printString("suddenly feel", 0, 2); + lcd.printString("all your", 0, 3); + lcd.printString("wounds heal", 0, 4); lcd.refresh(); - map[px][py] = CHEST_OPENED; - break; + wait(1.0); + Sleep(); } - if(StartFlag) { - - StartFlag = 0; + } else { //Leave the potion + DirFlag = 0; + StartFlag = 0; + lcd.clear(); + lcd.printString("You walk away", 0, 0); + lcd.printString("and leave the", 0, 1); + lcd.printString("potion", 0, 2); + lcd.refresh(); + wait(1.0); + Sleep(); + } +} - lcd.clear(); - lcd.printString("You leave the", 0, 1); - lcd.printString(ItemList[r].ItemName, 0, 2); - lcd.refresh(); - map[px][py] = CHEST_OPENED; - break; - } +void Chest() +{ + int c = rand()%4; //0- Item, 1- Booby trap, 2- Map, Else- Potion + if(c == 0) { + getItem(); + } else if(c == 1) { + BoobyTrap(); + } else if(c == 2) { + RevealMap(); + } else { + Potion(); } + + map[px][py] = CHEST_OPENED; + } void GameLoop() @@ -799,7 +1003,6 @@ LevelScreen(); - //Game loop while(1) { PlayerCamera();