Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tile Struct
- struct Graphics
- {
- //walkable
- static const char sand = 's';
- static const char grass = ',';
- static const char forest = 'T';
- static const char rock = 'R';
- //walkable but non buildable
- static const char swamp = '#';
- //non walkable non buildable
- static const char water = '~';
- static const char mountain = 'M';
- static const char seed = 'o';
- //static const char seedMat = 'o';
- static const char player = 1;
- };
- class generateNoise
- {
- public:
- // public area. visible to the 'user' of the new data type.
- private:
- // no one can see anything in this area except you.
- };
- class smoothNoise
- {
- public:
- // public area. visible to the 'user' of the new data type.
- private:
- // no one can see anything in this area except you.
- };
- Graphics graphics;
- #include <iostream>
- #include "windows.h"
- #include "time.h"
- //#import "\generateMap.*"
- using namespace std;
- //Game Declarations
- bool running = true;
- const int FPS = 6;
- //Map Declarations
- const int mapWidth = 64;
- const int mapHeight = 24;
- char map[mapWidth][mapHeight];
- //Player Declarations
- const char startingTile = graphics.sand;
- int posX = 0;
- int posY = 0;
- char currentTile;
- //Functions
- void generateMap();
- void printMap();
- void gameLoop();
- void getInput();
- void playerMove(char dir);
- void updateMap(int nextX, int nextY);
- void plantSeed();
- int main()
- {
- generateMap();
- printMap();
- gameLoop();
- system("CLS");
- cout << "Game Over. Press Enter to Quit";
- cin.ignore();
- cin.get();
- return 0;
- }
- void generateMap()
- {
- //referenced this http://breinygames.blogspot.co.uk/2012/06/generating-terrain-using-perlin-noise.html
- //map seed
- srand(time(NULL));
- //flood map
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- map[x][y] = graphics.sand;
- }
- }
- //grass island
- for(int y = 1; y < mapHeight-1; y++)
- {
- for(int x = 1; x < mapWidth-1; x++)
- {
- map[x][y] = graphics.grass;
- }
- }
- do
- {
- posX = rand() % (mapWidth - 1) + 1;
- posY = rand() % (mapHeight - 1) + 1;
- }
- while(map[posX][posY] != startingTile);
- currentTile = startingTile;
- map[posX][posY] = graphics.player;
- }
- void printMap()
- {
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- cout << map[x][y];
- }
- cout << endl;
- }
- }
- void gameLoop()
- {
- while(running)
- {
- getInput();
- Sleep(1000/FPS);
- }
- }
- void getInput()
- {
- if(GetAsyncKeyState(VK_ESCAPE))
- {
- running = false;
- }
- 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_SPACE))
- {
- plantSeed();
- }
- }
- void playerMove(char dir)
- {
- int nextX = posX;
- int nextY = posY;
- if(dir == 'N')
- {
- nextY -= 1;
- }
- if(dir == 'E')
- {
- nextX += 1;
- }
- if(dir == 'S')
- {
- nextY += 1;
- }
- if(dir == 'W')
- {
- nextX -= 1;
- }
- if(nextX >= 0 && nextX < mapWidth &&
- nextY >= 0 && nextY < mapHeight)
- {
- if(map[nextX][nextY] == graphics.sand
- || map[nextX][nextY] == graphics.grass
- || map[nextX][nextY] == graphics.forest
- || map[nextX][nextY] == graphics.rock
- || map[nextX][nextY] == graphics.swamp)
- {
- map[posX][posY] = currentTile;
- currentTile = map[nextX][nextY];
- map[nextX][nextY] = graphics.player;
- updateMap(nextX, nextY);
- posX = nextX;
- posY = nextY;
- }
- }
- }
- void updateMap(int nextX, int nextY)
- {
- COORD coord;
- coord.X = posX;
- coord.Y = 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];
- //move cursor to end of map
- coord.X = 0;
- coord.Y = mapHeight;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- }
- void plantSeed()
- {
- if(currentTile == graphics.sand
- || currentTile == graphics.grass
- || currentTile == graphics.forest
- || currentTile == graphics.rock)
- {
- bool plantable = true;
- for(int y = -2; y <= 2; y++)
- {
- for(int x = -2; x <= 2; x++)
- {
- if(map[posX + x][posY + y] == graphics.seed /*|| map[x][y] == building*/)
- {
- plantable = false;
- }
- }
- }
- if(plantable == true)
- {
- currentTile = graphics.seed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment