secret
anonymous / game.cpp
Created

  • Download Gist
game.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include "Header yo\game.h"
 
bool cPlayer::Move()
{
map[m_posY][m_posX] = T_FLOOR; //making sure the old player position isn't there anymore to avoid multiple players
m_Direction = _getch();
switch(m_Direction)
{
case 'a':
if (m_posX == 0) {} //moving commands and ifs preventing the player from going off-map
else {m_posX--;}
break;
 
case 'd':
if (m_posX == sizeX-1) {}
else {m_posX++;}
break;
case 'w':
if (m_posY == 0) {}
else {m_posY--;}
break;
 
case 's':
if (m_posY == sizeY-1) {}
else { m_posY++; }
break;
default:
cout << "what are you doing son";
break;
 
}
return true;
}
 
bool cPlayer::CheckPosition() //you probably won't guess that
{
{
 
for (int i = 0; i < sizeY; i++)
{
for (int j = 0; j < sizeX; j++) {
if (map[i][j] == T_PLAYER)
{
m_posY = i;
m_posX = j;
}
}
}
return true;
}
}
game.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/TO DO:
//-Generacja map, mniej sztywna na zasadzie że rząd nie musi być pełny (jaskiniowy)
//-Mniej debilny sposób na ramkę (Opcjonalne)
//-Kamienie blokujące przejście
//-Stworzyć klasę humanoida (;_;) - X
//v1.0.1
 
 
#ifndef GAME_H
#define GAME_H
 
#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
 
 
enum TYPE {T_PLAYER = '@', T_FLOOR = ' '};
const int sizeY = 6, sizeX = 6;
TYPE map[sizeY][sizeX]; //psst. you move on that
 
class cPlayer
{
int m_posY, m_posX; //coordinates of player, do you even brain
char m_Direction; //stores the input for Move()
 
public:
//cPlayer() {m_posY = 0, m_posX = 0}
bool CheckPosition(); //you probably won't guess that
bool Move(); //i highly doubt you know what that does
} ;
 
bool InitializeMap() //fills the map[][] table with T_EMPTY
{
for (int i = 0; i < sizeY; i++) //y
{
for (int j = 0; j < sizeX; j++) //x
{
map[i][j] = T_FLOOR;
}
}
map[1][1] = T_PLAYER; //places the player
return true;
}
 
bool DrawMap() //no way you know what that does
{
int k = 0;
 
for (int i = 0; i < (sizeX+2); i++) //draws top vertical border
{
cout << "-";
}
cout << "\n";
 
for (int i = 0; i < sizeY; i++)
{
for (int j = 0; j < sizeX; j++, k++)
{
switch(map[i][j])
{
 
case T_FLOOR:
if (j == 0) { cout << '|' << static_cast<char>(T_FLOOR); }
else if (j == (sizeX - 1)) { cout << static_cast<char>(T_FLOOR) << '|';}
else { cout << static_cast<char>(T_FLOOR); ;}
break;
 
case T_PLAYER:
if (j == 0) { cout << '|' << static_cast<char>(T_PLAYER); }
else if (j == (sizeX - 1)) { cout << static_cast<char>(T_PLAYER) << '|';}
else cout << static_cast<char>(T_PLAYER);
break;
 
default:
return false;
break;
 
}
 
}
if (k % sizeX == 0) { cout << "\n"; } //checks if a row is full
}
 
for (int i = 0; i < (sizeX+2); i++) //draws bottom vertical border >he doesn't make functions for every little thing
{
cout << "-";
}
cout << "\n";
 
 
return true;
}
 
#endif
main.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "Header yo\game.h"
#include <iostream>
 
using namespace std;
 
 
 
int main() {
cPlayer user;
InitializeMap();
for(;;) {
system("cls");
user.CheckPosition();
DrawMap();
user.Move();
}
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.