2021-08-11 17:53阅读: 30评论: 0推荐: 0

C和C++游戏趣味编程(二)Easyx图形库入门与贪吃蛇

一、用二维数组记录地图蛇身位置为1~n,其他位置为0。

二、初始化蛇身和地图,显示随机的食物。

三、循环显示地图,更新蛇位置、长度和食物位置。

四、效果展示在这里插入图片描述

五、遇到的问题。outtextxy()输出字符串需要Unicode编码的字符串。

TCHAR len[5];
swprintf_s(len, _T("%d"), snakeSize);//visual stdio 2019 提示用这个函数,不能用stprintf()
outtextxy(x, y, len);

六、用到Easyx库的函数

1、创建与关闭
initgraph()
closegraph()
cleardevice()
2、设置颜色画方框
setlinecolor()
setfillcolor()
fillrectangle()
3、输出字体
settextcolor()
settextstyle()
outtextxy()
4、批量绘制
BeginBatchDraw()
FlushBatchDraw()

#include <graphics.h>
#include <conio.h>
#include <iostream>
const int HEIGH = 30, WIDTH = 40, BLOCK_SIZE = 20;
int Blocks[HEIGH][WIDTH];//数组用来记录地图信息
char moveDirection = 'a';//移动方向
int food_i = rand() % HEIGH, food_j = rand() % WIDTH;//食物位置
int isFailure = 0;//是否失败
int snakeSize = 5;//蛇身长度

void startup() {//初始化
    srand(time(NULL));
    initgraph(WIDTH * BLOCK_SIZE+10*BLOCK_SIZE, HEIGH * BLOCK_SIZE);
    for (int i = 1; i <= snakeSize; i++) Blocks[15][19 + i] = i;
    setlinecolor(RGB(200, 200, 200));
    BeginBatchDraw();//执行后,任何绘图操作都将暂时不输出到屏幕上,直到执行 FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出。
}
void show() {//显示地图
    cleardevice();
    for (int j = 0; j < WIDTH; j++) for (int i = 0; i < HEIGH; i++) {//画游戏表格和蛇身
        if (Blocks[i][j]) setfillcolor(HSVtoRGB(Blocks[i][j] * 10, 0.9, 1));
        else setfillcolor(RGB(150, 150, 150));
        fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);
    }
    //画食物
    setfillcolor(RGB(0, 255, 0));
    fillrectangle(food_j * BLOCK_SIZE, food_i * BLOCK_SIZE, (food_j + 1) * BLOCK_SIZE, (food_i + 1) * BLOCK_SIZE);
    //右方显示蛇身长度
    setfillcolor(RGB(150, 150, 150));
    fillrectangle((WIDTH + 1) * BLOCK_SIZE, BLOCK_SIZE, (WIDTH + 9) * BLOCK_SIZE, (HEIGH - 1) * BLOCK_SIZE);
    settextcolor(RGB(0, 255, 0));
    settextstyle(30, 0, _T("宋体"));
    outtextxy((WIDTH + 2) * BLOCK_SIZE, 220, _T("蛇身长度:"));
    TCHAR len[5];//转换格式
    swprintf_s(len, _T("%d"), snakeSize);
    outtextxy((WIDTH + 2) * BLOCK_SIZE, 250, len);//右方显示
    if(isFailure){//判断游戏失败
        setbkcolor(TRANSPARENT);
        settextcolor(RGB(255, 0, 0));
        settextstyle(80, 0, _T("宋体"));
        outtextxy(240, 220, _T("游戏失败!"));
    }
    FlushBatchDraw();
}
void moveSnake() {//移动蛇身
    int oldHead_i, oldHead_j, oldRear_i = 0, oldRear_j = 0;
    for (int i = 0; i < HEIGH; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (Blocks[i][j]) Blocks[i][j]++;
            if (Blocks[i][j] == 2) { oldHead_i = i; oldHead_j = j; }
            if (Blocks[i][j] > Blocks[oldRear_i][oldRear_j]) { oldRear_i = i; oldRear_j = j; }
        }
    }
    int newHead_i = oldHead_i, newHead_j = oldHead_j;
    if (moveDirection == 'w') newHead_i--;
    else if (moveDirection == 's') newHead_i++;
    else if (moveDirection == 'a') newHead_j--;
    else if (moveDirection == 'd') newHead_j++;
    if (newHead_i < 0 || newHead_i >= HEIGH || newHead_j < 0 || newHead_j >= WIDTH || Blocks[newHead_i][newHead_j]) {
        isFailure = 1;
    }
    else Blocks[newHead_i][newHead_j] = 1;
    if(newHead_i!=food_i || newHead_j!=food_j){
        Blocks[oldRear_i][oldRear_j] = 0;
    }
    else {
        snakeSize++;
        food_i = rand() % HEIGH;
        food_j = rand() % WIDTH;
    }
}
void updateWithoutInput() {//没有输入更新
    if (isFailure) return;
    static int waitIndex = 1;
    waitIndex++;
    if (waitIndex == 30) {
        moveSnake();
        waitIndex = 1;
    }
}
void updateWithInput() {//有输入更新
    if (!isFailure && _kbhit()) {
        char input = _getch();
        if (input == 'a' || input == 'w' || input == 's' || input == 'd') {
            moveDirection = input;
            moveSnake();
        }
    }
}

int main()
{   
    startup();
    while (1) {
        show();
        updateWithoutInput();
        updateWithInput();
    }
    closegraph();
    return 0;
}

本文作者:TTMoon

本文链接:https://www.cnblogs.com/shen75/p/18140206

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   TTMoon  阅读(30)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑