easyX飞机大战demo
源代码
main.cpp
#include "game.h"
int main() {Run();
}
game.h
#include
#include
#include #pragma comment(lib,"Winmm.lib")
#pragma comment( lib, "MSIMG32.LIB")#define BulletMaxNum 20
#define EnemyMaxNum 5struct Plane { //飞机POINT pos = { 0,0 }; //位置IMAGE img; //图片
};
struct Bullet {POINT pos = { 0,0 };IMAGE img;int type = 0;int speed = 1;bool show = false;
};
struct Enemy {IMAGE img;POINT pos = { 100,100 };bool show = false;clock_t startClock=NULL, endClock=NULL; //时钟,用来减缓移动速度
};void putAlphaImage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor);
void initData();
void Draw();
void drawText();
void drawPlane();
void drawBullet();
void drawEnemy();
void Input();
void Logic();
void Run();
void enemyRandomMove();
void addEnemy();
bool moveDelay(clock_t c1, clock_t c2, int delay);
bool isBumpBullet(Bullet* bullet, Enemy* enemy);
bool isBumpEnemy(Enemy* enemy1, Enemy* enemy2);
bool isBumpPlane(Enemy* enemy1, Plane* enemy2);
void playBoomSound();
game.cpp
#include "game.h"clock_t startClock, processClock;
int directLvl = 1;
Plane plane;
Bullet bullets[BulletMaxNum];
Enemy enemys[EnemyMaxNum];
IMAGE bg;
bool gameQuit = false;
int bulletNum, enemyNum;
int planeSpeed;
double enemySpeed;
int enemyMoveDealy;void initData() {planeSpeed = 1;enemySpeed = 1;enemyMoveDealy = 5;for (int i = 0; i < BulletMaxNum; i++) {loadimage(&bullets[i].img, _T("data/bullet.png"));}for (int i = 0; i < EnemyMaxNum; i++) {loadimage(&enemys[i].img, _T("data/enemy.png"));}bulletNum = 0;enemyNum = 0;gameQuit = false;plane.pos.x = 500;plane.pos.y = 600;loadimage(&plane.img, _T("data/plane.png"));loadimage(&bg, _T("data/bg.png"));startClock = clock();
}
void Run() {initData();initgraph(1000, 800);while (!gameQuit) {Draw();Input();Logic();}closegraph();
}
void Draw() {BeginBatchDraw();putimage(0, 0, &bg);drawText();drawBullet();drawEnemy();drawPlane();EndBatchDraw();
}
void drawText() {//TCHAR s1[] = _T("飞机大战");//TCHAR s2[] = _T("移动控制飞机,左键开火");//outtextxy(800, 100, s1);//outtextxy(800, 150, s2);}void drawPlane() {//putimage(plane.pos.x, plane.pos.y, &plane.img);putAlphaImage(NULL, plane.pos.x, plane.pos.y, &plane.img, 0xffffff);}void drawBullet() {for (int i = 0; i < BulletMaxNum; i++) {if (bullets[i].show) {putAlphaImage(NULL, bullets[i].pos.x, bullets[i].pos.y, &bullets[i].img, 0xffffff);}}}void drawEnemy() {for (int i = 0; i < EnemyMaxNum; i++) {if (enemys[i].show) {putAlphaImage(NULL, enemys[i].pos.x, enemys[i].pos.y, &enemys[i].img, 0xffffff);}}}
void Input() {MOUSEMSG mouse;while (MouseHit()) {mouse = GetMouseMsg();switch (mouse.uMsg) {case WM_MOUSEMOVE:plane.pos.x = mouse.x - plane.img.getwidth() / 2;plane.pos.y = mouse.y - plane.img.getheight() / 2;break;case WM_LBUTTONUP:for (int i = 0; i < BulletMaxNum; i++) {if (bullets[i].show == false) {bullets[i].show = true;bullets[i].pos.x = plane.pos.x + plane.img.getwidth() / 2 - bullets[i].img.getwidth() / 2;bullets[i].pos.y = plane.pos.y - plane.img.getheight() / 2 - bullets[i].img.getheight() / 2;break;}}break;}}
}
void Logic() {for (int i = 0; i < BulletMaxNum; i++) {if (bullets[i].show) {bullets[i].pos.y -= 0.1;if (bullets[i].pos.y <= 0) {bullets[i].pos = { 0,0 };bullets[i].show = false;}}}for (int j = 0; j < EnemyMaxNum; j++) {if (enemys[j].show) {if (isBumpPlane(&enemys[j], &plane)) {//gameQuit = true;}for (int i = 0; i < BulletMaxNum; i++) {if (bullets[i].show) {if (isBumpBullet(&bullets[i], &enemys[j]))playBoomSound();}}for (int k = 0; k < EnemyMaxNum; k++) {if (k == j) {break;}if (enemys[k].show) {if (isBumpEnemy(&enemys[j], &enemys[k]))playBoomSound();}}}}addEnemy();enemyRandomMove();
}
void putAlphaImage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor) { //绘制透明图HDC dstDC = GetImageHDC(dstimg);HDC srcDC = GetImageHDC(srcimg);int w = srcimg->getwidth();int h = srcimg->getheight();// 使用 Windows GDI 函数实现透明位图TransparentBlt(dstDC, x, y, w, h, srcDC, 0, 0, w, h, transparentcolor);
}
void enemyRandomMove() {for (int i = 0; i < EnemyMaxNum; i++) {if (enemys[i].show) {enemys[i].endClock = clock();if (moveDelay(enemys[i].startClock, enemys[i].endClock, enemyMoveDealy)) {enemys[i].startClock = clock();POINT difPos = { enemys[i].pos.x - plane.pos.x,enemys[i].pos.y - plane.pos.y };difPos.x > 0 ? enemys[i].pos.x -= enemySpeed : enemys[i].pos.x += enemySpeed;difPos.y > 0 ? enemys[i].pos.y -= enemySpeed : enemys[i].pos.y += enemySpeed;}}}
}
void addEnemy() {for (int i = 0; i < EnemyMaxNum; i++) {processClock = clock(); //结束增加敌人计时if (!enemys[i].show) {if ((processClock - startClock) / CLK_TCK >= 2) { //两秒增加一个敌人srand((unsigned)time(NULL));POINT pos = { 0,0 };enemys[i].pos = pos;enemys[i].show = true;startClock = clock(); //增加敌人计时enemys[i].startClock = clock(); //敌人计时enemys[i].endClock = clock();}}}
}
bool moveDelay(clock_t c1, clock_t c2, int delay) {if (c2 - c1 >= delay) {return true;}return false;
}
bool isBumpBullet(Bullet* bullet, Enemy* enemy) {if (bullet->pos.y < enemy->pos.y + enemy->img.getheight() &&enemy->pos.y < bullet->pos.y + bullet->img.getheight() &&enemy->pos.x < bullet->pos.x + bullet->img.getwidth() &&bullet->pos.x < enemy->pos.x + enemy->img.getwidth()) {bullet->show = false;bullet->pos = { -100,-100 };enemy->show = false;enemy->pos = { -100,-100 };return true;}return false;
}
bool isBumpEnemy(Enemy* enemy1, Enemy* enemy2) {if (enemy1->pos.y < enemy2->pos.y + enemy2->img.getheight() &&enemy2->pos.y < enemy1->pos.y + enemy1->img.getheight() &&enemy2->pos.x < enemy1->pos.x + enemy1->img.getwidth() &&enemy1->pos.x < enemy2->pos.x + enemy2->img.getwidth()) {enemy1->show = false;enemy1->pos = { -100,-100 };enemy2->show = false;enemy2->pos = { -100,-100 };return true;}return false;
}
bool isBumpPlane(Enemy* enemy1, Plane* enemy2) {if (enemy1->pos.y < enemy2->pos.y + enemy2->img.getheight() &&enemy2->pos.y < enemy1->pos.y + enemy1->img.getheight() &&enemy2->pos.x < enemy1->pos.x + enemy1->img.getwidth() &&enemy1->pos.x < enemy2->pos.x + enemy2->img.getwidth()) {return true;}return false;
}
void playBoomSound() {mciSendString(_T("close boomMusic"), NULL, 0, NULL);mciSendString(_T("open data/boom.mp3 alias boomMusic"), NULL, 0, NULL);mciSendString(_T("play boomMusic"), NULL, 0, NULL);
}
游戏素材
一共就四张,背景,飞机,子弹,敌人
demo演示
完成版
https://blog.csdn.net/qq_25969985/article/details/109958409
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
