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:
- 16:a26d7519830e
- Parent:
- 15:05a227f970c2
- Child:
- 17:8790dd599b25
--- a/main.cpp Sat May 02 21:58:44 2015 +0000 +++ b/main.cpp Sat May 02 22:36:05 2015 +0000 @@ -53,7 +53,7 @@ ITEMS ItemList[] = { //Weapons - { "Dagger", 3}, //0 + { "Dagger", 4}, //0 { "Axe", 5}, //1 { "Mace", 6}, //2 { "Sword", 7}, //3 @@ -73,13 +73,14 @@ int EArmour; //Enemy armour int EDodge; //Enemy dodge chance int EHit; //Enemy hit chance + int ESpd; //Enemy speed (use for running away during fights) }; ENEMIES EnemyList[] = { - {"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 + {"Huge Rat", 5, 3, 0, 25, 70, 40}, //0- Huge Rat + {"Goblin", 6, 3, 2, 25, 60, 30}, //1- Goblin + {"Skeleton", 8, 5, 3, 10, 50, 10}, //2- Skeleton + {"Giant Spider", 6, 5, 0, 40, 60, 50}, //3- Giant Spider }; //Variables @@ -467,15 +468,52 @@ void GameOver() { - + lcd.inverseMode(); lcd.clear(); lcd.printString("GAME OVER", 12, 2); lcd.refresh(); wait(1.0); Sleep(); + lcd.normalMode(); MainMenu(); } +void MonsterAttack(int m) +{ + 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) { + damage = 0; + } + ph = ph - damage; //Apply damage and calculate the monster's health + + char buffer[14]; + int write = sprintf(buffer,"%d damage",damage); // print formatted data to buffer + + lcd.clear(); + lcd.printString(EnemyList[m].EName, 0 ,1); + lcd.printString("hits you for", 0 , 2); + lcd.printString(buffer, 0 , 3); + lcd.refresh(); + wait(1.0); + Sleep(); + + } else { //You dodge + lcd.clear(); + lcd.printString("You dodge the", 0 ,1); + lcd.printString("monster's", 0 , 2); + lcd.printString("attack", 0 , 3); + lcd.refresh(); + wait(1.0); + Sleep(); + } + if(ph <= 0) { //Check if player is dead + GameOver(); + } + +} + void Fight() { int m = rand()%level; @@ -543,24 +581,24 @@ 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.printString("You hit the", 0 , 1); + lcd.printString(EnemyList[m].EName, 0 ,2); + lcd.printString(buffer3, 0, 3); 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.printString(EnemyList[m].EName, 0 ,1); + lcd.printString("dodges your", 0 , 2); + lcd.printString("attack", 0 , 3); lcd.refresh(); wait(1.0); Sleep(); } - if(mh <= 0) { + if(mh <= 0) { //Check if monster is dead lcd.clear(); lcd.printString("You win!", 18, 2); lcd.refresh(); @@ -569,46 +607,56 @@ break; } - //Get hit - if(rand()%100 + 1 < EnemyList[m].EHit) { //If monster hits and isn't dead + MonsterAttack(m); + - int damage = EnemyList[m].EDamage - ItemList[pa].ItemValue + rand()%3 - rand()%3; //Calculate damage - if (damage < 0) { - damage = 0; - } - ph = ph - damage; //Apply damage and calculate the monster's health + } else { //Run away - char buffer4[14]; - write = sprintf(buffer4,"%d damage",damage); // print formatted data to buffer + int b = rand()%5; + + if(b == 0) { //Monster blocks the path lcd.clear(); - lcd.printString(EnemyList[m].EName, 0 ,0); - lcd.printString("hits you for", 0 , 1); - lcd.printString(buffer4, 0 , 2); + lcd.printString("You try to run", 0, 0); + lcd.printString("away but the", 0, 1); + lcd.printString("monster blocks", 0, 2); + lcd.printString("the path", 0, 3); 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) { - GameOver(); - } + MonsterAttack(m); + + } else { + + int s = rand()%100 + 1; + + if(s > EnemyList[m].ESpd) { //If you outspeed monster + + lcd.clear(); + lcd.printString("You run away", 6, 2); + lcd.refresh(); + Sleep(); + break; + + } else { + + lcd.clear(); + lcd.printString("You try to run", 0, 0); + lcd.printString("away but the", 0, 1); + lcd.printString("monster", 0, 2); + lcd.printString("catches up to", 0, 3); + lcd.printString("you", 0, 4); + lcd.refresh(); + wait(1.0); + Sleep(); + + MonsterAttack(m); + + } - } else { //Run away - lcd.clear(); - lcd.printString("You run away", 6, 2); - lcd.refresh(); - Sleep(); - break; + } } } } @@ -753,6 +801,9 @@ break; } } + if(StartFlag) { + StartFlag = 0; + } } } @@ -792,6 +843,7 @@ lcd.printString("-Yes (Action)", 0, 4); lcd.printString("-No (Other)", 0, 5); lcd.refresh(); + wait(1.0); Sleep(); @@ -809,24 +861,20 @@ lcd.printString("You take the", 0, 1); lcd.printString(ItemList[r].ItemName, 0, 2); lcd.refresh(); - } - if(DirFlag) { + wait(1.0); + Sleep(); + } else { + + StartFlag = 0; DirFlag = 0; lcd.clear(); 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(); + wait(1.0); + Sleep(); } } @@ -844,7 +892,7 @@ lcd.clear(); lcd.printString("The chest is", 0, 0); - lcd.printString("booby trapped!", 0, 2); + lcd.printString("booby trapped!", 0, 1); lcd.printString(buffer, 0, 3); lcd.printString("damage", 0, 4); lcd.refresh(); @@ -859,9 +907,9 @@ 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.printString("booby trapped", 0, 1); + lcd.printString("but the trap", 0, 2); + lcd.printString("fails", 0, 3); lcd.refresh(); wait(1.0); Sleep(); @@ -883,9 +931,9 @@ 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.printString("of the current", 0, 1); + lcd.printString("level inside", 0, 2); + lcd.printString("the chest", 0, 3); lcd.refresh(); wait(1.0); Sleep(); @@ -898,8 +946,8 @@ lcd.clear(); lcd.printString("You find a", 0, 0); - lcd.printString("potion inside", 0, 2); - lcd.printString("the chest", 0, 3); + lcd.printString("potion inside", 0, 1); + lcd.printString("the chest", 0, 2); lcd.printString("Drink (Action)", 0, 4); lcd.printString("Leave (Other)", 0, 5); lcd.refresh(); @@ -1005,6 +1053,10 @@ while(1) { + ActFlag = 0; + StartFlag = 0; + DirFlag = 0; + PlayerCamera(); if(map[px][py] == CHEST) {