白衣非少年
我之前写过一个打地鼠的小游戏,就是用鼠标操作的。发给你参考下。代码很简单。主要函数我在下方说明了,但更多信息你必须自己百度看,一言两语说不完。SetConsoleCursorPosition函数可以定位光标位置,也就是文字内容显示的起点。ReadConsoleInput(HANDLE hConsoleInput,*INPUT_RECORD lpBuffer,DWORD nLength,DWORD lpNumberOfEventsRead)函数可以获取鼠标的操作信息。调用过上面函数后,lpBuffer由于是传址,所以其地址里的值就包含了鼠标信息。lpBuffer.EventType == MOUSE_EVENT //判断是鼠标事件lpBuffer.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED//判断是鼠标左键点击两个判断一起就是鼠标左键点击的判断,其他事件参数你可以自己网上找。#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <conio.h>#include <time.h>#include <string.h>#include <malloc.h>#define gSizek 30//区域大小宽度#define gSizeg 20//区域大小高度#define gBegin 3//活动区域起始行int main(){ int t=0,s0,s1,i,j,count=0,fen=0,row=0,clo=0; char gameA[gSizeg][gSizek+1],fSave[10]={0}; SetConsoleTitle("打地鼠"); HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄 INPUT_RECORD inRec; DWORD res; COORD p0; p0.X=0; p0.Y=0; srand(time(0)); s0= time(NULL); strcpy(gameA[0]," GAME"); strcpy(gameA[1],"未命中次数:0,计分:000000"); for(i=gBegin-1;i<gSizeg;i++) { for(j=0;j<gSizek+1;j++) { if(i>gBegin-1 && i<gSizeg-1 && j>0 && j<gSizek-1) gameA[i][j]=' '; else gameA[i][j]=4; if(j==gSizek) gameA[i][j]=0; } } for(i=0;i<gSizeg;i++) printf("%s\n",gameA[i]); while (1) { if(t>=3) { if(row>0 && clo>0) gameA[row][clo]=' '; row=rand()%(gSizeg-1); clo=rand()%(gSizek-1); s0= time(NULL); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p0); if(row<3) row=4; if(clo<1) clo=1; gameA[row][clo]=2; for(i=0;i<gSizeg;i++) { //gameA[i][gSizeg+1]=0; printf("%s\n",gameA[i]); } } if(count==3) { p0.X=10; p0.Y=8; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p0); printf("GAME OVER!"); break; } s1= time(NULL); t=s1-s0; ReadConsoleInput(hInput, &inRec, 1, &res); if (inRec.EventType == MOUSE_EVENT && inRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //鼠标左键 { if(inRec.Event.MouseEvent.dwMousePosition.X==clo && inRec.Event.MouseEvent.dwMousePosition.Y==row) { if(fen==0) fen=1; if(fen>999999) fen=999999; else fen=fen*2; sprintf(fSave,"%06d",fen); gameA[1][18]=0; strcat(gameA[1],fSave); } else { count++; fen=0; gameA[1][11]=count+'0'; } t=4; } } while(1); return 0;}