首页 > 其他分享 >贪吃蛇100%能玩

贪吃蛇100%能玩

时间:2024-10-22 20:47:10浏览次数:8  
标签:hout case food 100% framey length 贪吃蛇 snake

#include<stdio.h>
#include<conio.h>
#include<iostream> 
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define framex 2
#define framey 2
#define wide 40
#define high 25

using namespace std;

int i,a[2];
int j=33; 
//更改颜色
int color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色
	return 0;
}


//将光标移动到指定位置 
void gotoxy(HANDLE hout,int x,int y){
    //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标
    COORD pos;
    pos.X=x;
    pos.Y=y;
    //SetConsoleCursorPosition是API中定位光标位置的函数。
    SetConsoleCursorPosition(hout,pos);
}
void cover (HANDLE hout);
void explation(HANDLE hout)
  {
  	int i,j=1;
  	system("cls");
  	color(13);
  	gotoxy(hout,44,3);
  	cout << "游戏说明"; 
	 color(2);
	 for(i=6;i<=22;i++)
	 {
	 	for(j=20;j<=75;j++)
	 	{
	 		gotoxy(hout,j,i);
	 		if(i==6||i==22)cout << "=" ;
	 		else if(j==20||j==75)cout << "||";
		 }
	  }
	  color(3);
	  gotoxy(hout,25,8);
	  gotoxy(hout,25,11);
	  cout << "1、不能穿墙,不能咬到自己" ;
	  color(10);
	  cout << "2、用 ↑(W)  ↓(S)  ←(A)  →(D)分别控制蛇的移动";
	  color(14);
	  gotoxy(hout,25,14);
	  cout << "3、按任意键(除去ESC)暂停游戏,方向键继续";
	  color(11);
	  gotoxy(hout,25,17);
	  cout << "4、ESC:自爆,游戏结束";
	  color(4);
	  getch();//按任意键返回主界面
	  system("cls");
	  cover (hout);  
  }


//游戏封面
void cover (HANDLE hout){
	gotoxy(hout,35,1);
 	color(6);
 	printf("/^\\/^\\");
 	
 	gotoxy(hout,34,2);
 	printf("|_| o|");
 	
 	gotoxy(hout,33,2);
 	color(2);
 	printf("_");
 	
 	gotoxy(hout,25,3);
 	color(12);
 	printf("\\/");
 	
 	gotoxy(hout,31,3);
 	color(2);
 	printf("/");
 	
 	gotoxy(hout,37,3);
 	color(6);
 	printf("\\_/");
 	
 	gotoxy(hout,41,3);
 	color(10);
 	printf(" \\");
 	
 	gotoxy(hout,26,4);
 	color(12);
 	printf("\\____");
 	
 	gotoxy(hout,32,4);
 	printf("_________");
 	
 	gotoxy(hout,31,4);
 	color(2);
 	printf("|");
 	
 	gotoxy(hout,43,4);
 	color(10);
 	printf("\\");
 	
 	gotoxy(hout,32,5);
 	color(2);
 	printf("\\_______");
 	
 	gotoxy(hout,44,5);
 	color(10);
 	printf("\\");
 	
 	gotoxy(hout,39,6);
 	printf("|    |              \\");
 	
 	gotoxy(hout,38,7);
 	printf("/    /                \\");
 	
 	gotoxy(hout,37,8);
 	printf("/    /               \\ \\");
 	
 	gotoxy(hout,35,9);
 	printf("/    /                \\ \\");
 	
 	gotoxy(hout,34,10);
 	printf(" /   /                  \\  \\");
 	
 	gotoxy(hout,33,11);
 	printf("/    /        _----_        \\  \\");
 	
 	gotoxy(hout,32,12);
 	printf("/    /         _-~   ~-_        |  |");
 	
 	gotoxy(hout,31,13);
 	printf("(     (     _-~    _--_    ~-_     _/ |");
 	
 	gotoxy(hout,32,14);
 	printf("\\    ~-____-~    _-~    ~-_    ~-_-~ /");
 	
 	gotoxy(hout,33,15);
 	printf("~-_         _-~        ~-_       _-~");
 	
 	gotoxy(hout,35,16);
 	printf("~--____-~                ~-___-~");  
 
 	color(9);
    gotoxy(hout,43,18);
	color(11);
	cout << "贪吃蛇大作战";
	color(14);
	for(i=20;i<=26;i++){
	    for(j=27;j<=74;j++)
		{
	    	gotoxy(hout,j,i);
	    	if(i==20||i==26)
			{
	    		cout << "-";
			}
			else if(j==27||j==74)
			{
				cout << "|";
			}
		}
	}
	color(12);
	gotoxy(hout,35,22);
	cout << "1.开始游戏";
	gotoxy(hout,55,22);
	cout << "2.游戏说明";
	gotoxy(hout,35,24);
	cout << "3.退出游戏";
	color(9);
	gotoxy(hout,framex+wide*2,framey+19);
    cout << "姓名:C++小将董议文"; 
    gotoxy(hout,framex+wide*2,framey+21);
    cout << "来自:婆婆家"; 
    gotoxy(hout,framex+wide*2,framey+23);
    cout << "完成时间:2024年02月04日";
    gotoxy(hout,29,27);
	color(3);
	printf("输入数字做出你的选择:");
    color(14);
	int n;
	scanf("%d",&n);
	switch(n){
	 case 1: 
	 	 system("cls");//清屏 
		 break;
	 case 2:
	 	 explation(hout);
	 	 break;
	 case 3:
	 	 exit(0);
	 break;
	}
} 
//定义蛇的结构体 
struct  Snake{
    int x[100];
    int y[100];
    int speed;
    int length;
    int count;
    int tm1;//开始时间 
    int tm2;//持续时间 
};
//定义食物的结构体 
struct Food{
    int x;
    int y;
};
//制作框架 
void makeframe(struct Snake snake){
    //定义显示器变量句柄 
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    gotoxy(hout,framex+wide*2+5,framey);
    cout << "   贪吃蛇游戏"; 
    gotoxy(hout,framex+wide*2+5,framey+3);
    cout << "使用方向键或wasd移动";
    gotoxy(hout,framex+wide*2+5,framey+5);
    cout << "长按方向键可加速";
    gotoxy(hout,framex+wide*2+5,framey+7);
    cout << "按任意键暂停,方向键继续";
    //打印上边框 
    for(i=0;i<wide*2+1;i++){
        gotoxy(hout,framex+i,framey);
        cout << "*";
    }
    //打印下边框 
    for(i=0;i<wide*2+2;i++){
        gotoxy(hout,framex+i,framey+high);
        cout << "*";
    }
    //打印左边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex,framey+i);
        cout << "*";
    }
    //打印右边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex+wide*2+1,framey+i);
        cout << "*";
    }
}
//游戏信息
void infor(HANDLE hout,struct Snake* snake){
    gotoxy(hout,framex+wide*2+5,framey+15);
    printf("当前速度:%d",-snake->speed+500);
    gotoxy(hout,framex+wide*2+5,framey+17);
    printf("当前得分:%d",snake->count);
    gotoxy(hout,framex+wide*2+5,framey+19);
    printf("当前长度:%d",snake->length);
    gotoxy(hout,framex+wide*2+5,framey+21);
    snake->tm2=time(NULL);
    snake->tm2-=snake->tm1;
    printf("蛇已经活了:%d秒",snake->tm2);
} 
//初始化蛇 
void initsnake(struct Snake *snake){
    snake->x[0]=framex+2;
    snake->y[0]=framey+high/2;
    snake->count=0;
    snake->length=3;
    snake->speed=400;
    snake->tm1=time(NULL);
	 
    //初始化由蛇尾至蛇头的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i]=snake->x[i-1]+1;
        snake->y[i]=snake->y[i-1];
    }
}
//打印蛇 
void printsnake(HANDLE hout ,struct Snake *snake){
    for(i=0;i<snake->length;i++){
        gotoxy(hout,snake->x[i],snake->y[i]);
        if(i==snake->length-1) 
            cout << "头";
        else if(i==0){
			cout << "尾";
            
		}
            
        else 
            cout << "身"; 
    }
}

//移动蛇 
void movesnake(HANDLE hout,struct Snake *snake){
    gotoxy(hout,snake->x[0],snake->y[0]);
    //清除蛇尾 
    printf("  ");
    //蛇的后一节坐标变成前一节的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i-1]=snake->x[i];
        snake->y[i-1]=snake->y[i];
    }
}
//打印食物 
void printfood (HANDLE hout,struct Snake *snake,struct Food* food){
    //用计算机时间获取随机值 
    srand((unsigned)time(NULL));
    while(1){
        //将食物的横纵坐标的大小限定在窗口大小内 
        food->x=rand()%(wide-2)+1;
        food->y=rand()%(high-2)+1;
        //避免食物出现在边框上 
        if(food->x==0&&food->y==0)
            continue;
        //将食物的坐标放置在窗口内 
        food->x=2*food->x+framex;
        food->y+=framey;
        //如果食物出现在蛇上,重新产生食物 
        for(i=0;i<snake->length;i++){
            if(food->x==snake->x[i]&&food->y==snake->y[i])
                break;
        }
        //食物不在蛇上,打印食物,结束循环 
        if(i==snake->length){
            gotoxy(hout,food->x,food->y);
            cout << "食";
            
            break;  
        }
    }

}
//吃食物 
void eatfood(HANDLE hout,struct Snake * snake,struct Food *food){
    //如果蛇头的坐标等于食物的坐标 
    if(snake->x[snake->length-1]==food->x&&snake->y[snake->length-1]==food->y){
        //蛇长加一 
        snake->length++;
        //蛇身(不包括蛇尾)整体像前移动一格 
        for(i=snake->length-1;i>0;i--){
            snake->x[i]=snake->x[i-1];
            snake->y[i]=snake->y[i-1];
        }
        //得到蛇尾的坐标(即蛇移动前的蛇尾坐标) 
        snake->x[0]=a[0];      
        snake->y[0]=a[1];
        printfood(hout,snake,food);
        snake->count++;
        if(snake->count%3==0)
            snake->speed-=50;
    }
}
//死亡判断 
int ifdead(struct Snake* snake){
    //如果碰到左边界 返回0;
    if(snake->x[snake->length-1]==framex)
        return 0;
    //如果碰到右边界 返回0;
    if(snake->x[snake->length-1]==framex+wide*2)
        return 0;
    //如果碰到上边界 返回0;
    if(snake->y[snake->length-1]==framey)
        return 0;
    //如果碰到下边界 返回0;
    if(snake->y[snake->length-1]==framey+high)
        return 0;
    //如果碰到自己身体 返回0; 
    for(i=0; i<snake->length-1; i++) 
        if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] ) 
            return 0;
    //否则返回1; 
    return 1;
}
//开始游戏 
int main(){
    unsigned char ch =77;
    //定义显示器句柄 
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    cover(hout);
    struct Snake s, *snake=&s;
    struct Food  f,  *food=&f;
    initsnake(snake);
    makeframe(*snake);
    printfood(hout,snake,food);
    Sleep(500);
    while(1){
        infor(hout,snake);
        //保存蛇尾横纵坐标 
        a[0]=snake->x[0];
        a[1]=snake->y[0];
        j=0;
        
        //如果用户敲击键盘
        if(kbhit()){ 
            //用ch接收输入 ,注意是getch(),不需敲击回车
            ch=getch();
            if(kbhit()){
            	
            //长按时间
                Sleep(20);
                j=1;
            }
        }
        
        
        
        switch(ch){
            //向上移动 
            case'W':
            case 'w' :
            case 72:{
                movesnake(hout,snake);
                //蛇头纵坐标减一 
                snake->y[snake->length-1]-=1;
                break;
            }
            //向下移动 
            case'S':
            case 's':
            case 80:{
                movesnake(hout,snake);
                //蛇头纵坐标加一 
                snake->y[snake->length-1]+=1;
                break;
            }
            
            //向左移动 
            case'A':
            case 'a':
            case 75:{
                movesnake(hout,snake);
                //蛇头横坐标减二 
                snake->x[snake->length-1]-=2;
                break;
            }
            //向右移动 
            case'D':
            case 'd':
            case 77:{
                movesnake(hout,snake);
                //蛇头横坐标加二 
                snake->x[snake->length-1]+=2;
                break;
            }
        }

        eatfood(hout,snake,food);
        printsnake(hout,snake);
        //如果判断蛇死亡,跳出循环
        if(ifdead(snake)==0||ch==27||snake->speed==0){
            gotoxy(hout,framex+wide/2,framey-2);
            if(snake->speed==0)
                printf("恭喜你通关了!!!");
            else
                printf("糟糕 T_T");
            break;
        }

        if(j==0)
        //如果没有长按,蛇滞留的时间为 
            Sleep(snake->speed);
        //如果长按,蛇滞留的时间为 20ms,即加速 
        else 
            Sleep(20);
    }
    
    //跳出循环时 ,滞留死亡场景一段时间
    Sleep(1000);
    //清屏 
    system("cls");
    //显示结束界面 
    if(snake->speed==0) 
        printf("\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你得到了满分:24\n\n\n");
    else 
        printf("\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:%d\n\n\n",snake->count); 
    Sleep(1000);
    system("cls");
    //递归调用main函数,死亡之后回到主界面 
    main();
    return 0; 
}

标签:hout,case,food,100%,framey,length,贪吃蛇,snake
From: https://blog.csdn.net/dfg555r/article/details/143136043

相关文章

  • 贪吃蛇编译就能玩c++
    #include<stdio.h>#include<conio.h>#include<iostream>#include<stdlib.h>#include<windows.h>#include<time.h>#defineframex2#defineframey2#definewide40#definehigh25usingnamespacestd;inti,a[2];intj=......
  • 贪吃蛇免费
    #include<stdio.h>#include<conio.h>#include<iostream>#include<stdlib.h>#include<windows.h>#include<time.h>#defineframex2#defineframey2#definewide40#definehigh25usingnamespacestd;inti,a[2];intj=......
  • CSC3100 Problem Scale & Subtasks
    RequirementsCode(90%)YoucanwriteyourcodeinJava,Python,C,orC++.Thetimelimitmayvaryamongdifferentlanguages,dependingontheperformanceofthelanguage.Yourcodemustbeacompleteexcutableprograminsteadofonlyafunction.Weg......
  • 【FMC163】基于VITA57.1标准的双通道3GSPS AD采集、双通道12GSPS DA回放FMC子卡模块(10
    板卡概述FMC163是一款基于VITA57.1标准的实现2路14-bit、3GSPSADC采集功能、2路14-bit12GSPSDA回放FMC子卡模块。该模块遵循VITA57.1标准,可直接与FPGA载卡配合使用,该板卡支持对6GHz的射频信号进行数字化采样以及信号生成,板内集成了高性能的时钟管理模块,具有极高的收发动态性能......
  • 《花100块做个摸鱼小网站! 》第八篇—增加词云组件和搜索组件
    ⭐️基础链接导航⭐️服务器→☁️阿里云活动地址看样例→......
  • Amazon Q Developer 实践:零基础创建贪吃蛇游戏
    本文探讨了如何使用AmazonQDeveloper根据结构化的提示词,直接生成一个贪吃蛇游戏原型,并剖析了其背后人工智能的思考和迭代完善过程,展示了人工智能能快速进行游戏原型创作的巨大潜力。原文出处来自作者于2024年9月在community.aws发表的技术文章:“FromConcepttoPlaya......
  • 该怎么设计,PCB才能过100A的电流?
    更多电路设计,PCB设计分享及分析,可关注本人微信公众号“核桃设计分享”!对于没参加工作的小伙伴来说,很少能接触到高功率的项目,特别是要求在PCB上过高电流的实例。今天就和大伙聊一聊怎么设计PCB,才能满足100A的要求。首先要知道PCB都是通过整张铜箔来实现线路流通的,常规的铜箔......
  • PAT (Basic Level)--1002写出这个数
    读入一个正整数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1空格,但一行中最后一个拼音数字后没有空格。输入样......
  • 免费试用、快速上云,注册还能100%中奖,竟有这种好事?!
    活动时间内完成注册亚马逊云科技账号,即可获得以下福利免费试用,带你体验快速上云热门场景,带你手把手轻松搭建51CTO博客专属福利,100%获得精美礼品罗技无线键鼠套装绿联充电宝50元京东E卡丰厚奖品等你来拿!中奖率100%!活动时间:即日起至10月31日24时51CTO博客福利领取流程:在活动有效期内......
  • 实现0-10000之间的水仙花数
    编译方式 链接数学库  gccshuixianhua.c-oshuixianhua-lm有问题可以私信我的呀 谢谢亲们//0-10000之间的水仙花数#include<stdio.h>#include<stdlib.h>#include<math.h>intmain(){    //遍历0-10000之间的数字    for(inti=0;i<=10000;......