突然想练练大模拟,就写了一个贪吃蛇,个人感觉比其他的版本要好许多……
//本程序属个人劳动成果,如有雷同,纯属巧合(全是手打的,思路也是自己的)
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
using namespace std;
inline void gotoxy(int x,int y){
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
return;
}
int mp[1000][1000],len=3,now_len;
vector<int> x,y;
int way,nowx,nowy;
int xinx,xiny;
int head;
bool now_has;
char opt;
int fx[5]= {0,1,-1,0,0},fy[5]= {0,0,0,-1,1};
void color(int a) {//颜色函数
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
bool check_canmove() {//判断是否有路可走
rep(i,1,4) {
if(mp[nowx+fx[i]][nowy+fy[i]]!=1)return true;
}
return false;
}
void make_map() { //生成地图
memset(mp,0,sizeof(mp));
vector<int> tmpx,tmpy;
int vlen=x.size()-1;
rep(i,head,vlen){
tmpx.push_back(x[i]);
tmpy.push_back(y[i]);
}
x.clear(),y.clear();
x=tmpx,y=tmpy;
mp[xinx][xiny]=2;
vlen=x.size()-1;
mp[x[0]][y[0]]=8;
rep(i,1,vlen-1) {
int how;
bool l,r,h,u;
l=h=r=u=0;
if(y[i]<y[i-1])l=1;
else if(y[i]>y[i-1])r=1;
else if(x[i]>x[i-1])h=1;
else u=1;
if(y[i]<y[i+1])l=1;
else if(y[i]>y[i+1])r=1;
else if(x[i]>x[i+1])h=1;
else u=1;
if(h&&l)how=5;
else if(l&&u)how=6;
else if(h&&u)how=4;
else if(h&&r)how=9;
else if(u&&r)how=3;
else if(l&&r)how=7;
mp[x[i]][y[i]]=how;
}
mp[x[vlen]][y[vlen]]=1;
head=0;
}
//1:Θ
//3:╗
//4:║
//5:╚
//6:╔
//7;═
//9:╝
//8:@
void print_map() {//输出地图
gotoxy(0,0);
cout<<"by lize yyd 2021.9.2 8:06~9.37\n";
cout<<"wsad:上下左右\n";
color(648);
cout<<"———————————————————————————\n";
rep(i,1,15) {
color(648);
cout<<"—";
color(7);
rep(j,1,25) {
if(mp[i][j]==0)color(51),cout<<"■";
color(63);
if(mp[i][j]==1)cout<<"Θ";
else if(mp[i][j]>2){
if(mp[i][j]==3)cout<<"╗ ";
else if(mp[i][j]==4)cout<<"║ ";
else if(mp[i][j]==5)cout<<"╚ ";
else if(mp[i][j]==6)cout<<"╔ ";
else if(mp[i][j]==7)cout<<"═ ";
else if(mp[i][j]==8)cout<<"·";
else cout<<"╝ ";
}
color(54);
if(mp[i][j]==2)cout<<"★";
color(7);
}
color(648);
cout<<"—\n";
}
color(648);cout<<"———————————————————————————\n";color(7);
cout<<"现在长度:" <<now_len<<endl<<"最长长度:"<<len<<endl<<"吃掉星星:"<<len-3<<endl;
}
void print_gameover() {//游戏结束
system("cls");
color(4);
printf(
" _____ __ __ ______ ______ ________ _____ \n"
" / ____| /\\ | \\/ | ____| / __ \\ \\ / / ____| __ \\\n"
" | | __ / \\ | \\ / | |__ | | | \\ \\ / /| |__ | |__) |\n"
" | | |_ | / /\\ \\ | |\\/| | __| | | | |\\ \\/ / | __| | _ / \n"
" | |__| |/ ____ \\| | | | |____ | |__| | \\ / | |____| | \\ \\ \n"
" \\_____/_/ \\_\\_| |_|______| \\____/ \\/ |______|_| \\_\\\n"
);
color(7);
exit(0);
}
void print_win() {//你赢了
system("cls");
color(10);
printf(
" __ ______ _ _ __ _______ _ _\n"
" \\ \\ / / __ \\| | | | \\ \\ / /_ _| \\ | |\n"
" \\ \\_/ / | | | | | | \\ \\ /\\ / / | | | \\| |\n"
" \\ /| | | | | | | \\ \\/ \\/ / | | | . ` |\n"
" | | | |__| | |__| | \\ /\\ / _| |_| |\\ |\n"
" |_| \\____/ \\____/ \\/ \\/ |_____|_| \\_|\n"
);
color(7);
exit(0);
}
int main() {
// system("stty -icanon");
way=rand()%4+1;
srand(time(NULL));
nowx=rand()%10+5,nowy=rand()%10+10;
x.push_back(nowx),y.push_back(nowy),now_len=1;
while(check_canmove()) {
srand(time(NULL));
if(!now_has) {
do {
xinx=rand()%15+1,xiny=rand()%25+1;
} while(mp[xinx][xiny]==1||mp[xinx][xiny]>2);
mp[xinx][xiny]=2;
now_has=true;
}
if(kbhit()){
opt=_getch();
}
if(opt=='w')way=2;
if(opt=='s')way=1;
if(opt=='d')way=4;
if(opt=='a')way=3;
nowx+=fx[way],nowy+=fy[way];
if(mp[nowx][nowy]==2)len++,now_has=false;
x.push_back(nowx),y.push_back(nowy);
if(now_len>=len)head++;else now_len++;
if(((nowx!=x[head-1]||nowy!=y[head-1])&&(mp[nowx][nowy]==1||mp[nowx][nowy]>2))||nowx==0||nowy==0||nowx==16||nowy==26)print_gameover();
make_map();
print_map();
Sleep(100);
//可以自己调时间,单位ms
if(len==15*25)print_win();
}
return 0;
}