贪吃蛇c语言 ---实操篇

相关知识:

1.链表

2.句柄相关了解

3.简单游戏策划

简单策划篇:

一、游戏概述

贪吃蛇是一款经典的小游戏,在此贪吃蛇中,加入了彩蛋和大彩蛋,一方面提高用户的可玩性,另一方面,通过彩蛋的制作,提高了本人的代码写作能力和兴趣。

二、游戏规则

贪食蛇移动规则:↑:上移↓:无←:左移动→:右移

得分规则:吃一个食物得10分,彩蛋中吃一个加100分

获胜规则:通过前5关,后进入彩蛋关卡,达到一定分数,进入大彩蛋,游戏结束。

游戏结束规则:撞墙,通关

同时,此小游戏最终大彩蛋送给我女票,感谢让我遇见了她,遇见不一样的人生。

代码篇

//author:CO-MI

//version:4.0

//up to date:2018/7/9

//禁止转载,如需转载,请联系本人同意后即可转载

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include<math.h>

#define iX 58
#define iY 26
#define sn_len 2
#define up 8
#define left 4
#define right 6
#define down 2
#define I 20
#define R 340

typedef struct SNAKE
{
    int x;                                         ///横坐标
    int y;                                         ///纵坐标
    struct SNAKE *NEXT;
} snake;

snake *Snake_Head;///蛇头
int direction;      ///方向
int m,n;
int Score;          ///得分
int sleeptime=250;///休眠时间

snake *SNAKE_INIT();
void main();
void SetPos(int x,int y);
void createMap();
int move();
void restart();
void egg();
void bigegg();
void createmap2();
void createfood2();
void score2();
void snake_move2();

/*
函数名:void SetPos(int x,int y)
函数功能:光标设置
参数:x,y位置坐标
返回值:无
*/
void SetPos(int x,int y)                            ///设置坐标
{
    COORD tempCrd;                                  ///声明变量--坐标变量
    HANDLE Houtput;                                 ///声明变量--句柄
    tempCrd.X=x;                                    ///coord position
    tempCrd.Y=y;                                    ///coord position
    Houtput=GetStdHandle(STD_OUTPUT_HANDLE);        ///使用GetStdHandle(STD_OUTPUT_HANDLE)来获取标准输出的句柄
    SetConsoleCursorPosition(Houtput, tempCrd);     ///设置光标位置
}

/*
函数名:void createMap()
函数功能:贪吃蛇地图创建
参数:无
返回值:无
*/
void createMap()                                    ///创造地图
{
    int i;
    for(i=0; i<iX; i+=2) ///x
    {
        SetPos(i,0);
        printf("■");
        SetPos(i,26);
        printf("■");
    }
    for(i=1; i<iY; i++) ///y
    {
        SetPos(0,i);
        printf("■");
        SetPos(56,i);
        printf("■");
    }
}

/*
函数名:snake *SNAKE_INIT()
函数功能:snake的初始化
参数:无
返回值:snake的头结点
*/
snake *SNAKE_INIT()
{
    srand((unsigned)time(NULL));
    int i;
    snake *temp,*prear;
    Snake_Head = (snake *)malloc(sizeof(snake));
    Snake_Head->x=rand()%30+5;           ///设置随机生成蛇的位置
    Snake_Head->y=rand()%20+5;
    SetPos(Snake_Head->x,Snake_Head->y);
    printf("S");
    Snake_Head->NEXT=NULL;
    prear=Snake_Head;

    for(i=0; i<sn_len; i++)            ///蛇身
    {
        temp = (snake * )malloc(sizeof(snake));
        temp->x=Snake_Head->x+i+1;
        temp->y=Snake_Head->y;
        SetPos(temp->x,temp->y);
        printf("S");
        prear->NEXT=temp;
        prear=temp;
    }
    prear->NEXT=NULL;
    return Snake_Head;
}

/*
函数名:void start()
函数功能:游戏启动界面
参数:无
返回值:无
*/
void start()
{
    SetPos(15,20);
    system("pause");
    MessageBox(NULL,"are you ready?","warning",1);
}

/*
函数名:int move()
函数功能:蛇的移动
参数:无
返回值:成功返回 0 ,撞墙返回 -1
*/
int move()
{
    snake *node,*temp,*prear;
    node = (snake *)malloc(sizeof(snake));
    node->NEXT=Snake_Head;

    if(direction==up)        //上
    {
        node->x=Snake_Head->x;
        node->y=Snake_Head->y-1;
    }
    else if(direction==left)       //左
    {
        node->x=Snake_Head->x-1;
        node->y=Snake_Head->y;
    }
    else if(direction==down)   //下
    {
        node->x=Snake_Head->x;
        node->y=Snake_Head->y+1;
    }
    else if(direction==right)   //右
    {
        node->x=Snake_Head->x+1;
        node->y=Snake_Head->y;
    }
    SetPos(node->x,node->y);
    printf("S");
    Snake_Head=node;
    if(Snake_Head->x==m&&Snake_Head->y==n)
    {
        createfood();
        Score+=10;
    }
    else
    {
        temp=Snake_Head;
        while(temp->NEXT!=NULL)
        {
            prear=temp;
            temp=temp->NEXT;
        }
        SetPos(temp->x,temp->y);
        printf(" ");
        free(temp);
        prear->NEXT=NULL;
    }
    Sleep(sleeptime);
    if(Snake_Head->x==2||Snake_Head->x==iX-3||Snake_Head->y==1||Snake_Head->y==iY-1)//边界测试
    {
        return -1;
    }

    return 0;
}

/*
函数名:void createfood()
函数功能:创造食物
参数:无
返回值:无
*/
void createfood()
{
    srand((unsigned)time(NULL));
    m = rand()%52+3;        //随机生成食物
    n = rand()%22+3;
    snake *temp;
    temp=Snake_Head;
    while(temp!=NULL)//条件测试 :如果生成的食物位置在蛇身上,则重新生成食物
    {
        if(temp->x==m&&temp->y==n)
        {
            createfood();
        }
        else
        {
            temp=temp->NEXT;
        }
    }
    SetPos(m,n);
    printf("O");
}

/*
函数名:void score()
函数功能:得分界面
参数:无
返回值:无
*/
void score()
{
    SetPos(70,15);
    srand((unsigned)time(NULL));
    int chance;
    printf("score: %d ",Score);
    if(Score==0)
    {
        SetPos(70,8);
        printf("difficulty: --0-- ");
        SetPos(22,29);
        printf("Let we start!                 ");
    }
    else if(Score==100)
    {
        sleeptime=220;
        SetPos(70,8);
        printf("difficulty: --1-- ");
        SetPos(22,29);
        printf("Oh you are good!               ");
    }
    else if(Score==200)
    {
        sleeptime=200;
        SetPos(70,8);
        printf("difficulty: --2-- ");
        SetPos(22,29);
        printf("Nice men,come on!                ");
    }
    else if(Score==300)
    {
        sleeptime=150;
        SetPos(70,8);
        printf("difficulty: --3-- ");
        SetPos(22,29);
        printf("Perfect!                         ");
    }
    else if((Score==400))
    {
        sleeptime=120;
        SetPos(70,8);
        printf("difficulty: --4-- ");
        SetPos(22,29);
        printf("We just need one more steep!      ");
    }
    else if((Score==500))
    {
        sleeptime=100;
        SetPos(70,8);
        printf("difficulty: --5-- ");
        SetPos(22,29);
        printf("The truth will done!               ");
    }
    if(Score==200)
    {
        chance=rand()%5+1;
        if(chance==2)
        {
            MessageBox(NULL,"The egg is finding!","attention",1);
            egg();
        }
    }
}

/*
函数名:void main()
函数功能:主函数
参数:无
返回值:无
*/
void main()
{
    int status;
    //start();
    system("cls");
    SetPos(70,10);
    printf("space pause");
    SetPos(70,12);
    printf("up down left righ to control!");
    createMap();
    createfood();
    SNAKE_INIT();
    score();
    direction=8;
    while(1)        //获取键值
    {
get:
        if(GetAsyncKeyState(VK_UP))
        {
            if(direction==down)
                goto get;
            direction=up;
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            if(direction==right)
                goto get;
            direction=left;
        }
        else if(GetAsyncKeyState(VK_RIGHT))
        {
            if(direction==left)
                goto get;
            direction=right;
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            if(direction==up)
                goto get;
            direction=down;
        }
        else if(GetAsyncKeyState(VK_SPACE))
        {
            SetPos(70,17);
            system("pause");
        }
        status=move();
        if(status==-1)
            break;
        score();
    }
    SetPos(70,20);
    printf("game over!");
    Sleep(10000);

}
/*
函数名:void egg()
函数功能:彩蛋
参数:无
返回值:无
*/
void egg()
{
    int status;
    system("cls");
    sleeptime=150;
    createmap2();
    createfood2();
    while(1)        //获取键值
    {
get:
        if(GetAsyncKeyState(VK_UP))
        {
            if(direction==down)
                goto get;
            direction=up;
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            if(direction==right)
                goto get;
            direction=left;
        }
        else if(GetAsyncKeyState(VK_RIGHT))
        {
            if(direction==left)
                goto get;
            direction=right;
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            if(direction==up)
                goto get;
            direction=down;
        }
        else if(GetAsyncKeyState(VK_SPACE))
        {
            SetPos(70,17);
            system("pause");
        }
        snake_move2();
        score2();
        if(Score>=1000)
        {
            SetPos(70,20);
            printf("game pass");
            bigegg();
            break;
        }
    }
}
void snake_move2()
{
    snake *node,*temp,*prear;
    node = (snake *)malloc(sizeof(snake));
    node->NEXT=Snake_Head;

    if(direction==up)        //上
    {
        node->x=Snake_Head->x;
        node->y=Snake_Head->y-1;
    }
    else if(direction==left)       //左
    {
        node->x=Snake_Head->x-1;
        node->y=Snake_Head->y;
    }
    else if(direction==down)   //下
    {
        node->x=Snake_Head->x;
        node->y=Snake_Head->y+1;
    }
    else if(direction==right)   //右
    {
        node->x=Snake_Head->x+1;
        node->y=Snake_Head->y;
    }
    SetPos(node->x,node->y);
    printf("S");
    Snake_Head=node;
    if(Snake_Head->x==m&&Snake_Head->y==n)
    {
        createfood();
        Score+=100;
    }
    else
    {
        temp=Snake_Head;
        while(temp->NEXT!=NULL)
        {
            prear=temp;
            temp=temp->NEXT;
        }
        SetPos(temp->x,temp->y);
        printf(" ");
        free(temp);
        prear->NEXT=NULL;
    }
    Sleep(100);
    return 0;
}
void score2()
{
    int i;
    SetPos(70,15);
    srand((unsigned)time(NULL));
    printf("score: %d ",Score);
    sleeptime=60;
    SetPos(70,8);
    printf("difficulty: --6--");
}

/*
函数名:void createmap2()
函数功能:地图2
参数:无
返回值:无
*/
void createmap2()
{
    int i;
    for(i=0; i<15; i+=2) ///x
    {
        SetPos(i,0);
        printf("■");
        SetPos(i,20);
        printf("■");
    }
    for(i=1; i<20; i++) ///y
    {
        SetPos(0,i);
        printf("■");
        SetPos(14,i);
        printf("■");
    }
}
void createfood2()
{
    srand((unsigned)time(NULL));
    m = rand()%55+3;        //随机生成食物
    n = rand()%25+3;
    snake *temp;
    temp=Snake_Head;
    while(temp!=NULL)//条件测试 :如果生成的食物位置在蛇身上,则重新生成食物
    {
        if(temp->x==m&&temp->y==n)
        {
            createfood();
        }
        else
        {
            temp=temp->NEXT;
        }
    }
    Score+=100;
    SetPos(m,n);
    printf("O");
}

void bigegg()
{
    system("cls");
    int i,j,e;
    int a;
    long time;
    for(i=1,a=I; i<I/2; i++,a--)
    {
        for(j=(int) ( I-sqrt(I*I-(a-i)*(a-i)) ); j>0; j--)
            printf(" ");
        for(e=1; e<=2*sqrt(I*I-(a-i)*(a-i)); e++)
            printf("\3");
        for(j=(int)
                ( 2*( I-sqrt(I*I-(a-i)*(a-i)) ) ); j>0; j--)
            printf(" ");
        for(e=1; e<=2*sqrt(I*I-(a-i)*(a-i)); e++)
            printf("\3");
        printf("\n");
    }
    for(i=1; i<80; i++)
    {
        if(i==25)
        {
            printf("------------------------------");
            i+=30;
        }
        printf("\3");
    }
    printf("\n");
    for(i=1; i<=R/2; i++)
    {
        if(i%2||i%3)
            continue;
        for(j=(int) ( R-sqrt(R*R-i*i) ); j>0; j--)
            printf(" ");
        for(e=1; e<=2*( sqrt(R*R-i*i) - (R-2*I) ); e++)
            printf("\3");
        printf("\n");
    }
    for(; ; )
    {

        system("color b");///blue
        for(time=0; time<99999999; time++);
        system("color c");///red
        for(time=0; time<99999999; time++);
        system("color d");///紫色
        for(time=0; time<99999999; time++);
    }
}




同时最后,本文中有些函数相似度极高,但由于作者本人偷懒,并没有做相关的修改,有兴趣的同学可以@我,欢迎大家点评纠正,大家共同进步。


猜你喜欢

转载自blog.csdn.net/qq_25233621/article/details/80977566
今日推荐