跟着童晶老师学c语言//贪吃蛇

本文介绍了使用C++编写的控制台版蛇游戏,包括游戏初始化、画布操作、移动逻辑、升级点检测和用户输入处理。程序展示了如何在命令行环境中创建一个经典的Snake游戏体验。
摘要由CSDN通过智能技术生成

#include<bits/stdc++.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <dos.h>
#define Speed 4
#define High 29
#define Width 116
#define F_long 8
#define Setwall 5   //设置障碍数 

using namespace std;

int canvas[High][Width];    //显示游戏画布
                     //0是空格,-1是边框#,1是蛇头@
                     //  >1是蛇身* 
int speed;
int iskill;
int up_x,up_y;            //升级点 
int wall_x,wall_y;        //墙 
int Long;
int move;            //数字表示方向 
 
void gotoxy(int x, int y)   //光标移动到x,y位置 
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);          //两个参数分别是指定哪个窗体,具体位置
}

void HideCursor() // 用于隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//函数和结构体都在windows.h中定义。
}

void setup()
{
    up_x=rand()%High;
    up_y=rand()%Width;
}

void setwall()
{
    wall_x=rand()%High;
    wall_y=rand()%Width;
    for(int i=0;i<High;i++)
    {
        for(int x=0;x<Width;x++)
        {
            if(wall_x==i&&wall_x==x)
            {
                if(canvas[i][x]!=0)
                {
                    setwall();
                    i=0;x=0;
                    break;
                }
            }
        }
    }
        canvas[wall_x][wall_y]=-1;
}

void checkup()   //检查升级点 
{
        for(int i=0;i<High;i++)
    {
        for(int x=0;x<Width;x++)
        {
            if(up_x==i&&up_y==x)
            {
                if(canvas[i][x]!=0)
                {
                //    canvas[up_x][up_y]=0;
                    setup();
                    i=0;x=0;
                    break;
                }
            }
        }
    }
        canvas[up_x][up_y]=-2;
}

void Move_she()
{
//    int old_x,old_y;
    int out=0; 
    for(int i=1;i<High-1;i++)
    {
        for(int x=1;x<Width-1;x++)
        {
            if(canvas[i][x]>0)
            canvas[i][x]++;
            if(canvas[i][x]>
            Long+1) canvas[i][x]=0;
        }
    }
    for(int i=1;i<High-1;i++)
    {
        for(int x=1;x<Width-1;x++)
        {
            if(canvas[i][x]==2)
            {
                if(move==1)
                {
                    if(canvas[i-1][x]==-2)
                    {
                        Long++;
                        canvas[i-1][x]=1;
                        checkup();
                        for(int p=0;p<Setwall;p++)
                            setwall();
                        break;
                    }
                    if(canvas[i-1][x]==0)
                    canvas[i-1][x]=1;
                    else iskill=1;
                }
                if(move==2)
                {
                    if(canvas[i][x+1]==-2)
                    {
                        Long++;
                        canvas[i][x+1]=1;
                        checkup();
                        for(int p=0;p<Setwall;p++)
                            setwall();
                        break;
                    }
                    if(canvas[i][x+1]==0)
                    canvas[i][x+1]=1;
                    else iskill=1;
                }
                if(move==3)
                {
                    if(canvas[i+1][x]==-2)
                    {
                        Long++;
                        canvas[i+1][x]=1;
                        checkup();
                        for(int p=0;p<Setwall;p++)
                            setwall();
                        break;
                    }
                    if(canvas[i+1][x]==0)
                    canvas[i+1][x]=1;
                    else iskill=1;
                }
                if(move==4)
                {
                    if(canvas[i][x-1]==-2)
                    {
                        Long++;
                        canvas[i][x-1]=1;
                        checkup();
                        for(int p=0;p<Setwall;p++)
                            setwall();
                        break;
                    }
                    if(canvas[i][x-1]==0)
                    canvas[i][x-1]=1;
                    else iskill=1;
                }
            }
    //        if(out==1) break;
        }
    //    if(out==1) break;
    }
    
    
}

void startup()
{
    srand(time(NULL));
    HideCursor();
    speed=0;
    iskill=0;
    Long=F_long;
    memset(canvas,0,sizeof(canvas));
    //初始化边框 
    for(int i=0;i<High;i++)
    {
        canvas[i][0]=-1;
        canvas[i][Width-1]=-1;
    }
    for(int i=0;i<Width;i++)
    {
        canvas[0][i]=-1;
        canvas[High-1][i]=-1;
    }
    //初始化小蛇
     canvas[High/2][Width/2]=1;
     for(int i=1;i<=F_long;i++)
         canvas[High/2][Width/2-i]=1+i;
    //设置升级点 
    setup();
    checkup();
}

void show()

    gotoxy(0,0);
    for(int i=0;i<High;i++)
        {
            for(int j=0;j<Width;j++)
            {
                if(canvas[i][j]==-1)
                cout<<'#';
                if(canvas[i][j]==0)
                cout<<' ';
                if(canvas[i][j]==1)
                cout<<'@';
                if(canvas[i][j]>1)
                cout<<'*';
                if(canvas[i][j]==-2)
                cout<<'$'; 
            }
            printf("\n");
        }
        cout<<"当前长度"<<Long; 
    if(iskill==1)
    {
        cout<<endl<<"呜呜,失败了\n"<<
        "输入空格退出"<<endl;
        char input;
        while(1)
        {
            input=getch();
            if(input==' ')
                break;
        }
//         system("pause");
        exit(0);
    } 
    
//    printf("*");
}

void updateWithoutInput()
{
    if(move!=0) Move_she();
}

void updateWithInput()

     char input;
     if(kbhit())
     {
     input=getch();
    if(input=='a') move=4;
    if(input=='w') move=1;
    if(input=='d') move=2;
    if(input=='s') move=3;
    if(input==27) system("pause");
    }
//     Sleep(50);
}

int main()
{
    startup();   //数据初始化 
    while(1)     //游戏循环执行 
    {
        show();        //游戏画面 
        updateWithoutInput();    //与输入无关更新 
        updateWithInput();        //与输入有关更新 
    }
    return 0;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值