前言
家人们我只是单纯的分享一下自己学习easyx时创造的小作品,水平不高,而且做的比较粗糙。做的比我好的大有人在加油兄弟们,希望能和大家共同分享一起进步。(这个需要easyx)兄弟们直接去官网下吧,这个很好玩的。
关于easyx
关于easyx这是一款基于C/C++的图形库,EasyX Graphics Library 是针对 Visual C++ 的免费绘图库,支持 VC6.0 ~ VC2022,简单易用,学习成本极低,应用领域 广泛。目前已有许多大学将 EasyX 应用在教学当中。
EasyX 含有一些简单的函数集合,几乎不用学习,直接翻看参考手册就可以直接使用。在线参考手册地址 https://docs.easyx.cn
正因为 EasyX 足够简单,在进行 C/C++、图形学、图像学、分形学等课程实验时,可以专注在课程知识上,不被绘图部分牵扯太多精力。以下是 EasyX 的使用演示:
在 VC6 的控制台程序中使用 EasyX,请参考 https://easyx.cn/used-in-vc6-console
在 VC6 的窗口程序中使用 EasyX,请参考 https://easyx.cn/used-in-vc6-win32
在 VC2010 的控制台程序中使用 EasyX,请参考 https://easyx.cn/used-in-vc2010-console
在 VC2010 的窗口程序中使用 EasyX,请参考 https://easyx.cn/used-in-vc2010-win32
在其它各版本 VC 中使用 EasyX,和 VC2010 区别不大,不再演示。
游戏介绍
这次的游戏比较有意思,这是一架星际飞船在宇宙中迷路的游戏,为了找回回地球的road必须击碎宇宙中的陨石才能完整的回到地球(当然是我瞎编的嘿嘿嘿)。
这次游戏比较肝正经的算法其实没用到啥,主要是那个五子棋比较胜负判断比较有意思,大家康康吧
一,开始游戏
这次的游戏为了让我的飞机看起来更加的逼真导入了一张图片,这张飞机图片可不是我的,给大家原作者的设计网址吧
https://opengameart.org/content/64-x-64-animated-plane
这个是一个开源的游戏素材网站挺不错的。那么接下来就是导入图片,easyx带有导入图片的功能,这个要用到一些函数,当然了这个是理所当然的。
这是个IMAGE类型的变量,这个是easyx有的一个变量。
IMAGE plane;
loadimage(&plane, "./Plane.png", 64, 64);
然后putimage给出这张照片你想让他出现的位置"./Plane.png"关于这个,这个“./”表示的是这个代码同一文件夹的目录下,所以你需要把那个照片放到这个文件夹内,当然这个只是相对路径你也可以用绝对路径,不多说上图片
那个黄色的东东一个文件夹的样子点一下就可以打开了,当然这个是相对路径,在./后面加一个图片的名字就OK记得加png哈。就像这样
那么什么是绝对路径呢?就是上面这一串了,在吧绝对路径复制粘贴过来时记得把那个反斜杠改一下哈,这个就是绝对路径:
图片的就说到这里,接下来还是老样子吧图片和方块放上去然后字体设置一下,开始游戏大概就是这个样子当然了这个开始游戏,就是老样子设置方块的函数
loadimage(&plane, "./Plane.png", 64, 64);
initgraph(640, 640);
creat_brick(270, 290, 120, 80, "开始游戏");
putimage(288, 10, &plane);
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy((640 - textwidth("星际迷航")) / 2, 100, "星际迷航");
这个就是具体代码,这个还是挺简单看看效果吧。
就是这样吧。然后老样子设置一个鼠标来点击那个坐标信息。
ExMessage mou;
int t = 0;
while (true)
{
if (peekmessage(&mou, EX_MOUSE))
{
switch (mou.message)
{
case WM_LBUTTONDOWN:
if (mou.x >= 270 && mou.x <= 270 + 120 && mou.y >= 290 && mou.y <= 290 + 80)
{
closegraph();
t = 1;
}
break;
default:
break;
}
}
if (t == 1) break;
}
这个就是老样子了那么第一部分到这就完成了。
游戏部分
这一个部分就比较复杂,特别肝,我用比较多的函数来控制物体的运动,不然都放在主函数里面脑子很乱。
先看飞机
void setplane()
{
putimage(px, py, &plane);
}
void planemove()
{
if (GetAsyncKeyState(VK_LEFT) & 0x8000) px -= speedp;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) px += speedp;
if (GetAsyncKeyState(VK_UP) & 0x8000) py -= speedp;
if (GetAsyncKeyState(VK_DOWN) & 0x8000) py += speedp;
}
这两个函数分别是控制飞机运动以及画出飞机的,这个就比较简单了,这次我是用的是上下左右键,而不是"W",“S”,“D”,“A”。因为会乱看着难受,那么那个planmove就是主要代码了,这个网上找找都有的。
接着是子弹:
void setbullet()
{
setfillcolor(RED);
fillrectangle(bx, by, bx+4, by+20);
}
void buttletmove()
{
by -= speedb;
if (by < 0)
{
by = py;
bx = px + 32;
}
}
这个比较简单因为他是因变量,它随着飞机的改变而改变,当然射出去子弹是不会改变路径的,虽然物理上如果飞机在水平方向上有速度的话那么便不会直线飞,但是我为了快就没做出那个。
接着是陨石:
void cretebrick(int x,int y)
{
setfillcolor(WHITE);
fillrectangle(x, y, x + 20, y + 20);
}
//这上面的是函数下面的是判断陨石有无被击中
if (bx >= x && bx <= x + 20 && by < y + 20) temp = 1;
if (temp == 0)
{
cretebrick(x, y);
}
else if (temp == 1)
{
score++;
y = 0;
srand((unsigned)time(NULL) * (unsigned)rand());
x = rand()%640;
temp = 0;
}
y += speedck;
if (y + 20 >= 640)
{
swap = 1;
break;
}
如果陨石被击中则会随机在上方重新生成一个陨石,且陨石掉了那么游戏失败。
ok接下来看看这一整个部分的代码
initgraph(640,750);
cleardevice();
int n;
srand((unsigned)time(NULL) * (unsigned)rand());
n = rand()%640;
int temp = 0, swap = 0, score = 0;
int x = n, y = 0, speedck = 2;
while (1)
{
cleardevice();
sc(score);
setplane();
setbullet();
planemove();
buttletmove();
if (bx >= x && bx <= x + 20 && by < y + 20) temp = 1;
if (temp == 0)
{
cretebrick(x, y);
}
else if (temp == 1)
{
score++;
y = 0;
srand((unsigned)time(NULL) * (unsigned)rand());
x = rand()%640;
temp = 0;
}
y += speedck;
if (y + 20 >= 640)
{
swap = 1;
break;
}
Sleep(10);
if (kbhit())
{
char ch = getch();
if (ch == 27)
{
break;
}
}
}
上照片
这个就是效果了,那个就是score给大家看看吧
void sc(int score)
{
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy(0, 720, scor);
sprintf(num, "%d", score);
outtextxy(textwidth(scor), 720, num);
}
这个主要还是没啥难度就是把那个转数字输出了,这个不难说实话,人给函数了嘛!就算没有函数用高精呗,反正高精就是数组模拟的。
游戏结束
这个结束嘛就是和开始一样,不过多了个输出score
大家看看吧:
if (swap == 1)
{
closegraph();
initgraph(640,640);
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy((640 - textwidth("游戏结束")) / 2, 250, "游戏结束");
outtextxy((640 - textwidth(scor)) / 2, 320, scor);
sprintf(num, "%d", score);
outtextxy((640 - textwidth(scor)) / 2+textwidth(scor), 320, num);
getchar();
}
closegraph();
这个就比较简单了看看小锅巴;
就是这样比较简单的,这些天比较摆,最近回一下状态。加油兄弟们;
code
#include<easyx.h>
#include<iostream>
#include<graphics.h>
#include<time.h>
#include<conio.h>
using namespace std;
IMAGE plane;
int px=288, py=640;
int bx = px + 32;
int by = py;
int speedp=5, speedb = 10;
char num[20], scor[20] = "Score:";
void setbullet()
{
setfillcolor(RED);
fillrectangle(bx, by, bx+4, by+20);
}
void setplane()
{
putimage(px, py, &plane);
}
void buttletmove()
{
by -= speedb;
if (by < 0)
{
by = py;
bx = px + 32;
}
}
void cretebrick(int x,int y)
{
setfillcolor(WHITE);
fillrectangle(x, y, x + 20, y + 20);
}
void planemove()
{
if (GetAsyncKeyState(VK_LEFT) & 0x8000) px -= speedp;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) px += speedp;
if (GetAsyncKeyState(VK_UP) & 0x8000) py -= speedp;
if (GetAsyncKeyState(VK_DOWN) & 0x8000) py += speedp;
}
void creat_brick(int x, int y, int w, int h, const char c[])
{
setbkmode(TRANSPARENT);
setfillcolor(WHITE);
fillroundrect(x, y, x + w, y + h, 10, 10);
int tx, ty;
settextstyle(30, 0, "黑体");
settextcolor(BLACK);
tx = x + (w - textwidth(c)) / 2;
ty = y + (h - textheight(c)) / 2;
outtextxy(tx, ty, c);
}
void sc(int score)
{
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy(0, 720, scor);
sprintf(num, "%d", score);
outtextxy(textwidth(scor), 720, num);
}
int main()
{
loadimage(&plane, "./Plane.png", 64, 64);
initgraph(640, 640);
creat_brick(270, 290, 120, 80, "开始游戏");
putimage(288, 10, &plane);
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy((640 - textwidth("星际迷航")) / 2, 100, "星际迷航");
ExMessage mou;
int t = 0;
while (true)
{
if (peekmessage(&mou, EX_MOUSE))
{
switch (mou.message)
{
case WM_LBUTTONDOWN:
if (mou.x >= 270 && mou.x <= 270 + 120 && mou.y >= 290 && mou.y <= 290 + 80)
{
closegraph();
t = 1;
}
break;
default:
break;
}
}
if (t == 1) break;
}
initgraph(640,750);
cleardevice();
int n;
srand((unsigned)time(NULL) * (unsigned)rand());
n = rand()%640;
int temp = 0, swap = 0, score = 0;
int x = n, y = 0, speedck = 2;
while (1)
{
cleardevice();
sc(score);
setplane();
setbullet();
planemove();
buttletmove();
if (bx >= x && bx <= x + 20 && by < y + 20) temp = 1;
if (temp == 0)
{
cretebrick(x, y);
}
else if (temp == 1)
{
score++;
y = 0;
srand((unsigned)time(NULL) * (unsigned)rand());
x = rand()%640;
temp = 0;
}
y += speedck;
if (y + 20 >= 640)
{
swap = 1;
break;
}
Sleep(10);
if (kbhit())
{
char ch = getch();
if (ch == 27)
{
break;
}
}
}
if (swap == 1)
{
closegraph();
initgraph(640,640);
settextstyle(30, 0, "黑体");
settextcolor(WHITE);
outtextxy((640 - textwidth("游戏结束")) / 2, 250, "游戏结束");
outtextxy((640 - textwidth(scor)) / 2, 320, scor);
sprintf(num, "%d", score);
outtextxy((640 - textwidth(scor)) / 2+textwidth(scor), 320, num);
getchar();
}
closegraph();
}
兄弟们最近接着加油吧。