飞机大战——图文详解
发布日期:2021-05-07 11:08:13 浏览次数:26 分类:精选文章

本文共 5322 字,大约阅读时间需要 17 分钟。

EasyX图形库飞机游戏开发实践

前言

本次实践将使用EasyX图形库开发一个简单的飞机游戏。作为初次接触EasyX的小伙伴,本文会从安装、基本操作开始,逐步展开游戏开发过程。对感兴趣的朋友,也可以参考我的技术博客,获取更多开发技巧。

效果展示

游戏采用简单的2D视角设计,玩家操控飞机进行射击,对敌机进行消灭。画面效果包括飞机、子弹和敌机的动态显示,操作简单,效果不错。

图片素材准备

游戏开发过程中需要用到的图片素材包括飞机、子弹和敌机的背景图以及原图。建议将这些图片存放在同一文件夹中,开发时直接引用文件名即可。图片命名时请注意规范,避免找图时花费过多时间。

函数构建

下图展示了我们需要构建的函数以及定义的结构体。接下来的内容将详细解释每个函数的实现逻辑。

变量定义

在编程过程中,需要定义和加载各种图片资源。以下图中展示了变量的定义方式。

数据初始化

游戏数据初始化部分主要包括飞机、子弹和敌机的初始设置。飞机起始位置设置在界面中央下方,子弹和敌机初始化采用数组形式以实现多机制操作。

画面呈现

通过判断飞机、子弹和敌机的存活状态,决定是否在屏幕上绘制对应图像。子弹和敌机采用数组存储,利用时间差产生连续动态效果。

飞机移动

玩家可以通过键盘上的上、下、左、右方向键控制飞机移动。飞机的移动速度设置为0.8单位,确保操作灵活。

子弹构建与移动

子弹的发射基于空格键控制,每颗子弹间隔80ms发射一次。子弹的移动速度为1单位/帧,子弹会在到达屏幕外时自动销毁。

敌机创建与移动

敌机的创建和移动逻辑与子弹类似,采用随机生成方式产生敌机,移动速度为0.3单位/帧。敌机会在超出屏幕时自动销毁。

敌机消灭

当子弹与敌机发生交叉时,双方都会被销毁。通过循环判断子弹和敌机的存活状态,确保每次判断都能正确识别交叉事件。

结束语

感谢大家的阅读和支持。如果对内容有任何疑问或需要进一步学习,请随时联系。后续将持续更新更多有趣的内容,期待和大家一起探索更多精彩开发知识。

完整代码

#ifndef _PLANE_H_
#define _PLANE_H_
#include
#include
#include
#define SIZE 600 // 背景大小
#define PSIZE 80 // 飞机大小
#define BSIZE 40 // 子弹大小
#define MAX 10 // 子弹数目
#define FMAX 10 // 敌机数目
typedef struct Member {
double x;
double y;
bool lable;
} mem;
void Game();
void Load();
void DataInit();
void PlaneCtr();
void Show();
void BullterCre();
void BullterMove();
void FplaneCre();
void FplaneMove();
void Hit();
#endif
#include "plane.h"
IMAGE background;
IMAGE plane_back;
IMAGE plane_mask;
IMAGE fplane_back;
IMAGE fplane_mask;
IMAGE bullte_back;
IMAGE bullte_mask;
mem plane;
mem bullte[MAX];
mem fplane[FMAX];
DWORD time_start, time_last;
void Load() {
loadimage(&background, "background.jpg", SIZE, SIZE);
loadimage(&plane_back, "plane_back.jpg", PSIZE, PSIZE);
loadimage(&plane_mask, "plane_mask.jpg", PSIZE, PSIZE);
loadimage(&fplane_back, "fplane_back.jpg", PSIZE, PSIZE);
loadimage(&fplane_mask, "fplane_mask.jpg", PSIZE, PSIZE);
loadimage(&bullte_back, "bullte_back.jpg", BSIZE, BSIZE);
loadimage(&bullte_mask, "bullte_mask.jpg", BSIZE, BSIZE);
}
void DataInit() {
plane.x = SIZE / 2 - PSIZE / 2;
plane.y = SIZE - PSIZE;
plane.lable = true;
for (int i = 0; i < MAX; i++) {
bullte[i].lable = false;
}
time_start = time_last = GetTickCount();
}
void PlaneCtr() {
if (GetAsyncKeyState(VK_UP)) {
if (plane.y > 0) {
plane.y -= 0.8;
}
}
if (GetAsyncKeyState(VK_DOWN)) {
if (plane.y < SIZE - PSIZE) {
plane.y += 0.8;
}
}
if (GetAsyncKeyState(VK_LEFT)) {
if (plane.x > 0) {
plane.x -= PSIZE / 2;
}
}
if (GetAsyncKeyState(VK_RIGHT)) {
if (plane.x < SIZE - PSIZE) {
plane.x += PSIZE / 2;
}
}
if (GetAsyncKeyState(VK_SPACE) && time_last - time_start > 80) {
BullterCre();
time_start = time_last;
}
time_last = GetTickCount();
}
void Show() {
BeginBatchDraw();
putimage(0, 0, background);
if (plane.lable) {
putimage((int)plane.x, (int)plane.y, plane_mask, SRCAND);
putimage((int)plane.x, (int)plane.y, plane_back, SRCPAINT);
}
for (int i = 0; i < MAX; i++) {
if (bullte[i].lable) {
putimage((int)bullte[i].x, (int)bullte[i].y, bullte_mask, SRCAND);
putimage((int)bullte[i].x, (int)bullte[i].y, bullte_back, SRCPAINT);
}
}
for (int i = 0; i < FMAX; i++) {
if (fplane[i].lable) {
putimage((int)fplane[i].x, (int)fplane[i].y, fplane_mask, SRCAND);
putimage((int)fplane[i].x, (int)fplane[i].y, fplane_back, SRCPAINT);
}
}
EndBatchDraw();
}
void BullterCre() {
for (int i = 0; i < MAX; i++) {
if (!bullte[i].lable) {
bullte[i].x = plane.x + PSIZE / 2 - BSIZE / 2;
bullte[i].y = plane.y - BSIZE;
bullte[i].lable = true;
break;
}
}
}
void BullterMove() {
for (int i = 0; i < MAX; i++) {
if (bullte[i].lable) {
bullte[i].y--;
if (bullte[i].y < 0) {
bullte[i].lable = false;
}
}
}
}
void FplaneCre() {
for (int i = 0; i < FMAX; i++) {
if (!fplane[i].lable && time_last - time_start > 100) {
fplane[i].lable = true;
fplane[i].x = rand() % SIZE - PSIZE;
fplane[i].y = 0;
time_start = time_last;
break;
}
time_last = GetTickCount();
}
}
void FplaneMove() {
for (int i = 0; i < FMAX; i++) {
if (fplane[i].lable) {
fplane[i].y += 0.3;
if (fplane[i].y > SIZE) {
fplane[i].lable = false;
}
}
}
}
void Hit() {
for (int i = 0; i < FMAX; i++) {
if (!fplane[i].lable) {
continue;
}
for (int j = 0; j < MAX; j++) {
if (!bullte[j].lable) {
continue;
}
if (bullte[j].x > fplane[i].x - PSIZE &&
bullte[j].x < fplane[i].x + PSIZE &&
bullte[j].y > fplane[i].y &&
bullte[j].y < fplane[i].y + PSIZE) {
bullte[j].lable = false;
fplane[i].lable = false;
}
}
}
}
#endif
int main() {
initgraph(SIZE, SIZE);
Game();
getchar();
return 0;
}

内容更新

如有疑问或需要进一步学习,请随时联系。后续将持续更新更多精彩内容。

上一篇:飞机大战------功能更新
下一篇:位段、枚举、联合

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年05月10日 16时58分01秒