Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include "time.h"
- using namespace std;
- //Functions
- void initialiseMap();
- void printMap();
- void updateMap(int nextX, int nextY);
- void updateScore();
- void gameLoop();
- void getInput();
- void playerMove(char dir);
- //Game State
- bool running = true;
- const int mapWidth = 32;
- const int mapHeight = 8;
- char map [mapWidth][mapHeight];
- int coinsLeft = 0;
- //Graphic Representations
- struct Graphics
- {
- static const char space = ' ';
- static const char player = 'T';
- static const char block = '#';
- static const char coin = '*';
- static const char boulder = 'O';
- static const char doorL = '[';
- static const char doorR = ']';
- };
- //Player State
- struct Player
- {
- int posX;
- int posY;
- int score;
- };
- Graphics graphics;
- Player dave;
- int main()
- {
- dave.posX = 1;
- dave.posY = 1;
- dave.score = 0;
- initialiseMap();
- gameLoop();
- system("CLS");
- if(dave.score < 50)
- {
- cout << "Game Ended. Final Score: " << dave.score << "! Press Enter to Quit.";
- }
- else
- {
- cout << "Congratulations! A Winner is You! Score: " << dave.score << "! Press Enter to Quit.";
- }
- cin.get();
- return 0;
- }
- void gameLoop()
- {
- while(running == true)
- {
- getInput();
- if(coinsLeft == 0 && dave.score < 50)
- {
- initialiseMap();
- }
- if(dave.score >= 50)
- {
- running = false;
- }
- Sleep(150);
- }
- }
- void initialiseMap()
- {
- srand (time(NULL));
- //Populate map
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- if(x == 0 || x == mapWidth - 1 || y == 0 || y == mapHeight - 1)
- {
- map[x][y] = graphics.block;
- }
- else
- {
- map[x][y] = graphics.space;
- }
- }
- }
- //Spawn player
- map[dave.posX][dave.posY] = graphics.player;
- //Spawn blocks
- for(int count = 0; count < 10; count++)
- {
- int randX = 0;
- int randY = 0;
- do
- {
- randX = rand() % (mapWidth - 1) + 1;
- randY = rand() % (mapHeight - 1) + 1;
- }
- while(map[randX][randY] != graphics.space);
- map[randX][randY] = graphics.block;
- }
- //Spawn coins
- for(int count = 0; count < 10; count++)
- {
- int randX = rand() % (mapWidth - 1) + 1;
- int randY = rand() % (mapHeight - 1) + 1;
- while(map[randX][randY] != graphics.space)
- {
- randX = rand() % (mapWidth - 1) + 1;
- randY = rand() % (mapHeight - 1) + 1;
- }
- map[randX][randY] = graphics.coin;
- }
- //Spawn boulders
- for(int count = 0; count < 2; count++)
- {
- int randX = rand() % (mapWidth - 1) + 1;
- int randY = rand() % (mapHeight - 1) + 1;
- while(map[randX][randY] != graphics.space)
- {
- randX = rand() % (mapWidth - 1) + 1;
- randY = rand() % (mapHeight - 1) + 1;
- }
- map[randX][randY] = graphics.boulder;
- }
- //Spawn portals
- int randDoor = rand() % (mapHeight - 2) + 1;
- map[0][randDoor] = graphics.doorL;
- map[mapWidth-1][randDoor] = graphics.doorR;
- coinsLeft = 10;
- printMap();
- }
- void printMap()
- {
- system("CLS");
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- cout << map[x][y];
- }
- cout << endl;
- }
- updateScore();
- }
- void updateMap(int nextX, int nextY, int pushX, int pushY, bool pushing)
- {
- COORD coord;
- coord.X = dave.posX;
- coord.Y = dave.posY;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- cout << map[coord.X][coord.Y];
- coord.X = nextX;
- coord.Y = nextY;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- cout << map[coord.X][coord.Y];
- if(pushing)
- {
- coord.X = pushX;
- coord.Y = pushY;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- cout << map[coord.X][coord.Y];
- }
- coord.X = 0;
- coord.Y = mapHeight;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- updateScore();
- }
- void updateScore()
- {
- cout << "Score: " << dave.score << ". Press Esc to End the Game.";
- }
- void getInput()
- {
- if(GetAsyncKeyState(VK_UP))
- {
- playerMove('N');
- }
- if(GetAsyncKeyState(VK_RIGHT))
- {
- playerMove('E');
- }
- if(GetAsyncKeyState(VK_DOWN))
- {
- playerMove('S');
- }
- if(GetAsyncKeyState(VK_LEFT))
- {
- playerMove('W');
- }
- if(GetAsyncKeyState(VK_ESCAPE))
- {
- running = false;
- }
- }
- void playerMove(char dir)
- {
- int nextX = dave.posX;
- int nextY = dave.posY;
- int pushX = dave.posX;
- int pushY = dave.posY;
- if(dir == 'N')
- {
- nextY -= 1;
- pushY -= 2;
- }
- if(dir == 'E')
- {
- nextX += 1;
- pushX += 2;
- }
- if(dir == 'S')
- {
- nextY += 1;
- pushY += 2;
- }
- if(dir == 'W')
- {
- nextX -= 1;
- pushX -= 2;
- }
- //move into space
- if(map[nextX][nextY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- updateMap(nextX, nextY, 0, 0, false);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- //Collect coin and move
- if(map[nextX][nextY] == graphics.coin)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- dave.score += 1;
- coinsLeft -= 1;
- updateMap(nextX, nextY, 0, 0, false);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- //Push boulder and move
- if(map[nextX][nextY] == graphics.boulder)
- {
- if(map[pushX][pushY] == graphics.doorL)
- {
- pushX = mapWidth - pushX - 2;
- }
- if(map[pushX][pushY] == graphics.doorR)
- {
- pushX = mapWidth - pushX;
- }
- if(map[pushX][pushY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- map[pushX][pushY] = graphics.boulder;
- updateMap(nextX, nextY, pushX, pushY, true);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- }
- //Move through portals
- if(map[nextX][nextY] == graphics.doorL)
- {
- nextX = mapWidth - nextX - 2;
- if(map[nextX][nextY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- updateMap(nextX, nextY, 0, 0, false);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- if(map[nextX][nextY] == graphics.boulder)
- {
- pushX = nextX - 1;
- if(map[pushX][pushY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- map[pushX][pushY] = graphics.boulder;
- updateMap(nextX, nextY, pushX, pushY, true);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- }
- }
- if(map[nextX][nextY] == graphics.doorR)
- {
- nextX = mapWidth - nextX;
- if(map[nextX][nextY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- updateMap(nextX, nextY, 0, 0, false);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- if(map[nextX][nextY] == graphics.boulder)
- {
- pushX = nextX + 1;
- if(map[pushX][pushY] == graphics.space)
- {
- map[dave.posX][dave.posY] = graphics.space;
- map[nextX][nextY] = graphics.player;
- map[pushX][pushY] = graphics.boulder;
- updateMap(nextX, nextY, pushX, pushY, true);
- dave.posX = nextX;
- dave.posY = nextY;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement