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;
- HANDLE colour = GetStdHandle(STD_OUTPUT_HANDLE);
- //cursor position
- void gotoxy(int x, int y)
- {
- COORD c;
- c.X=x-1;
- c.Y=y-1;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
- }
- //Map dimension
- #define MAP_WIDTH 20
- #define MAP_HEIGHT 15
- //Tile Types
- #define TILE_FLOOR 0
- #define TILE_WALL 1
- // Map declaration
- int nMapArray[MAP_HEIGHT][MAP_WIDTH] =
- {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
- };
- //Draw map
- void DrawMap (void)
- {
- for(int y=0; y < MAP_HEIGHT; y++)
- {
- gotoxy( 0, y);
- for(int x=0; x < MAP_WIDTH; x++)
- {
- //Draw the tile
- switch (nMapArray[y][x])
- {
- case TILE_FLOOR:
- {
- SetConsoleTextAttribute(colour, 7);
- cout<<".";
- SetConsoleTextAttribute(colour, 15);
- break;
- }
- case TILE_WALL:
- {
- SetConsoleTextAttribute(colour, 8);
- cout<<"#";
- SetConsoleTextAttribute(colour, 15);
- break;
- }
- }
- }
- }
- }
- 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;
- choose = getch();
- switch(choose)
- {
- case '1':
- {
- //Declare the player's position
- int nPlayerX=4, nPlayerY=4;
- //Main game loop
- while (true)
- {
- //Map drawing
- system("cls");
- DrawMap();
- //Draw the player to the screen
- gotoxy( nPlayerX, nPlayerY);
- SetConsoleTextAttribute(colour, 14);
- cout<<"@"<<"("<<nPlayerX<<","<<nPlayerY<<")";
- SetConsoleTextAttribute(colour, 15);
- //Input
- char input = getch();
- //Processing
- switch(input)
- {
- //Move up
- case 'w':
- if(nPlayerY<=1)
- break;
- else nPlayerY--;
- break;
- //Move left
- case 'a':
- if(nPlayerX<=1)
- break;
- else nPlayerX--;
- break;
- //Mode down
- case 's':
- if(nPlayerY>=14)
- break;
- else nPlayerY++;
- break;
- //Move right
- case 'd':
- if(nPlayerX>=29)
- break;
- else nPlayerX++;
- break;
- //Main Menu
- case '0':
- goto menu;
- //Ignore
- default:
- break;
- }
- }
- 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