以下代码我在vs2022中编译的游戏代码,代码要用到easyx库,请热爱游戏开发新朋友们先配置easyx。
代码共分为三部分,以下是函数功能部分,我将它命名为AirPlay.h
#include <easyx.h>
#include <iostream>
#include <conio.h>
#include <graphics.h>
#include <ctime>
using namespace std;
#define BGWEIGHT 400 //宽
#define BGHIGHT 600 //高
#define BULLET_NUM 20 //子弹数量
// 我方飞机的大小
#define myAirWEIGHT 25
#define myAirHEIGHT 40
// 敌机的大小
#define enemyPlaneWEIGHT 20
#define enemyPlaneHEIGHT 32
// 子弹的大小
#define BulletWEIGHT 8
#define BulletHEIGHT 8
IMAGE img[5]; //图片数组
// 初始化函数
void init()
{
//加载图片
loadimage(&img[0], "res\\背景.jpg", BGWEIGHT, BGHIGHT);
loadimage(&img[1], "res\\飞机.jpg", myAirWEIGHT, myAirHEIGHT);
loadimage(&img[2], "res\\敌机.jpg", enemyPlaneWEIGHT, enemyPlaneHEIGHT);
loadimage(&img[3], "res\\子弹.jpg", BulletWEIGHT, BulletHEIGHT);
loadimage(&img[4], "res\\敌机.jpg", 40, 64);
//右键项目名称修改:属性->高级->字符集->选择使用多字节字符集。否则会报错。
"res\\背景.jpg"等为文件调用,也就是你的背景图和飞机子弹图片,我用的是朋友的照片大家不要轻易模仿哦。
}
enum My
{
DBULLET_NUM = 20,
ENEMY_NUM = 15,
BIG,
SAMLL
};
class Plane
{
public:
int x;
int y; //坐标
bool live; //是否存活
int width;
int height;
int hp; //敌方血量
int type;
// 构造函数
Plane()
{
x = 0;
y = 0;
live = false;
width = 0;
height = 0;
hp = 0;
type = 0;
}
};
Plane player, bull[BULLET_NUM], enemy[ENEMY_NUM];
bool Time(int ms, int id)
{
static DWORD t[10];
if (clock() - t[id] > ms)
{
t[id] = clock();
return true;
}
return false;
}
// 敌机数据
void enemyHP(int i)
{
if (rand() % 8)
{
enemy[i].type = SAMLL;
enemy[i].hp = 1;
enemy[i].width = 20;
enemy[i].height = 32;
}
else
{
enemy[i].type = BIG;
enemy[i].hp = 2;
enemy[i].width = 40;
enemy[i].height = 64;
}
}
// 生成敌机
void createEnemy()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!enemy[i].live)
{
bool overlap;
do
{
overlap = false;
enemy[i].x = rand() % (BGWEIGHT - 64);
enemy[i].y = -enemy[i].height;
for (int j = 0; j < i; j++)
{
if (enemy[j].live && abs(enemy[i].x - enemy[j].x) < enemyPlaneWEIGHT && abs(enemy[i].y - enemy[j].y) < enemyPlaneHEIGHT)
{
overlap = true;
break;
}
}
} while (overlap);
enemy[i].live = true;
break;
}
}
}
// 检测子弹与敌机碰撞
void checkCollision()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!enemy[i].live)
{
continue;
}
for (int k = 0; k < BULLET_NUM; k++)
{
if (bull[k].live && bull[k].x > enemy[i].x && bull[k].x < (enemy[i].x + enemy[i].width) && bull[k].y > enemy[i].y && bull[k].y < (enemy[i].y + enemy[i].height))
{
bull[k].live = false;
enemy[i].hp--;
if (enemy[i].hp <= 0)
{
enemy[i].live = false;
createEnemy();
}
}
}
}
}
// 敌机移动
void enemyMove(int speed)
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (enemy[i].live)
{
enemy[i].y += speed;
// 判断是否出界
if (enemy[i].y > BGHIGHT)
{
enemy[i].live = false;
}
}
}
}
// 初始化飞机
void gameInit()
{
player.x = BGWEIGHT / 2 - myAirWEIGHT / 2;
player.y = BGHIGHT - myAirHEIGHT;
player.live = true; // true代表活着
// 初始化子弹
for (int i = 0; i < BULLET_NUM; i++)
{
bull[i].x = 0;
bull[i].y = 0;
bull[i].live = false;
}
// 初始化敌机
for (int i = 0; i < ENEMY_NUM; i++)
{
enemy[i].live = false;
enemyHP(i);
}
}
void createBullet()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!bull[i].live)
{
bull[i].x = player.x + myAirWEIGHT / 2 - BulletWEIGHT / 2;
bull[i].y = player.y - BulletHEIGHT;
bull[i].live = true;
break;
}
}
}
// 子弹移动
void bulletMove()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bull[i].live)
{
bull[i].y -= 3;
if (bull[i].y < 0)
{
bull[i].live = false;
}
}
}
}
// 操作键盘
void playerMove(int speed)
{
if (GetAsyncKeyState(VK_UP) && player.y > 0)
{
player.y -= speed;
}
if (GetAsyncKeyState(VK_DOWN) && player.y < BGHIGHT - myAirHEIGHT)
{
player.y += speed;
}
if (GetAsyncKeyState(VK_LEFT) && player.x > 0)
{
player.x -= speed;
}
if (GetAsyncKeyState(VK_RIGHT) && player.x < BGWEIGHT - myAirWEIGHT)
{
player.x += speed;
}
static DWORD t1 = 0, t2 = 0;
// 按空格发射子弹
if (GetAsyncKeyState(VK_SPACE) && t2 - t1 > 80)
{
createBullet();
t1 = t2;
}
t2 = GetTickCount();
}
// 绘制
void Draw()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bull[i].live)
{
putimage(bull[i].x, bull[i].y, &img[3]);
}
}
for (int i = 0; i < ENEMY_NUM; i++)
{
// 如果有敌机
if (enemy[i].live)
{
if (enemy[i].type == BIG)
{
// 绘制
putimage(enemy[i].x, enemy[i].y, &img[4]);
}
else
{
putimage(enemy[i].x, enemy[i].y, &img[2]);
}
}
}
}
以下为音乐播放部分 ,我将它命名为file.h
#include <iostream>
#include <Windows.h>
#include <fstream>
#pragma comment(lib,"winmm.lib")//导入声音头文件库
void song()
{
sndPlaySound("song.wav", SND_ASYNC);
//音乐文件类型必须为wav,否则闪一下黑色窗口继续执行代码不会继续播放音乐
}
以下为主函数部分,我将他命名为“飞机大战.cpp”
#include "AirPlay.h"
#include "file.h"
int main()
{
initgraph(BGWEIGHT, BGHIGHT); //弹出窗口
init();
gameInit();
song();
// 双缓存 主要是防止程序一闪一闪
BeginBatchDraw();
while (1)
{
putimage(0, 0, &img[0]);
putimage(player.x, player.y, &img[1]);
Draw();
FlushBatchDraw();
playerMove(2);
bulletMove();
if (Time(400, 0))
{
createEnemy();
}
enemyMove(2);
checkCollision();
Sleep(10);
}
EndBatchDraw();
return 0;
}