贪吃蛇C++

本文介绍了一个使用C++和CodeBlocks编写的贪吃蛇游戏代码。游戏包含等级、速度调整,通过键盘控制蛇的方向,吃食物增加长度,同时避免碰到墙壁或自身。代码详细展示了游戏逻辑和界面刷新过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

贪吃蛇(codeblocks)C++

#include
#include <windows.h>
#include <time.h>
#include <conio.h>
using namespace std;
//int N = 22;
inline void Refresh( char q[][22],int grade,int gamespeed)
{
system(“cls”);
cout << endl;
for( int i = 0 ; i < 22; ++i)
{
cout << “\t”;
for( int j = 0; j < 22; ++j)
cout << q[i][j] << “”;
if( i == 0 ) cout << “\t等级为:” << grade;
if( i == 4 ) cout <<"\t自动前进时间";
if( i == 6 ) cout << “\t间隔为:” <<gamespeed << “ms” << endl;
}
}
int main()
{
char tcsQipan[22][22];
int i , j ;
for( i = 1; i <= 20 ; ++i)
for( j = 1; j <= 20; ++j)
tcsQipan[i][j] = ’ ';//初始化贪吃蛇棋盘中间部分
for( i = 0; i <=21; ++i)
tcsQipan[0][i] = tcsQipan[21][i] = ‘-’;
for( j = 0; j <=20; ++j)
tcsQipan[i][0] = tcsQipan[i][21] = ‘-’;
int tcsZB[2][100];
for( i = 0; i < 4; ++i)
{
tcsZB[0][i] = 1;
tcsZB[1][i] = i+1;
}
int head = 3,tail = 0;
for( int i = 1; i <= 3; ++i )
tcsQipan[1][i] = ‘’;//蛇身
tcsQipan[1][4] = ‘#’;//蛇头
int x1,y1;
srand(time(0));
do
{
x1 = rand()% 20+1;
y1 = rand()% 20 +1;
}while( tcsQipan[x1][y1] != ’ ');
tcsQipan[x1][y1] = '
’;
cout << “\n\n\t\t贪吃蛇游戏即将开始!”<<endl;
long start;
int grade = 1,length = 4;
int gamespeed = 500;
for( int i = 3; i >= 0 ; --i)
{
start = clock();
while( clock() - start <= 1000 );
system(“cls”);
if(i > 0)
cout << “\n\n\t\t进入倒计时:” << i << endl;
else
Refresh(tcsQipan,grade,gamespeed);
}
int timeover;
char direction = 77;
int x,y;
while(1)
{
timeover = 1;
start = clock();
while((timeover = (clock()-start <= gamespeed)) && !kbhit());//如果按键按下或时间超过前进时间间隔则终止循环
if(timeover)
{
getch();direction = getch();
}
switch(direction)
{
case 72: x = tcsZB[0][head] -1 ; y = tcsZB[1][head]; break;//向上
case 80: x = tcsZB[0][head] + 1 ; y = tcsZB[1][head]; break;//向下
case 75: x = tcsZB[0][head] + 1 ; y = tcsZB[1][head] - 1; break;//向左
case 77: x = tcsZB[0][head] ; y = tcsZB[1][head] + 1; break;//向右
}
if ( !(direction == 72)|| direction == 80 || direction == 75 || direction == 77)//按键非方向键
{
cout << “\tgame over!” << endl;
system(“pause”);return 0;
}
if( x0 || x 21 || y== 0 || y== 21)//碰壁
{
cout << “\tgame over!” << endl;
system(“pause”);return 0;
}
if(tcsQipan[x][y]!= ’ ’ && !( x == x1 && y == y1))
{
cout << “\tgame over!” << endl;
system(“pause”);return 0;
}
if(x == x1 && y == y1)//吃米
{
length++;
if(length >= 8)
{
length -= 8;
grade ++;
if (gamespeed >= 200)
gamespeed = 550 - grade * 50;
}
tcsQipan[x][y] = ‘#’;
tcsQipan[tcsZB[0][head]][tcsZB[1][head]] = ‘’;
head = (head + 1)%100;
tcsZB[0][head] = x;
tcsZB[1][head] = y;
do
{
x1 = rand()% 20+1;
y1 = rand() % 20 +1;
}while( tcsQipan[x1][y1] != ’ ');
tcsQipan[x1][y1] = '
’;
Refresh(tcsQipan,grade,gamespeed);
}
else
{
tcsQipan[tcsZB[0][tail]][tcsZB[1][tail]] = ’ ';
tail = ( tail + 1)%100;
tcsQipan[tcsZB[0][head]][tcsZB[1][head]] = ‘*’;
head = ( head + 1)% 100;
tcsZB[0][head] = x;
tcsZB[1][head] = y;
tcsQipan[tcsZB[0][head]][tcsZB[1][head]]= ‘#’;
Refresh(tcsQipan,grade,gamespeed);
}
}
return 0;
}

### 如何用C++编写贪吃蛇游戏 要实现一个基于控制台的贪吃蛇游戏,可以按照以下逻辑结构设计程序。以下是完整的解决方案: #### 游戏核心概念 1. **地图表示**:使用二维数组模拟游戏区域。 2. **蛇的身体**:通过坐标列表存储蛇的位置。 3. **食物生成**:随机生成食物位置并更新分数。 4. **移动机制**:根据键盘输入调整方向,并实时刷新屏幕。 --- #### 完整代码示例 以下是一个基础版本的C++贪吃蛇游戏代码: ```cpp #include <iostream> #include <conio.h> // 用于获取按键输入 #include <vector> #include <cstdlib> #include <ctime> using namespace std; // 定义常量 const int WIDTH = 20; const int HEIGHT = 15; // 初始化变量 int x, y; // 蛇头位置 int fruitX, fruitY;// 食物位置 bool gameOver; char direction = 'R'; // 初始向右 vector<pair<int, int>> snakeTail; // 存储蛇身部分 int score = 0; void Setup() { srand(time(0)); gameOver = false; x = WIDTH / 2; y = HEIGHT / 2; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; } void Draw() { system("cls"); // 清屏 for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) cout << "#"; // 边界墙 else if (i == y && j == x) cout << "O"; // 蛇头 else if (fruitX == j && fruitY == i) cout << "*"; // 食物 else { bool isPartOfSnake = false; for(auto &part : snakeTail){ if(part.first == j && part.second == i){ isPartOfSnake = true; break; } } if(isPartOfSnake) cout << "o"; // 蛇身 else cout << " "; // 空白处 } } cout << endl; } cout << "Score: " << score << endl; } void Input() { if (_kbhit()) { // 检测是否有键被按下 switch(_getch()){ case 'w': if(direction != 'S') direction = 'W'; break; case 's': if(direction != 'W') direction = 'S'; break; case 'a': if(direction != 'D') direction = 'A'; break; case 'd': if(direction != 'A') direction = 'D'; break; case 'q': gameOver = true; break; // 按Q退出 } } } void Logic() { pair<int, int> prevHead = make_pair(x, y); switch(direction){ case 'W': --y; break; case 'S': ++y; break; case 'A': --x; break; case 'D': ++x; break; } // 添加新的头部到尾巴 snakeTail.push_back(prevHead); // 如果吃到食物 if(x == fruitX && y == fruitY){ score += 10; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; }else{ snakeTail.erase(snakeTail.begin()); // 移除最后一节尾巴 } // 检查碰撞 if(x >= WIDTH || x < 0 || y >= HEIGHT || y < 0){ gameOver = true; } for(auto &part : snakeTail){ if(part.first == x && part.second == y){ gameOver = true; } } } int main(){ Setup(); while(!gameOver){ Draw(); Input(); Logic(); } cout << "Game Over! Final Score: " << score << endl; return 0; } ``` --- #### 关于代码的关键说明 上述代码实现了以下几个主要功能: - 使用`_kbhit()`和`_getch()`处理即时键盘输入[^2]。 - 动态管理蛇体长度变化以及检测是否撞墙或自咬[^3]。 - 提供简单易懂的地图边界绘制方法[^1]。 此代码适用于Windows环境下的编译器(如Visual Studio),如果是在Linux/Mac上运行,则需替换清屏命令`system("clear")`替代`system("cls")`,同时可能需要安装额外库支持类似`ncurses`的功能来捕获按键事件。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值