C语言---一只不会死的贪吃蛇

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define SNAKE_NUM 500
enum DIR
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
};

struct Snake
{
    int size;
    int dir;
    int speed;
    POINT coor[SNAKE_NUM];
}snake;

struct Food
{
    int x;
    int y;
    int r;  
    bool flag; 
    DWORD color; 
 }food;
//数据的初始化
void GameImit()
{

    initgraph(640, 480/*SHOWCONSOLE*/);
   
    srand(GetTickCount());

    snake.size = 3;
    snake.speed = 10;
    snake.dir = RIGHT;
    for (int i = 0; i < snake.size; i++) {
        snake.coor[i].x = 40-10*i;
        snake.coor[i].y = 10;
    }
   2 % 5 ==1  //0--4
  
    food.x = rand() % 640;
    food.y = rand() % 480;
    food.color = RGB(rand() % 256, rand() % 256, rand() % 256, );
    food.r = rand() % 10 + 5;
    food.flag = true;
}
void GameDraw()
{
    //双缓冲绘图
    BeginBatchDraw();

    setbkcolor(RGB(27, 115, 119));
    cleardevice();

    setfillcolor(RED);
    for (int i = 0; i < snake.size; i++)
    {
        solidcircle(snake.coor[i].x, snake.coor[i].y, 5);
    }

    if (food.flag)
    {
        solidcircle(food.x, food.y, food.r);
    }
    EndBatchDraw();
}

void snakeMove()
{

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

        snake.coor[i] = snake. coor[i - 1];
    }
    switch (snake.dir)
    {
    case UP:
        snake.coor[0].y-=snake.speed;
        if (snake.coor[0].y+10 <= 0)
        {
            snake.coor[0].y = 480;
        }
        break;
    case DOWN:
        snake.coor[0].y+= snake.speed;
        if (snake.coor[0].y - 10 >= 480)
        {
            snake.coor[0].y = 0;
        }
        break;
    case LEFT:
        snake.coor[0].x-= snake.speed;
        if (snake.coor[0].x+10 <= 0)
        {
            snake.coor[0].x = 640;
        }
        break;
    case RIGHT:
        snake.coor[0].x+= snake.speed;
        if (snake.coor[0].x + 10 >= 640)
        {
            snake.coor[0].x = 0;
        }
        break;
    }
        
    
}

void keyControl()//阻塞函数
{

    if (_kbhit()) {
        //72 80 75 77上下左右键值
        switch (_getch())
        {
        case 'w':
        case'W':
        case 72:
     
            if (snake.dir != DOWN)
            {
                snake.dir = UP;
            }
            break;
        case 's':
        case'S':
        case 80:
            if (snake.dir != UP)
            {
                snake.dir = DOWN;
            }
            break;
        case 'a':
        case'A':
        case 75:
            if (snake.dir != RIGHT)
            {
                snake.dir = LEFT;
            }
            break;
        case 'd':
        case'D':
        case 77:
            if (snake.dir != LEFT)
            {
                snake.dir = RIGHT;
            }
            break;
        case ' ':     
            while (1)
            {
                if (_getch() == ' ')
                    return;
            }
            break;
        }
    }
}
int EatFood(int i)
{

    if (food.flag && snake.coor[0].x >= food.x-food.r && snake.coor[0].x <= food.x + food.r&&snake.coor[0].y>=food.y-food.r
        &&snake.coor[0].y<=food.y+food.r)
    {
        food.flag = false;
        snake.size++;
    }

    if (!food.flag) {
        food.x = rand() % 640;
        food.y = rand() % 480;
        food.color = RGB(rand() % 256, rand() % 256, rand() % 256, );
        food.r = rand() % 10 + 5;
        food.flag = true;
    
    }
 

}


int main()
{


    GameImit();
    while (1)
    {
        snakeMove();
        GameDraw();
        keyControl();
        EatFood(n);
           Sleep(100);

    }


    return 0;
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值