【问题标题】:Mouse stops working after changing function更改功能后鼠标停止工作
【发布时间】: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


【解决方案1】:

您需要在控制台窗口上将游戏板定义为可点击的 x y 区域,并在循环中处理每个板部分的 x 和 y 坐标的鼠标事件。所以每个板子部分都需要一个 x y ,如果板子有 8x8 个部分,则需要在鼠标事件循环中处理很多 x y 区域。

X 和 Y 区域可以这样定义:

      if ((mouseX>1) && (mouseX<8) && (mouseY>1) && (mouseY<4))
      {

      }

如果是 3x3 tic tac toe 控制台棋盘游戏,则需要 3x3 = 9 个单独的 x 和 y 区域以及 9 个if(mouse... 语句。

简单的鼠标点击事件代码:

#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;


int main()
{ 
    cout<<"click anywhere in console window to write - hello world -\n\n\n\n\n\n\n\n\n\n\n\n\n"
    "Press Ctrl+C to Exit"; 

        HANDLE hout= GetStdHandle(STD_OUTPUT_HANDLE);
        HANDLE hin = GetStdHandle(STD_INPUT_HANDLE); 
        INPUT_RECORD InputRecord; 
        DWORD Events; 
        COORD coord;
        CONSOLE_CURSOR_INFO cci;
        cci.dwSize = 25;
        cci.bVisible = FALSE;
        SetConsoleCursorInfo(hout, &cci); 
        SetConsoleMode(hin, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT); 


    while(1)
    { 

        ReadConsoleInput(hin, &InputRecord, 1, &Events); 

        if(InputRecord.EventType == MOUSE_EVENT) 
        {
            if(InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) 
            { 
                coord.X = InputRecord.Event.MouseEvent.dwMousePosition.X; 
                coord.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y; 
                SetConsoleCursorPosition(hout,coord);
                SetConsoleTextAttribute(hout,rand() %7+9);

                cout<<"Hello world" ; 

            } 
        }
        FlushConsoleInputBuffer(hin);
    }
    return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多