C++ 写的贪吃蛇小游戏
  • 板块灌水区
  • 楼主Craft_Mine
  • 当前回复5
  • 已保存回复6
  • 发布时间2024/10/4 10:44
  • 上次更新2024/10/4 10:47:35
查看原帖
C++ 写的贪吃蛇小游戏
1166052
Craft_Mine楼主2024/10/4 10:44

仅在 Windows 下运行(win11 不行)
代码有点史山,但是还挺好玩的

#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#define Height 15
#define Width 15
#define MaxWait 3

int map[Height][Width];
int hi,hj,ti,tj,si[Height*Width],sj[Height*Width],len,dir,fi,fj;
int loop,score,wait;

void food(){
	do fi=rand()%(Height-2)+1,fj=rand()%(Width-2)+1;
	while(map[fi][fj]);
	map[fi][fj]=-1;
}

static void setpos(int i,int j){
    COORD point={j,i};
    HANDLE HOutput=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(HOutput,point);
}

void start(){
	printf("\n  SSSS  N   N    A    K   K  EEEEE");
	printf("\n S      NN  N   A A   K KK   E");
	printf("\n  SSS   N N N  A   A  KK     EEEE");
	printf("\n     S  N  NN  AAAAA  K KK   E");
	printf("\n SSSS   N   N  A   A  K   K  EEEEE\n\n");
	printf("\n             1: Start");
	printf("\n             2: Exit");
	char ch;
	do ch=getch();
	while(ch!='1' && ch!='2');
	if(ch=='2') exit(0);
	system("cls");
	printf("Tips:\n");
	printf("Press WASD to move\n");
	printf("Eat [] to grow\n");
	printf("Don't touch your body\n");
	printf("Press any key to begin\n");
	getch();
	system("mode con cols=30 lines=16");
}

void init(){	
	//init wondow
	system("title C++ Snake");
	system("mode con cols=35 lines=12");
	
	//init snake
	map[Height/2][Width/2]=1;
	map[Height/2][Width/2-1]=2;
	map[Height/2][Width/2-2]=3;
	dir='d',hi=Height/2,hj=Width/2,len=3;
	si[0]=si[1]=si[2]=Height/2;
	sj[0]=Width/2,sj[1]=Width/2-1,sj[2]=Width/2-2;
	
	//init game control
	loop=1;
	score=0;
	wait=0;
	
	//init food
	srand(time(0));
	food();
}

void print(bool flag=true){
	Sleep(50);
	if(wait<MaxWait && flag) return;
	system("cls");
	setpos(fi,fj*2);
	printf("[]");
	for(int i=0;i<len;i++,printf("()")) setpos(si[i],sj[i]*2);
	setpos(Height,0);
	printf("Score:%d    %s",score,loop?"":"GAME OVER!");
}

void update(){
	//update direction
	if(kbhit()){
		char ch=getch();
		if(ch=='w' || ch=='s' || ch=='a' || ch=='d')
			switch(dir){
				case 'w':if(ch=='a' || ch=='d') dir=ch; break;
				case 's':if(ch=='a' || ch=='d') dir=ch; break;
				case 'a':if(ch=='w' || ch=='s') dir=ch; break;
				case 'd':if(ch=='w' || ch=='s') dir=ch;
			}
	}
	if(wait++<MaxWait) return;
	wait=0;
	
	//update game map
	int tmp=0;
	for(int i=0;i<Height;i++)
		for(int j=0;j<Width;j++){
			if(map[i][j]>tmp) tmp=map[i][j],ti=i,tj=j;
			if(map[i][j]>0) map[i][j]++;
		}
	
	//move snake
	if(dir=='w') hi--;
	if(dir=='s') hi++;
	if(dir=='a') hj--;
	if(dir=='d') hj++;
	
	//loop position
	if(hi<0) hi=Height-1;
	if(hi==Height) hi=0;
	if(hj<0) hj=Width-1;
	if(hj==Width) hj=0;
	
	//update game map again
	if(map[hi][hj]==-1){
		food();
		map[hi][hj]=1,score+=5,len++;
	}else if(!map[hi][hj])
		map[hi][hj]=1,map[ti][tj]=0;
	else loop=0;
	
	//update position list
	int cnt=0;
	for(int i=0;i<Height;i++)
		for(int j=0;j<Width;j++)
			if(map[i][j]>0)
				si[cnt]=i,sj[cnt++]=j;
}

int main(){
	init();
	start();
	while(loop){
		update();
		print();
	}
	print(false);
	getch();
	return 0;
}
2024/10/4 10:44
加载中...