【精选】从诸天进化页面跳转到三子棋游戏

一、项目环境
1.Visual Studio 2022
2.安装easyx图形库,可以调用头文件

#include

简单介绍一下easyx图形库

EasyX库是一个基于C语言的图形界面库,可以用于Windows操作系统下的图形界面应用程序开发。该库提供了一些易于使用的图形绘制函数和简单的事件处理功能,可以帮助开发者快速地创建各种图形应用程序,如游戏、图形编辑器等。
EasyX库提供了丰富的绘图功能,如直线、矩形、圆形、椭圆、多边形等基本形状的绘制,同时还支持图片、文字、音频等多种媒体资源的加载和处理。此外,EasyX库还支持鼠标、键盘等多种事件的处理,可以让用户与应用程序进行交互。
EasyX库的另一个特点是易于学习和使用。它提供了简单的API,使得初学者也可以轻松地入门,并且具有丰富的在线文档和示例程序,帮助开发者快速地学习和理解如何使用这个库。除此之外,EasyX库还可以和Visual
Studio等常见的集成开发环境进行配合使用,使得开发工作更加高效。
总之,EasyX库是一款简单易用、功能强大的图形界面库,适用于初学者和有一定编程基础的开发者,可以用于快速开发各种图形应用程序。

Easyx图形库

点击跳转到Easyx图形库下载官网

二、游戏规则介绍

此游戏点击YES会跳转到三子棋游戏程序中,其次无法点击NO,点击NO窗口会在屏幕中跳转使其无法捕捉到。

三、运行效果展示

功能1:字体会缓慢出现

功能2:鼠标移动到YES和NO上会从白色变成黄色

功能3:点击NO窗口会移动,使其无法捕捉,从而只能选中YES

功能4:点击YES会跳转到,三子棋程序页面

四、项目介绍

游戏模式可以作为开始页面的菜单,玩家只能选择YES从而开始游戏,并进行游戏页面的跳转,跳转到之前写的程序(井字棋)中。

五、项目源代码分享

#include#include#include#include #include #include #includeusing namespace std;/*-----------------------------------------序章----------------------------------------------------*/void Thefirstpass();void ClickNO();void startpage(){//初始页面大小initgraph(800, 200);//移动坐标setorigin(400, 100);setaspectratio(1, 1);int i = 50, y = 1, xx = -400, yy = 100;//设置红黑渐变背景for (int j = 0; j < 201; j++){setlinecolor(RGB(i, 0, 0));line(-400, yy, 400, yy);yy--;i++;}//设置字体抗锯齿LOGFONT fontStyle;gettextstyle(&fontStyle);fontStyle.lfQuality = ANTIALIASED_QUALITY;settextstyle(&fontStyle);//设置字体大小和字体settextstyle(32, 0, "宋体");//设置字体背景透明setbkmode(TRANSPARENT);//输入字体"想了解生命的奥秘吗?想真正地......活着吗?"const char* ptext[24] = {"想","了","解","生","命","的","奥","秘","吗","" />,"想","真","正","地",".",".",".",".",".",".","活","着","吗","?"};int x=-325;for (int i = 0; i < 24; i++){outtextxy(x, -60, ptext[i]);//ptext = ptext->next;x = x + 29;Sleep(230);}Sleep(20);//在指定区域绘制YES和NORECT kaishirect;kaishirect.left = -250;kaishirect.top = 25;kaishirect.right = -40;kaishirect.bottom = 75;const char* kaishistr = "YES";drawtext(kaishistr, &kaishirect, DT_CENTER);kaishirect.left = 40;kaishirect.top = 25;kaishirect.right = 250;kaishirect.bottom = 75;kaishistr = "NO";drawtext(kaishistr, &kaishirect, DT_CENTER);//用线圈出YES和NOsetlinecolor(RED);setlinestyle(PS_ENDCAP_ROUND, 4);line(-250, 25, -40, 25);line(-250, 75, -250, 25);line(-40, 75, -40, 25);line(-250, 75, -40, 75);line(40, 25, 250, 25);line(40, 75, 40, 25);line(40, 75, 250, 75);line(250, 75, 250, 25);}void startpagemouse(){int f = 0;RECT rect1; //RECT是一个矩形结构体,相当于保存了一个矩形的四条边的坐标HWND hwnd = NULL, oldhwnd = NULL; //两个窗口句柄ExMessage msg;int x, y, width, height; //用来保存窗口横纵坐标和宽度、高度的变量//GetWindowRect(hwnd, &rect); //获取指定窗口的位置while (1){hwnd = GetForegroundWindow(); //一个API函数,获取活动窗口的句柄if (MouseHit())//判断是否有鼠标信息{MOUSEMSG msg = GetMouseMsg();//接收鼠标信息 //YES的区域if (msg.x > 151 && msg.x < 359 && msg.y>124 && msg.y < 172){printf("坐标为(%d,%d)\n", msg.x, msg.y);RECT kaishirect;kaishirect.left = -250;kaishirect.top = 25;kaishirect.right = -40;kaishirect.bottom = 75;settextcolor(RGB(255, 242, 0));const char* kaishistr = "YES";drawtext(kaishistr, &kaishirect, DT_CENTER);}else{RECT kaishirect;kaishirect.left = -250;kaishirect.top = 25;kaishirect.right = -40;kaishirect.bottom = 75;settextcolor(WHITE);const char* kaishistr = "YES";drawtext(kaishistr, &kaishirect, DT_CENTER);}if (msg.x > 441 && msg.x < 648 && msg.y>126 && msg.y < 173){RECT kaishirect;kaishirect.left = 40;kaishirect.top = 25;kaishirect.right = 250;kaishirect.bottom = 75;settextcolor(RGB(255, 242, 0));const char* kaishistr = "NO";drawtext(kaishistr, &kaishirect, DT_CENTER);}else{RECT kaishirect;kaishirect.left = 40;kaishirect.top = 25;kaishirect.right = 250;kaishirect.bottom = 75;settextcolor(WHITE);const char* kaishistr = "NO";drawtext(kaishistr, &kaishirect, DT_CENTER);}//诡异移动if (msg.x > 441 && msg.x < 648 && msg.y>126 && msg.y < 173){if (hwnd != oldhwnd){GetWindowRect(hwnd, &rect1); //获取指定窗口的位置x = rect1.left;y = rect1.top;width = rect1.right - x;height = rect1.bottom - y;oldhwnd = hwnd; //把刚刚获取的窗口句柄保存起来。}MoveWindow(hwnd, rand()%x-10, rand()%y-10, width, height, TRUE);Sleep(5); //暂停5毫秒}switch (msg.uMsg){case WM_LBUTTONDOWN://YES的区域if (msg.x > 151 && msg.x < 359 && msg.y>124 && msg.y < 172){printf("坐标为(%d,%d)\n", msg.x, msg.y);closegraph();Thefirstpass();}if (msg.x > 441 && msg.x < 648 && msg.y>126 && msg.y < 173){closegraph();ClickNO();}}}}}void ClickNO(){initgraph(800, 200);const char* text[100] ={"本","来","想","设","置","强","制","关","机","但","想","了","想","还","是","算","了",".",".",".",".",".","."};int y = 10,x=10;for (int i = 0; i < 20; i++){outtextxy(x, y, text[i]);x = x + 15;Sleep(100);}//system("shutdown -s -t 60");//system("shutdown -a");//system("pause");getchar();closegraph();}/*-------------------------------------------------------------------------------------------------*//*-----------------------------------------第一关——井字棋----------------------------------------*/int jingziqi(int& kk){//画格子setlinestyle(PS_SOLID, 4);setlinecolor(BLACK);//横线line(-300, 298, 300, 298);line(-300, 100, 300, 100);line(-300, -100, 300, -100);line(-300, -298, 300, -298);//竖线line(-298, 300, -298, -300);line(-100, 300, -100, -300);line(100, 300, 100, -300);line(298, 300, 298, -300);//将格子初始化为0int a[3][3];for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){a[i][j] = 0;}}//鼠标消息——由于玩家先手标记并画圈圈//用1来标记圆圈//用2来标记叉叉int f = 2;while (1){if (MouseHit())//判断是否有鼠标信息{MOUSEMSG msg = GetMouseMsg();//接收鼠标信息 printf("坐标(%d,%d)\n", msg.x, msg.y);switch (msg.uMsg){case WM_LBUTTONDOWN://第一行if (msg.x > 1 && msg.x < 199 && msg.y>1 && msg.y < 199){a[0][0] = 1;setlinecolor(BLUE);fillcircle(-200, 200, 100);f = f + 1;continue;}if (msg.x > 200 && msg.x < 400 && msg.y>1 && msg.y < 199){a[0][1] = 1;setlinecolor(BLUE);fillcircle(0, 200, 100);f = f + 1;continue;}if (msg.x > 400 && msg.x < 600 && msg.y>1 && msg.y < 199){a[0][2] = 1;setlinecolor(BLUE);fillcircle(200, 200, 100);f = f + 1;continue;}//第二行if (msg.x > 1 && msg.x < 200 && msg.y>200 && msg.y < 400){a[1][0] = 1;setlinecolor(BLUE);fillcircle(-200, 0, 100);f = f + 1;continue;}if (msg.x > 200 && msg.x < 400 && msg.y>200 && msg.y < 400){a[1][1] = 1;setlinecolor(BLUE);fillcircle(0, 0, 100);f = f + 1;continue;}if (msg.x > 400 && msg.x < 600 && msg.y>200 && msg.y < 400){a[1][2] = 1;setlinecolor(BLUE);fillcircle(200, 0, 100);f = f + 1;continue;}//第三行if (msg.x > 1 && msg.x < 200 && msg.y>400 && msg.y < 600){a[2][0] = 1;setlinecolor(BLUE);fillcircle(-200, -200, 100);f = f + 1;continue;}if (msg.x > 200 && msg.x < 400 && msg.y>400 && msg.y < 600){a[2][1] = 1;setlinecolor(BLUE);fillcircle(0, -200, 100);f = f + 1;continue;}if (msg.x > 400 && msg.x < 600 && msg.y>400 && msg.y < 600){a[2][2] = 1;setlinecolor(BLUE);fillcircle(200, -200, 100);f = f + 1;continue;}}}//随机画叉叉int x1 = -300, y1 = 300, x2 = -100, y2 = 100;//撇int x3 = -100, y3 = 300, x4 = -300, y4 = 100;//捺int i, j;i = rand() % 3;//取0~2的随机数j = rand() % 3;//取0~2的随机数if (a[j][i] == 0 && f % 2 != 0 && a[j][i] != 1){a[j][i] = 2;//标记叉叉为2setlinecolor(RED);line(-300 + i * 200, 300 - j * 200, -100 + i * 200, 100 - j * 200);line(-100 + i * 200, 300 - j * 200, -300 + i * 200, 100 - j * 200);f++;continue;}//横向if (a[0][0] == 1 && a[0][1] == 1 && a[0][2] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, 200, 200);break;}if (a[1][0] == 1 && a[1][1] == 1 && a[1][2] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 0, 200, 0);break;}if (a[2][0] == 1 && a[2][1] == 1 && a[2][2] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, -200, 200, -200);break;}//竖向if (a[0][0] == 1 && a[1][0] == 1 && a[2][0] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, -200, -200);break;}if (a[0][1] == 1 && a[1][1] == 1 && a[2][1] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(0, 200, 0, -200);break;}if (a[0][2] == 1 && a[1][2] == 1 && a[2][2] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(200, 200, 200, -200);break;}//斜向if (a[0][0] == 1 && a[1][1] == 1 && a[2][2] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, 200, -200);break;}if (a[0][2] == 1 && a[1][1] == 1 && a[2][0] == 1){//cout << "xiaoa wins.";setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, -200, 200, 200);break;}//输了if (a[0][0] == 2 && a[0][1] == 2 && a[0][2] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, 200, 200);kk = 1;break;}if (a[1][0] == 2 && a[1][1] == 2 && a[1][2] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 0, 200, 0);kk = 1;break;}if (a[2][0] == 2 && a[2][1] == 2 && a[2][2] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, -200, 200, -200);kk = 1;break;}//竖向if (a[0][0] == 2 && a[1][0] == 2 && a[2][0] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, -200, -200);kk = 1;break;}if (a[0][1] == 2 && a[1][1] == 2 && a[2][1] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(0, 200, 0, -200);kk = 1;break;}if (a[0][2] == 2 && a[1][2] == 2 && a[2][2] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(200, 200, 200, -200);kk = 1;break;}//斜向if (a[0][0] == 2 && a[1][1] == 2 && a[2][2] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, 200, 200, -200);kk = 1;break;}if (a[0][2] == 2 && a[1][1] == 2 && a[2][0] == 2){setlinecolor(RGB(0, 240, 255));setlinestyle(PS_ENDCAP_ROUND, 8);line(-200, -200, 200, 200);kk = 1;break;}//平局现象if (a[0][0] != 0 && a[0][1] != 0 && a[0][2] != 0 && a[1][0] != 0 && a[1][1] != 0 && a[1][2] != 0 && a[2][0] != 0 && a[2][1] != 0 && a[2][2] != 0){kk = 2;break;}}return kk;}int Interactioninthecheckerswindow(int kk, int& kaishichongqi){setlinestyle(PS_ENDCAP_ROUND, 3);setlinecolor(BLACK);setfillcolor(WHITE);fillrectangle(-250, 80, 250, -80);LOGFONT fontstyle;gettextstyle(&fontstyle);fontstyle.lfQuality = ANTIALIASED_QUALITY;//抗锯齿setaspectratio(1, 1);RECT rect;rect.left = -250;rect.top = -250;rect.right = 250;rect.bottom = 250;settextstyle(&fontstyle);setbkmode(TRANSPARENT);//设置字体背景settextcolor(BLACK);//字体颜色settextstyle(45, 0, "微软雅黑");//设置字体样式//赢了if (kk == 0){drawtext(_T("恭喜玩家成功通过第1关(*^__^*)"), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//获取句柄HWND hnd = GetHWnd();//设置窗口标题SetWindowText(hnd, "井字棋");//弹出窗口int isok = MessageBox(hnd, "是否进入第2关", "游戏提示", MB_YESNO);if (isok == IDYES){printf("你点击了YES\n");}else if (isok == IDNO){printf("你点击了NO\n");closegraph();return 0;}}//输了if (kk == 1){drawtext(_T("你输了是否选择重新再来!"), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//获取句柄HWND hnd = GetHWnd();//设置窗口标题SetWindowText(hnd, "井字棋");//弹出窗口int isok = MessageBox(hnd, "是否重新开始游戏!", "游戏提示", MB_YESNO);if (isok == IDYES){printf("你点击了YES\n");kaishichongqi = 1;return kaishichongqi;}else if (isok == IDNO){printf("你点击了NO\n");closegraph();return 0;}}//平局现象if (kk == 2){drawtext(_T("平局是否选择重新开始!"), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//获取句柄HWND hnd = GetHWnd();//设置窗口标题SetWindowText(hnd, "井字棋");//弹出窗口int isok = MessageBox(hnd, "是否重新开始游戏!", "游戏提示", MB_YESNO);if (isok == IDYES){printf("你点击了YES\n");kaishichongqi = 1;return kaishichongqi;}else if (isok == IDNO){printf("你点击了NO\n");closegraph();return 0;}}}int Tickgamewindow(){int kk = 0, kaishichongqi = 0;initgraph(600, 600);setbkcolor(WHITE);cleardevice();setorigin(300, 300);setaspectratio(1, -1);jingziqi(kk);cout << kk;Interactioninthecheckerswindow(kk, kaishichongqi);cout << kaishichongqi << endl;if (kaishichongqi == 1){Tickgamewindow();}return kk;}void Thefirstpass(){Tickgamewindow();}/*-------------------------------------------------------------------------------------------------*/int main(){//开始页面startpage();//鼠标消息startpagemouse();//第一关——井字棋Thefirstpass();//第一关getchar();closegraph();return 0;}

六、总结与思考

通过窗口句柄的应用结合easyx库进行应用,实现了程序在窗口中移动,并与三子棋游戏结合使程序更具连贯性

开头的句子有些中二,如果有需要的小伙伴们可以自行修改哦