
Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen
Dependencies: N5110 PowerControl mbed
Revision 35:2c290fa78f1d, committed 2015-05-11
- Comitter:
- ThomasBGill
- Date:
- Mon May 11 22:03:41 2015 +0000
- Parent:
- 34:e58c8322884d
- Child:
- 36:b64696135142
- Commit message:
- Game source file commenting finished
Changed in this revision
--- a/Game.cpp Mon May 11 12:56:26 2015 +0000 +++ b/Game.cpp Mon May 11 22:03:41 2015 +0000 @@ -1,5 +1,33 @@ #include "Game.h" +int main() +{ + calibrateJoystick(); // get centred values of joystick + pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second + + //Power Saving + PHY_PowerDown(); + int result = semihost_powerdown(); + + //Initilize switches + Start.mode(PullDown); + Act.mode(PullDown); + Start.fall(&StartPressed); + Act.fall(&ActPressed); + + //Generate random seed + srand(Noise * 1000000); + + //Initilize screen + lcd.init(); + + //Display game title screen + Intro(); + + //Start game + MainMenu(); +} + //Power Saving int semihost_powerdown() { @@ -32,7 +60,6 @@ joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state - //joystick.button = button; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction @@ -313,13 +340,13 @@ ActFlag = 0; StartFlag = 0; - if (map[px][py] == CHEST) { + if (map[px][py] == CHEST) { //If player's current tile is a chest, call the chest function Chest(); } - if (px == exx && py == exy) { + if (px == exx && py == exy) { //If plyer's current tile is the exit - if(level%5 == 0) { + if(level%5 == 0) { //If the current level number is a multiple of 5 start a boss fight BossFight = true; Fight(); } @@ -335,14 +362,14 @@ PlayerMove(); - if (rand() % 40 == 0) { + if (rand() % 40 == 0) { //Random chance to heal player as they move if (ph < PH_MAX) { ph++; } } - if (rand() % 50 == 0) { + if (rand() % 50 == 0) { //Random chance for fight to start when player moves Fight(); } @@ -375,7 +402,7 @@ for (int j = 0; j < 6; j++) { if (i == 6 && j == 2) { - lcd.printString("@", (6 * i) + 1, j); + lcd.printString("@", (6 * i) + 1, j); //Make player's location a '@' symbol } else { int diffx = i - 6; int diffy = j - 2; @@ -388,7 +415,7 @@ lcd.printChar(TileList[tile].Symbol, (6 * i) + 1, j); if (map[px + diffx][py + diffy] == FLOOR) { - map[px + diffx][py + diffy] = FLOOR_SEEN; + map[px + diffx][py + diffy] = FLOOR_SEEN; //Set the floor that has been in view of the player camera to seen to be displayed on the map } } @@ -399,6 +426,8 @@ void PlayerMove() { + //If direction analogue stick is moved is passable then update the player position + if (joystick.direction == Up) { int tile = map[px][py - 1]; if (TileList[tile].Passable) { @@ -432,13 +461,14 @@ lcd.clear(); if(BossFight) { - BossFight = false; + BossFight = false; //Reset BossFight to false m = 5; //Monster is a minotaur lcd.printString("BOSS!", 30, 2); - } else { + } else { //If not a boss fight randomly choose a monster to fight + //Lvl 1- Huge Rat + Goblin, Lvl 2 +Skeleton, Lvl 3 +Wraith, Lvl 4 +Ogre int mr = level + 1; if (mr > 5) { @@ -458,9 +488,9 @@ int write = sprintf(buffer1, "%d/%d", mh, EnemyList[m].EHealth); // print formatted data to buffer lcd.clear(); - lcd.printString(EnemyList[m].EName, 0, 0); - lcd.printString(buffer1, 54, 0); - DrawGraphic(m, 23, 7); + lcd.printString(EnemyList[m].EName, 0, 0); + lcd.printString(buffer1, 54, 0); //Current enemy health/max enemy health + DrawGraphic(m, 23, 7); //Image of monster lcd.refresh(); wait(2.0); @@ -474,13 +504,13 @@ lcd.printString(EnemyList[m].EName, 0, 0); write = sprintf(buffer1, "%d/%d", mh, EnemyList[m].EHealth); // print formatted data to buffer - lcd.printString(buffer1, 54, 0); + lcd.printString(buffer1, 54, 0); //Current enemy health/max enemy health lcd.printString("You", 0, 2); char buffer2[14]; int phm = PH_MAX; write = sprintf(buffer2, "%d/%d", ph, phm); // print formatted data to buffer - lcd.printString(buffer2, 54, 2); + lcd.printString(buffer2, 54, 2); //Current player health/max player health if (menu) { lcd.printString("Fight <", 0, 4); @@ -533,7 +563,7 @@ MonsterAttack(m); - } else { + } else { //Monster doesn't block the path int s = rand() % 100 + 1; @@ -546,7 +576,7 @@ Sleep(); break; - } else { + } else { //You don't outspeed the monster and it attacks you lcd.clear(); lcd.printString("You try to run", 0, 0); @@ -584,6 +614,7 @@ char damBuffer[14]; int write = sprintf(damBuffer,"-%d", damage); + //Flash image of monster on the screen with the amount of damage it recieved lcd.clear(); DrawGraphic(m, 23, 7); lcd.printString(damBuffer, 62, 2); @@ -660,7 +691,7 @@ while (1) { if (menu == 0) { lcd.clear(); - lcd.printString(buffer, 0, 0); + lcd.printString(buffer, 0, 0); //Player current/max health lcd.printString("Map <", 0, 2); lcd.printString("Map Legend", 0, 3); lcd.printString("Inventory", 0, 4); @@ -669,8 +700,8 @@ Sleep(); } else if (menu == 1) { lcd.clear(); - lcd.printString(buffer, 0, 0); - lcd.printString("Map", 0, 2); + lcd.printString(buffer, 0, 0); //Player current/max health + lcd.printString("Map", 0, 2); lcd.printString("Map Legend <", 0, 3); lcd.printString("Inventory", 0, 4); lcd.printString("Continue", 0, 5); @@ -678,7 +709,7 @@ Sleep(); } else if (menu == 2) { lcd.clear(); - lcd.printString(buffer, 0, 0); + lcd.printString(buffer, 0, 0); //Player current/max health lcd.printString("Map", 0, 2); lcd.printString("Map Legend", 0, 3); lcd.printString("Inventory <", 0, 4); @@ -687,7 +718,7 @@ Sleep(); } else if (menu == 3) { lcd.clear(); - lcd.printString(buffer, 0, 0); + lcd.printString(buffer, 0, 0); //Player current/max health lcd.printString("Map", 0, 2); lcd.printString("Map Legend", 0, 3); lcd.printString("Inventory", 0, 4); @@ -784,16 +815,16 @@ { lcd.clear(); lcd.printString("Armour:", 0, 0); - lcd.printString(ItemList[pa].ItemName, 0, 1); + lcd.printString(ItemList[pa].ItemName, 0, 1); //Current armour char buffer1[14]; int write = sprintf(buffer1, "+%d Armour", ItemList[pa].ItemValue); // print formatted data to buffer - lcd.printString(buffer1, 0, 2); + lcd.printString(buffer1, 0, 2); //Current armour value lcd.printString("Weapon:", 0, 3); - lcd.printString(ItemList[pw].ItemName, 0, 4); + lcd.printString(ItemList[pw].ItemName, 0, 4); //Current weapon char buffer2[14]; write = sprintf(buffer2, "+%d Damage", ItemList[pw].ItemValue); // print formatted data to buffer - lcd.printString(buffer2, 0, 5); + lcd.printString(buffer2, 0, 5); //Current weapon value lcd.refresh(); while (1) { @@ -813,6 +844,7 @@ { score = score + 10; + //Generate what is in the chest int c = rand() % 4; //0- Item, 1- Booby trap, 2- Map, Else- Potion if (c == 0) { @@ -825,13 +857,13 @@ Potion(); } - map[px][py] = CHEST_OPENED; + map[px][py] = CHEST_OPENED; //Change the current tile to CHEST_OPENED } void getItem() { - + //Generate item from ItemList int r = rand() % 10; lcd.clear(); @@ -846,7 +878,7 @@ while(1) { Sleep(); - if (ActFlag) { + if (ActFlag) { //Take item ActFlag = 0; @@ -864,7 +896,7 @@ Sleep(); } - if(StartFlag) { + if(StartFlag) { //Leave item StartFlag = 0; @@ -880,7 +912,7 @@ void BoobyTrap() { - + //Generate damage int damage = rand() % 5; if (damage != 0) { @@ -919,7 +951,7 @@ void RevealMap() { - + //Iterates through each cell in the array, changing any unseen floor tiles to seen ones for (int i = 0; i < 84; i++) { for (int j = 0; j < 48; j++) { @@ -941,7 +973,7 @@ void Potion() { - + //Generates what the potion is int p = rand() % 5; //0- Poison, 1- Teleport, Else- Full heal lcd.clear(); @@ -1043,7 +1075,7 @@ lcd.clear(); lcd.printString("You scored", 6, 1); - lcd.printString(buffer, 6, 2); + lcd.printString(buffer, 6, 2); //Player's final score lcd.refresh(); wait(1.0); Sleep(); @@ -1055,8 +1087,6 @@ { HighScoreCheck(); - //readDataFromFile(); - char buffer1[14]; int write = sprintf(buffer1, "1. %d", HScore1); @@ -1071,10 +1101,10 @@ lcd.clear(); lcd.printString("High Scores", 6, 0); - lcd.printString(buffer1, 6, 2); - lcd.printString(buffer2, 6, 3); - lcd.printString(buffer3, 6, 4); - lcd.printString(buffer4, 6, 5); + lcd.printString(buffer1, 6, 2); //High score 1 + lcd.printString(buffer2, 6, 3); //High score 2 + lcd.printString(buffer3, 6, 4); //High score 3 + lcd.printString(buffer4, 6, 5); //High score 4 lcd.refresh(); wait(1.0); @@ -1089,33 +1119,4 @@ ActFlag = 0; } } -} - -int main() -{ - calibrateJoystick(); // get centred values of joystick - pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second - - //Power Saving - PHY_PowerDown(); - int result = semihost_powerdown(); - - Start.mode(PullDown); - Act.mode(PullDown); - - Start.fall(&StartPressed); - Act.fall(&ActPressed); - - Start.mode(PullDown); - Act.mode(PullDown); - - //Generate random seed - srand(Noise * 1000000); - - //Initilize screen - lcd.init(); - - Intro(); - - MainMenu(); } \ No newline at end of file
--- a/Game.h Mon May 11 12:56:26 2015 +0000 +++ b/Game.h Mon May 11 22:03:41 2015 +0000 @@ -1,9 +1,11 @@ /** -@file Game.h -@brief Header file containing the main game member functions and variables -@brief Revision 1.0 -@author Thomas Barnaby Gill -@date 11th May 2015 +* @file Game.h +* @brief Header file containing the main game member functions and variables +* @brief Revision 1.0 +* +* @brief Code for reading joystick by Craig Evans https://developer.mbed.org/users/eencae/code/Joystick/file/026fa541af7a/main.cpp +* @author Thomas Barnaby Gill +* @date 11th May 2015 */ #include "N5110.h" @@ -39,28 +41,51 @@ Centre, Unknown }; - -// struct for Joystick + +/** Joystick Structure +* +* It includes the x, x0, y, y0 and direction members +* @param x - current x direction potentiometer value +* @param x0 - centred x direction potentiometer value +* @param y - current y direction potentiometer value +* @param y0 - centred y direction potentiometer value +* @param direction - allowed direction values +*/ typedef struct JoyStick Joystick; struct JoyStick { float x; // current x value float x0; // 'centred' x value float y; // current y value float y0; // 'centred' y value - int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) DirectionName direction; // current direction }; // create struct variable Joystick joystick; -// function prototypes +/** Calibrate Joystick +* +* Reads defualt position of joystick for further readings +* +*/ void calibrateJoystick(); + +/** Update Joystick +* +* Reads cuurent position of joystick relative to the defualt position +* +*/ void updateJoystick(); #define USR_POWERDOWN (0x104) LocalFileSystem local("local"); // create local filesystem +/** Tile Structure +* +* It includes the symbol and passable members +* @param Symbol - contains the tile symbols for each tile type +* @param Passable - whether tile type can be walked on by player +*/ struct TILES { char Symbol; // Symbol for this tile bool Passable; // Can tile be walked on @@ -76,6 +101,12 @@ { '/', true }, // 6- CHEST_OPENED }; +/** Item Structure +* +* It includes the ItemName and the ItemValue members +* @param ItemName - contains the name of the item as a char array +* @param ItemValue - contains damage/armour value of the item +*/ struct ITEMS { char ItemName[15]; //Item name int ItemValue; //Damage/ armour value @@ -96,6 +127,18 @@ { "Plate armour", 4 }, //9 }; +/** Enemy Structure +* +* It includes the EName, EHealth, EDamage, EArmour, EDodge, EHit, ESpd and EPoints members +* @param EName - contains the name of the enemy as a char array +* @param EHealth - contains the monster's full health value +* @param EADamage - contains the monster's damage value +* @param EArmour - contains the monster's armour value +* @param EDodge - contains the monster's dodge chance value +* @param EHit - contains the monster's hit chance value +* @param ESpd - contains the monster's speed value +* @param EPoints- contains the score value of the monster +*/ struct ENEMIES { char EName[9]; //Enemy name int EHealth; //Enemy health @@ -118,34 +161,92 @@ }; //Variables +/** Act Flag +* +* @param ActFlag - Used in the ISR. It is set to 1 when the act button is pressed +* +*/ int ActFlag = 0; +/** Start Flag +* +* @param StartFlag - Used in the ISR. It is set to 1 when the start button is pressed +* +*/ int StartFlag = 0; -//int DirFlag = 0; -//Space type player is on -int pSpace; +/** Player Health +* +* @param ph - contains the player's current health value +* +*/ +//Player Health +int ph = PH_MAX; -//Player Health -int ph = PH_MAX; - +/** Player Weapon +* +* @param pw - contains the player's current weapon identifier value +* +*/ //Player weapon int pw = 0; //0 to 4 +/** Player Armour +* +* @param pa - contains the player's current armour identifier value +* +*/ //Player armour int pa = 5; //5 to 9 +/** Monster Health +* +* @param mh - contains the monster's current health value +* +*/ //Monster health int mh; +/** Score +* +* @param score - contains the player's current score value +* +*/ //Player score int score = -100; //100 level, x monster, 10 chest, -2 running away +/** Highest Score +* +* @param HScore1 - contains the top high score value +*/ //High scores int HScore1; + +/** Second Highest Score +* +* @param HScore2 - contains the second high score value +* +*/ int HScore2; + +/** Third Highest Score +* +* @param HScore3 - contains the third high score value +* +*/ int HScore3; + +/** Fourth Highest Score +* +* @param HScore4 - contains the fourth high score value +* +*/ int HScore4; +/** Boss Fight +* +* @param BossFight - Defualt false. Set to true at the exit of every level that is a multiple of 5 (labyrinth levels). Causes a minotaur to appear in the fight function. +* +*/ bool BossFight = false; //Voids @@ -194,7 +295,7 @@ /** Flash Screen * * Inverts and then uninverts the screen's colours -* @param n- Number of times the screen flashes +* @param n - contains the number of times the screen flashes * */ void FlashScreen(int n); @@ -202,9 +303,9 @@ /** Draw Graphic * * Draws a 38x38 pixel image from a starting point -* @param p- Which image to draw (0- Huge Rat, 1- Goblin, 2- Skeleton, 3- Wraith, 4- Ogre, 5- Minotaur) -* @param x- x co-ordinate of starting position -* @param y- y co-ordinate of starting position +* @param p - contains the which image to draw (0- Huge Rat, 1- Goblin, 2- Skeleton, 3- Wraith, 4- Ogre, 5- Minotaur) +* @param x - contains the x co-ordinate of starting position +* @param y - contains the y co-ordinate of starting position * */ void DrawGraphic(int p, int x, int y); @@ -291,6 +392,7 @@ /** Monster Attack * * Calculates the damage the opponent does to the player +* @param m - contains the current monster identifier * */ void MonsterAttack(int m); @@ -298,6 +400,7 @@ /** Player Attack * * Calculates the damage the player does to the opponent +* @param m - contains the current monster identifier * */ void PlayerAttack(int m);
--- a/WorldBuilder/WorldBuilder.cpp Mon May 11 12:56:26 2015 +0000 +++ b/WorldBuilder/WorldBuilder.cpp Mon May 11 22:03:41 2015 +0000 @@ -12,7 +12,7 @@ int sx; int sy; -int dir; + int level; @@ -319,7 +319,6 @@ LabyrinthBuilder(); } else { DungeonBuilder(); - //LabyrinthBuilder(); } px = enx;
--- a/WorldBuilder/WorldBuilder.h Mon May 11 12:56:26 2015 +0000 +++ b/WorldBuilder/WorldBuilder.h Mon May 11 22:03:41 2015 +0000 @@ -1,3 +1,12 @@ +/** +* @file WorldBuilder.h +* @brief Header file containing the world generation member functions and variables +* @brief Revision 1.0 +* +* @author Thomas Barnaby Gill +* @date 11th May 2015 +*/ + #include "mbed.h" #ifndef WORLDBUILDER_H @@ -19,37 +28,173 @@ #define MAP_WIDTH 84 #define MAP_HEIGHT 48 +/** Map Array +* +* @param map - Array which stores the map data for the game +* +*/ extern int map[MAP_WIDTH][MAP_HEIGHT]; //Enterance coordinates +/** Enterance x co-ordinate +* +* @param enx - Variable which stores the x co-ordinate of the enterance of the current level +* +*/ extern int enx; + +/** Enterance y co-ordinate +* +* @param eny - Variable which stores the y co-ordinate of the enterance of the current level +* +*/ extern int eny; //Exit coordinates +/** Exit x co-ordinate +* +* @param exx - Variable which stores the x co-ordinate of the exit of the current level +* +*/ extern int exx; + +/** Exit y co-ordinate +* +* @param exy- Variable which stores the y co-ordinate of the exit of the current level +* +*/ extern int exy; +/** Start x co-ordinate +* +* @param sx - Variable which stores the x co-ordinate of the start of a room +* +*/ extern int sx; + +/** Start y co-ordinate +* +* @param sy - Variable which stores the y co-ordinate of the start of a room +* +*/ extern int sy; -extern int dir; +/** Current Level +* +* @param level - Variable which stores the level number the player is currently on +* +*/ extern int level; -//Player coordinates +/** Player x co-ordinate +* +* @param px - Variable which stores the current x co-ordinate of the player +* +*/ extern int px; +/** Player y co-ordinate +* +* @param py - Variable which stores the current x co-ordinate of the player +* +*/ extern int py; +/** Wall Fill +* +* Fills the map array with walls +* +*/ void Walls(); + +/** First room Creator +* +* Creates a room at a random position in the map with an enterance in it +* +*/ void FirstRoom(); + +/** Exit room Creator +* +* Creates a room at a random position in the map with an exit in it +* +*/ void ExitRoom(); + +/** Dungeon Room Creator +* +* Creates a room at a random position in the map with a chance of having a chest in it +* +*/ void DungeonRoomBuilder(); + +/** Neighbours +* +* Checks how many neighbouring cells at the current position are floors. It returns an integer value equal to the number of neighbours with floors. +* @param i - x co-ordinate of current position +* @param j - y co-ordinate of current position +* +*/ int Neighbours(int i, int j); + +/** Dead Ends +* +* Deletes corridors that are dead ends +* @param d - How many times the function should iterate through the map +* +*/ void DeadEnds(int d); + +/** Border +* +* Creates a 1 cell thick border of walls at the extremeties of the map (where the x or y co-ordinates are 0) +* +*/ void Border(); + +/** Random Floor Setter +* +* Randomly turns a wall in the map into a floor. +* @pararm r - How many times the function should be iterated +* +*/ +void RandFloor(int r) + +/** Maze Kill +* +* The 'kill' phase of a hunt and kill maze algorithm. +* The algorithm walks in random directions (which do not conflict with previously made paths), turning walls into floors in the current direction until it hits a dead end +* +*/ void MazeKill(); + +/** Maze Maker +* +* Implements a 'hunt and kill' perfect maze algorithm +* +* (See http://www.astrolog.org/labyrnth/algrithm.htm for more information on the algorithm) +* +*/ void Maze(); + +/** Dungeon Builder +* +* Creates a 'dungeon' style level on the map array by generating a maze, adding enterance, exit and filler rooms, turning random tiles into floors and then removing the dead ends +* +*/ void DungeonBuilder(); + +/** Labyrinth Builder +* +* Creates a labyrinth level on the map array by generating a maze, adding enterance and exit rooms, turning random tiles into floors and then removing some of the dead ends +* +*/ void LabyrinthBuilder(); + +/** World Generator +* +* Clears the current map using the Walls() function and then generates a dungeon or labyrinth level +* +*/ void World(); #endif \ No newline at end of file