c++贪吃蛇代码[通俗易懂]

c++贪吃蛇代码[通俗易懂]游戏小代码

大家好,又见面了,我是你们的朋友全栈君。

最近看到很多网友寻找游戏代码,10年前火爆全网的贪吃蛇,我们可以重拾继续使用!!!!

1:首先是我们的头文件

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#define HENG 80
#define SHU 25  //HENG是宽度,SHU是高度
#define TIME 400 //初始速度

2:中间部分,也是整个代码最核心的部分

使用了我们从语言最基本的语句

void Gotoxy(int x, int y);
void Food(struct SHE *head);
int Move(struct SHE *head);
int Panduan(struct SHE *head, int x, int y);
struct SHE *Link(struct SHE *head);
void FreeShe(struct SHE *head);
void Gameover(void);
void Huitu(struct SHE *head);


int Fx, Fy;  //食物的位置
int fen;   //得分
int fang; //方向参数
int Tx, Ty;

//构造蛇身
struct SHE
{
    int x;
    int y;
    struct SHE *next;
};

int main(void)
{
    struct SHE *head = NULL;
    int temp;
    int times = TIME;
    char m;

    do
    {
        fen = 0;
        fang = 2;
        temp = fen;
        times = TIME;
        head = NULL;

        head = (struct SHE *)malloc(sizeof(struct SHE));
        head -> x = 40;
        head -> y = 13;
        head -> next = NULL;

        Food(head);

        while (1)
        {
            Huitu(head);
            Sleep(times);
            if (0 == Move(head))
            {
                FreeShe(head);
                Gameover();
                break;
            }
            if (temp < fen)
            {
                Food(head);
                temp = fen;
                if (times > 50)
                {
                    times -=10;
                }
                else if (times > 20)
                {
                    times -=1;
                }
                else
                    ;
            }
        }
        do
        {
            Gotoxy(19,21);
            printf("是否继续?Y/N:");
            scanf(" %c", &m);
            system("cls");
        } while (m != 'Y' && m != 'y' && m != 'n' && m != 'N');
    } while (m == 'Y' || m == 'y');

    return 0;
}

//绘图
void Huitu(struct SHE *head)
{
    int flag = 1;

    while (head != NULL)
    {
        Gotoxy(head -> x, head -> y);
        if (flag == 1)
        {
            printf("□");
        }
        else if (flag == 2)
        {
            printf("■");
        }
        else
            ;
        Gotoxy(Tx, Ty);
        printf(" ");
        flag++;
        head = head -> next;
    }
}

//用于光标的移动
void Gotoxy(int x, int y)
{ 
  COORD coord; 
  coord.X = x; 
  coord.Y = y; 
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//食物位置构造
void Food(struct SHE *head)
{
    int flag = 0;
    struct SHE *pr = head;

    do
    {
        srand(GetTickCount()); //基于毫秒级的随机数产生办法,需头文件winbase.h, windows中设计程序可以用windows.h
        Fx = (rand()%40) * 2;
        Fy = (rand()%12) * 2;
        flag = 0;
        pr = head;
        while (pr != NULL) //防止食物位置与蛇形重合
        {
            if (pr -> x == Fx && pr -> y == Fy)
            {
                flag = 1;
                break;
            }
            pr = pr -> next;
        }
    } while (flag == 1);
    Gotoxy(Fx, Fy);
    printf("■");
}

//用于蛇的移动
int Move(struct SHE *head)
{
    char op;
    int tox, toy, flag, temp;

    if (kbhit())  //kbhit函数用于检查当前是否有键盘输入,若有则返回一个非0值,否则返回0,需头文件conio.h
    {
        op = getch();
        switch (op)
        {
            case 'w':
                if (fang != 3)
                fang = 1;
                break;
            case 'd':
                if (fang != 4)
                fang = 2;
                break;
            case 's':
                if (fang != 1)
                fang = 3;
                break;
            case 'a':
                if (fang != 2)
                fang = 4;
                break;
            default:
                break;
        }
    }

    switch (fang)
    {
        case 1:
            toy = (head -> y) - 1;
            tox = (head -> x);
            break;
        case 2:
            tox = (head -> x) + 2;
            toy = (head -> y);
            break;
        case 3:
            toy = (head -> y) + 1;
            tox = (head -> x);
            break;
        case 4:
            tox = (head -> x) - 2;
            toy = (head -> y);
            break;
        default:
            break;
    }
    flag = Panduan(head, tox, toy);
    if (flag == 0)
    {
        return 0; //game over
    }
    else if (flag == 1)
    {
        head = Link(head);
        fen++;
    }
    else
    {
        ;
    }
    while (head != NULL)
    {
        temp = head -> x;
        head -> x = tox;
        tox = temp;

        temp = head -> y;
        head -> y = toy;
        toy = temp;

        head = head -> next;
    }
    return 1;
}

//用于食物与失败条件的判断
int Panduan(struct SHE *head, int x, int y)
{

    int flag = 1;

    if (x < 0 || x > HENG - 1 || y < 0 || y > SHU - 1)
    {
        return 0;  //撞墙
    }
    while (head != NULL)
    {
        if (head -> x == x && head -> y == y && flag != 1)
        {
            return 0; //咬到自己
        }
        flag++;
        if (head -> next == NULL)
        {
            Tx = head -> x;
            Ty = head -> y;
        }
        head = head -> next;
    }
    if (x == Fx && y == Fy)
    {
        return 1;//有食物
    }
    return 2;//无异常
}

//蛇身增长
struct SHE *Link(struct SHE *head)
{
    struct SHE *p, *pr;

    pr = head;
    p = (struct SHE *)malloc(sizeof(struct SHE));
    if (p == NULL)
    {
        printf("发生错误!!!");
        exit(0);
    }
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        while (pr -> next != NULL)
        {
            pr = pr -> next;
        }
        pr -> next = p;
    }
    p -> x = Tx;
    p -> y = Ty;
    p -> next = NULL;
    return head;
}

//释放内存
void FreeShe(struct SHE *head)
{
    struct SHE *pr = NULL;

    while (head != NULL)
    {
        pr = head;
        head = head -> next;
        free(pr);
    }
}

//游戏失败画面,如果窗口大小不是80*25导致画面不协调可舍去

3:这一部分我们将会对于代码小块进行整理,也是代码的结束:

void Gameover(void)
{
    int i,j;

    int a[SHU][HENG]={
  
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,1,1,1,0,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

    Gotoxy(0, 0);
    for (i = 0; i < SHU; i++)
    {
        for (j = 0; j< HENG; j++)
        {
            if (a[i][j] == 0)
            {
                printf(" ");
            }
            else
            {
                printf("0");
            }
        }
    }

    Gotoxy(19, 20);
    printf("游戏最终得分是 %d 分!!!\n", fen);
}

代码运行使用

VC6.0 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141551.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • docker中宿主机与容器(container)互相拷贝传递文件的方法「建议收藏」

    docker中宿主机与容器(container)互相拷贝传递文件的方法「建议收藏」转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/71425077本文出自【我是干勾鱼的博客】前面讲解过如何进入、退出docker的container。今天来讲一下在docker中宿主机与容器(container)互相拷贝传递文件的方法。1从宿主机拷贝文件到容器拷贝方式为:dockercp容器名:要拷贝的宿主机的文件名

  • sigaction 使用

    sigaction 使用函数sigaction原型:intsigaction(intsigno,conststructsigaction*restrictact,structsigaction*restrictoact);@signo信号编号@act要注册的信号动作@oact原信号动作参数structsigactionstructsigaction{

  • ubuntu ???????????? no permissions 问题解决

    ubuntu ???????????? no permissions 问题解决

  • Lambda架构和Kappa架构

    Lambda架构和Kappa架构 数据系统架构——Lambdaarchitecture(Lambda架构)传统系统的问题“我们正在从IT时代走向DT时代(数据时代)。IT和DT之间,不仅仅是技术的变革,更是思想意识的变革,IT主要是为自我服务,用来更好地自我控制和管理,DT则是激活生产力,让别人活得比你好”——阿里巴巴董事局主席马云。数据量从M的…

  • 【Java面试题】一次完整的Http请求过程(非常详细)

    【Java面试题】一次完整的Http请求过程(非常详细)当我们在浏览器的地址栏输入 www.google.com ,然后回车,回车这一瞬间到看到页面到底发生了什么呢?以下过程仅是个人理解:域名解析–>发起TCP的3次握手–>建立TCP连接后发起http请求–>服务器响应http请求,浏览器得到html代码–>浏览器解析html代码,并请求html代码中的资源(如js、css、图片等)

  • Lua和Luajit

    Lua和Luajit一、什么是lua&luaJitlua(www.lua.org)其实就是为了嵌入其它应用程序而开发的一个脚本语言,luajit(www.luajit.org)是lua的一个Just-In-Time也就是运行时编译器,也可以说是lua的一个高效版。二、优势1)lua是一个免费、小巧、简单、强大、高效、轻量级的嵌入式的脚本语言,lua当前的发行版本5.3.1只有276k。

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号