easyx与ege图形库的比较学习(一)

首先对比一下两者的库文件,头文件,以vs2015 win10 64位为例:

Easyx 头文件 graphics.h , 库文件两个
EasyXa.lib // VC2015 ~ 2017 库文件(x64, MBCS 版本)
EasyXw.lib // VC2015 ~ 2017 库文件(x64, Unicode 版本)

ege头文件 graphics.h,ege.h,库文件是graphics15.lib

所以如果同时安装这两个库,只需要替换一下头文件graphics.h就可以了,其他的文件不会互相覆盖。

//ege第一个图形窗口程序
#include <graphics.h>

int main()
{
	//初始化为640*480大小
	initgraph(640, 480);

  // 画圆,圆心(200, 200),半径 100
  circle(200, 200, 100);

	//等待用户按键
	getch();

	//关闭图形界面
	closegraph();
	return 0;
}
//easyx 第一个图形窗口程序
#include <graphics.h>    
#include <conio.h>
int main()
{
    initgraph(640, 480);   // 创建绘图窗口,大小为 640x480 像素
    circle(200, 200, 100); // 画圆,圆心(200, 200),半径 100
    _getch();              // 按任意键继续
    closegraph();          // 关闭绘图窗口
    return 0;
}

流程一样,都是初始化窗口,操作,最后关闭窗口。
但是有一点细微区别&

  • 14
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,以下是一个使用 EasyX 图形库实现的贪吃蛇游戏的示例代码: ```c++ #include <graphics.h> #include <conio.h> #include <time.h> const int WIDTH = 600; // 游戏窗口宽度 const int HEIGHT = 600; // 游戏窗口高度 const int CELL_SIZE = 20; // 每个蛇身格子的大小 const int ROWS = HEIGHT / CELL_SIZE - 1; // 游戏窗口中行数 const int COLS = WIDTH / CELL_SIZE - 1; // 游戏窗口中列数 struct Snake { int x, y; struct Snake *next; }; enum Direction { UP, DOWN, LEFT, RIGHT }; Direction direction = RIGHT; // 蛇的初始方向 Snake *head = NULL; // 蛇头指针 int score = 0; // 得分 void initSnake() { head = new Snake; head->x = 5; head->y = 5; head->next = NULL; } void drawSnake() { setfillcolor(YELLOW); for (Snake *p = head; p; p = p->next) { solidrectangle(p->x * CELL_SIZE, p->y * CELL_SIZE, (p->x + 1) * CELL_SIZE, (p->y + 1) * CELL_SIZE); } } void moveSnake() { int x = head->x, y = head->y; switch (direction) { case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } Snake *p = new Snake; p->x = x; p->y = y; p->next = head; head = p; if (x < 0 || x > COLS || y < 0 || y > ROWS) { // 撞墙 closegraph(); printf("Game Over!\n"); exit(0); } for (Snake *p = head->next; p; p = p->next) { // 撞自己 if (p->x == head->x && p->y == head->y) { closegraph(); printf("Game Over!\n"); exit(0); } } if (x == food_x && y == food_y) { // 吃到食物 score += 10; food_x = rand() % COLS; food_y = rand() % ROWS; } else { Snake *p = head; while (p->next->next) { p = p->next; } delete p->next; p->next = NULL; } } void drawFood() { setfillcolor(RED); solidcircle(food_x * CELL_SIZE + CELL_SIZE / 2, food_y * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 2); } void updateScore() { setbkmode(TRANSPARENT); settextcolor(WHITE); char buf[32]; sprintf(buf, "Score: %d", score); outtextxy(10, 10, buf); } int food_x, food_y; int main() { initgraph(WIDTH, HEIGHT); setbkcolor(DARKGRAY); initSnake(); food_x = rand() % COLS; food_y = rand() % ROWS; while (true) { cleardevice(); drawSnake(); drawFood(); updateScore(); moveSnake(); Sleep(100); if (_kbhit()) { switch (_getch()) { case 'W': case 'w': if (direction != DOWN) direction = UP; break; case 'S': case 's': if (direction != UP) direction = DOWN; break; case 'A': case 'a': if (direction != RIGHT) direction = LEFT; break; case 'D': case 'd': if (direction != LEFT) direction = RIGHT; break; } } } return 0; } ``` 在这个示例代码中,我们使用了 EasyX 图形库来实现游戏窗口和绘制图形。我们使用 Snake 结构体来表示蛇身,其中的 next 指针指向下一个蛇身。当蛇移动时,我们将一个新的 Snake 节点插入到蛇头位置,然后删除蛇尾节点,从而实现蛇的移动。当蛇头碰到墙壁或者自己的身体时,游戏结束。当蛇头碰到食物时,得分加 10 分,并在随机位置生成一个新的食物。 在游戏循环中,我们使用 cleardevice() 函数清空屏幕,然后分别绘制蛇身、食物和得分。我们使用 Sleep(100) 函数控制游戏的帧率,从而实现动画效果。我们使用 _kbhit() 和 _getch() 函数来读取键盘输入,从而控制蛇的方向。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值