C++#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
using ll=long long;
#define print(x) cout<<#x<<"="<<x<<"\n"
#define pii pair<int,int>
#define fi first
#define se second
const ll LINF=1e18;
const int INF=1e9;
const int p=998244353;
const int N=1e5;
const int width=60;
const int height=25;
const int MAX_FOOD=50;
enum GameMode{
TWO_PLAYER,
VS_AI
};
GameMode gameMode;
vector<pii>snake1;
int dir1;
vector<pii>snake2;
int dir2;
bool isAI;
struct Food{
int x,y;
bool active;
int value;
int color;
};
vector<Food>foods;
int score1,score2;
bool gameOver;
string winner;
int speed=80;
int colors[]={12,14,13,11,6,9,10,15,8,5};
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
void gotoxy(int x,int y){
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void hideCursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
int checkFood(int x,int y){
for(int i=0;i<foods.size();i++){
if(foods[i].active&&foods[i].x==x&&foods[i].y==y)return i;
}
return -1;
}
void spawnFood(){
if(foods.size()>=MAX_FOOD)return;
int attempts=0;
while(attempts<1000){
int x=rand()%width;
int y=rand()%height;
bool onSnake=0;
for(auto&s:snake1)if(s.fi==x&&s.se==y){onSnake=1;break;}
if(!onSnake)for(auto&s:snake2)if(s.fi==x&&s.se==y){onSnake=1;break;}
if(!onSnake)for(auto&f:foods)if(f.active&&f.x==x&&f.y==y){onSnake=1;break;}
if(!onSnake){
Food newFood;
newFood.x=x;
newFood.y=y;
newFood.active=1;
newFood.value=rand()%20+5;
newFood.color=colors[rand()%10];
foods.push_back(newFood);
break;
}
attempts++;
}
}
void setup(){
gameOver=0;
winner="";
srand(time(0));
dir1=3;
snake1.clear();
snake1.push_back({10,10});
snake1.push_back({11,10});
snake1.push_back({12,10});
dir2=3;
snake2.clear();
snake2.push_back({30,10});
snake2.push_back({31,10});
snake2.push_back({32,10});
foods.clear();
for(int i=0;i<5;i++)spawnFood();
score1=score2=0;
}
void draw(){
gotoxy(0,0);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
if(gameMode==TWO_PLAYER){
gotoxy(width/2-5,-1);
cout<<"=== 双人对战模式 ===";
}else{
gotoxy(width/2-5,-1);
cout<<"=== 对战AI模式 ===";
}
gotoxy(0,0);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
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)cout<<"#";
bool isSnake1=0,isSnake2=0;
int foodIdx=checkFood(j,i);
for(auto&s:snake1){
if(s.fi==j&&s.se==i){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"O";
isSnake1=1;
break;
}
}
if(!isSnake1){
for(auto&s:snake2){
if(s.fi==j&&s.se==i){
if(gameMode==VS_AI)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
cout<<"O";
isSnake2=1;
break;
}
}
}
if(!isSnake1&&!isSnake2){
if(foodIdx!=-1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),foods[foodIdx].color);
cout<<"$";
}else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
cout<<" ";
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
if(j==width-1)cout<<"#";
}
cout<<endl;
}
for(int i=0;i<width+2;i++)cout<<"#";
cout<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"玩家1 (WASD): "<<score1<<" ";
if(gameMode==TWO_PLAYER){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
cout<<"玩家2 (方向键): "<<score2<<" ";
}else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
cout<<"AI: "<<score2<<" ";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
cout<<"食物: "<<foods.size()<<"/"<<MAX_FOOD<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
cout<<"按 ESC 返回菜单"<<endl;
}
void input(){
if(_kbhit()){
int key=_getch();
if(key==27){
gameOver=1;
winner="返回菜单";
return;
}
if(key==224){
key=_getch();
if(gameMode==TWO_PLAYER){
switch(key){
case 72:if(dir2!=1)dir2=0;break;
case 80:if(dir2!=0)dir2=1;break;
case 75:if(dir2!=3)dir2=2;break;
case 77:if(dir2!=2)dir2=3;break;
}
}
}else{
switch(tolower(key)){
case 'w':if(dir1!=1)dir1=0;break;
case 's':if(dir1!=0)dir1=1;break;
case 'a':if(dir1!=3)dir1=2;break;
case 'd':if(dir1!=2)dir1=3;break;
case 'p':gotoxy(0,height+4);system("pause");break;
}
}
}
}
void AI_move(){
if(!isAI)return;
pii head=snake2.back();
int minDist=10000;
int targetFood=-1;
for(int i=0;i<foods.size();i++){
int dist=abs(foods[i].x-head.fi)+abs(foods[i].y-head.se);
if(dist<minDist){
minDist=dist;
targetFood=i;
}
}
if(targetFood==-1)return;
int targetX=foods[targetFood].x;
int targetY=foods[targetFood].y;
vector<int>possibleDirs;
if(targetX>head.fi&&dir2!=2)possibleDirs.push_back(3);
if(targetX<head.fi&&dir2!=3)possibleDirs.push_back(2);
if(targetY>head.se&&dir2!=0)possibleDirs.push_back(1);
if(targetY<head.se&&dir2!=1)possibleDirs.push_back(0);
if(possibleDirs.empty()){
for(int d=0;d<4;d++){
if(d!=dir2&&(d+dir2)!=3)possibleDirs.push_back(d);
}
}
if(!possibleDirs.empty()){
int bestDir=possibleDirs[rand()%possibleDirs.size()];
int nx=head.fi+dx[bestDir];
int ny=head.se+dy[bestDir];
if(nx>=0&&nx<width&&ny>=0&&ny<height){
bool safe=1;
for(int i=0;i<snake2.size()-1;i++){
if(snake2[i].fi==nx&&snake2[i].se==ny){
safe=0;
break;
}
}
if(safe)dir2=bestDir;
}
}
}
bool checkCollision(vector<pii>&snake,pii newHead){
if(newHead.fi<0||newHead.fi>=width||newHead.se<0||newHead.se>=height)return 1;
for(auto&s:snake)if(s==newHead)return 1;
return 0;
}
void logic(){
if(isAI)AI_move();
pii newHead1=snake1.back();
switch(dir1){
case 0:newHead1.se--;break;
case 1:newHead1.se++;break;
case 2:newHead1.fi--;break;
case 3:newHead1.fi++;break;
}
pii newHead2=snake2.back();
switch(dir2){
case 0:newHead2.se--;break;
case 1:newHead2.se++;break;
case 2:newHead2.fi--;break;
case 3:newHead2.fi++;break;
}
if(newHead1==newHead2){
gameOver=1;
winner="平局!";
return;
}
for(auto&s:snake2)if(newHead1==s){
gameOver=1;
winner=isAI?"AI 赢了!":"玩家2赢了!";
return;
}
for(auto&s:snake1)if(newHead2==s){
gameOver=1;
winner="玩家1赢了!";
return;
}
if(checkCollision(snake1,newHead1)){
gameOver=1;
winner=isAI?"AI 赢了!":"玩家2赢了!";
return;
}
if(checkCollision(snake2,newHead2)){
gameOver=1;
winner="玩家1赢了!";
return;
}
snake1.push_back(newHead1);
int foodIdx1=checkFood(newHead1.fi,newHead1.se);
if(foodIdx1!=-1){
score1+=foods[foodIdx1].value;
foods.erase(foods.begin()+foodIdx1);
}else snake1.erase(snake1.begin());
snake2.push_back(newHead2);
int foodIdx2=checkFood(newHead2.fi,newHead2.se);
if(foodIdx2!=-1){
score2+=foods[foodIdx2].value;
foods.erase(foods.begin()+foodIdx2);
}else snake2.erase(snake2.begin());
if(rand()%10<3)spawnFood();
while(foods.size()<2)spawnFood();
}
void gameOverScreen(){
system("cls");
gotoxy(width/2-5,height/2-2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
cout<<"游戏结束";
gotoxy(width/2-10,height/2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"玩家1: "<<score1<<" ";
if(gameMode==TWO_PLAYER){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
cout<<"玩家2: "<<score2;
}else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
cout<<"AI: "<<score2;
}
gotoxy(width/2-10,height/2+2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
cout<<winner;
gotoxy(width/2-12,height/2+4);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
cout<<"按任意键返回菜单...";
_getch();
}
void showMenu(){
system("cls");
gotoxy(width/2-10,height/2-5);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
cout<<"=== 贪吃蛇双人对战 ===";
gotoxy(width/2-10,height/2-2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"1. 双人对战模式";
gotoxy(width/2-10,height/2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
cout<<"2. 对战AI模式";
gotoxy(width/2-10,height/2+2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
cout<<"3. 退出";
gotoxy(width/2-15,height/2+5);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
cout<<"请选择模式 (1-3): ";
char ch=_getch();
if(ch=='1'){
gameMode=TWO_PLAYER;
isAI=0;
}else if(ch=='2'){
gameMode=VS_AI;
isAI=1;
}else if(ch=='3')exit(0);
}
signed main(){
hideCursor();
while(1){
showMenu();
setup();
while(!gameOver){
draw();
input();
logic();
Sleep(speed);
}
if(winner!="返回菜单")gameOverScreen();
}
CONSOLE_CURSOR_INFO cursor_info={1,1};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
system("cls");
return 0;
}