Skip to content

Instantly share code, notes, and snippets.

/Main.cpp Secret

Created July 22, 2015 19:25
Show Gist options
  • Save anonymous/9a5b70b6b5214659e33f to your computer and use it in GitHub Desktop.
Save anonymous/9a5b70b6b5214659e33f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <array>
#include <algorithm>
#include <chrono>
#include <string>
#include <sstream>
#include <thread>
#include <conio.h>
class Map
{
public:
Map()
{
Reset();
}
void Draw()
{
std::stringstream frame;
for (int y = 0; y < 16; ++y)
{
for (int x = 0; x < 16; ++x)
{
frame << map[x][y];
}
frame << "\n";
}
std::cout << frame.str();
}
char GetBlock(int x, int y)
{
return map[x][y];
}
void SetBlock(char character, int x, int y)
{
map[x][y] = character;
}
void Reset()
{
for (int x = 0; x < 16; x++)
{
map[x].fill(' ');
}
}
private:
std::array<std::array<char, 16>, 16> map;
};
class Player
{
public:
int GetPositionX()
{
return position[0];
}
void SetPositionX(int newPosition)
{
position[0] = newPosition;
}
int GetPositionY()
{
return position[1];
}
void SetPositionY(int newPosition)
{
position[1] = newPosition;
}
int GetHealth()
{
return health;
}
void SetHealth(int newHealth)
{
health = newHealth;
}
int GetMetadata()
{
return metadata;
}
void SetMetadata(int newMetadata)
{
metadata = newMetadata;
}
private:
int position[2] = {0, 2};
int health = 100;
int metadata = 0;
};
bool HandlePlayerMovements(Map& map, Player& player)
{
if (kbhit())
{
char key = getch();
if (key == 'w')
{
if (player.GetPositionY() > 2)
{
if (map.GetBlock(player.GetPositionX(), player.GetPositionY() + 1) != ' ')
{
if (map.GetBlock(player.GetPositionX(), (player.GetPositionY() - 1)) == ' ' && map.GetBlock(player.GetPositionX(), (player.GetPositionY() - 2)) == ' ')
{
player.SetPositionY((player.GetPositionY() - 2));
}
}
}
}
if (key == 'a')
{
if (player.GetPositionX() > 0)
{
player.SetPositionX((player.GetPositionX() - 1));
}
}
if (key == 's')
{
if (player.GetPositionY() < 15)
{
player.SetPositionY((player.GetPositionY() + 1));
}
}
if (key == 'd')
{
if (player.GetPositionX() < 15)
{
player.SetPositionX((player.GetPositionX() + 1));
}
}
if (key == 'q') // Sneaking in that close key
{
return true;
}
}
return false;
}
void Gravity(Map& map, Player& player)
{
if (player.GetPositionY() < 15)
{
if (map.GetBlock(player.GetPositionX(), (player.GetPositionY() + 1)) == ' ')
{
player.SetPositionY(player.GetPositionY() + 1);
}
}
}
void Loop()
{
while (true)
{
static Map map;
static Player player;
static long int refreshRate = 100;
static int frameCount = 0;
if (frameCount % 2 == 0)
{
Gravity(map, player);
}
++frameCount;
std::this_thread::sleep_for(std::chrono::milliseconds(refreshRate));
system("cls");
map.Reset();
map.SetBlock('X', player.GetPositionX(), player.GetPositionY());
map.SetBlock('=', 4, 13); map.SetBlock('=', 5, 13); map.SetBlock('=', 6, 13); map.SetBlock('=', 7, 13); map.SetBlock('=', 8, 13); map.SetBlock('=', 10, 14); map.SetBlock('=', 11, 14);
// Look at that fancy hardcoded map ^
map.Draw();
std::cout << "^^^^^^^^^^^^^^^^^^^" << std::endl; // Photo-realistic spikes, I know.
std::cout << "\nUse WASD for movement\nPress Q to quit" << std::endl;
std::cout << "\nX: " << player.GetPositionX() << " " << "Y: " << (-(player.GetPositionY()) + 15) << std::endl;
std::cout << "\nWatch out for the spikes below!" << std::endl;
if (HandlePlayerMovements(map, player))
{
break;
}
if (player.GetPositionY() > 14)
{
std::cout << "You fell to your death. Game over!" << std::endl;
break;
}
}
}
int main()
{
Loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment