基于EasyX的井字棋游戏制作
从零开始的游戏制作:
1.下载vs
选择2020免费版本
下载连接:https://visualstudio.microsoft.com/zh-hans/vs/
2.安装EasyX
安装下载连接:https://easyx.cn/easyx-20230723
选择版本
3.查看安装版本
安装vs时选择空项目,安装EasyX时选择合适的包。
右键源文件新建,新建项。自己命名文件名称。
测试代码:
#include <graphics.h>#include <iostream>int main() {printf("%ws", GetEasyXVer());return 0;
}
运行结果:
井字棋实现
#include <graphics.h>
#include <iostream>// 棋盘数据
char board_data[3][3] = {{'-', '-', '-'},{'-', '-', '-'},{'-', '-', '-'},
};// 实时数据
char current_piece = 'o';// 获胜
bool checkwin(char c) {if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c) {return true;}if (board_data[1][0] == c && board_data[1][1] == c && board_data[1][2] == c) {return true;}if (board_data[2][0] == c && board_data[2][1] == c && board_data[2][2] == c) {return true;}if (board_data[0][0] == c && board_data[1][0] == c && board_data[2][0] == c) {return true;}if (board_data[0][1] == c && board_data[1][1] == c && board_data[2][1] == c) {return true;}if (board_data[0][2] == c && board_data[1][2] == c && board_data[2][2] == c) {return true;}if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c) {return true;}if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c) {return true;}return false;}// 胜利条件判断后,棋盘是否还有空位置
bool checkdraw() {for (size_t i = 0; i < 3; i++) {for (size_t j = 0; j < 3; j++) {if (board_data[i][j] == '-') {return false;}}}return true;
}// 棋盘
void Drawboard() {line(0, 200, 600, 200);line(0, 400, 600, 400);line(200, 0, 200, 600);line(400, 0, 400, 600);
}
//棋子
void Drawpiece() {for (size_t i = 0; i < 3; i++) {for (size_t j = 0; j < 3; j++) {switch (board_data[i][j]){case 'o':circle(200 * j + 100, 200 * i + 100, 100);break;case 'x':line(200 * j, 200 * i, 200 * (j + 1), 200 * (i + 1));line(200 * (j + 1), 200 * i, 200 * j, 200 * (i + 1));break;}}}
}
// 提示
void Drawtiptext() {static TCHAR str[64];_stprintf_s(str, _T("当前棋子类型:%c"), current_piece);settextcolor(RGB(255, 175, 45));outtextxy(0, 0, str);
}int main() {// 初始化窗口initgraph(600, 600);int x = 300;int y = 300;// 初始化批量绘图 双缓冲BeginBatchDraw();bool running = true;ExMessage msg;while (running) {// 获取时间DWORD start_time = GetTickCount();// 绘制圆形 坐标 半径//solidcircle(300, 300, 100);//// 消息队列 获取输入事件//ExMessage msg;while (peekmessage(&msg)) {// 事件 若为鼠标坐标//if (msg.message == WM_MOUSEMOVE) {// // 赋值给外面xy// x = msg.x;// y = msg.y;//}// 鼠标左键触发事件if (msg.message == WM_LBUTTONDOWN) {// 计算点击位置属于哪个格子int x = msg.x;int y = msg.y;int index_x = x / 200;int index_y = y / 200;if (board_data[index_y][index_x] == '-') {board_data[index_y][index_x] = current_piece;// 切换与本次不同的棋子类型if (current_piece == 'o') {current_piece = 'x';}else {current_piece = 'o';}}}// 获取绘制的时间DWORD end_time = GetTickCount();DWORD del_time = end_time - start_time;// 计算绘制画面的停留时间if (del_time < (1000 / 60)) {Sleep((1000 / 60) - del_time);}}cleardevice();//绘制圆形 坐标 半径//solidcircle(x, y, 100);Drawboard();Drawpiece();Drawtiptext();// 缓冲图一次性加载 双缓冲FlushBatchDraw();if (checkwin('x')) {MessageBox(GetHWnd(), _T("x 玩家获胜"), _T("游戏结束"), MB_OK);running = false;}else if (checkwin('o')) {MessageBox(GetHWnd(), _T("o 玩家获胜"), _T("游戏结束"), MB_OK);running = false;}else if (checkdraw()) {MessageBox(GetHWnd(), _T("平局"), _T("游戏结束"), MB_OK);running = false;}}// 批量绘图关闭 双缓冲 必要EndBatchDraw();return 0;
}