贪吃蛇小游戏

这里写图片描述

#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#define W_S_F "■"                       //地图&&蛇&&食物 占两个字符位置
#define MapLength 50                    //地图长度
#define MapHeigth 35                    //地图高度
#define U 1                             //上
#define D 2                             //下
#define L 3                             //左
#define R 4                             //右


typedef struct LNode                    //蛇节点
{
    int x,y;                            //蛇以地图左下角为原点的每个节点坐标(x,y)
    struct LNode *next;
}LNode, *Snake;     
typedef struct position                 //坐标
{
    int x, y;
}PosValue, Food;    

int end;
int sleeptime = 200;                    //运行间隔时间
int endstate;                           //游戏结束状态
int score;                              //游戏得分
Snake Shead;                            //蛇头节点
Food  food;                             //食物坐标
void Position(int x, int y);            //光标定位函数
void CreatMap(int Length, int Heigth);  //创建地图
void Welcome();                         //游戏欢迎界面
void StartGame();                       //开始游戏
void CircleGame();                      //循环游戏 游戏按键检测函数
void EndGame();                         //结束游戏
void InitSnake();                       //初始化蛇
PosValue ChangePosition(int x,int y);   //相对坐标转绝对坐标
void CreatFood();                       //随机生成食物
void SnakeMove(int state);              //蛇移动
void Pause();                           //暂停
int CrossWall();                        //撞墙检测
int CrossItself();                      //咬到自己检测
void SetColor(int c);                   //设置颜色
void AutoMove();

int main()
{
    int choose;
    printf("请选择游戏模式\n1:手动模式   2:自动模式\n");
    scanf("%d", &choose);
    if (choose == 1)
    {
        StartGame();
        CircleGame();
    }
    else if(choose==2)
    {
        while (1)
        {
            StartGame();
            AutoMove();
        }
    }
    system("pause");
    return 0;
}
void StartGame()
{
    score = 0;
    endstate = 0;
    end = 0;
    Welcome();
    CreatMap(MapLength, MapHeigth);
    InitSnake();
    CreatFood();
    Position(1, 40);
}
void CircleGame()
{
    int state=0;
    while (1)
    {
        if (GetAsyncKeyState(VK_UP)&&state!=D)          state = U;
        else if (GetAsyncKeyState(VK_DOWN)&&state!=U)   state = D;
        else if (GetAsyncKeyState(VK_LEFT)&&state!=R)   state = L;
        else if (GetAsyncKeyState(VK_RIGHT)&&state!=L)  state = R;
        else if (GetAsyncKeyState(VK_SPACE))            Pause();
        else if (GetAsyncKeyState(VK_ESCAPE))           { endstate = 1; EndGame(); }
        Sleep(sleeptime);
        SnakeMove(state);
        if (end)
            break;
    }
}

void EndGame()
{
    end = 1;
    system("cls");
    Position(10, 9);
    if (endstate == 1) { printf("您已经结束游戏"); }
    else if (endstate == 2) { printf("对不起,您撞到墙了。游戏结束"); }
    else if (endstate == 3) { printf("对不起,您咬到自己了。游戏结束"); }
    Position(10, 10);
    printf("您的得分是%d", score);
    Position(10, 11);
    Sleep(1000);
    //system("pause");
    //exit(0);
}
void SnakeMove(int state)
{
    int ish = 0;
    Snake Q, P, Sbody;
    PosValue p, tail, AutoNode;
    tail.x = 1;
    tail.y = 1;
    Q = Shead;
    if (state != 0)
    {
        AutoNode.x = Shead->x;      //自动移动部分代码
        AutoNode.y = Shead->y;
        if (state == U) AutoNode.y++;
        if (state == D) AutoNode.y--;
        if (state == L) AutoNode.x--;
        if (state == R) AutoNode.x++;
        if (AutoNode.x == 0 || AutoNode.x == MapLength - 1)
            return 1;
        if (AutoNode.y == 0 || AutoNode.y == MapHeigth - 1)
            return 1; 


        while (Q->next != NULL)         
        {
            P = Q;
            Q = Q->next;
        }
        tail.x = Q->x;
        tail.y = Q->y;  //记录尾巴坐标
        P->next = NULL; 
        Q->next = Shead;//把尾巴节点转换为头节点
        Shead = Q;
        Shead->x = Shead->next->x;
        Shead->y = Shead->next->y;
        if (state == U) Shead->y++;
        if (state == D) Shead->y--;
        if (state == L) Shead->x--;
        if (state == R) Shead->x++;                 //设置头节点坐标
        if (Shead->x == food.x&&Shead->y == food.y) //吃到食物
        {
            Q = Shead;
            while (Q->next != NULL)
            {
                P = Q;
                Q = Q->next;
            }
            Sbody = (LNode *)malloc(sizeof(LNode));
            if (P->x == Q->x)
            {
                Sbody->x = Q->x;
                if (Q->y > P->y)
                    Sbody->y = Q->y + 1;
                else
                    Sbody->y = Q->y - 1;
            }
            else if(P->y==Q->y)
            {
                Sbody->y = Q->y;
                if (Q->x > P->x)
                    Sbody->x = Q->x + 1;
                else
                    Sbody->x = Q->x - 1;
            }
            Q->next = Sbody;
            Sbody->next = NULL; //添加节点
            CreatFood();
            score++;
        }

    }
    while (Q != NULL)
    {
        p = ChangePosition(Q->x, Q->y);
        Position(p.x, p.y);
        printf(W_S_F);
        Q = Q->next;
    }
    p= ChangePosition(tail.x, tail.y);
    Position(p.x, p.y);
    printf("  ");
    Position(103, 1);
    SetColor(15);
    printf("得分=%d", score);
    SetColor(7);
    /*if (CrossWall())
    {
        endstate = 2;
        EndGame();
    }
    if (CrossItself())
    {
        endstate = 3;
        EndGame();
    }*/

}
void Welcome()
{
    system("mode con cols=150 lines=45");
    Position(10, 10);
    printf("Welcome to the snake game\n");
    Position(1, 15);
    Sleep(1000);
    system("cls");
}
PosValue ChangePosition(int x, int y)
{
    PosValue p;
    p.x = 2*x+1;
    p.y = MapHeigth-y;
    return p;
}
void InitSnake()
{
    int i;
    Snake Sbody,Q;
    PosValue p;
    Shead = (LNode *)malloc(sizeof(LNode));
    Shead->x = 1;
    Shead->y = 10;
    Shead->next = NULL;
    i = 5;
    while (i--)
    {
        Sbody = (LNode *)malloc(sizeof(LNode));
        Sbody->next = Shead->next;
        Sbody->x = Shead->x;
        Sbody->y = Shead->y;
        Shead->x++;
        Shead->next = Sbody;
    }
    Q = Shead;
    while (Q != NULL)
    {
        p = ChangePosition(Q->x, Q->y);
        Position(p.x, p.y);
        printf(W_S_F);
        Q = Q->next;
    }
}
void Position(int x, int y)
{
    COORD pos;
    HANDLE hOutput;
    pos.X = x;
    pos.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleCursorPosition(hOutput, pos);
}
void CreatMap(int Length, int Heigth)
{
    int i;
    system("cls");
    for (i = 1; i <= Length*2; i += 2)
    {
        Position(i, 1);
        printf(W_S_F);
        Position(i, Heigth);
        printf(W_S_F);
    }
    for (i = 1; i <= Heigth; i++)
    {
        Position(1, i);
        printf(W_S_F);
        Position(Length * 2-1, i);
        printf(W_S_F);
    }
    Position(103, 10);
    printf("请用↑↓← →控制蛇的移动");
    Position(103, 11);
    printf("空格暂停");
    Position(103, 12);
    printf("ESC结束游戏");
    Position(115,35);
    printf("-----LC");

}
void CreatFood()
{
    int f;
    srand((unsigned)time(NULL));
    Snake Q;
    Q = Shead;
    PosValue p;
    while (1)
    {
        f = 1;
        food.x = rand() % (MapLength - 2) + 1;
        food.y = rand() % (MapHeigth - 2) + 1;

        p = ChangePosition(food.x, food.y);
        while (Q)
        {
            if (Q->x == p.x&&Q->y == p.y)
            {
                break;
                f = 0;
            }
            Q = Q->next;
        }
        if (f == 1)
            break;
    }
    Position(p.x, p.y);
    printf(W_S_F);

}
void Pause()
{
    while (1)
    {
        Sleep(100);
        if (GetAsyncKeyState(VK_SPACE))
            break;
    }
}
int CrossWall()
{
    Snake Q;
    Q = Shead;
    if (Q->x == 0 || Q->x == MapLength-1)
        return 1;
    if (Q->y == 0 || Q->y == MapHeigth - 1)
        return 1;
    return 0;
}
int CrossItself()
{
    Snake Q,P;
    Q = Shead;
    P = Shead->next;
    while (P)
    {
        if (Q->x == P->x&&Q->y == P->y)
            return 1;
        else
            P = P->next;
    }
    return 0;
}
void SetColor(int c)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void AutoMove()
{
    int state = 0;
    int f;
    srand((unsigned)time(NULL));
    while (1)
    {
        f = rand() % 5;
        if (f==U && state != D)               state = U;
        else if (f==D && state != U)          state = D;
        else if (f==L && state != R)          state = L;
        else if (f==R && state != L)          state = R;
        else if (GetAsyncKeyState(VK_SPACE))  Pause();
        else if (GetAsyncKeyState(VK_ESCAPE)) { endstate = 1; EndGame(); }
        Sleep(sleeptime);
        SnakeMove(state);
        if (end)
            break;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>