C语言用鼠标点击下五子棋,c语言写的鼠标操作的五子棋游戏,欢迎观赏!

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include "string.h"

#include

#include

#include

int seat[20][20];

int usr;

struct GotoXy_xy

{

int GotoXy_temp[100][3];

}GotoXy_xy;

void HideCursor(int n) /*隐藏光标*/

{

CONSOLE_CURSOR_INFO cursor_info={1,n};

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

}

void GotoXy(int x,int y) /*将光标移到(x,y)*/

{

HANDLE hout;

COORD coord;

coord.X=x;

coord.Y=y;

hout=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hout,coord);

}

void init()

{

int i,j;

usr=0;

GotoXy(0,0);

for(i=0;i<20;i++)

{

for(j=0;j<20;j++)

seat[i][j]=0;

}

printf("%2c",' ');

for(i=0;i<20;i++)

printf("%3.2d",i);

printf("\n");

for(i=0;i<20;i++)

{

printf("%3.2d",i);

for(j=0;j<20;j++)

printf("· ");

printf("\n");

}

GotoXy(68,7);

printf("欢迎使用!");

GotoXy(66,9);

printf("五子棋小游戏");

GotoXy(68,11);

printf("by 熊清熹");

GotoXy(68,13);

printf("2015.3.4");

}

int osheng()

{

int i,j;//判断横着的5个是否都相等

for(i=0;i<20;i++)

{

for(j=0;j<16;j++)

if(seat[i][j]==1&&seat[i][j+1]==1&&seat[i][j+2]==1&&seat[i][j+3]==1&&seat[i][j+4]==1)

return 1;

}

for(j=0;j<20;j++)//判断竖着的5个是否都相等

{

for(i=0;i<16;i++)

if(seat[i][j]==1&&seat[i+1][j]==1&&seat[i+2][j]==1&&seat[i+3][j]==1&&seat[i+4][j]==1)

return 1;

}

for(i=0;i<16;i++)//判断左斜5个

{

for(j=0;j<16;j++)

if(seat[i][j]==1&&seat[i+1][j+1]==1&&seat[i+2][j+2]==1&&seat[i+3][j+3]==1&&seat[i+4][j+4]==1)

return 1;

}

for(i=0;i<16;i++)//右斜5个

{

for(j=19;j>3;j--)

if(seat[i][j]==1&&seat[i+1][j-1]==1&&seat[i+2][j-2]==1&&seat[i+3][j-3]==1&&seat[i+4][j-4]==1)

return 1;

}

return 0;

}

int xsheng()

{

int i,j;//判断横着的5个是否都相等

for(i=0;i<20;i++)

{

for(j=0;j<16;j++)

if(seat[i][j]==2&&seat[i][j+1]==2&&seat[i][j+2]==2&&seat[i][j+3]==2&&seat[i][j+4]==2)

return 1;

}

for(j=0;j<20;j++)//判断竖着的5个是否都相等

{

for(i=0;i<16;i++)

if(seat[i][j]==2&&seat[i+1][j]==2&&seat[i+2][j]==2&&seat[i+3][j]==2&&seat[i+4][j]==2)

return 1;

}

for(i=0;i<16;i++)//判断左斜5个

{

for(j=0;j<16;j++)

if(seat[i][j]==2&&seat[i+1][j+1]==2&&seat[i+2][j+2]==2&&seat[i+3][j+3]==2&&seat[i+4][j+4]==2)

return 1;

}

for(i=0;i<16;i++)//右斜5个

{

for(j=19;j>3;j--)

if(seat[i][j]==2&&seat[i+1][j-1]==2&&seat[i+2][j-2]==2&&seat[i+3][j-3]==2&&seat[i+4][j-4]==2)

return 1;

}

return 0;

}

int he()

{

int i,j;

for( i=0;i<20;i++)

for( j=0;j<20;j++)

{

if(seat[i][j]==0)//当棋盘全部子都不是0时才能return 1,即棋盘已下满

return 0;

}

return 1;

}

void clear()

{

int i,j;

GotoXy(0,0);

for(i=0;i<61;i++)

{

for(j=0;j<20;j++)

printf(" ");

}

GotoXy(28,22);

printf(" ");

GotoXy(30,23);

printf(" ");

GotoXy(25,24);

printf(" ");

}

void main()

{

int M_x,M_y,P_x,P_y;

INPUT_RECORD inRec;

DWORD res;

HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄

init();

HideCursor(0);

while(1)

{

if(usr==0)

{

GotoXy(28,22);

printf("当前下棋方:黑方");

}

else if(usr==1)

{

GotoXy(28,22);

printf("当前下棋方:白方");

}

else if(usr==2)

{

GotoXy(28,22);

printf("GAME OVER! ");

GotoXy(25,24);

printf("再来一局 退出");

}

ReadConsoleInput(hInput, &inRec, 1, &res);

if(inRec.EventType == MOUSE_EVENT && inRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //鼠标左键

{

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),inRec.Event.MouseEvent.dwMousePosition);

M_x=inRec.Event.MouseEvent.dwMousePosition.X;

M_y=inRec.Event.MouseEvent.dwMousePosition.Y;

if(M_x>=3&&M_x<=60&&M_y>=1&&M_y<=20)

{

if(((M_x-3)%3)==0)

{

P_x=(M_x-3)/3;

P_y=M_y-1;

if(usr==0)

{

if(!seat[P_x][P_y])

{

usr=1;

printf("●");

seat[P_x][P_y]=1;

if(osheng())

{

GotoXy(30,23);

printf("黑棋胜!");

usr=2;

}

}

}

else if(usr==1)

{

if(!seat[P_x][P_y])

{

usr=0;

printf("○");

seat[P_x][P_y]=2;

if(xsheng())

{

GotoXy(30,23);

printf("白棋胜!");

usr=2;

}

}

}

if(he())

printf("平局!");

}

}

else if(M_x>=25&&M_x<=31&&M_y==24&&usr==2)

{

clear();

init();

}

else if(M_x>=37&&M_x<=39&&M_y==24&&usr==2)

{

GotoXy(25,9);

exit(1);

}

}

}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C 语言中实现鼠标点击五子棋游戏,需要使用图形界面库,比如 Windows 下的 Win32 API 或者 Linux 下的 GTK+ 等。这里以 Win32 API 为例,提供一个简单的实现思路: 1. 初始化窗口,包括标题、大小、背景颜色等,以及注册窗口类和消息处理函数。 2. 在窗口中创建一个棋盘,可以使用 Windows 下的 GDI 绘制。 3. 处理鼠标消息,根据鼠标位置判断落子点,并在棋盘上绘制相应的棋子。 4. 判断胜负,当有一方连成五子时,显示胜利信息并结束游戏。 以下是一个简单的示例代码,仅供参考: ```c #include <windows.h> #define BOARD_SIZE 15 #define CELL_SIZE 30 #define WINDOW_WIDTH (BOARD_SIZE * CELL_SIZE) #define WINDOW_HEIGHT (BOARD_SIZE * CELL_SIZE) LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = "MyWindowClass"; RegisterClass(&wc); HWND hwnd = CreateWindow("MyWindowClass", "Five in a Row", WS_OVERLAPPEDWINDOW, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static int board[BOARD_SIZE][BOARD_SIZE] = {0}; static int curPlayer = 1; switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制棋盘 for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { Rectangle(hdc, i * CELL_SIZE, j * CELL_SIZE, (i + 1) * CELL_SIZE, (j + 1) * CELL_SIZE); } } // 绘制棋子 for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == 1) { SelectObject(hdc, GetStockObject(BLACK_BRUSH)); Ellipse(hdc, i * CELL_SIZE + 3, j * CELL_SIZE + 3, (i + 1) * CELL_SIZE - 3, (j + 1) * CELL_SIZE - 3); } else if (board[i][j] == 2) { SelectObject(hdc, GetStockObject(WHITE_BRUSH)); Ellipse(hdc, i * CELL_SIZE + 3, j * CELL_SIZE + 3, (i + 1) * CELL_SIZE - 3, (j + 1) * CELL_SIZE - 3); } } } EndPaint(hwnd, &ps); break; } case WM_LBUTTONDOWN: { int x = LOWORD(lParam) / CELL_SIZE; int y = HIWORD(lParam) / CELL_SIZE; if (board[x][y] == 0) { board[x][y] = curPlayer; curPlayer = (curPlayer == 1) ? 2 : 1; InvalidateRect(hwnd, NULL, TRUE); // 刷新窗口 } // TODO: 判断胜负 break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ``` 注意:上述代码仅提供一个简单的实现思路,还有很多细节需要处理,比如判断胜负、禁手规则、AI 对手等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值