更改功能后鼠标停止工作

时间:2014-05-29 18:41:57

标签: c++ mouse console-application

我正在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");


}

1 个答案:

答案 0 :(得分:0)

您需要在控制台窗口中将游戏板定义为可点击的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;

}