|c++游戏|贪吃蛇
2025-08-03 18:10:11
发布于:广东
(如果编译有问题有可能是因为环境不支持)
awsd移动
*@*是果子,可以加分
红色+10
黄色+20
蓝色+50
碰墙会死
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
bool gameOver;
int fen=0;
int zfen=0;
int width = 25;
int height = 20;
int x, y, fruitX, fruitY, score;
vector<pair<int, int>> tail;
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
enum FRuit{
a=0,b,c
};
FRuit fruittype=a;
// 颜色定义
enum Color {
BLACK = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
YELLOW = 6,
WHITE = 7
};
// 技能类型(修正命名)
enum PowerUp {
NONE = 0,
DOUBLE_SCORE, // 双倍得分
SPEED_BOOST, // 速度提升
INVINCIBLE, // 无敌模式
SHRINK, // 缩小体型
MAGNET // 磁铁(修正命名)
};
PowerUp currentPower = NONE;
int powerDuration = 0;
int powerX = -1, powerY = -1;
bool powerActive = false;
const int BASE_SPEED = 200; // 基础速度(修正为常量)
int speed = BASE_SPEED;
// 设置控制台编码为UTF-8以支持中文
void setupConsole() {
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
}
void setColor(Color text, Color background) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
}
void Setup() {
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
srand(time(0));
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
tail.clear();
tailLength = 0;
speed = BASE_SPEED; // 使用基础速度常量
currentPower = NONE;
powerDuration = 0;
powerActive = false;
}
void Draw() {
system("cls");
// 绘制顶部边界
setColor(YELLOW, BLACK);
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0) {
setColor(YELLOW, BLACK);
cout << "#";
}
if (i == y && j == x) {
setColor(GREEN, BLACK);
cout << "O"; // 蛇头
}
else if (i == fruitY && j == fruitX && fruittype==0) {
setColor(RED, BLACK);
cout << "@"; // 水果
}
else if(i == fruitY && j == fruitX && fruittype==1) {
setColor(YELLOW, BLACK);
cout << "@"; // 水果
}
else if(i == fruitY && j == fruitX && fruittype==2) {
setColor(CYAN, BLACK);
cout << "@"; // 水果
}
else if (powerActive && i == powerY && j == powerX) {
setColor(MAGENTA, BLACK);
cout << "P"; // 技能道具
}
else {
bool print = false;
for (auto segment : tail) {
if (segment.first == j && segment.second == i) {
setColor(CYAN, BLACK);
cout << "o"; // 蛇身
print = true;
}
}
if (!print) {
setColor(WHITE, BLACK);
cout << " ";
}
}
if (j == width - 1) {
setColor(YELLOW, BLACK);
cout << "#";
}
}
cout << endl;
}
// 绘制底部边界
setColor(YELLOW, BLACK);
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
setColor(WHITE, BLACK);
cout << "分数: " << score << endl;
cout << "长度: " << tailLength << endl;
if (powerActive) {
setColor(MAGENTA, BLACK);
cout << "技能道具出现! 快去收集(P)!" << endl;
}
if (powerDuration > 0) {
setColor(YELLOW, BLACK);
cout << "生效技能: ";
switch(currentPower) {
case DOUBLE_SCORE:
cout << "双倍得分";
break;
case SPEED_BOOST:
cout << "速度提升";
break;
case INVINCIBLE:
cout << "无敌模式";
break;
case SHRINK:
cout << "体型缩小";
break;
case MAGNET:
cout << "磁铁";
break;
default: break;
}
// 转换为秒显示(每帧约等于100ms)
cout << " (" << powerDuration / 10 << "秒)" << endl;
}
setColor(WHITE, BLACK);
cout << "控制: WASD移动, X退出游戏" << endl;
cout << "技能说明:" << endl;
setColor(CYAN, BLACK);
cout << "双倍得分: 吃水果得分翻倍" << endl;
cout << "速度提升: 蛇移动速度加快" << endl;
cout << "无敌模式: 可穿过边界和自身" << endl;
cout << "体型缩小: 蛇身长度减半" << endl;
cout << "磁铁: 扩大食物吸收范围" << endl;
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
if (dir != RIGHT) dir = LEFT;
break;
case 'd':
if (dir != LEFT) dir = RIGHT;
break;
case 'w':
if (dir != DOWN) dir = UP;
break;
case 's':
if (dir != UP) dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void SpawnPowerUp() {
// 确保道具不会出现在蛇身上
bool onSnake;
do {
onSnake = false;
powerX = rand() % width;
powerY = rand() % height;
// 检查是否出现在蛇头
if (powerX == x && powerY == y) {
onSnake = true;
continue;
}
// 检查是否出现在蛇身
for (auto segment : tail) {
if (segment.first == powerX && segment.second == powerY) {
onSnake = true;
break;
}
}
} while (onSnake);
currentPower = static_cast<PowerUp>(1 + rand() % 5);
powerActive = true;
}
void Logic() {
// 记录蛇头之前的位置
pair<int, int> prev = { x, y };
tail.insert(tail.begin(), prev);
// 保持蛇身长度
if (tail.size() > tailLength)
tail.pop_back();
// 移动蛇头
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
// 边界处理 - 无敌模式可以穿墙
if (currentPower != INVINCIBLE) {
if (x >= width || x < 0 || y >= height || y < 0)
gameOver = true;
} else {
// 穿墙效果
if (x >= width) x = 0;
else if (x < 0) x = width - 1;
if (y >= height) y = 0;
else if (y < 0) y = height - 1;
}
// 自撞检测 - 无敌模式免疫
if (currentPower != INVINCIBLE) {
for (auto segment : tail) {
if (segment.first == x && segment.second == y)
gameOver = true;
}
}
// 吃水果逻辑(普通模式或磁铁模式)
bool ateFruit = false;
if (x == fruitX && y == fruitY) {
ateFruit = true;
}
// 磁铁模式下扩大吸收范围
else if (currentPower == MAGNET && powerDuration > 0) {
if (abs(x - fruitX) <= 2 && abs(y - fruitY) <= 2) {
ateFruit = true;
}
}
if (ateFruit) {
int points = 10;
if(fruittype==1) points=20;
else if(fruittype==2) points=50;
if (currentPower == DOUBLE_SCORE) points *= 2;
score += points;
// 重新生成水果,避免出现在蛇身上
bool onSnake;
do {
onSnake = false;
fruitX = rand() % width;
fruitY = rand() % height;
int type1=rand()%10;
if(type1<7) fruittype=a;
else if(type1<9) fruittype=b;
else fruittype=c;
if (fruitX == x && fruitY == y) {
onSnake = true;
continue;
}
for (auto segment : tail) {
if (segment.first == fruitX && segment.second == fruitY) {
onSnake = true;
break;
}
}
} while (onSnake);
tailLength++;
// 随机生成技能道具
if (rand() % 5 == 0) { // 20%概率生成技能
SpawnPowerUp();
}
}
// 吃技能道具
if (powerActive && x == powerX && y == powerY) {
switch(currentPower) {
case DOUBLE_SCORE:
powerDuration = 100; // 约5秒
break;
case SPEED_BOOST:
speed = max(50, BASE_SPEED - 100); // 速度提升
powerDuration = 40; // 约2秒
break;
case INVINCIBLE:
powerDuration = 60; // 约3秒
break;
case SHRINK:
if (tailLength > 3) {
tailLength = max(2, tailLength / 2); // 长度减半
tail.resize(tailLength);
}
powerDuration = 10; // 立即生效后结束
break;
case MAGNET:
powerDuration = 100; // 约5秒
break;
}
powerActive = false;
}
// 更新技能持续时间
if (powerDuration > 0) {
powerDuration--;
if (powerDuration == 0) {
// 技能结束,恢复原始状态
if (currentPower == SPEED_BOOST) {
speed = BASE_SPEED; // 恢复基础速度
}
currentPower = NONE;
}
}
}
int main() { // 设置控制台支持中文
Setup();
auto lastTime = chrono::steady_clock::now();
while(true){
bool isbig=true;
cout << "===== 贪吃蛇游戏 - 技能版 =====" << endl;
cout <<"上局得分:" << fen << endl;
cout <<"最高得分:" << zfen << endl;
cout << "按G键开始游戏..."<<endl;
cout << "b大地图s小地图"<<endl;
char ch='0';
while(ch!='g'){
system("cls");
if(ch=='b'){
width=25;
height=20;
isbig=true;
}else if(ch=='s'){
width=20;
height=15;
isbig=false;
}
cout << "===== 贪吃蛇游戏 - 技能版 =====" << endl;
cout <<"上局得分:" << fen << endl;
cout <<"最高得分:" << zfen << endl;
cout << "按G键开始游戏..."<<endl;
cout << "b大地图s小地图"<<endl;
if(isbig) cout << "大地图" << endl;
else cout << "小地图" << endl;
ch=_getch();
}
cout << "游戏即将开始,请准备..." << endl;
Sleep(2000);
while (!gameOver) {
auto currentTime = chrono::steady_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(currentTime - lastTime).count();
if (elapsed >= speed) {
Draw();
Input();
Logic();
lastTime = currentTime;
}
// 随机生成技能道具
if (!powerActive && rand() % 200 == 0) { // 降低生成频率
SpawnPowerUp();
}
}
setColor(RED, BLACK);
cout << "游戏结束! 最终分数: " << score << endl;
fen=score;
zfen=max(zfen,fen);
setColor(WHITE, BLACK);
system("pause");
system("cls");
Setup();
}
return 0;
}
这里空空如也
有帮助,赞一个