Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <cstdlib>
- #include <windows.h>
- using namespace std;
- //////////////////// Constants //////////////////////
- //Map dimension
- #define MAP_WIDTH 30
- #define MAP_HEIGHT 17
- //Tile Types
- #define TILE_GRASS 0
- #define TILE_WALL 1
- #define TILE_FLOOR 2
- #define TILE_TREE 3
- #define TILE_OPENDOOR 4
- #define TILE_CLOSEDDOOR 5
- #define TILE_WATER 6
- /////////////////// Data Structures //////////////////
- //Tile Types
- struct TILE_TYPE
- {
- char nCharacter; //ASCII character for this tile type
- short nColourCode; //Colour code for this used in the game
- bool bPassable; //TRUE if you can walk on this
- };
- ////////////////// Globals /////////////////////////////
- //Declare the player's position
- int nPlayerX=4, nPlayerY=4;
- //Colour
- HANDLE colour = GetStdHandle(STD_OUTPUT_HANDLE);
- // Map declaration
- int nMapArray[MAP_HEIGHT][MAP_WIDTH] =
- {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3 },
- { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3 },
- { 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 1, 2, 2, 2, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 3 },
- { 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 1, 2, 2, 2, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 0, 0, 0 },
- { 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 1, 2, 2, 2, 1, 6, 6, 6, 6, 6,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, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }
- };
- //Tile definition
- TILE_TYPE sTileIndex [] = {
- {',', 10, true}, // (0) - TILE_GRASS
- {'#', 7, false}, // (1) - TILE_WALL
- {'.', 7, true}, // (2) - TILE_FLOOR
- {'T', 2, false}, // (3) - TILE_TREE
- {'/', 6, true}, // (4) - TILE_OPENDOOR
- {'_', 6, false}, // (5) - TILE_CLOSEDDOOR
- {'~', 9, false}, // (6) - TILE_WATER
- };
- ////////////////// Functions /////////////////////////
- //cursor position
- void gotoxy(int x, int y)
- {
- COORD c;
- c.X=x;
- c.Y=y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
- }
- //Pauli's exclusion
- bool isPassable(int nMapX, int nMapY)
- {
- //Are the coordinates valid?
- if ( nMapX <0 || nMapX >= MAP_WIDTH || nMapY <0 || nMapY >= MAP_HEIGHT)
- return false;
- // Number of specified tile
- int nTileValue = nMapArray[nMapY][nMapX];
- // Return True if it's passable
- return sTileIndex[nTileValue].bPassable;
- }
- //Draw Tile
- void DrawTile ( int x, int y)
- {
- void gotoxy(int x, int y);
- int nType = nMapArray[y][x];
- SetConsoleTextAttribute(colour, sTileIndex[nType].nColourCode);
- cout<<sTileIndex[nType].nCharacter;
- }
- //Draw map
- void DrawMap (void)
- {
- for(int y=0; y < MAP_HEIGHT; y++)
- {
- gotoxy( 0, y);
- for(int x=0; x < MAP_WIDTH; x++)
- {
- DrawTile(x, y);
- }
- }
- }
- //Door management
- void OpenDoorCommand( void )
- {
- // Notification
- gotoxy(2, 18);
- SetConsoleTextAttribute(colour, 14);
- cout<< "Opening the door...which direction? (w,a,s,d)";
- SetConsoleTextAttribute(colour, 15);
- // Decision
- char input;
- input = getch();
- int nDeltaX = 0;
- int nDeltaY = 0;
- switch(input)
- {
- //NORTH
- case 'w':
- nDeltaX =0;
- nDeltaY= -1;
- break;
- //WEST
- case 'a':
- nDeltaX = -1;
- nDeltaY= 0;
- break;
- //SOUTH
- case 's':
- nDeltaX = 0;
- nDeltaY= 1;
- break;
- //EAST
- case 'd':
- nDeltaX = 1;
- nDeltaY= 0;
- break;
- //Not a valid direction
- default:
- return;
- }
- // Is there a closed door present in the direction specified?
- if( nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] == TILE_CLOSEDDOOR)
- {
- //If there is a closed door, change it to an open one
- nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] = TILE_OPENDOOR;
- }
- system("cls");
- }
- void CloseDoorCommand (void)
- {
- // Notification
- gotoxy(2, 18);
- SetConsoleTextAttribute(colour, 14);
- cout<< "Closing the door...which direction? (w,a,s,d)";
- SetConsoleTextAttribute(colour, 15);
- // Decision
- char input;
- input = getch();
- int nDeltaX = 0;
- int nDeltaY = 0;
- switch(input)
- {
- //NORTH
- case 'w':
- nDeltaX =0;
- nDeltaY= -1;
- break;
- //WEST
- case 'a':
- nDeltaX = -1;
- nDeltaY= 0;
- break;
- //SOUTH
- case 's':
- nDeltaX = 0;
- nDeltaY= 1;
- break;
- //EAST
- case 'd':
- nDeltaX = 1;
- nDeltaY= 0;
- break;
- //Not a valid direction
- default:
- return;
- }
- // Is there a closed door present in the direction specified?
- if( nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] == TILE_OPENDOOR)
- {
- //If there is a closed door, change it to an open one
- nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] = TILE_CLOSEDDOOR;
- }
- system("cls");
- }
- ///////////////// Main function ///////////////////////
- int main()
- {
- char choose;
- while(choose='2')
- {
- menu:
- system("cls");
- SetConsoleTextAttribute(colour, 15);
- cout<<"=========================== Why hello there! ========================" <<endl;
- SetConsoleTextAttribute(colour, 14);
- cout<<" 1. Let's play the game"<<endl;
- cout<<" 2. I'm quitting"<<endl;
- SetConsoleTextAttribute(colour, 15);
- cout<<"=====================================================================" <<endl;
- cout<<endl;
- SetConsoleTextAttribute(colour, 14);
- cout<<" pPPPPPPpp rRRRRRRrr cCCCCCc Ll "<<endl;
- cout<<" PP Pp RR Rr cCc cCc LL "<<endl;
- cout<<" PPp Pp RRr Rr cCc LL "<<endl;
- cout<<" PPPPPPPPP RRRRRRRRR Cc LL "<<endl;
- cout<<" PPp RRRr Cc LL "<<endl;
- cout<<" PP RR RRr Cc LL "<<endl;
- cout<<" PP RR RRr cCc LL "<<endl;
- cout<<" PP RR RRr cCc cCc LLl "<<endl;
- cout<<" PP RR RRr cCCCCCc LLLLLLLLLLL "<<endl;
- SetConsoleTextAttribute(colour, 15);
- cout<<endl;
- cout<<"====================================================================="<<endl;
- choose = getch();
- switch(choose)
- {
- case '1':
- {
- //Main game loop
- while (true)
- {
- //Map drawing
- system("cls");
- DrawMap();
- //Sleep
- Sleep(100);
- //Draw the player to the screen
- gotoxy( nPlayerX, nPlayerY);
- SetConsoleTextAttribute(colour, 14);
- cout<<"@"<<"("<<nPlayerX<<","<<nPlayerY<<")";
- SetConsoleTextAttribute(colour, 15);
- //Input
- char input = getch();
- //Processing
- int nDeltaX;
- int nDeltaY;
- switch(input)
- {
- //Move up
- case 'w':
- nDeltaX = 0;
- nDeltaY = -1;
- break;
- //Move left
- case 'a':
- nDeltaX = -1;
- nDeltaY = 0;
- break;
- //Mode down
- case 's':
- nDeltaX = 0;
- nDeltaY = 1;
- break;
- //Move right
- case 'd':
- nDeltaX = 1;
- nDeltaY = 0;
- break;
- //Open door
- case 'o':
- OpenDoorCommand();
- break;
- //Close door
- case 'c':
- CloseDoorCommand();
- break;
- //Main Menu
- case '0':
- goto menu;
- //Ignore
- default:
- break;
- }
- //Check and see if we're allowed to move in the specified direction
- if(isPassable(nPlayerX + nDeltaX, nPlayerY + nDeltaY))
- { //if allowed, move in the direction specified
- nPlayerX += nDeltaX;
- nPlayerY += nDeltaY;
- }
- }
- system("pause");
- }
- break;
- //exit
- case '2':
- {
- cerr<<endl<<"That's not very nice of you."<<endl;
- cout<<endl;
- system("pause");
- exit(0);
- }
- break;
- //error
- default:
- {
- cerr<<endl<<"There is no such option. Please try again, it's not that complicated."<<endl;
- cout<<endl;
- system("pause");
- cout<<endl;
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement