CLION 基于EasyX的贪吃蛇小游戏(数组)

       

目录

一、定义需要用的属性:

0.点的定义:

1.蛇的属性定义:

2.食物的定义:

3.其他的一些定义:

二、编写函数

1.初始化蛇

2.画蛇

3.让蛇运动

4.键盘控制

5.食物初始化

6.画食物

7.食物刷新

8.蛇吃食物

9.设置分数

10.判断蛇死以及穿墙处理

11.主函数

三、完整代码

四、运行结果

五、相关程序


        寒假打算用STM32做一个贪吃蛇小游戏,现在电脑上实现基本的功能,最后再用硬件(单片机)去实现。

        数据结构直接使用的数组,后面可能会写一个用链表实现的贪吃蛇,先从简单的开始。

        完整代码在末尾,直接复制即可运行

一、定义需要用的属性:

0.点的定义:

typedef struct pointXY{
    int x;
    int y;
}MyPoint;

1.蛇的属性定义:

typedef struct Snake{
    int size;
    MyPoint xy[100];
    char dir;
}snake;

2.食物的定义:

typedef struct Food{
    MyPoint foodpoint;
    int flag;
    int grade;
}food;

3.其他的一些定义:

#define up 87//W
#define down 83//S
#define left 65//A
#define right 68//D
#define screen_long 640//x
#define screen_width 480//y
#define Snake_Init_Size 5
HWND hwnd=NULL;

二、编写函数

1.初始化蛇

void Snake_Init(void )
{
    mysnake.dir=right;
    mysnake.size=Snake_Init_Size;
    mysnake.xy[2].x=screen_long;
    mysnake.xy[2].y=screen_long/2;

    mysnake.xy[1].x=screen_long+10;
    mysnake.xy[1].y=screen_long/2;

    mysnake.xy[0].x=screen_long+20;
    mysnake.xy[0].y=screen_long/2;

}

2.画蛇

void Snake_Draw(void )
{
    for (int i = 0; i < mysnake.size; ++i) {
        setlinecolor(RGB(0 ,134 ,139));
        setfillcolor(RGB(0 ,245 ,255));
        fillrectangle(mysnake.xy[i].x,mysnake.xy[i].y,mysnake.xy[i].x+10,mysnake.xy[i].y+10);
    }
}

3.让蛇运动

void Snake_Move(void)
{
    for (int i =  mysnake.size-1; i > 0; i--) {
        mysnake.xy[i].x=mysnake.xy[i-1].x;
        mysnake.xy[i].y=mysnake.xy[i-1].y;
    }
    switch (mysnake.dir) {
        case right:
            mysnake.xy[0].x+=10;
            break;
        case left:
            mysnake.xy[0].x-=10;
            break;
        case up:
            mysnake.xy[0].y-=10;
            break;
        case down:
            mysnake.xy[0].y+=10;
            break;
        default:
            mysnake.dir=mysnake.dir;
            break;
    }
    Snake_Draw();
}

4.键盘控制

void Get_Key(void )
{
    char KeyNum;
    KeyNum=getch();
    switch (mysnake.dir)
    {
        case right:
            if (KeyNum==up)
                mysnake.dir=up;
            else
                mysnake.dir=down;
            break;
        case left:
            if (KeyNum==up)
                mysnake.dir=up;
            else
                mysnake.dir=down;
            break;
        case up:
            if (KeyNum==right)
                mysnake.dir=right;
            else
                mysnake.dir=left;
            break;
        case down:
            if (KeyNum==right)
                mysnake.dir=right;
            else
                mysnake.dir=left;
            break;
    }

}

5.食物初始化

void Food_Init(void )
{
    myfood.foodpoint.x=rand()%(screen_long/10)*10;
    myfood.foodpoint.y=rand()%(screen_width/10)*10;
    myfood.flag=1;
    myfood.grade=0;
}

6.画食物

void Food_Draw(void )
{
    setlinecolor(RGB(0 ,134 ,139));
    setfillcolor(RGB(0 ,200 ,220));
    fillrectangle(myfood.foodpoint.x,myfood.foodpoint.y,myfood.foodpoint.x+10,myfood.foodpoint.y+10);
}

7.食物刷新

void Food_Refresh(void )
{
    myfood.foodpoint.x=rand()%(screen_long/10)*10;
    myfood.foodpoint.y=rand()%(screen_width/10)*10;
}

8.蛇吃食物

void Food_Eat(void )
{
    if((myfood.foodpoint.x==mysnake.xy[0].x)&&(myfood.foodpoint.y==mysnake.xy[0].y))
    {
        mysnake.size+=3;
        Food_Refresh();
    }
}

9.设置分数

void Grade_Show(void )
{
    char grade[100]="";
    sprintf(grade,"%d",mysnake.size*10-Snake_Init_Size*10);
    settextcolor(RGB(0 ,150 ,150));
    outtextxy(screen_long-100,20,"Grade:");
    outtextxy(screen_long-40,20,grade);

}

10.判断蛇死以及穿墙处理

void Snake_Die(void )
{
    for (int i = mysnake.size-1; i >0 ; i--)
    {
        if((mysnake.xy[0].x==mysnake.xy[i].x)&&(mysnake.xy[0].y==mysnake.xy[i].y))
        {
            outtextxy(200,200,"eat yourself!");
            MessageBox(hwnd,"Game Over!","eat yourself",MB_OK);
            mysnake.size=Snake_Init_Size;
            return;
        }
    }
    if(mysnake.xy[0].x>screen_long)
    {
        mysnake.xy[0].x=mysnake.xy[0].x-screen_long-10;
    } else if (mysnake.xy[0].y>screen_width)
    {
        mysnake.xy[0].y=mysnake.xy[0].y-screen_width-10;
    }else if (mysnake.xy[0].x<0)
    {
        mysnake.xy[0].x=mysnake.xy[0].x+screen_long;
    }else if (mysnake.xy[0].y<0)
    {
        mysnake.xy[0].y=mysnake.xy[0].y+screen_width;
    }
}

11.主函数

int main()
{
    srand((unsigned int)time(NULL));
    hwnd=initgraph(screen_long,screen_width);
    setbkcolor(WHITE);

    Snake_Init();
    Food_Init();
    cleardevice();

    while (1)
    {
        Grade_Show();
        Snake_Move();
        Food_Draw();
        Food_Eat();
        Snake_Die();
        while (kbhit())
        {
            Get_Key();
        }
        Sleep(50);
        cleardevice();
    }

    getch();
    closegraph();

    return 0;
}

三、完整代码

#include 		// 引用图形库头文件
#include 
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
using namespace std;
//坐标属性
typedef struct pointXY{
    int x;
    int y;
}MyPoint;
//蛇的属性
typedef struct Snake{
    int size;
    MyPoint xy[100];
    char dir;
}snake;
//食物
typedef struct Food{
    MyPoint foodpoint;
    int flag;
    int grade;
}food;
//定义变量
snake mysnake;
food  myfood;
#define up 87//W
#define down 83//S
#define left 65//A
#define right 68//D
#define screen_long 640//x
#define screen_width 480//y
#define Snake_Init_Size 5
HWND hwnd=NULL;
//初始化蛇
void Snake_Init(void )
{
    mysnake.dir=right;
    mysnake.size=Snake_Init_Size;
    mysnake.xy[2].x=screen_long;
    mysnake.xy[2].y=screen_long/2;

    mysnake.xy[1].x=screen_long+10;
    mysnake.xy[1].y=screen_long/2;

    mysnake.xy[0].x=screen_long+20;
    mysnake.xy[0].y=screen_long/2;

}
//画蛇
void Snake_Draw(void )
{
    for (int i = 0; i < mysnake.size; ++i) {
        setlinecolor(RGB(0 ,134 ,139));
        setfillcolor(RGB(0 ,245 ,255));
        fillrectangle(mysnake.xy[i].x,mysnake.xy[i].y,mysnake.xy[i].x+10,mysnake.xy[i].y+10);
    }
}
//蛇动
void Snake_Move(void)
{
    for (int i =  mysnake.size-1; i > 0; i--) {
        mysnake.xy[i].x=mysnake.xy[i-1].x;
        mysnake.xy[i].y=mysnake.xy[i-1].y;
    }
    switch (mysnake.dir) {
        case right:
            mysnake.xy[0].x+=10;
            break;
        case left:
            mysnake.xy[0].x-=10;
            break;
        case up:
            mysnake.xy[0].y-=10;
            break;
        case down:
            mysnake.xy[0].y+=10;
            break;
        default:
            mysnake.dir=mysnake.dir;
            break;
    }
    Snake_Draw();
}
//键盘控制
void Get_Key(void )
{
    char KeyNum;
    KeyNum=getch();
    switch (mysnake.dir)
    {
        case right:
            if (KeyNum==up)
                mysnake.dir=up;
            else
                mysnake.dir=down;
            break;
        case left:
            if (KeyNum==up)
                mysnake.dir=up;
            else
                mysnake.dir=down;
            break;
        case up:
            if (KeyNum==right)
                mysnake.dir=right;
            else
                mysnake.dir=left;
            break;
        case down:
            if (KeyNum==right)
                mysnake.dir=right;
            else
                mysnake.dir=left;
            break;
    }

}
void Food_Init(void )
{
    myfood.foodpoint.x=rand()%(screen_long/10)*10;
    myfood.foodpoint.y=rand()%(screen_width/10)*10;
    myfood.flag=1;
    myfood.grade=0;
}
void Food_Draw(void )
{
    setlinecolor(RGB(0 ,134 ,139));
    setfillcolor(RGB(0 ,200 ,220));
    fillrectangle(myfood.foodpoint.x,myfood.foodpoint.y,myfood.foodpoint.x+10,myfood.foodpoint.y+10);
}
void Food_Refresh(void )
{
    myfood.foodpoint.x=rand()%(screen_long/10)*10;
    myfood.foodpoint.y=rand()%(screen_width/10)*10;
}
void Food_Eat(void )
{
    if((myfood.foodpoint.x==mysnake.xy[0].x)&&(myfood.foodpoint.y==mysnake.xy[0].y))
    {
        mysnake.size+=3;
        Food_Refresh();
    }
}
void Grade_Show(void )
{
    char grade[100]="";
    sprintf(grade,"%d",mysnake.size*10-Snake_Init_Size*10);
    settextcolor(RGB(0 ,150 ,150));
    outtextxy(screen_long-100,20,"Grade:");
    outtextxy(screen_long-40,20,grade);

}
void Snake_Die(void )
{
    for (int i = mysnake.size-1; i >0 ; i--)
    {
        if((mysnake.xy[0].x==mysnake.xy[i].x)&&(mysnake.xy[0].y==mysnake.xy[i].y))
        {
            outtextxy(200,200,"eat yourself!");
            MessageBox(hwnd,"Game Over!","eat yourself",MB_OK);
            mysnake.size=Snake_Init_Size;
            return;
        }
    }
    if(mysnake.xy[0].x>screen_long)
    {
        mysnake.xy[0].x=mysnake.xy[0].x-screen_long-10;
    } else if (mysnake.xy[0].y>screen_width)
    {
        mysnake.xy[0].y=mysnake.xy[0].y-screen_width-10;
    }else if (mysnake.xy[0].x<0)
    {
        mysnake.xy[0].x=mysnake.xy[0].x+screen_long;
    }else if (mysnake.xy[0].y<0)
    {
        mysnake.xy[0].y=mysnake.xy[0].y+screen_width;
    }
}
int main()
{
    srand((unsigned int)time(NULL));
    hwnd=initgraph(screen_long,screen_width);
    setbkcolor(WHITE);

    Snake_Init();
    Food_Init();
    cleardevice();

    while (1)
    {
        Grade_Show();
        Snake_Move();
        Food_Draw();
        Food_Eat();
        Snake_Die();
        while (kbhit())
        {
            Get_Key();
        }
        Sleep(50);
        cleardevice();
    }

    getch();
    closegraph();

    return 0;
}

四、运行结果

五、相关程序

链接:https://pan.baidu.com/s/1wmXdD58zWndge0vu_6j-4g 
提取码:yhnz

你可能感兴趣的:(c语言)