贪吃蛇游戏C++命令行版实例代码
本文实例讲述了贪吃蛇游戏C++命令行版的实现代码,是非常经典的游戏。分享给大家供大家参考。具体实现方法如下:
众所周知,贪吃蛇游戏是经典的计算机游戏。
游戏描述如下:
1. 贪吃蛇可以自动直线前进,或者玩家可以通过方向键操纵贪吃蛇上下左右前进,每次前进一格。
2. 贪吃蛇在规定的区域内活动,当:
①贪吃蛇触碰到墙壁时;
②贪吃蛇的蛇头触碰到蛇身或者蛇尾时;
③玩家的键盘输入不是方向键时;
命令行显示“Game Over!”并且退出游戏。
3. 贪吃蛇活动的区域内每次随机产生一颗“豆豆”,当贪吃蛇吃到“豆豆”后蛇身增长一格,自动前进时间缩 短100ms(默认是1000ms,且不能少于100ms)。贪吃蛇长度每为8的倍数Improve a Level。
C++代码如下:
#include <bios.h> #include <conio.h> #include <dos.h> #include <graphics.h> #include <stdlib.h> #include <time.h> using namespace std; inline void display(char gsDomain[][22], int level, int moveSpeed) { system("cls"); //清屏 cout << endl << endl; for (int i = 0; i < 22; i++) { cout << "\t"; for (int j = 0; j < 22; j++) cout << gsDomain[i][j] << " "; if (i == 0) { cout << "\tLevel:" << level; } else if (i == 3) { cout << "\t自动前进时间"; } else if (i == 5) { cout << "\t间隔:" << moveSpeed << " ms"; } cout << endl; } } int main() { char gsDomain[22][22]; //贪吃蛇活动区域(包括墙壁) //初始化贪吃蛇活动区域(不包括墙壁) for (int i = 1; i <= 21; i++) { for (int j = 1; j <= 21; j++) gsDomain[i][j] = ' '; } //初始化贪吃蛇活动区域的上下墙壁 for (int i = 0; i < 22; i++) gsDomain[0][i] = gsDomain[21][i] = '-'; //初始化贪吃蛇活动区域的左右墙壁 for (int i = 1; i < 21; i++) gsDomain[i][0] = gsDomain[i][21] = '|'; //初始化蛇身 for (int i = 1; i <= 3; i++) gsDomain[1][i] = '*'; //初始化蛇头 gsDomain[1][4] = '#'; int snake[2][100]; //记录贪吃蛇每次出现的位置的坐标 for (int i = 0; i < 4; i++) { snake[0][i] = 1; //记录贪吃蛇所在位置的x坐标 snake[1][i] = i + 1; //记录贪吃蛇所在位置的y坐标 } int head = 3, tail = 0, length = 4; int beanX, beanY; //豆豆出现的位置 srand(time(0)); do { beanX = rand() % 20 + 1; beanY = rand() % 20 + 1; } while (gsDomain[beanX][beanY] != ' '); gsDomain[beanX][beanY] = '*'; //豆豆 cout << "\n\n\t\t贪吃蛇游戏即将开始!\n"; long start; int level = 1, moveSpeed = 1000; for (int i = 3; i >= 0; i--) { start = clock(); while (clock() - start <= 1000){} system("cls"); if (i) { cout << "\n\n\t\t进入游戏倒计时:" << i << endl; } else display(gsDomain, level, moveSpeed); } char direction = 77; //贪吃蛇默认自动向右直线前进 while (true) { bool timeFlag = true; int x, y; start = clock(); //若时间超过自动前进时间或者键盘上有键按下则终止循环 while ((timeFlag = (clock() - start <= moveSpeed)) && !kbhit()){} if (timeFlag) { //键盘上有键按下时读取键盘输入 getch(); direction = getch(); } switch (direction) { //向上 case 72: x = snake[0][head] - 1, y = snake[1][head]; break; //向下 case 80: x = snake[0][head] + 1, y = snake[1][head]; break; //向左 case 75: x = snake[0][head], y = snake[1][head] - 1; break; //向右 case 77: x = snake[0][head], y = snake[1][head] + 1; break; default: cout << "\tGame Over!\n"; return 0; } if (x == 0 || x == 21 || y == 0 || y == 21) { //贪吃蛇触碰到墙壁 cout << "\tGame Over!\n"; return 0; } if (gsDomain[x][y] != ' ' && !(x == beanX && y == beanY)) { //贪吃蛇的蛇头触碰到蛇身或者蛇尾 cout << "\tGame Over!\n"; return 0; } if (x == beanX && y == beanY) { //吃豆豆 length++; //长度加1 if (length >= 8) { //游戏升级处理 length -= 8; level++; if (moveSpeed > 100) moveSpeed -= 100; } gsDomain[snake[0][head]][snake[1][head]] = '*'; gsDomain[x][y] = '#'; head = (head + 1) % 100; snake[0][head] = x; snake[1][head] = y; do { beanX = rand() % 20 + 1; beanY = rand() % 20 + 1; } while (gsDomain[beanX][beanY] != ' '); gsDomain[beanX][beanY] = '*'; display(gsDomain, level, moveSpeed); //屏幕上显示 } else { //不吃豆豆 gsDomain[snake[0][tail]][snake[1][tail]] = ' '; //蛇尾前移一格 tail = (tail + 1) % 100; gsDomain[snake[0][head]][snake[1][head]] = '*'; head = (head + 1) % 100; snake[0][head] = x; snake[1][head] = y; gsDomain[x][y] = '#'; //蛇头前移一格 display(gsDomain, level, moveSpeed); //屏幕上显示 } } return 0; }
希望本文所述实例对大家C程序设计的学习有所帮助。
赞 (0)