Hello there folks. Since I finished my programming course (Cause Im too young to study at university) I started experimenting and learning by myself, C++ caught my attention.
I started a few months ago and as always I started experimenting. So I tried to create a simple board limited by walls represented by '#' and a character represented by a '+'.
I started by creating the board, a bidimensional array int board[5][20] and doing some for loops that create the board with 2 representing a wall, a 1 representing the player, a 0 representing an emty space.
for(int i=0; i<=4; i++)
{
for(int x=0; x<=19; x++)
{
if(i == 0 || i == 4)
{
board[i][x] = 2;
}
else
{
if(x == 0 || x == 19)
{
board[i][x] = 2;
}
else
{
board[i][x] = 0;
}
}
}
}
Now this is the main loop with the game loop, Im using windows.h - GetAsyncKeyState
like this, now since I havent any tutor or professor to instruct me I dont know allot of this function, and maybe Im using it in an unpropper way.
int main()
{
Board table(3,10);
showGame(table);
while(!GetAsyncKeyState(VK_ESCAPE))
{
showGame(table);
if(GetAsyncKeyState(VK_UP))
{
table.moveUp();
}
if(GetAsyncKeyState(VK_DOWN))
{
table.moveDown();
}
if(GetAsyncKeyState(VK_RIGHT))
{
table.moveRight();
}
if(GetAsyncKeyState(VK_LEFT))
{
table.moveLeft();
}
}
return 0;
}
Here are the functions that move the player:
void Board::moveDown()
{
int p1 = Y + 1;
int move = board[Y][X] + board[p1][X];
if(move == 1)
{
board[p1][X] = player;
board[Y][X] = empty;
}
}
void Board::moveUp()
{
int p2 = Y - 1;
int move = board[Y][X] + board[p2][X];
if(move == 1)
{
board[p2][X] = player;
board[Y][X] = empty;
}
}
void Board::moveRight()
{
int p3 = X + 1;
int move = board[Y][X] + board[Y][p3];
if(move == 1)
{
board[Y][p3] = player;
board[Y][X] = empty;
}
}
void Board::moveLeft()
{
int p4 = X - 1;
int move = board[Y][X] + board[Y][p4];
if(move == 1)
{
board[Y][p4] = player;
board[Y][X] = empty;
}
}
It creates the board, displays it with the player. The problem it only allows me to do 1 move then it either doesnt display the change or it doesnt do it.
And this is the code that reads the board:
system("CLS");
for(int i=0;i<=4; i++)
{
for(int x=0;x<=19; x++)
{
if(cb.board[i][x] == cb.valueWall())
{
cout << "#";
}
if(cb.board[i][x] == cb.valueEmpty())
{
cout << " ";
}
if(cb.board[i][x] == cb.valuePlayer())
{
cout << "+";
}
}
cout << "\n";
}