1 /******************************************** 2 * 程序名称:控制台点灯游戏 3 * 作 者:WindAutumn <fluttingleaves@hotmail.com> 4 * 最后修改:2012-7-20 5 * 版 本 号:1.0 6 * *****************************************/ 7 8 #include<stdio.h> 9 #include<windows.h> 10 #include<conio.h> 11 12 #define X_PIANYI 4 13 #define Y_PIANYI 2 14 typedef struct 15 { 16 int x; 17 int y; 18 } CursorPosition; 19 int LightCodition[10][10]= {0}; 20 21 void EnterGame(); 22 void ShowMenu(); 23 void StartGame(int row,int column); 24 void UserDef(); 25 void MyExit(); 26 int GetMousePos(CursorPosition * cursor); 27 void HideCursor(HANDLE hOutput); 28 void GotoXy(HANDLE hOutput, int x, int y); 29 void InitScreen(HANDLE hOutput); 30 31 int main() 32 { 33 SetConsoleTitle("控制台点灯游戏"); 34 while(1) 35 { 36 EnterGame(); 37 } 38 39 return 0; 40 } 41 42 /***************进入游戏的入口*******************/ 43 void EnterGame() 44 { 45 char choice; 46 do 47 { 48 ShowMenu(); 49 choice=getchar(); 50 } 51 while(choice!='1'&&choice!='2'&&choice!='3'&&choice!='4'&&choice!='5'&&choice!='9'); 52 53 switch(choice) 54 { 55 case '1': 56 StartGame(5,5); 57 case '2': 58 StartGame(6,6); 59 case '3': 60 StartGame(7,7); 61 case '4': 62 StartGame(8,8); 63 case '5': 64 UserDef(); 65 case '9': 66 MyExit(); 67 } 68 } 69 70 /***************显示游戏菜单*******************/ 71 void ShowMenu() 72 { 73 printf(" 请选择游戏模式: "); 74 printf(" 模式(1):5 X 5 "); 75 printf(" 模式(2):6 X 6 "); 76 printf(" 模式(3):7 X 7 "); 77 printf(" 模式(4):8 X 8 "); 78 printf(" 模式(5):自己输入行列数 "); 79 printf(" 退出游戏(9) "); 80 printf(" ═════════════════════ "); 81 printf(" 请选择(输入对应数字):"); 82 } 83 84 /***************开始游戏*******************/ 85 void StartGame(int row,int column) 86 { 87 CursorPosition cursor; 88 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); 89 int backrow,backcolumn; 90 91 HideCursor(hOutput); 92 InitScreen(hOutput); 93 GotoXy(hOutput, 0, 0); 94 95 printf(" row: %d,column:%d ",row,column); 96 for(backcolumn=0; backcolumn<column; backcolumn++) 97 { 98 GotoXy(hOutput, X_PIANYI, Y_PIANYI + backcolumn);// 第一个可用的绘图点 99 for(backrow=0; backrow<row; backrow++) 100 printf("█"); 101 } 102 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED);// 亮红色字体 103 while(1) 104 { 105 if(GetMousePos(&cursor)&&cursor.x>=X_PIANYI&&cursor.x<X_PIANYI+row*2&&cursor.y>=Y_PIANYI&&cursor.y<Y_PIANYI+column) 106 { 107 GotoXy(hOutput, cursor.x, cursor.y); 108 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI] = ! LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI]; 109 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI] ? 110 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED) : 111 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE); 112 printf("█");// 覆盖原来的块 113 if(cursor.y-1>=Y_PIANYI&&cursor.y-1<Y_PIANYI+column) 114 { 115 GotoXy(hOutput, cursor.x, cursor.y-1);// 上面的块,并保证两种颜色重合 116 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI-1] = ! LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI-1]; 117 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI-1] ? 118 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED) : 119 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE); 120 printf("█"); 121 } 122 if(cursor.x-2>=X_PIANYI&&(cursor.x-2)<X_PIANYI+row*2) 123 { 124 GotoXy(hOutput, cursor.x-2, cursor.y);// 左面的块,并保证两种颜色重合 125 LightCodition[(cursor.x-X_PIANYI)/2-1][cursor.y-Y_PIANYI] = ! LightCodition[(cursor.x-X_PIANYI)/2-1][cursor.y-Y_PIANYI]; 126 LightCodition[(cursor.x-X_PIANYI)/2-1][cursor.y-Y_PIANYI] ? 127 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED) : 128 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE); 129 printf("█"); 130 } 131 if(cursor.x+2>=X_PIANYI&&(cursor.x+2)<X_PIANYI+row*2) 132 { 133 GotoXy(hOutput, cursor.x+2, cursor.y);// 右面的块,并保证两种颜色重合 134 LightCodition[(cursor.x-X_PIANYI)/2+1][cursor.y-Y_PIANYI] = ! LightCodition[(cursor.x-X_PIANYI)/2+1][cursor.y-Y_PIANYI]; 135 LightCodition[(cursor.x-X_PIANYI)/2+1][cursor.y-Y_PIANYI] ? 136 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED) : 137 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE); 138 printf("█"); 139 } 140 if(cursor.y+1>=Y_PIANYI&&cursor.y+1<Y_PIANYI+column) 141 { 142 GotoXy(hOutput, cursor.x, cursor.y+1);// 下面的块,并保证两种颜色重合 143 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI+1] = ! LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI+1]; 144 LightCodition[(cursor.x-X_PIANYI)/2][cursor.y-Y_PIANYI+1] ? 145 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_RED) : 146 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE); 147 printf("█"); 148 } 149 } 150 } 151 } 152 153 /***************用户自定义游戏行列*******************/ 154 void UserDef() 155 { 156 int row=0,column=0; 157 system("cls"); 158 printf(" 请输入行数:"); 159 scanf("%d",&row); 160 printf(" 请输入列数:"); 161 scanf("%d",&column); 162 printf(" 现在,游戏开始!"); 163 StartGame(row,column); 164 } 165 166 /***************退出游戏*******************/ 167 void MyExit() 168 { 169 system("cls"); 170 printf("欢迎再次使用!"); 171 getch(); 172 exit(0); 173 } 174 175 /***************用于处理鼠标点击事件***************/ 176 int GetMousePos(CursorPosition * cursor) 177 { 178 HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); 179 INPUT_RECORD inRec; 180 DWORD res; 181 182 ReadConsoleInput(hInput, &inRec, 1, &res); 183 if (inRec.EventType == MOUSE_EVENT && inRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //鼠标左键 184 { 185 cursor->x = inRec.Event.MouseEvent.dwMousePosition.X / 2 * 2; 186 cursor->y = inRec.Event.MouseEvent.dwMousePosition.Y; 187 return 1; 188 } 189 190 return 0; 191 } 192 void HideCursor(HANDLE hOutput) 193 { 194 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 195 SetConsoleCursorInfo(hOutput, &cursor_info); 196 } 197 void GotoXy(HANDLE hOutput, int x, int y) 198 { 199 COORD coord; 200 coord.X = x; 201 coord.Y = y; 202 SetConsoleCursorPosition(hOutput, coord); 203 } 204 205 /**************屏幕初始化函数*****************/ 206 void InitScreen(HANDLE hOutput)//边框宽 37*2+2*2=78 字 / 高 20 行 / 从(1,1)开始绘框 / (2,2)可用 207 { 208 int i,j; 209 GotoXy(hOutput, X_PIANYI-3, Y_PIANYI-1); 210 211 SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY | FOREGROUND_BLUE);// 亮蓝色边框 212 printf("╔"); 213 for(i=0; i<37; i++) 214 printf("═"); 215 printf("╗ "); 216 217 for(j=0; j<20; j++) 218 { 219 printf(" ║"); 220 for(i=0; i<37*2; i++) 221 printf(" "); 222 printf("║ "); 223 } 224 225 printf(" ╚"); 226 for(i=0; i<37; i++) 227 printf("═"); 228 printf("╝ "); 229 }