C++实现贪吃蛇小游戏

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
//包含easyx图形库,可以使用给我们提供的一些函数,绘图,贴图
#include<graphics.h>
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define MAX_SNAKE 500 //蛇的最大节数

//枚举
enum DIR //枚举蛇的方向
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
};
struct Food //食物
{
    int x;
    int y;
    DWORD color;
    bool flag;//是否被吃掉,是否要重新生成食物
}food;
struct Pos
{
    int x;
    int y;
    DWORD color;
};
struct Snake//蛇的结构体
{
    int num;//当前节数
    int dir;//蛇的方向
    int score;//分数
    int size;//蛇的宽和高
    int speed;//蛇的移动速度
    //表示每一节蛇的坐标 数组
    struct Pos coor[MAX_SNAKE];
}snake;
void GameInit()
{
    //设置随机数种子
    srand(GetTickCount());
    snake.num = 3;
    snake.dir = RIGHT;
    snake.score = 0;
    snake.size = 10;
    snake.speed = 10;
    snake.coor[2].x = 0;
    snake.coor[2].y = 0;
    snake.coor[2].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    snake.coor[1].x = 10;
    snake.coor[1].y = 0;
    snake.coor[1].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    snake.coor[0].x = 20;
    snake.coor[0].y = 0;
    snake.coor[0].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    //初始化食物
    food.x = rand() % (WIN_WIDTH / 10) * 10;//1*10  10 20 30 40 50 60 
    food.y = rand() % (WIN_HEIGHT / 10) * 10;
    food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    food.flag = true;
}
void GameDraw()
{
    cleardevice();

    for (int i = 0; i < snake.num; i++)
    {
        //设置填充颜色
        setfillcolor(snake.coor[i].color);
        //画矩形
        fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + 10, snake.coor[i].y + 10);
    }
    //绘制食物
    //solidcircle(food.x,food.y,5)
    if (food.flag)
    {
        setfillcolor(food.color);
        solidellipse(food.x, food.y, food.x + 10, food.y + 10);
    }
    //绘制分数
      //先把整数转成字符串
    char temp[20] = " ";
    sprintf(temp, "分数:%d", snake.score);
    outtextxy(10, 10, temp);//如果报错  “outtextxy”: 2 个重载中没有一个可以转换所有参数类型  一定是字符集问题

}
void GameMove()
{
    for (int i = snake.num - 1; i > 0; i--)
    {
        snake.coor[i].x = snake.coor[i - 1].x;
        snake.coor[i].y = snake.coor[i - 1].y;
    }
    switch (snake.dir)
    {
    case UP:
        snake.coor[0].y -= snake.speed;
        if (snake.coor[0].y < 0)
        {
            snake.coor[0].y = WIN_HEIGHT;
        }
        break;
    case DOWN:
        snake.coor[0].y += snake.speed;
        if (snake.coor[0].y > WIN_HEIGHT)
        {
            snake.coor[0].y = 0;
        }
        break;
    case LEFT:
        snake.coor[0].x -= snake.speed;
        if (snake.coor[0].x < 0)
        {
            snake.coor[0].x = WIN_WIDTH;
        }
        break;
    case RIGHT:
        snake.coor[0].x += snake.speed;
        if (snake.coor[0].x > WIN_WIDTH)
        {
            snake.coor[0].x = 0;
        }
        break;
    }
}
void GameControl()
{
    //获取键盘输入,_getch(); 不回显函数,
    char key = _getch();
    switch (key)
    {
    case 72:
        if (snake.dir != DOWN)
        {
            snake.dir = UP;
        }
        break;
    case 80:
        if (snake.dir != UP)
        {
            snake.dir = DOWN;
        }
        break;
    case 75:
        if (snake.dir != RIGHT)
        {
            snake.dir = LEFT;
        }
        break;
    case 77:
        if (snake.dir != LEFT)
        {
            snake.dir = RIGHT;
        }
        break;
    }
    printf("%d\n", key);
}
void CreateFood()
{
    if (!food.flag)
    {
        food.x = rand() % (WIN_WIDTH / 10) * 10;//1*10  10 20 30 40 50 60 
        food.y = rand() % (WIN_HEIGHT / 10) * 10;
        food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
        food.flag = true;
    }
}
void EatFood()
{
    if (food.flag && snake.coor[0].x == food.x && snake.coor[0].y == food.y)
    {
        food.flag = false;
        snake.num++;
        snake.score += 10;
        snake.coor[snake.num - 1].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
        CreateFood();
    }
}
int main()
{
    //1,创建窗口,init初始化 graphics 图形窗口
    initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);
    setbkcolor(RGB(207, 214, 229));//设置颜色
    cleardevice();//刷新窗口
    GameInit();

    while (1)
    {

        GameDraw();
        GameMove();
        if (_kbhit())
        {
            GameControl();
        }
        EatFood();
        Sleep(100);

    }

    getchar();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然,这是一个简单的C语言贪吃游戏的源代码示例: ```c #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <windows.h> int width = 20, height = 20; int gameover; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum eDirection dir; void Setup() { gameover = 0; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruitY && j == fruitX) printf("F"); else { int printTail = 0; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); printTail = 1; } } if (!printTail) printf(" "); } if (j == width - 1) printf("#"); } printf("\n"); } for (int i = 0; i < width + 2; i++) printf("#"); printf("\n"); printf("Score:%d\n", score); } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameover = 1; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameover = 1; } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameover) { Draw(); Input(); Logic(); Sleep(10); // 控制游戏速度 } return 0; } ``` 这个代码示例实现了一个基本的贪吃游戏,使用了C语言的基本语法和控制台输入输出函数。你可以将代码复制到一个C语言编译器中(如Dev-C++、Code::Blocks等)进行编译和运行。希望这能帮到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科技语者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值