C语言小游戏贪吃蛇

2019-09-04 15:50:21 浏览数 (1)

代码语言:javascript复制
#include <stdio.h>

#include <graphics.h> //图形界面头文件

#include <time.h>

#include <conio.h>

#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")



#define N 1000 //蛇的最大节数



struct Coor //坐标

{

    int x;

    int y;

};



enum Ch{ up = 72, down = 80, left = 75, right = 77 }; //上下左右四个方向



//一条蛇 N节 数组

struct Snake

{

    int n;           //当前蛇的节数

    struct Coor scr[N]; //100节蛇的坐标数组

    enum Ch ch;         //蛇的方向

}snake;



struct Food

{

    struct Coor fsr; //食物坐标

    int flag; //标志,食物有没有被吃掉,1代表没有

}food;



void GameInit(); //初始化游戏

void SnakePaint(); //绘制蛇

void SnakeMove(); //蛇移动

void ChangeSnakeCh(); //改变蛇的方向

void FoodCoor(); //食物坐标

void FoodPaint();       //绘制食物

void EatFood(); //吃食物

void GameBreak(); //判断游戏退出

void OverGame(); //游戏结束标志



//主函数

int main()

{

    GameInit(); //初始化的函数.

    while (1)

    {

        while (!kbhit()) //kbhit(检测键盘是否有消息)

        {

            if (food.flag == 0)//没有食物

                FoodCoor(); //生成一个食物坐标

            cleardevice(); //刷新

            FoodPaint(); //绘制食物

            SnakePaint(); //绘制蛇

            SnakeMove(); //蛇移动

            Sleep(100); //蛇的移动速度

            EatFood(); //吃食物

            GameBreak(); //判断游戏是否结束

        }

        ChangeSnakeCh();

    }



    return 0;

}




//游戏初始化

void GameInit()

{

    //打开一个窗口

    initgraph(640, 480);

    srand((unsigned int)time(NULL));

    

    setbkcolor(RGB(50, 70, 80));

    cleardevice(); //使用设置好的背景颜色刷新屏幕

    



    //蛇初始化

    snake.scr[0].x = 0;     //蛇头的x,y初始化为0

    snake.scr[0].y = 0;

    snake.n = 3;            //当前有一节蛇

    snake.ch = down;        //方向往下

    

    

    //初始化一条小蛇

    //roundrect(0, 0, 10, 10, 10, 10);



    food.flag = 0; //初始化没有食物

    

}




//绘制蛇

void SnakePaint()

{

    //n节蛇 最大的下标是多少? n-1 100

    for (int i = snake.n - 1; i >= 0; i--)

    {

        setlinecolor(RGB(rand() % 100   156, rand() % 100   156, rand() % 100   156));

        rectangle(snake.scr[i].x, snake.scr[i].y, snake.scr[i].x   10, snake.scr[i].y   10);

    }

}




//蛇移动

void SnakeMove()

{

    for (int i = snake.n - 1; i > 0; i--)

    {

        snake.scr[i].x = snake.scr[i - 1].x;

        snake.scr[i].y = snake.scr[i - 1].y;

    }



    switch (snake.ch)

    {

    case up:

        snake.scr[0].y -= 10;

        break;

    case down:

        snake.scr[0].y  = 10;

        break;

    case left:

        snake.scr[0].x -= 10;

        break;

    case right:

        snake.scr[0].x  = 10;

        break;

    }

}



//人来控制 检测到按键getchar getch

//通过不同的按键(分支结构 switch if)

// 改变蛇的方向

void ChangeSnakeCh()

{

    switch (getch())

    {

    case 72: //up

        if (snake.ch != down)

            snake.ch = up;

        break;

    case 80: //down

        if (snake.ch != up)

            snake.ch = down;

        break;

    case 75:

        if (snake.ch != right)

            snake.ch = left;

        break;

    case 77:

        if (snake.ch != left)

            snake.ch = right;

        break;

    }

}



void FoodCoor() //食物坐标

{

    food.fsr.x = rand() % 64 * 10; //0 10 20 30 40 50 ... 630 0-63 *10

    food.fsr.y = rand() % 48 * 10; //0 10 20 30... 470 0-47 *10



    food.flag = 1; //食物没有被吃

}



void FoodPaint()//绘制食物

{

    setfillcolor(LIGHTRED);

    solidroundrect(food.fsr.x, food.fsr.y, food.fsr.x   10, food.fsr.y   10, 10, 10);

}



void EatFood()//吃食物

{

    if (snake.scr[0].x == food.fsr.x&&snake.scr[0].y == food.fsr.y)

    {

        snake.n  ;

        food.flag = 0;

    }

}



//判断游戏退出

void GameBreak()

{

    //撞墙

    if (snake.scr[0].y < -1 || snake.scr[0].y>471 || snake.scr[0].x < -1 || snake.scr[0].x>631)

    {

        //退出

        OverGame();

    }



    for (int i = snake.n - 1; i > 0; i--)

    {

        if (snake.scr[0].x == snake.scr[i].x&&snake.scr[0].y == snake.scr[i].y)

        {

            //退出

            OverGame();

        }

    }

}



void OverGame()

{

    cleardevice();

    settextcolor(GREEN);

    settextstyle(50, 0, L"黑体");

    outtextxy(200, 200, L"game over");

    Sleep(30000);

    exit(0);

}

声明:本文为原创,作者为 对弈,转载时请保留本声明及附带文章链接:http://www.duiyi.xyz/c实现雷霆战机-52/

0 人点赞