tyro_ck
2021-12-03 11:17
采纳率: 100%
浏览 44

利用EasyX的C语言贪吃蛇小游戏,出了好多问题,大家提提建议,改一改可以吗?

1.蛇头方向无法改变
         2.蛇无法自动移动
         3.在敲击键盘后,蛇会消失,再次敲击才会出现
         4.问题还有很多。。。
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#define SIZE 10//蛇每一节和食物的大小
#define N 1000//蛇的最大节数
//坐标结构体
struct Coor
{
    int x;
    int y;
};
//枚举上下左右四个方向
enum Ch
{
    up = 119,
    down =115,
    left = 97,
    right = 100
};
//蛇的结构体
struct Snake
{
    int n;
    Coor scr[N];
    Ch ch;//蛇的方向
}snake;
//食物的结构体
struct Food
{
    Coor fcr;//食物坐标
    int flag;//食物有没有被吃掉 1代表没有
    int grade = 0;
}food;
IMAGE snakeleft, snakeright, snakeup, snakedown, snakeimage, foodimage;
void GameInit();
void FoodCoor();
void FoodPaint(IMAGE foodimage);
void Direction();
void SnakeMove();
void SnakePaint(IMAGE snakeright, IMAGE snakeleft, IMAGE snakeup, IMAGE  snakedown, IMAGE snakeimage);
void EatFood();
void GameOver();
void GamePause();
int main()
{
    GameInit();
    while (1)
    {
        while (!_kbhit())
        {
            if (food.flag == 0)
            {
                FoodCoor();
            }
            cleardevice();
            FoodPaint(foodimage);
            Direction();
            SnakeMove();
            SnakePaint(snakeright, snakeleft, snakeup, snakedown, snakeimage);
            Sleep(100);
            EatFood();
            GameOver();
            GamePause();
        }
    }
    return 0;
}
//初始化页面
void GameInit()
{
    initgraph(640, 480);
    setbkcolor(WHITE);
    cleardevice();
    loadimage(&snakeleft, "蛇头左.jpeg", SIZE, SIZE);
    loadimage(&snakeright, "蛇头右.jpeg", SIZE, SIZE);
    loadimage(&snakedown, "蛇头下.jpeg", SIZE, SIZE);
    loadimage(&snakeup, "蛇头上.jpeg", SIZE, SIZE);
    loadimage(&snakeimage, "蛇身.bmp", SIZE, SIZE);
    loadimage(&foodimage, "苹果.jpg", SIZE, SIZE);
    //蛇初始化
    snake.scr[0].x = 0;
    snake.scr[0].y = 0;
    snake.n = 1;
    snake.ch = right;
    food.flag = 0;
}
//生成食物的坐标
void FoodCoor()
{
    srand((unsigned int)time(NULL));
    food.fcr.x = rand() % 65 * SIZE;
    food.fcr.y = rand() % 48 * SIZE;
    for (int i = 0; i < snake.n; i++)
    {
        while (1)
        {
            if (food.fcr.x == snake.scr[i].x && food.fcr.y == snake.scr[i].y)
            {
                food.fcr.x = rand() % 65 * SIZE;
                food.fcr.y = rand() % 48 * SIZE;
            }
            else {
                break;
            }
        }
    }
    food.flag = 1;
}
//绘制食物
void FoodPaint(IMAGE foodimage)
{
    putimage(food.fcr.x, food.fcr.y, &foodimage);
}
//蛇的移动
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 right:
        snake.scr[0].x += SIZE;
        break;
    case left:
        snake.scr[0].x -= SIZE;
        break;
    case up:
        snake.scr[0].y += SIZE;
        break;
    case down:
        snake.scr[0].y -= SIZE;
        break;
    default:
        break;
    }
}
//蛇的方向
void Direction()
{
    int read_in;
    read_in = _getch();
    switch (read_in)
    {
    case left:
        if (snake.ch != right)
        {
            snake.ch = left;
        }
        break;
    case right:
        if (snake.ch != left)
        {
            snake.ch = right;
        }
        break;
    case up:
        if (snake.ch != down)
        {
            snake.ch = up;
        }
        break;
    case down:
        if (snake.ch != up)
        {
            snake.ch = down;
        }
        break;
    default:
        break;
    }
}
//绘制蛇
void SnakePaint(IMAGE snakeright, IMAGE snakeleft, IMAGE snakeup, IMAGE  snakedown, IMAGE snakeimage)
{
    switch (snake.ch)
    {
    case right:
        putimage(snake.scr[0].x, snake.scr[0].y, &snakeright);
        break;
    case left:
        putimage(snake.scr[0].x, snake.scr[0].y, &snakeleft);
        break;
    case up:
        putimage(snake.scr[0].x, snake.scr[0].y, &snakeup);
        break;
    case down:
        putimage(snake.scr[0].x, snake.scr[0].y, &snakedown);
        break;
    default:
        break;
    }
    for (int i = snake.n; i > 0; i--)
    {
        putimage(snake.scr[i].x, snake.scr[i].y, &snakeimage);
    }
}
//判断蛇是否吃掉了食物
void EatFood()
{
    if (snake.scr[0].x == food.fcr.x && snake.scr[0].y == food.fcr.y)
    {
        food.flag = 0;
        snake.n++;
        food.grade++;
    }
}
//判断游戏是否结束
void GameOver()
{
    //将分数转化为字符形式
    char grade[100], gradeinverted[100];
    int fdgrade = food.grade, len;
    for (len = 0;; len++)
    {
        gradeinverted[len] = fdgrade % 10 + '0';
        fdgrade /= 10;
        if (fdgrade == 0)
        {
            break;
        }
    }
    for (int i = len; i >= 0; i--)
    {
        grade[i] = gradeinverted[len - i];
    }
    setbkmode(0);//文字背景透明
    outtextxy(570, 20, "分数:");
    outtextxy(610, 20, grade);
    if (snake.scr[0].x > 640 || snake.scr[0].x < 0 || snake.scr[0].y < 0 || snake.scr[0].y>480)
    {
        outtextxy(240, 320, "撞墙了,游戏结束!");
    }
    for (int i = 1; i < snake.n; i++)
    {
        if (snake.scr[0].x == snake.scr[i].x && snake.scr[0].y == snake.scr[i].y)
        {
            outtextxy(240, 320, "你咬死了自己,游戏结束!");
        }
    }
}
//暂停游戏
void GamePause()
{
    if (_getch() == 32)
    {
        while (_getch() != 32);
    }
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题