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:
- 23:1b8b1d043403
- Parent:
- 22:dae750e4d749
- Child:
- 24:4c4467971c91
--- a/main.cpp Thu May 07 15:00:04 2015 +0000
+++ b/main.cpp Thu May 07 17:04:22 2015 +0000
@@ -59,16 +59,17 @@
int EDodge; //Enemy dodge chance
int EHit; //Enemy hit chance
int ESpd; //Enemy speed (used for running away during fights)
+ int EPoints; //How many points the monster is worth
};
ENEMIES EnemyList[] = {
- //Name HP Dmg Arm Dg Ht Spd
- { "Huge Rat", 5, 3, 0, 25, 70, 40 }, //0- Huge Rat
- { "Goblin", 6, 3, 1, 25, 60, 30 }, //1- Goblin
- { "Skeleton", 8, 4, 2, 10, 50, 10 }, //2- Skeleton
- { "Wraith", 5, 4, 0, 40, 60, 50 }, //3- Wraith
- { "Ogre", 10, 7, 3, 10, 35, 15 }, //4- Ogre
- { "Minotaur", 15, 8, 4, 20, 45, 100 }, //5- Minotaur
+ //Name HP Dmg Arm Dg Ht Spd Pts
+ { "Huge Rat", 5, 3, 0, 25, 70, 40, 1 }, //0- Huge Rat
+ { "Goblin", 6, 3, 1, 25, 60, 30, 3 }, //1- Goblin
+ { "Skeleton", 8, 4, 2, 10, 50, 10, 5 }, //2- Skeleton
+ { "Wraith", 5, 4, 0, 40, 60, 50, 10 }, //3- Wraith
+ { "Ogre", 10, 7, 3, 10, 35, 15, 15 }, //4- Ogre
+ { "Minotaur", 15, 8, 4, 20, 45, 100, 50 }, //5- Minotaur
};
//Variables
@@ -88,6 +89,14 @@
//Player armour
int pa = 5; //5 to 9
+//Player score
+int score = -100; //100 level, x monster, 10 chest, -2 running away
+
+//High scores
+int HScore1;
+int HScore2;
+int HScore3;
+
//Power Saving
int semihost_powerdown()
{
@@ -131,8 +140,52 @@
void RevealMap();
void Potion();
void GameOver();
+void ScoreScreen();
+void HighScoreScreen();
void DrawGraphic(int p, int x, int y);
+//High score system
+//Write txt setup
+LocalFileSystem local("local"); // create local filesystem
+
+void writeDataToFile()
+{
+ FILE *fp = fopen("/local/highscore.txt", "w"); // open 'highscore.txt' for writing
+ fprintf(fp,"%d\r\n",HScore1); // print score to file
+ fprintf(fp,"%d\r\n",HScore2); // print score to file
+ fprintf(fp,"%d\r\n",HScore3); // print score to file
+ fclose(fp); // close file
+}
+
+void readDataFromFile()
+{
+ FILE *fp = fopen("/local/highscore.txt", "w"); // open 'highscore.txt' for writing
+ fscanf(fp,"%d", &HScore1); // read score from file and save to int
+ fscanf(fp,"%d", &HScore2); // read score from file and save to int
+ fscanf(fp,"%d", &HScore3); // read score from file and save to int
+ fclose(fp); // close file
+}
+
+void HighScoreCheck()
+{
+ readDataFromFile();
+
+ if(score >= HScore1) {
+ HScore3 = HScore2;
+ HScore2 = HScore1;
+ HScore1 = score;
+ } else if(score >= HScore2) {
+ HScore3 = HScore2;
+ HScore2 = score;
+ } else if(score > HScore3) {
+ HScore3 = score;
+ }
+
+ writeDataToFile();
+}
+
+//Game functions
+
void DrawGraphic(int p, int x, int y)
{
if(p == 0) {
@@ -159,6 +212,8 @@
void MainMenu()
{
+ score = -100;
+
level = 0;
//Player Health
@@ -170,18 +225,27 @@
//Player armour
pa = 5; //5 to 9
- bool menu = 0;
+ int menu = 0;
while (1) {
lcd.clear();
if (menu == 0) {
- lcd.printString("New Game <", 20, 1);
- lcd.printString("Options", 20, 3);
+ lcd.printString("New Game <", 6, 1);
+ lcd.printString("High Scores", 6, 2);
+ lcd.printString("Options", 6, 3);
+ lcd.refresh();
+ Sleep();
+ }
+ if (menu == 1) {
+ lcd.printString("New Game", 6, 1);
+ lcd.printString("High Scores <", 6, 2);
+ lcd.printString("Options", 6, 3);
lcd.refresh();
Sleep();
} else {
- lcd.printString("New Game", 20, 1);
- lcd.printString("Options <", 20, 3);
+ lcd.printString("New Game", 6, 1);
+ lcd.printString("High Scores", 6, 2);
+ lcd.printString("Options <", 6, 3);
lcd.refresh();
Sleep();
}
@@ -190,7 +254,15 @@
DirFlag = 0;
- menu = !menu;
+ if(Up) {
+ if(menu > 0) {
+ menu--;
+ }
+ } else if(Down) {
+ if(menu < 3) {
+ menu++;
+ }
+ }
}
if (ActFlag) {
@@ -198,6 +270,8 @@
if (menu == 0) {
GameLoop();
+ } else if (menu == 1) {
+ HighScoreScreen();
} else {
Options();
}
@@ -214,13 +288,13 @@
lcd.setBrightness(bright);
lcd.clear();
-
+
if(bright == 1) {
lcd.printString("Backlight ON", 6, 2);
} else {
lcd.printString("Backlight OFF", 6, 2);
}
-
+
lcd.refresh();
if (DirFlag) {
@@ -259,11 +333,13 @@
level++;
+ score = score + 100;
+
World();
LevelScreen();
- RevealMap(); //Uncomment for debugging to view map
+ //RevealMap(); //Uncomment for debugging to view map
while (1) {
@@ -469,6 +545,7 @@
}
if (mh <= 0) { //Check if monster is dead
+ score = score + EnemyList[m].EPoints;
lcd.clear();
lcd.printString("You win!", 18, 2);
lcd.refresh();
@@ -482,6 +559,8 @@
} else { //Run away
+ score = score - 2;
+
int b = rand() % 5;
if (b == 0) { //Monster blocks the path
@@ -740,6 +819,8 @@
void Chest()
{
+ score = score + 10;
+
int c = rand() % 4; //0- Item, 1- Booby trap, 2- Map, Else- Potion
if (c == 0) {
@@ -959,10 +1040,51 @@
wait(1.0);
Sleep();
lcd.normalMode();
- MainMenu();
+ ScoreScreen();
+}
+
+void ScoreScreen()
+{
+ char buffer[14];
+
+ int write = sprintf(buffer, "%d points", score);
+
+ lcd.clear();
+ lcd.printString("You scored", 6, 1);
+ lcd.printString(buffer, 6, 2);
+ lcd.refresh();
+ wait(1.0);
+ Sleep();
+
+ HighScoreScreen();
}
+void HighScoreScreen()
+{
+ HighScoreCheck();
+
+ char buffer1[14];
+ int write = sprintf(buffer1, "1. %d", HScore1);
+
+ char buffer2[14];
+ write = sprintf(buffer2, "2. %d", HScore2);
+
+ char buffer3[14];
+ write = sprintf(buffer3, "3. %d", HScore3);
+
+ lcd.clear();
+ lcd.printString("High Scores", 6, 0);
+ lcd.printString(buffer1, 6, 2);
+ lcd.printString(buffer2, 6, 3);
+ lcd.printString(buffer3, 6, 4);
+ lcd.refresh();
+ wait(1.0);
+ Sleep();
+
+ MainMenu();
+
+}
int main()
{