当前位置:首页 > 通信资讯 > 正文

c语言控制台贪吃蛇(c++编程小游戏贪吃蛇)

本文实例讲述了C++基于控制台实现的贪吃蛇小游戏。分享给大家供大家参考。具体实现方法如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 #include <windows.h> #include <time.h> #include <stdio.h> #define MAX 100 #define UP 1 #define DOWN 2 #define LEFT 3 #define RIGHT 4 #define MOVING 5 #define STOP 0 HANDLE hMain_Out = NULL; HANDLE hMain_In = NULL; struct Pos { int x; int y; }; struct Body { int state; int len; int Direction; // int ZZZZ; // int HHHH; Pos pos[MAX]; }; Pos NewPos[MAX]; Pos Food; SMALL_RECT Wall; int count = 0; int grade = 0; int level = 1; int amount = 0; int speed = 200; void Init(Body &b); void Print(const Body &b); void Print(int x,int y); void Move(Body &b); void Clean(int x,int y); void Clean(const Body &b); void ShowInfo(); int GetDirection(Body &b); void TurnRound(int Direction,Body &b); void PosCopy(Body &b,Pos NewPos[]); void MoveBody(Body &b); void HideCursor(); void CreateWall(); void CreateFood(); bool IsKnock_Food(const Body &b); bool IsKnock_Wall(const Body &b); void AddBody(Body &b); int main() { Body b; Init(b); Print(b); HideCursor(); while(TRUE) { Sleep(speed); Move(b); GetDirection(b); } return 0; } void Init(Body &b) { b.len = 3; b.Direction = RIGHT; b.state = STOP; b.pos[0].x = 2; b.pos[0].y = 1; b.pos[1].x = 4; b.pos[1].y = 1; b.pos[2].x = 6; b.pos[2].y = 1; hMain_Out = GetStdHandle(STD_OUTPUT_HANDLE); hMain_In = GetStdHandle(STD_INPUT_HANDLE); CreateWall(); CreateFood(); ShowInfo(); } void Print(const Body &b) { COORD coord; for(int ix = b.len -1;ix >= 0;--ix) { coord.X = b.pos[ix].x; coord.Y = b.pos[ix].y; SetConsoleCursorPosition(hMain_Out,coord); printf("●"); } } void Move(Body &b) { ShowInfo(); if(IsKnock_Wall(b)) { MessageBox(NULL,"You are dead !","Oh my God",0); exit(0); } if(IsKnock_Food(b)) { if(amount > 5) { ++level; amount = 0; speed-= 50; } AddBody(b); grade += 10; ++amount; Clean(Food.x,Food.y); CreateFood(); } if(STOP == b.state) { if(RIGHT == b.Direction) { for(int ix = 0;ix < b.len;++ix) { Clean(b.pos[ix].x,b.pos[ix].y); b.pos[ix].x+=2; } } if(UP == b.Direction) { for(int ix = 0;ix < b.len;++ix) { Clean(b.pos[ix].x,b.pos[ix].y); b.pos[ix].y--; } } if(DOWN == b.Direction) { for(int ix = 0;ix < b.len;++ix) { Clean(b.pos[ix].x,b.pos[ix].y); b.pos[ix].y++; } } if(LEFT == b.Direction) { for(int ix = 0;ix < b.len;++ix) { Clean(b.pos[ix].x,b.pos[ix].y); b.pos[ix].x-=2; } } } if(MOVING == b.state) { PosCopy(b,NewPos); if(UP == b.Direction) { if(b.len == count) { b.state = STOP; b.Direction = UP; count = 0; } if(count < b.len && MOVING == b.state) { b.pos[b.len - 1].y--; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); } } if(DOWN == b.Direction) { if(b.len == count) { b.state = STOP; b.Direction = DOWN; count = 0; } if(count < b.len && MOVING == b.state) { b.pos[b.len - 1].y++; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); } } if(LEFT == b.Direction) { if(b.len == count) { b.state = STOP; b.Direction = LEFT; count = 0; } if(count < b.len && MOVING == b.state) { b.pos[b.len - 1].x-=2; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); } } if(RIGHT == b.Direction) { if(b.len == count) { b.state = STOP; b.Direction = RIGHT; count = 0; } if(count < b.len && MOVING == b.state) { b.pos[b.len - 1].x+=2; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); } } } Print(b); } void Clean(int x,int y) { COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(hMain_Out,c); printf(" "); } void Clean(const Body &b) { for(int ix = 0;ix < b.len;++ix) { Clean(b.pos[ix].x,b.pos[ix].y); } } int GetDirection(Body &b) { if(GetAsyncKeyState(VK_UP)) { count = 0; TurnRound(UP,b); } if(GetAsyncKeyState(VK_DOWN)) { count = 0; TurnRound(DOWN,b); } if(GetAsyncKeyState(VK_LEFT)) { count = 0; TurnRound(LEFT,b); } if(GetAsyncKeyState(VK_RIGHT)) { count = 0; TurnRound(RIGHT,b); } return 0; } void TurnRound(int d,Body &b) { switch(d) { case UP: if(RIGHT == b.Direction || LEFT == b.Direction) { PosCopy(b,NewPos); --b.pos[b.len -1].y; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); b.Direction = d; b.state = MOVING; } break; case DOWN: if(RIGHT == b.Direction || LEFT == b.Direction) { PosCopy(b,NewPos); ++b.pos[b.len -1].y; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); b.Direction = d; b.state = MOVING; } break; case LEFT: if(UP == b.Direction || DOWN == b.Direction) { PosCopy(b,NewPos); b.pos[b.len -1].x-=2; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); b.Direction = d; b.state = MOVING; } break; case RIGHT: if(UP == b.Direction || DOWN == b.Direction) { PosCopy(b,NewPos); b.pos[b.len -1].x+=2; Clean(b.pos[0].x,b.pos[0].y); MoveBody(b); Print(b); b.Direction = d; b.state = MOVING; } break; default: break; } } void PosCopy(Body &b,Pos NewPos[]) { for(int ix = 0;ix < b.len;++ix) { NewPos[ix].x=0; NewPos[ix].y=0; } for(int ix = 0;ix <b.len;++ix) { NewPos[ix] = b.pos[ix]; } } void MoveBody(Body &b) { for(int ix = b.len - 1;ix > 0;--ix) { b.pos[ix - 1] = NewPos[ix]; } ++count; PosCopy(b,NewPos); } void HideCursor() { CONSOLE_CURSOR_INFO info; GetConsoleCursorInfo(hMain_Out,&info); info.bVisible = FALSE; SetConsoleCursorInfo(hMain_Out,&info); } void CreateWall() { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hMain_Out,&info); info.srWindow.Right-=19; info.srWindow.Bottom-=5; Wall = info.srWindow; for(int i = 0;i <= info.srWindow.Right;i+=2) { Print(i,info.srWindow.Top); Print(i,info.srWindow.Bottom); } for(int y = 0;y <= info.srWindow.Bottom;++y) { Print(0,y); Print(info.srWindow.Right,y); } } void Print(int x,int y) { COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(hMain_Out,c); printf("■"); } void CreateFood() { srand(unsigned(time(NULL))); unsigned x_t = RAND_MAX / Wall.Right; unsigned y_t = RAND_MAX / Wall.Bottom; while(true) { int x = rand() / x_t; int y = rand() / y_t; Food.x = x - 4; Food.y = y - 4; if((0 == Food.x % 2) && (0 == Food.y % 2)) { if(Food.x < 5) { Food.x+=8; } if(Food.y<5) { Food.y+=8; } Print(Food.x,Food.y); break; } } } bool IsKnock_Food(const Body &b) { if(b.pos[b.len - 1].x == Food.x && b.pos[b.len - 1].y== Food.y) { return true; } else { return false; } } bool IsKnock_Wall(const Body &b) { if(0 == b.pos[b.len - 1].x || 0 == b.pos[b.len - 1].y || Wall.Right == b.pos[b.len - 1].x || Wall.Bottom == b.pos[b.len - 1].y) { return true; } Pos Head = b.pos[b.len - 1]; for(int ix = 0;ix <= b.len - 3;++ix) { if(Head.x == b.pos[ix].x && Head.y == b.pos[ix].y) { return true; } } return false; } void ShowInfo() { COORD c; c.X = Wall.Right + 2; c.Y = 3; SetConsoleCursorPosition(hMain_Out,c); printf(" 分数:%d",grade); c.Y+=10; SetConsoleCursorPosition(hMain_Out,c); printf(" 难度等级:%d",level); } void AddBody(Body &b) { if(b.len < MAX) { if(UP == b.Direction) { b.pos[b.len].y = b.pos[b.len - 1].y - 1; b.pos[b.len].x = b.pos[b.len - 1].x; ++b.len; } if(DOWN == b.Direction) { b.pos[b.len].y = b.pos[b.len - 1].y + 1; b.pos[b.len].x = b.pos[b.len - 1].x; ++b.len; } if(LEFT == b.Direction) { b.pos[b.len].x = b.pos[b.len - 1].x - 2; b.pos[b.len].y = b.pos[b.len - 1].y; ++b.len; } if(RIGHT == b.Direction) { b.pos[b.len].x = b.pos[b.len - 1].x + 2; b.pos[b.len].y = b.pos[b.len - 1].y; ++b.len; } } }

效果图如下所示:

c语言控制台贪吃蛇(c++编程小游戏贪吃蛇)

希望本文所述对大家的C++程序设计有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。