Dev C++5.11如何使用自带的图形库graphics.h

1.问题描述

        笔者在用C++语言写A*算法代码的上机实验课时,发现明明引用了正确的头文件

#include<graphics.h>,为什么其中该有的函数在调用时依然报未声明/定义的错误。

        通过搜索引擎发现,该图形库需要额外下载才能正常使用。

2.问题解决

2.1EasyX下载

链接:https://codebus.cn/f/a/0/0/488/easyx4mingw_20240601.zip

2.2文件迁移

现假设你下载的dev c++的路径为E:\Dev-Cpp

1.将下载好的 easyx4mingw_20240601.zip解压缩。

2.将 include 文件夹下的 easyx.h 和 graphics.h 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include 文件夹里。

3.将 lib64\libeasyx.a 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib 文件夹里。

4.将 lib32\libeasyx.a 拷贝到 E:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib32 文件夹里。

至此迁移完成。

2.3参数配置

在dev中新建空白项目、写代码、保存、编译......(这里省略)

在导航栏中寻找:项目->项目属性

随后:参数->链接

复制 -leasyx输入进去,点击确定

3.代码测试

这是我调整后的可视化A*算法代码样例展示

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<graphics.h>
#include<cstdlib>
#include<ctime>
#include<unordered_set>
using namespace std;
const int WIDTH_BLOCK_NUM = 20;  //格子宽
const int HEIGHT_BLOCK_NUM = 20;  //格子高
const int WINDOWS_WIDTH = 800;  //屏幕宽
const int WINDOWS_HEIGHT = 800;  //屏幕高
const int MAX_MARK = 20;  //图格子最大值
const int MIN_MARK = 0;  //图格子最小值
int dir_x[]={1,1,1,0,0,-1,-1,-1};
int dir_y[]={1,-1,0,1,-1,1,-1,0};
struct Block{
public:
	Block(){}
    Block(int x, int y, int f, int g){
        this->x=x;
        this->y=y;
        this->f=f;
        this->g=g;
    }
    friend bool operator<(Block a, Block b) {
        return a.f+a.g>b.f+b.g;
    }
    int x,y,f,g;
};
priority_queue<Block>pq;
vector<Block>vec;
vector<Block>local_vec;
unordered_set<int>us;  //记录哪个点
Block start_block,end_block;
int randInt(int left, int right){
    return rand()%(right-left+1)+left;
}
void init(){
    start_block.x=randInt(MIN_MARK,MAX_MARK);  //随机生成出起始和截止点的坐标
    start_block.y=randInt(MIN_MARK,MAX_MARK);
    end_block.x=randInt(MIN_MARK, MAX_MARK);
    end_block.y=randInt(MIN_MARK, MAX_MARK);
    while(start_block.x==end_block.x||start_block.y==end_block.y){
        end_block.x=randInt(MIN_MARK,MAX_MARK);
        end_block.y=randInt(MIN_MARK,MAX_MARK);
    }
}
int position2mark(int x, int y){
    return x+y*WIDTH_BLOCK_NUM;
}
bool is_valid(int x,int y){
    if (x<0||y<0||x>=WIDTH_BLOCK_NUM||y>=HEIGHT_BLOCK_NUM){
        return false;
    }else return true;
}
void position2rectangle(struct Block&b,int rect[][2]){
    int width=WINDOWS_WIDTH/WIDTH_BLOCK_NUM;
    int height=WINDOWS_HEIGHT/HEIGHT_BLOCK_NUM;
    int temp_x=b.x*width;
    int temp_y=b.y*height;
    rect[0][0]=temp_x;
    rect[0][1]=temp_y;
    rect[1][0]=temp_x + width;
    rect[1][1]=temp_y + height;
}
void draw(){
    int width=WINDOWS_WIDTH / WIDTH_BLOCK_NUM;
    int height=WINDOWS_HEIGHT / HEIGHT_BLOCK_NUM;
    for(int i=0;i<WINDOWS_WIDTH;i+=width){
        line(i,0,i,WINDOWS_HEIGHT);
    }
    for(int i=0;i<WINDOWS_HEIGHT;i+=height){
        line(0,i,WINDOWS_WIDTH,i);
    }
    int arr[2][2];
    COLORREF prev_color=getfillcolor();
    setfillcolor(BLUE);
    for(Block b:local_vec){
        position2rectangle(b,arr);
        fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
    }
    setfillcolor(RED);
    for(Block b:vec){
        position2rectangle(b,arr);
        fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
    }
    setfillcolor(GREEN);
    position2rectangle(start_block,arr);
    fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
    position2rectangle(end_block,arr);
    fillrectangle(arr[0][0],arr[0][1],arr[1][0],arr[1][1]);
    setfillcolor(prev_color);
    Sleep(1000);
}
int main(){
    srand(unsigned(time(NULL)));
    init();
    initgraph(WINDOWS_WIDTH,WINDOWS_HEIGHT);
    Block temp=start_block;
    pq.push(temp);  //起点入队
    us.insert(position2mark(temp.x, temp.y));
    while(!(temp.x==end_block.x&&temp.y==end_block.y)){
        temp=pq.top();
        pq.pop();
        vec.push_back(temp);
        for(int i=0;i<8; ++i){  //枚举8个方向
            int temp_x=temp.x+dir_x[i];
            int temp_y=temp.y+dir_y[i];
            if(is_valid(temp_x,temp_y)&&!us.count(position2mark(temp_x,temp_y))){  //没越界且为第一次到达
                Block b;
                b.x=temp_x;
                b.y=temp_y;
                b.f=temp.f+(abs(dir_x[i])+abs(dir_y[i]))>1?14:10;
                b.g=(abs(temp_x-end_block.x)+abs(temp_y-end_block.y))*10;
                pq.push(b);
                us.insert(position2mark(temp_x,temp_y));
                local_vec.push_back(b);
            }
        }
        draw();
    }
    closegraph();
    return 0;
}

样例运行结果展示:

4.参考:在 CLion、Dev-C++ 或 Code::Blocks 下面配置 EasyX(2024-6-1 更新) - CodeBus

### Dev-C++ 支持的图形库及其使用方法 #### EGE 图形库 在 Windows 下,EGE 是一个适合初学者使用的简单图形库。为了使该库能够在 Dev-C++ 中正常工作,需按照特定步骤配置编译器选项[^1]。 对于 EGE 的集成,具体操作如下: - 打开 Dev-C++ 软件; - 进入 `工具` 菜单下的 `Compiler Options`; - 将 `-static -static-libgcc -lgraphics -lgdi32 -lgdiplus` 添加至链接参数框内并保存设置[^2]; ```cpp #include <egelib.h> int main() { initgraph(640, 480); // 初始化绘图窗口大小为 640x480 像素 setcolor(GREEN); circle(320, 240, 150); // 绘制绿色圆形 getch(); closegraph(); // 关闭绘图环境 } ``` #### EasyX 图形库 另一个流行的中文开源图形库是 EasyX,它提供了更丰富的功能集以及良好的文档支持。针对 DEV-C++ 版本 (如 5.11),可以遵循以下指南完成安装过程[^3]。 要让程序能够调用 EasyX 函数,则应执行下列指令来调整项目属性: - 启动 dev-cpp 应用程序; - 寻找顶部菜单栏里的 “项目” -> "项目属性"; - 定位到 参数 面板内的 右侧链接 区域追加 `-leasyx` 并确认更改[^4]; ```cpp #include <graphics.h> #include <conio.h> int main(){ initgraph(640, 480); // 创建画布尺寸设定为宽高各 640 和 480 单位长度 setbkcolor(LIGHTBLUE); cleardevice(); ellipse(320, 240, 0, 360, 200, 100); // 描述椭圆中心坐标、起始角度终止角半径等信息 _getch(); closegraph(); } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值