这篇文章将为大家详细讲解有关C++如何实现鼠标控制的黑框象棋,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本文实例为大家分享了C++实现鼠标控制的黑框象棋的具体代码,
有颜色标注出 红方和绿方
可以用鼠标控制
直接调用用Windows自带的颜色API
用到了 颜色头文件.h
代码.
//consolecolor.hpp这是着色的实现头文件 #pragma once #include//调用win32API函数 #include //调用flush成员函数,首先刷新缓冲区 namespace color//命名空间为color,防止名字冲突 { //颜色常量定义,分为掩码,前景色,背景色 static const WORD bgMask(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); static const WORD fgMask(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); static const WORD fgBlack(0); static const WORD fgLoRed(FOREGROUND_RED); static const WORD fgLoGreen(FOREGROUND_GREEN); static const WORD fgLoBlue(FOREGROUND_BLUE); static const WORD fgLoCyan(fgLoGreen | fgLoBlue); static const WORD fgLoMagenta(fgLoRed | fgLoBlue); static const WORD fgLoYellow(fgLoRed | fgLoGreen); static const WORD fgLoWhite(fgLoRed | fgLoGreen | fgLoBlue); static const WORD fgGray(fgBlack | FOREGROUND_INTENSITY); static const WORD fgHiWhite(fgLoWhite | FOREGROUND_INTENSITY); static const WORD fgHiBlue(fgLoBlue | FOREGROUND_INTENSITY); static const WORD fgHiGreen(fgLoGreen | FOREGROUND_INTENSITY); static const WORD fgHiRed(fgLoRed | FOREGROUND_INTENSITY); static const WORD fgHiCyan(fgLoCyan | FOREGROUND_INTENSITY); static const WORD fgHiMagenta(fgLoMagenta | FOREGROUND_INTENSITY); static const WORD fgHiYellow(fgLoYellow | FOREGROUND_INTENSITY); static const WORD bgBlack(0); static const WORD bgLoRed(BACKGROUND_RED); static const WORD bgLoGreen(BACKGROUND_GREEN); static const WORD bgLoBlue(BACKGROUND_BLUE); static const WORD bgLoCyan(bgLoGreen | bgLoBlue); static const WORD bgLoMagenta(bgLoRed | bgLoBlue); static const WORD bgLoYellow(bgLoRed | bgLoGreen); static const WORD bgLoWhite(bgLoRed | bgLoGreen | bgLoBlue); static const WORD bgGray(bgBlack | BACKGROUND_INTENSITY); static const WORD bgHiWhite(bgLoWhite | BACKGROUND_INTENSITY); static const WORD bgHiBlue(bgLoBlue | BACKGROUND_INTENSITY); static const WORD bgHiGreen(bgLoGreen | BACKGROUND_INTENSITY); static const WORD bgHiRed(bgLoRed | BACKGROUND_INTENSITY); static const WORD bgHiCyan(bgLoCyan | BACKGROUND_INTENSITY); static const WORD bgHiMagenta(bgLoMagenta | BACKGROUND_INTENSITY); static const WORD bgHiYellow(bgLoYellow | BACKGROUND_INTENSITY); //设置颜色的实现函数,主要是调用win32的API和一些位运算。 void setConsoleColor(WORD mask, WORD c) { static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if(hStdout==NULL)return; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hStdout, &csbi); csbi.wAttributes &= ~mask; csbi.wAttributes |= c; SetConsoleTextAttribute(hStdout, csbi.wAttributes); } //用到模板是为了通用,不仅cout,cerr可以染色,wcout,wcerr等也可以着色,自己定义的流只要满足模板都可以。 template inline std::basic_ostream & fg_white(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgLoWhite); return os; } template inline std::basic_ostream & fg_blue(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiBlue); return os; } template inline std::basic_ostream & fg_green(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiGreen); return os; } template inline std::basic_ostream & fg_red(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiRed); return os; } template inline std::basic_ostream & fg_cyan(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiCyan); return os; } template inline std::basic_ostream & fg_magenta(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiMagenta); return os; } template inline std::basic_ostream & fg_yellow(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgHiYellow); return os; } template inline std::basic_ostream & fg_black(std::basic_ostream & os) { os.flush(); setConsoleColor(fgMask, fgBlack); return os; } template inline std::basic_ostream & bg_white(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgLoWhite); return os; } template inline std::basic_ostream & bg_blue(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiBlue); return os; } template inline std::basic_ostream & bg_green(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiGreen); return os; } template inline std::basic_ostream & bg_red(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiRed); return os; } template inline std::basic_ostream & bg_cyan(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiCyan); return os; } template inline std::basic_ostream & bg_magenta(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiMagenta); return os; } template inline std::basic_ostream & bg_yellow(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgHiYellow); return os; } template inline std::basic_ostream & bg_black(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgBlack); return os; } //清空屏幕,还原本色 template inline std::basic_ostream & clr(std::basic_ostream & os) { os.flush(); setConsoleColor(bgMask, bgBlack); setConsoleColor(fgMask, fgLoWhite); system("cls"); return os; } }
直接调用windows自带的鼠标控制函数
注意,如果直接使用清屏函数 system(“clr”)会导致鼠标输入阻塞所以要自己重新编译一个清屏函数
鼠标代码.
void MyCls(HANDLE hConsole) { COORD coordScreen={0,0};//设置清屏后光标返回的屏幕左上角坐标 BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi;//保存缓冲区信息 DWORD dwConSize;//当前缓冲区可容纳的字符数 bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息 PERR(bSuccess,"GetConsoleScreenBufferInfo"); dwCOnSize=csbi.dwSize.X * csbi.dwSize.Y;//缓冲区容纳字符数目 //用空格填充缓冲区 bSuccess=FillConsoleOutputCharacter(hConsole,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten); PERR(bSuccess,"FillConsoleOutputCharacter"); bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息 PERR(bSuccess,"ConsoleScreenBufferInfo"); //填充缓冲区属性 bSuccess=FillConsoleOutputAttribute(hConsole,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten); PERR(bSuccess,"FillConsoleOutputAttribute"); //光标返回屏幕左上角坐标 bSuccess=SetConsoleCursorPosition(hConsole,coordScreen); PERR(bSuccess,"SetConsoleCursorPosition"); return; }
自定义清屏函数.
void clrscr(void) { HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE); MyCls(hStdOut); return; }
定义了一个棋盘chessboard,和一个棋子控制的类chess,对不同的棋子有自己的类,定义有特定的ID和每个棋子特定的走法规则。还有对棋盘控制的move()函数。
总体代码.
#include#include #include #include #include #include"颜色头文件.h" #include #include #include #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n", __FILE__, GetLastError(), api, __LINE__);} void MyCls(HANDLE) ; void clrscr(void) { HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE); MyCls(hStdOut); return; } void MyCls(HANDLE hConsole) { COORD coordScreen={0,0};//设置清屏后光标返回的屏幕左上角坐标 BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi;//保存缓冲区信息 DWORD dwConSize;//当前缓冲区可容纳的字符数 bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息 PERR(bSuccess,"GetConsoleScreenBufferInfo"); dwCOnSize=csbi.dwSize.X * csbi.dwSize.Y;//缓冲区容纳字符数目 //用空格填充缓冲区 bSuccess=FillConsoleOutputCharacter(hConsole,(TCHAR)&#39; &#39;,dwConSize,coordScreen,&cCharsWritten); PERR(bSuccess,"FillConsoleOutputCharacter"); bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//获得缓冲区信息 PERR(bSuccess,"ConsoleScreenBufferInfo"); //填充缓冲区属性 bSuccess=FillConsoleOutputAttribute(hConsole,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten); PERR(bSuccess,"FillConsoleOutputAttribute"); //光标返回屏幕左上角坐标 bSuccess=SetConsoleCursorPosition(hConsole,coordScreen); PERR(bSuccess,"SetConsoleCursorPosition"); return; } using namespace color; using namespace std;//存储结构:chess类是基类,派生类是各种棋子,在chessboard类中用chess的指针调用各个棋子 class chessboard; //鼠标控制参数 // bool turn=0; class chess{ private: int id;//等级 public: chess(int i):id(i){} int get(){return id;} virtual bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy)=0; virtual ~chess(){};//虚析构 }; class chessboard{ private: chess *c[10][9];//用指针调用各个棋子,实现多态 char chessword[15][4]={"帅","相","炮","士","車","馬","兵","","卒","馬","車","士","炮","象","将"};//名字 public: chessboard(){memset(c, NULL, sizeof(c));};//把指针初始化为零指针 void init(); chess* get(int x,int y){return c[x][y];} int getid(int x,int y){ if(c[x][y]!=NULL) return c[x][y]->get();return 0;} void show(); void play(); bool move(int startx,int starty,int aimx,int aimy); ~chessboard();//析构函数 static bool end;//判断结束 static int player; }; bool chessboard::end=true; int chessboard::player=-1; bool chessboard::move(int startx,int starty,int aimx,int aimy){ if(startx>=0&&startx<10&&starty>=0&&starty<9//初步判断传入的点是否符合规则 &&aimx>=0&&aimx<10&&aimy>=0&&aimy<9 &&getid(startx,starty)&&getid(startx,starty)*player>0 &&c[startx][starty]->judge_move(*this,startx,starty,aimx,aimy)){ if(c[aimx][aimy]!=NULL) delete c[aimx][aimy];//吃子 c[aimx][aimy]=c[startx][starty]; c[startx][starty]=NULL; player*=-1; if(turn==0) turn=1; else turn=0; return true; } //cout<<"走法错误,不符合规则"< 0)||(startx/5==1&&sid<0)) return true; return false; } return false; } }; class general : public chess{//帅(将)的实现 public: general(int c) : chess((c==0?-7:7)){} bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){ int tempx=aimx-startx,tempy=aimy-starty; int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy); if(sid*aid<=0&&tempy*tempy+tempx*tempx==1&&aimx%7>=0&&aimx%7<=2&&aimy>=3&&aimy<=5) return true; return false; } ~general(){chessboard::end=false;} }; class elephant : public chess{//象(相)的实现 public: elephant(int c) : chess((c==0?-6:6)){} bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){ int tempx=aimx-startx,tempy=aimy-starty; int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy); if(sid*aid<=0&&tempy*tempy+tempx*tempx==8&&startx/5==aimx/5&&!cb.get(startx+tempx/2,starty+tempy/2)) return true; return false; } }; class cannon : public chess{//炮的实现 public: cannon(int c) : chess((c==0?-5:5)){} bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){ int tempx=aimx-startx,tempy=aimy-starty; int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy); if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){ int tot=0; if(tempx!=0){ int sign=tempx>0?1:-1; for(int i=1;i 0?1:-1; for(int i=1;i =0&&aimx%7<=2&&aimy>=3&&aimy<=5) return true; return false; } }; class rook : public chess{//车的实现 public: rook(int c) : chess((c==0?-3:3)){} bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){ int tempx=aimx-startx,tempy=aimy-starty; int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy); if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){ if(tempx!=0){ int sign=tempx>0?1:-1; for(int i=1;i 0?1:-1; for(int i=1;i get()>0) cout< get()+7]; else cout< get()+7]; } else if((i==1&&j==4)||(i==8&&j==4)) cout< get()>0) cout< get()+7]; else cout< get()+7]; } else if((i==1&&j==4)||(i==8&&j==4)) cout< init(); this->show(); do{ int startx,starty,aimx,aimy; int sid,aid; do{ sid=aid=0; if(turn==0) { clrscr(); this->show(); cout< show(); cout< show(); ReadConsoleInput(hIn, &mouseRec, 1, &res); if(mouseRec.Event.MouseEvent.dwButtOnState==FROM_LEFT_1ST_BUTTON_PRESSED){ //system("cls"); //this->show(); if(mouseRec.Event.MouseEvent.dwMousePosition.X%2==1) starty=(mouseRec.Event.MouseEvent.dwMousePosition.X-5)/2; else starty=(mouseRec.Event.MouseEvent.dwMousePosition.X-4)/2; if(mouseRec.Event.MouseEvent.dwMousePosition.Y<7) startx=mouseRec.Event.MouseEvent.dwMousePosition.Y-2; else startx=mouseRec.Event.MouseEvent.dwMousePosition.Y-3; if(c[startx][starty]==NULL) goto repeat; //cout<<"startx:"< show(); ReadConsoleInput(hIn, &mouseRec, 1, &res); if(mouseRec.Event.MouseEvent.dwButtOnState==FROM_LEFT_1ST_BUTTON_PRESSED){ //system("cls"); //this->show(); if(mouseRec.Event.MouseEvent.dwMousePosition.X%2==1) aimy=(mouseRec.Event.MouseEvent.dwMousePosition.X-5)/2; else aimy=(mouseRec.Event.MouseEvent.dwMousePosition.X-4)/2; if(mouseRec.Event.MouseEvent.dwMousePosition.Y<7) aimx=mouseRec.Event.MouseEvent.dwMousePosition.Y-2; else aimx=mouseRec.Event.MouseEvent.dwMousePosition.Y-3; //cout<<"aimx:"< >startx>>starty>>aimx>>aimy; }while(!this->move(startx,starty,aimx,aimy)); //clrscr(); //this->show(); }while(chessboard::end); if(chessboard::player==1) cout<<"结束,赢家是绿方"< 运行图片
居中的图片:
本质类似与键盘输入的象棋小游戏和鼠标控制的五子棋小游戏的综合。其中碰到一个小问题,就是黑窗要把快速便捷模式取消了才可以利用鼠标点击,为了解决这个问题,我在主函数插入了自动取消快速编辑的函数。
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode; GetConsoleMode(hStdin, &mode); mode &= ~ENABLE_QUICK_EDIT_MODE; //移除快速编辑模式 SetConsoleMode(hStdin, mode);关于“C++如何实现鼠标控制的黑框象棋”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。