【发布时间】:2014-05-29 18:41:57
【问题描述】:
我正在 C++ 上制作控制台棋盘游戏,并且我已经能够使鼠标在第一个功能(菜单之一)中工作,但是,当我使用 getmove 功能并需要单击房屋时,它根本不起作用..有人可以帮忙吗? 这是有鼠标的类。
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <windows.h>
#include <time.h>
#include <stdio.h>
using namespace std;
void Game();
int Chu();
int rato(int &row, int &col)
{
HANDLE hIn;
hIn = GetStdHandle(STD_INPUT_HANDLE);
bool Continue = TRUE;
INPUT_RECORD InRec;
DWORD NumRead;
HWND window = GetConsoleWindow();
POINT cursorPos;
RECT wpos;
int x = 0;
int y = 0;
//cout << hIn << endl;
FlushConsoleInputBuffer(hIn);
while (Continue) {
ReadConsoleInput(hIn, &InRec, 1, &NumRead);
switch (InRec.EventType)
{
case MOUSE_EVENT: if (GetAsyncKeyState(VK_LBUTTON))
{
cout << "RATO"<<endl;
GetWindowRect(window, &wpos);
GetCursorPos(&cursorPos);
cursorPos.x -= wpos.left;
cursorPos.y -= wpos.top;
x = (cursorPos.x - 5) / 16;
y = (cursorPos.y - 25) / 24;
cout << x << " " << y << endl;
row = x; col = y;
return row;
}
else if (GetAsyncKeyState(VK_RBUTTON)){
GetWindowRect(window, &wpos);
GetCursorPos(&cursorPos);
cursorPos.x -= wpos.left;
cursorPos.y -= wpos.top;
x = (cursorPos.x - 5) / 16;
y = (cursorPos.y - 25) / 24;
cout << x << " " << y << endl;
row = x; col = y;
return row;
}
break;
}
}
}
int main()
{
cout << "\n\n\n click on the stars" << endl;
cout << " \n\n\n *******" << endl;
int z = 0;
int x = 0;
int y = 0;
int xo = 0;
switch (rato(x,y))
{
case 1: Game(); break;
case 2: Game(); break;
case 3: Game(); break;
case 4: rato(x, y); break;
case 5: rato(x, y); break;
case 6: Game(); break;
case 7: Game(); break;
case 8: Game(); break;
case 9: Game(); break;
default: cout << "click again"; break;
}
return 0;
}
void Game()
{
int x = 0;
int y = 0;
int i = 0;
cout << "GAME" << endl;
do{
i++;
rato(x, y);
} while (i <= 2);
Chu();
}
int Chu()
{
int x = 0;
int y = 0;
int a = 0;
int b = 0;
int xo = 0;
int yo = 0;
cout << "\ click on the stars" << endl;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
do{
xo = rato(x, y);
if (0 <= xo && xo <= 5) { a = 1;}
else cout << "CLICK AGAIN" << endl;
} while (xo!=0);
cout << a;
return a;
system("PAUSE");
}
【问题讨论】:
-
请发布一个重现问题的minimal代码示例。
-
在你说我做了一个例子之后,(令我惊讶的是)它工作得很好,所以我猜问题真的在游戏代码内部..
-
在此处发布您的示例。 :)
-
这是问题的新代码 :)
-
这肯定和真代码上的菜单有关,因为没有一种情况会打开鼠标..
标签: c++ mouse console-application