Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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.

C#
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:

C#
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:

C#
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";
}
Posted
Updated 13-Aug-12 14:40pm
v2
Comments
[no name] 13-Aug-12 21:46pm    
My bad. It was buried. You might want to expand upon what your problem actually is.
[no name] 13-Aug-12 21:48pm    
What exactly happens when you run it through a debugger? Does it exit main? Does it read the keyboard?

Try setting a Sleep() in the while loop . Its better to set a while(1) and a case of break inside when GetAsyncKeyState(VK_ESCAPE)returns that Esc has been pushed . I am not so good at explaining but the problem is in the time one human needs to push a button . The cpu needs a lot less time to loop though the while loop

Please see this link
Question about the "GetAsyncKeyState"
 
Share this answer
 
Comments
Member 8437747 14-Aug-12 16:17pm    
Thanks for the help, although using sleep wasnt the problem (it was the way changed the Y variable) it was an usefull help by minimizing the speed of the object
I had to add a command that changes the initial variables XY so it werent the same every time I moved
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 22:26pm    
Not an answer. Please stop it.
—SA
All what you need to do is to add [
C++
& 0x8000
after for each call to GetAsyncKeyState.
 
Share this answer
 
v2
Comments
Dave Kreskowiak 20-Mar-17 0:27am    
You know this was answered FIVE YEARS ago, right?
EZCodeProject 20-Mar-17 16:21pm    
How do you know this? And if what you say is really true, then why Code Project does allow visitors/guests and registered users to answer very old and ancient questions then? It should say that this question is very old and ancient and block everyone from answering and post comments. Don't you think so? Even though this question is five years old, someone visited it :) Even though the OP doesn't see this page anymore, anybody else who has problem with GetAsyncKeyState, because they didn't write & 0x8000 after the call, can see this page and see my solution. The other two solutions don't say that this is what the programmer should do when using GetAsyncKeyState, so it doesn't matter how old this question is. Even if it will be 100 years old, someone (from the next generation) will benefit from my answer, if this page will exist then.
Dave Kreskowiak 20-Mar-17 16:33pm    
I didn't write the site so I can't comment on how it should work.

Look at the dates on the posts. What did you add to the discussion over an above what was already said? Nothing.
EZCodeProject 21-Mar-17 22:01pm    
If someone new to GetAsyncKeyState, that doesn't know that he/she shoud type the & 0x8000, won't visit this page and see my solution? If so, why the page exists then? You can tell the administrator to delete this page right now and free up more memory. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900