C ++初学者,GetAsyncKeyState麻烦,这么认为 [英] Beginner in C++, Trouble with GetAsyncKeyState, think so
问题描述
大家好。自从我完成了我的编程课程(因为我太大了,无法在大学学习),我开始自己进行实验和学习,C ++引起了我的注意。
我开始了几个月以前和我一如既往地开始尝试。所以我尝试创建一个简单的板,由'#'表示的墙和由'+'表示的字符限制。
我开始创建电路板,二维数组板[5] [20]并做一些用于创建电路板的电路板,其中2表示墙,a 1代表玩家,0代表emty空间。
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;
}
}
}
}
现在这是主循环游戏循环,我使用windows.h - GetAsyncKeyState
这样,现在因为我没有任何导师或教授指导我我不知道这个功能的分配,也许我以不支持的方式使用它。
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;
}
}
创建电路板,与播放器一起显示。这个问题只允许我做1次移动然后它没有显示更改或它没有做到。
这是读取板的代码:< br $> b $ b
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";
}
推荐答案
尝试在while循环中设置Sleep()。当GetAsyncKeyState(VK_ESCAPE)返回Esc已被推送时,最好设置while(1)和break内部的情况。我不太擅长解释,但问题出在一个人需要按下按钮的时候。通过while循环,cpu需要更少的时间循环
请看这个链接
关于GetAsyncKeyState的问题
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"
我必须添加一个更改初始变量XY的命令,因此它不是每次移动时都一样
I had to add a command that changes the initial variables XY so it werent the same every time I moved
你需要做的就是添加[
& 0x8000
。
这篇关于C ++初学者,GetAsyncKeyState麻烦,这么认为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!