刷罐3.0
2026-02-28 20:35:42
发布于:浙江
尽情的刷罐头吧
https://www.acgo.cn/team/1995060051988025344 求进QWQ 被人踢光了
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <conio.h>
#include <windows.h>
#include <cmath>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
struct Point {
int x;
int y;
Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};
enum FoodType { ORIGINAL, AI_DEAD };
struct Food {
Point pos;
FoodType type;
Food(Point p, FoodType t) {
pos = p;
type = t;
}
};
struct AISnake {
vector<Point> body;
char direction;
char headShape;
char bodyShape;
AISnake() : direction('l'), headShape('Q'), bodyShape('q') {}
};
void setColor(WORD color);
void gotoXY(int x, int y);
void hideCursor();
void drawBorder();
void clearPos(int x, int y);
bool isPosValid(const Point& p, const vector<Point>& excludeSnake);
Point generateFoodPos();
void saveHighScore();
void loadSave();
void drawStartMenu();
void initGame();
void handleInput();
void updateAISnake(AISnake& ai, int aiIndex);
void updatePlayerSnake();
void gameLoop();
const int WIDTH = 80;
const int HEIGHT = 30;
const int PLAYER_SPEED = 250;
const int AI_SPEED = 300;
const int MAX_FOOD = 8;
const int AI_SNAKE_COUNT = 3;
const WORD COLOR_PLAYER = FOREGROUND_RED | FOREGROUND_INTENSITY;
const WORD COLOR_AI = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD COLOR_FOOD = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD COLOR_DEFAULT = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD COLOR_TITLE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
vector<Point> playerSnake;
vector<AISnake> aiSnakes;
vector<Food> foods;
char playerDir = 'r';
bool gameOver = false;
int score = 0;
int aiUpdateTimer = 0;
int highScore = 0;
const string SAVE_FILE = "snake_save.txt";
void setColor(WORD color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void gotoXY(int x, int y) {
COORD coord = { (SHORT)x, (SHORT)y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cci = { 1, FALSE };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void drawBorder() {
setColor(COLOR_DEFAULT);
gotoXY(0, 0);
for (int i = 0; i < WIDTH; ++i) cout << "#";
for (int y = 1; y < HEIGHT; ++y) {
gotoXY(0, y); cout << "#";
gotoXY(WIDTH - 1, y); cout << "#";
}
gotoXY(0, HEIGHT);
for (int i = 0; i < WIDTH; ++i) cout << "#";
}
void clearPos(int x, int y) {
gotoXY(x, y);
setColor(COLOR_DEFAULT);
cout << " ";
}
bool isPosValid(const Point& p, const vector<Point>& excludeSnake) {
if (p.x <= 0 || p.x >= WIDTH - 1 || p.y <= 0 || p.y >= HEIGHT) return false;
for (size_t i = 0; i < excludeSnake.size(); ++i)
if (p == excludeSnake[i]) return false;
for (size_t i = 0; i < aiSnakes.size(); ++i)
for (size_t j = 0; j < aiSnakes[i].body.size(); ++j)
if (p == aiSnakes[i].body[j]) return false;
return true;
}
Point generateFoodPos() {
Point food;
do {
food.x = rand() % (WIDTH - 2) + 1;
food.y = rand() % (HEIGHT - 2) + 1;
bool overlap = false;
for (size_t i = 0; i < foods.size(); ++i) {
if (food == foods[i].pos) {
overlap = true;
break;
}
}
if (overlap) continue;
} while (!isPosValid(food, playerSnake));
return food;
}
void saveHighScore() {
ofstream fout(SAVE_FILE.c_str());
if (fout.is_open()) {
fout << highScore;
fout.close();
}
}
void loadSave() {
ifstream fin(SAVE_FILE.c_str());
if (fin.is_open()) {
fin >> highScore;
fin.close();
} else {
highScore = 0;
saveHighScore();
}
}
void drawStartMenu() {
system("cls");
hideCursor();
gotoXY(WIDTH / 2 - 10, HEIGHT / 2 - 5);
setColor(COLOR_TITLE);
cout << "===== 贪吃蛇大作战 =====";
gotoXY(WIDTH / 2 - 12, HEIGHT / 2 - 2);
setColor(COLOR_DEFAULT);
cout << "操作说明:W/A/S/D 移动 | Q 退出";
gotoXY(WIDTH / 2 - 10, HEIGHT / 2);
cout << "历史最高分:" << highScore;
gotoXY(WIDTH / 2 - 12, HEIGHT / 2 + 2);
cout << "AI蛇会互撞!死后变食物(仅吃一次)";
gotoXY(WIDTH / 2 - 10, HEIGHT / 2 + 4);
setColor(COLOR_TITLE);
cout << "按 E 开始游戏 | 按 Q 退出";
char input;
do {
input = _getch();
input = tolower(input);
if (input == 'q') exit(0);
} while (input != 'e');
initGame();
}
void initGame() {
srand((unsigned)time(NULL));
gameOver = false;
score = 0;
aiUpdateTimer = 0;
playerSnake.clear();
playerSnake.push_back(Point(WIDTH / 2, HEIGHT / 2));
playerSnake.push_back(Point(WIDTH / 2 - 1, HEIGHT / 2));
playerSnake.push_back(Point(WIDTH / 2 - 2, HEIGHT / 2));
playerDir = 'r';
aiSnakes.clear();
AISnake ai1;
ai1.body.push_back(Point(WIDTH / 4, HEIGHT / 4));
ai1.body.push_back(Point(WIDTH / 4 + 1, HEIGHT / 4));
ai1.body.push_back(Point(WIDTH / 4 + 2, HEIGHT / 4));
ai1.direction = 'l';
ai1.headShape = 'Q';
ai1.bodyShape = 'q';
aiSnakes.push_back(ai1);
AISnake ai2;
ai2.body.push_back(Point(WIDTH * 3 / 4, HEIGHT / 4));
ai2.body.push_back(Point(WIDTH * 3 / 4 - 1, HEIGHT / 4));
ai2.body.push_back(Point(WIDTH * 3 / 4 - 2, HEIGHT / 4));
ai2.direction = 'r';
ai2.headShape = 'D';
ai2.bodyShape = 'd';
aiSnakes.push_back(ai2);
AISnake ai3;
ai3.body.push_back(Point(WIDTH / 2, HEIGHT * 3 / 4));
ai3.body.push_back(Point(WIDTH / 2 + 1, HEIGHT * 3 / 4));
ai3.body.push_back(Point(WIDTH / 2 + 2, HEIGHT * 3 / 4));
ai3.direction = 'u';
ai3.headShape = 'G';
ai3.bodyShape = 'g';
aiSnakes.push_back(ai3);
foods.clear();
for (int i = 0; i < MAX_FOOD; ++i) {
Point p = generateFoodPos();
foods.push_back(Food(p, ORIGINAL));
}
system("cls");
drawBorder();
for (size_t i = 0; i < playerSnake.size(); ++i) {
gotoXY(playerSnake[i].x, playerSnake[i].y);
setColor(COLOR_PLAYER);
cout << (i == 0 ? 'O' : 'o');
}
for (size_t i = 0; i < aiSnakes.size(); ++i) {
for (size_t j = 0; j < aiSnakes[i].body.size(); ++j) {
gotoXY(aiSnakes[i].body[j].x, aiSnakes[i].body[j].y);
setColor(COLOR_AI);
cout << (j == 0 ? aiSnakes[i].headShape : aiSnakes[i].bodyShape);
}
}
for (size_t i = 0; i < foods.size(); ++i) {
gotoXY(foods[i].pos.x, foods[i].pos.y);
setColor(COLOR_FOOD);
cout << "*";
}
gotoXY(0, HEIGHT + 1);
setColor(COLOR_DEFAULT);
cout << "分数: " << score << " | 最高分: " << highScore << " | W/A/S/D移动 | Q退出";
}
void handleInput() {
if (_kbhit()) {
switch (_getch()) {
case 'w': case 'W': if (playerDir != 'd') playerDir = 'u'; break;
case 's': case 'S': if (playerDir != 'u') playerDir = 'd'; break;
case 'a': case 'A': if (playerDir != 'r') playerDir = 'l'; break;
case 'd': case 'D': if (playerDir != 'l') playerDir = 'r'; break;
case 'q': case 'Q': gameOver = true; break;
}
}
}
// ====================== 关键:AI 可以互相碰撞 ======================
void updateAISnake(AISnake& ai, int aiIndex) {
if (foods.empty()) return;
Point target = foods[0].pos;
double minDist = sqrt(pow(ai.body[0].x - target.x, 2) + pow(ai.body[0].y - target.y, 2));
for (size_t i = 1; i < foods.size(); ++i) {
double dist = sqrt(pow(ai.body[0].x - foods[i].pos.x, 2) + pow(ai.body[0].y - foods[i].pos.y, 2));
if (dist < minDist) {
minDist = dist;
target = foods[i].pos;
}
}
char newDir = ai.direction;
if (ai.body[0].x < target.x && ai.direction != 'l') newDir = 'r';
else if (ai.body[0].x > target.x && ai.direction != 'r') newDir = 'l';
else if (ai.body[0].y < target.y && ai.direction != 'u') newDir = 'd';
else if (ai.body[0].y > target.y && ai.direction != 'd') newDir = 'u';
Point newHead = ai.body[0];
switch (newDir) {
case 'u': newHead.y--; break;
case 'd': newHead.y++; break;
case 'l': newHead.x--; break;
case 'r': newHead.x++; break;
}
bool collision = false;
// 撞墙
if (newHead.x <= 0 || newHead.x >= WIDTH - 1 || newHead.y <= 0 || newHead.y >= HEIGHT)
collision = true;
// 撞玩家
if (!collision) {
for (size_t i = 0; i < playerSnake.size(); ++i)
if (newHead == playerSnake[i]) { collision = true; break; }
}
// 撞自己
if (!collision) {
for (size_t i = 0; i < ai.body.size(); ++i)
if (newHead == ai.body[i]) { collision = true; break; }
}
// ====================== 新增:撞其他 AI ======================
if (!collision) {
for (size_t i = 0; i < aiSnakes.size(); ++i) {
if ((int)i == aiIndex) continue;
for (size_t j = 0; j < aiSnakes[i].body.size(); ++j) {
if (newHead == aiSnakes[i].body[j]) {
collision = true;
break;
}
}
if (collision) break;
}
}
// ============================================================
if (collision) {
for (size_t j = 0; j < ai.body.size(); ++j) {
foods.push_back(Food(ai.body[j], AI_DEAD));
gotoXY(ai.body[j].x, ai.body[j].y);
setColor(COLOR_FOOD);
cout << "*";
}
ai.body.clear();
ai.body.push_back(Point(rand() % (WIDTH - 10) + 5, rand() % (HEIGHT - 10) + 5));
ai.body.push_back(Point(ai.body[0].x + 1, ai.body[0].y));
ai.body.push_back(Point(ai.body[0].x + 2, ai.body[0].y));
int d = rand() % 4;
ai.direction = (d == 0) ? 'u' : (d == 1) ? 'd' : (d == 2) ? 'l' : 'r';
for (size_t j = 0; j < ai.body.size(); ++j) {
gotoXY(ai.body[j].x, ai.body[j].y);
setColor(COLOR_AI);
cout << (j == 0 ? ai.headShape : ai.bodyShape);
}
return;
}
ai.direction = newDir;
ai.body.insert(ai.body.begin(), newHead);
bool ateFood = false;
int foodIdx = -1;
for (size_t i = 0; i < foods.size(); ++i) {
if (newHead == foods[i].pos) {
ateFood = true;
foodIdx = (int)i;
break;
}
}
if (ateFood) {
clearPos(foods[foodIdx].pos.x, foods[foodIdx].pos.y);
if (foods[foodIdx].type == ORIGINAL) {
Point newP = generateFoodPos();
foods[foodIdx] = Food(newP, ORIGINAL);
gotoXY(newP.x, newP.y);
setColor(COLOR_FOOD);
cout << "*";
} else {
foods.erase(foods.begin() + foodIdx);
}
} else {
Point tail = ai.body.back();
clearPos(tail.x, tail.y);
ai.body.pop_back();
}
gotoXY(ai.body[0].x, ai.body[0].y);
setColor(COLOR_AI);
cout << ai.headShape;
if (ateFood) {
gotoXY(ai.body[1].x, ai.body[1].y);
setColor(COLOR_AI);
cout << ai.bodyShape;
}
}
void updatePlayerSnake() {
Point newHead = playerSnake[0];
switch (playerDir) {
case 'u': newHead.y--; break;
case 'd': newHead.y++; break;
case 'l': newHead.x--; break;
case 'r': newHead.x++; break;
}
if (newHead.x <= 0 || newHead.x >= WIDTH - 1 || newHead.y <= 0 || newHead.y >= HEIGHT)
{ gameOver = true; return; }
for (size_t i = 1; i < playerSnake.size(); ++i)
if (newHead == playerSnake[i]) { gameOver = true; return; }
for (size_t i = 0; i < aiSnakes.size(); ++i)
for (size_t j = 0; j < aiSnakes[i].body.size(); ++j)
if (newHead == aiSnakes[i].body[j]) { gameOver = true; return; }
playerSnake.insert(playerSnake.begin(), newHead);
bool ateFood = false;
int foodIdx = -1;
for (size_t i = 0; i < foods.size(); ++i) {
if (newHead == foods[i].pos) {
ateFood = true;
foodIdx = (int)i;
break;
}
}
if (ateFood) {
clearPos(foods[foodIdx].pos.x, foods[foodIdx].pos.y);
if (foods[foodIdx].type == ORIGINAL) {
Point newP = generateFoodPos();
foods[foodIdx] = Food(newP, ORIGINAL);
gotoXY(newP.x, newP.y);
setColor(COLOR_FOOD);
cout << "*";
} else {
foods.erase(foods.begin() + foodIdx);
}
score += 10;
} else {
Point tail = playerSnake.back();
clearPos(tail.x, tail.y);
playerSnake.pop_back();
}
gotoXY(playerSnake[0].x, playerSnake[0].y);
setColor(COLOR_PLAYER);
cout << 'O';
if (ateFood) {
gotoXY(playerSnake[1].x, playerSnake[1].y);
setColor(COLOR_PLAYER);
cout << 'o';
}
gotoXY(0, HEIGHT + 1);
setColor(COLOR_DEFAULT);
cout << "分数: " << score << " | 最高分: " << highScore << " | W/A/S/D移动 | Q退出";
}
void gameLoop() {
while (!gameOver) {
handleInput();
updatePlayerSnake();
aiUpdateTimer++;
if (aiUpdateTimer >= AI_SPEED / PLAYER_SPEED) {
for (size_t i = 0; i < aiSnakes.size(); ++i)
updateAISnake(aiSnakes[i], (int)i);
aiUpdateTimer = 0;
}
Sleep(PLAYER_SPEED);
}
if (score > highScore) {
highScore = score;
saveHighScore();
}
gotoXY(0, HEIGHT + 2);
setColor(COLOR_TITLE);
cout << "游戏结束!当前分数: " << score << " | 最高分: " << highScore << endl;
cout << "3秒后回到开始界面...";
Sleep(3000);
gotoXY(0, HEIGHT + 2);
setColor(COLOR_DEFAULT);
for (int i = 0; i < WIDTH; ++i) cout << " ";
}
int main() {
loadSave();
while (true) {
drawStartMenu();
gameLoop();
}
setColor(COLOR_DEFAULT);
return 0;
}
#include<windows.h>
#define a 262
#define b 294
#define c 330
#define d 349
#define e 392
#define f 440
#define g 494
#define h 523
#define i 196
#define j 500
#define k 250
#define l 1000
int main(){
Beep(e,k);Sleep(50);
Beep(f,k);Sleep(50);
Beep(e,k);Sleep(50);
Beep(c,k);Sleep(50);
Beep(e,k);Sleep(50);
Beep(f,k);Sleep(50);
Beep(e,k);Sleep(50);
Beep(c,k);Sleep(50);
Beep(e,k);Sleep(50);
Beep(g,k);Sleep(50);
Beep(h,l);Sleep(100);
Beep(f,k);Sleep(50);
Beep(g,k);Sleep(50);
Beep(h,k);Sleep(50);
Beep(f,k);Sleep(50);
Beep(e,j);Sleep(100);
Beep(c,j);Sleep(100);
Beep(e,l);Sleep(200);
return 0;
}
全部评论 7
666
10小时前 来自 吉林
1球,顶一下
1小时前 来自 浙江
0##100%编译成功
2小时前 来自 浙江
0其实AI
2小时前 来自 浙江
0实力不够
2小时前 来自 浙江
0
建议紫砂。建议紫砂。
2小时前 来自 广东
0为啥我上线了你也上线了??



2小时前 来自 浙江
0何意味
2小时前 来自 广东
0
2小时前 来自 浙江
0
2小时前 来自 浙江
0你没急吧
11小时前 来自 广东
0??
1小时前 来自 浙江
0



















有帮助,赞一个