貪吃蛇游戲C++命令行版實(shí)例代碼
本文實(shí)例講述了貪吃蛇游戲C++命令行版的實(shí)現(xiàn)代碼,是非常經(jīng)典的游戲。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
眾所周知,貪吃蛇游戲是經(jīng)典的計(jì)算機(jī)游戲。
游戲描述如下:
1. 貪吃蛇可以自動(dòng)直線前進(jìn),或者玩家可以通過(guò)方向鍵操縱貪吃蛇上下左右前進(jìn),每次前進(jìn)一格。
2. 貪吃蛇在規(guī)定的區(qū)域內(nèi)活動(dòng),當(dāng):
①貪吃蛇觸碰到墻壁時(shí);
②貪吃蛇的蛇頭觸碰到蛇身或者蛇尾時(shí);
③玩家的鍵盤輸入不是方向鍵時(shí);
命令行顯示“Game Over!”并且退出游戲。
3. 貪吃蛇活動(dòng)的區(qū)域內(nèi)每次隨機(jī)產(chǎn)生一顆“豆豆”,當(dāng)貪吃蛇吃到“豆豆”后蛇身增長(zhǎng)一格,自動(dòng)前進(jìn)時(shí)間縮 短100ms(默認(rèn)是1000ms,且不能少于100ms)。貪吃蛇長(zhǎng)度每為8的倍數(shù)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自動(dòng)前進(jìn)時(shí)間";
}
else if (i == 5)
{
cout << "\t間隔:" << moveSpeed << " ms";
}
cout << endl;
}
}
int main()
{
char gsDomain[22][22]; //貪吃蛇活動(dòng)區(qū)域(包括墻壁)
//初始化貪吃蛇活動(dòng)區(qū)域(不包括墻壁)
for (int i = 1; i <= 21; i++)
{
for (int j = 1; j <= 21; j++)
gsDomain[i][j] = ' ';
}
//初始化貪吃蛇活動(dòng)區(qū)域的上下墻壁
for (int i = 0; i < 22; i++)
gsDomain[0][i] = gsDomain[21][i] = '-';
//初始化貪吃蛇活動(dòng)區(qū)域的左右墻壁
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]; //記錄貪吃蛇每次出現(xiàn)的位置的坐標(biāo)
for (int i = 0; i < 4; i++)
{
snake[0][i] = 1; //記錄貪吃蛇所在位置的x坐標(biāo)
snake[1][i] = i + 1; //記錄貪吃蛇所在位置的y坐標(biāo)
}
int head = 3, tail = 0, length = 4;
int beanX, beanY; //豆豆出現(xiàn)的位置
srand(time(0));
do
{
beanX = rand() % 20 + 1;
beanY = rand() % 20 + 1;
} while (gsDomain[beanX][beanY] != ' ');
gsDomain[beanX][beanY] = '*'; //豆豆
cout << "\n\n\t\t貪吃蛇游戲即將開(kāi)始!\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進(jìn)入游戲倒計(jì)時(shí):" << i << endl;
}
else
display(gsDomain, level, moveSpeed);
}
char direction = 77; //貪吃蛇默認(rèn)自動(dòng)向右直線前進(jìn)
while (true)
{
bool timeFlag = true;
int x, y;
start = clock();
//若時(shí)間超過(guò)自動(dòng)前進(jìn)時(shí)間或者鍵盤上有鍵按下則終止循環(huán)
while ((timeFlag = (clock() - start <= moveSpeed)) && !kbhit()){}
if (timeFlag)
{
//鍵盤上有鍵按下時(shí)讀取鍵盤輸入
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++; //長(zhǎng)度加1
if (length >= 8)
{
//游戲升級(jí)處理
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;
}
希望本文所述實(shí)例對(duì)大家C程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
- 利用C/C++實(shí)現(xiàn)較完整貪吃蛇游戲
- C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲
- C++基于控制臺(tái)實(shí)現(xiàn)的貪吃蛇小游戲
- C++結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)貪吃蛇
- C++代碼實(shí)現(xiàn)貪吃蛇小游戲
- C/C++實(shí)現(xiàn)貪吃蛇逐步運(yùn)動(dòng)效果
- C++ vector容器實(shí)現(xiàn)貪吃蛇小游戲
- C++控制臺(tái)實(shí)現(xiàn)貪吃蛇游戲
- C++控制臺(tái)循環(huán)鏈表實(shí)現(xiàn)貪吃蛇
- 利用C/C++實(shí)現(xiàn)貪吃蛇游戲
相關(guān)文章
c++11 多線程編程——如何實(shí)現(xiàn)線程安全隊(duì)列
這篇文章主要介紹了c++ 如何實(shí)現(xiàn)線程安全隊(duì)列,幫助大家更好的理解和學(xué)習(xí)c++的相關(guān)知識(shí),感興趣的朋友可以了解下2020-11-11
C++控制權(quán)限關(guān)鍵字protected
這篇文章主要介紹了C++控制權(quán)限關(guān)鍵字protected,protected和private類似,而對(duì)于派生類來(lái)說(shuō),protected與public類似,下面來(lái)一起倆姐更多詳細(xì)內(nèi)容吧,需要的小伙伴可以參考一下2022-01-01
C++中的動(dòng)態(tài)分派在HotSpot?VM中的應(yīng)用小結(jié)
多態(tài)是面向?qū)ο缶幊陶Z(yǔ)言的重要特性,它允許基類的指針或引用指向派生類的對(duì)象,而在具體訪問(wèn)時(shí)實(shí)現(xiàn)方法的動(dòng)態(tài)綁定,這篇文章主要介紹了C++的動(dòng)態(tài)分派在HotSpot?VM中的重要應(yīng)用,需要的朋友可以參考下2023-09-09
C++中四種對(duì)象生存期和作用域以及static的用法總結(jié)分析
以下是對(duì)C++中四種對(duì)象生存期和作用域以及static的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09
算法之排序算法的算法思想和使用場(chǎng)景總結(jié)
這篇文章主要介紹了算法之排序算法的算法思想和使用場(chǎng)景總結(jié),本文講解了插入排序、交換排序、選擇排序等幾大類排序算法的特點(diǎn)、思想和使用場(chǎng)景,需要的朋友可以參考下2014-08-08

