gpt4 book ai didi

c++ - 使用事件处理程序

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:40 25 4
gpt4 key购买 nike

有没有一种方法可以在 c/c++ 中使用鼠标作为事件处理程序,我正在制作蛇和梯子游戏(著名的棋盘游戏),并尝试使用基本的 borland c++ 编译器和名为 graphics 的头文件来制作它。 h,这是非常基本的并提供 640 X 480 res 的输出,所以我想知道是否有可能使用鼠标作为事件处理程序(我对此没有经验)来控制板上的 palyer 硬币。

最佳答案

我不确定您使用的是哪个版本的 graphics.h,但是有函数 getmouseygetmouseyclearmouseclick获取鼠标点击See this获取一些可能对您有用的文档。

看来您可以使用 registermousehandler 和回调函数来执行某种级别的基于事件的编程。这是我发送给您的文档中的示例。

// The click_handler will be called whenever the left mouse button is
// clicked. It checks copies the x,y coordinates of the click to
// see if the click was on a red pixel. If so, then the boolean
// variable red_clicked is set to true. Note that in general
// all handlers should be quick. If they need to do more than a little
// work, they should set a variable that will trigger the work going,
// and then return.
bool red_clicked = false;
void click_handler(int x, int y)
{
if (getpixel(x,y) == RED)
red_clicked = true;
}

// Call this function to draw an isosoles triangle with the given base and
// height. The triangle will be drawn just above the botton of the screen.
void triangle(int base, int height)
{
int maxx = getmaxx( );
int maxy = getmaxy( );
line(maxx/2 - base/2, maxy - 10, maxx/2 + base/2, maxy - 10);
line(maxx/2 - base/2, maxy - 10, maxx/2, maxy - 10 - height);
line(maxx/2 + base/2, maxy - 10, maxx/2, maxy - 10 - height);
}
void main(void)
{
int maxx, maxy; // Maximum x and y pixel coordinates
int divisor; // Divisor for the length of a triangle side
// Put the machine into graphics mode and get the maximum coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
// Register the function that handles a left mouse click
registermousehandler(WM_LBUTTONDOWN, click_handler);
// Draw a white circle with red inside and a radius of 50 pixels:
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
fillellipse(maxx/2, maxy/2, 50, 50);
// Print a message and wait for a red pixel to be double clicked:
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(20, 20, "Left click in RED to end.");
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
{
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
}
cout << "The mouse was clicked at: ";
cout << "x=" << mousex( );
cout << " y=" << mousey( ) << endl;
// Switch back to text mode:
closegraph( );
}

关于c++ - 使用事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/492366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com