这是我自己写的一个飞机大战的源码,目前还没有完善,只写了我方飞机和敌方飞机,通过上下左右四个箭头控制我方飞机行动,按空格键,飞机发射子弹。可自行加入其它功能,比如子弹敌机碰撞之后爆炸。下面是我写了的项目源码。
plane.cpp
```cpp
#include "plane.h"
Plane::Plane()
{
}
Plane::Plane(int x, int y, char *imgURL, char *imgURLY, int speed)
{
this->x = x;
this->y = y;
loadimage(img + 1, imgURL); //背景图
loadimage(img + 0, imgURLY);
this->speed = speed;
}
void Plane::drawPlane() //画飞机
{
//SRCAND 方式贴掩码图
putimage(x, y, img + 0,SRCAND);
//SRCPAIN方式贴图背景图
putimage(x, y, img + 1, SRCPAINT);
}
void Plane::keyDown(char userKey) //按键操作
{
switch (userKey)
{
case 'w':
case 'W':
case 72:
this->y -= this->speed;
break;
case 'S':
case 's':
case 80:
this->y += this->speed;
break;
case 'a':
case 'A':
case 75:
this->x -= this->speed;
break;
case 'd':
case 'D':
case 77:
this->x += this->speed;
break;
}
}
int& Plane::getX() //得到飞机的x坐标
{
return x;
}
int& Plane::getY() //得到飞机的y坐标
{
return y;
}
int& Plane::getSpeed() //得到飞机的速度
{
return speed;
}
void Plane::movePlane()
{
this->y += this->speed;
}
```
bullet.cpp
```cpp
#include "bullet.h"
Bullet::Bullet()
{
}
Bullet::Bullet(int x, int y, char *imgURL, char *imgURLY, int speed)
{
this->x = x;
this->y = y;
this->speed = speed;
loadimage(img + 1, imgURL);
loadimage(img + 0, imgURLY);
}
void Bullet::drawBullet() //画飞机
{
putimage(x, y, img + 0, SRCAND);
putimage(x, y, img + 1, SRCPAINT);
}
void Bullet::moveBullet()
{
this->y -= this->speed;
}
int& Bullet::getX() //得到飞机的x坐标
{
return x;
}
int& Bullet::getY() //得到飞机的y坐标
{
return y;
}
int& Bullet::getSpeed() //得到飞机的速度
{
return speed;
}
```
test.cpp
```cpp
#include "plane.h"
#include "bullet.h"
#include <conio.h>
#include <time.h>
#include <list>
using namespace std;
/*
1.创建窗口
2.显示图片
2.1 为图片起名字
int iNum; 存放
IMAGE img; //起一个名字
2.2 把名字分配给图片
loadimage(&img,"图片路径");
2.3 显示图片
putimage(x,y,&img);
*/
//敌机的图片路径
char planeName1[2][20] = { "resources\\1.bmp", "resources\\1y.bmp" };
char planeName2[2][20] = { "resources\\2.bmp", "resources\\2y.bmp" };
char planeName3[2][20] = { "resources\\3.bmp", "resources\\3y.bmp" };
void 画图函数()
{
}
int main()
{
int 整数 = 1001;
srand((unsigned int)time(NULL));
initgraph(800, 800);
IMAGE background;
loadimage(&background, "resources\\background.bmp");
Plane *pRole = new Plane(300, 800 - 70, "resources\\role.bmp", "resources\\roley.bmp", 10);
list<Plane *> myPlane; //创建链表
list<Plane *>::iterator iterPlane;
list<Bullet*> myBullet;
list<Bullet*>::iterator iterBullet;
Plane *pObject = NULL;
Bullet *pBullet = NULL;
while (1)
{
BeginBatchDraw();
putimage(0, 0, &background);
if (myPlane.size() < 3)
{
int pos = rand() % 3; //0-1-2
switch (pos)
{
case 0:
pObject = new Plane(rand() % 6 * 100+100, -100, planeName1[0], planeName1[1], 1);
break;
case 1:
pObject = new Plane(rand() % 6 * 100+100, -200, planeName2[0], planeName2[1], 1);
break;
case 2:
pObject = new Plane(rand() % 6 * 100+100, -200, planeName3[0], planeName3[1], 1);
break;
}
myPlane.push_back(pObject);
}
pRole->drawPlane();
if (_kbhit())
{
char userKey = _getch();
pRole->keyDown(userKey);
if (userKey == ' ')
{
pBullet = new Bullet(pRole->getX(), pRole->getY() - 25, "resources\\bullet.bmp", "resources\\bullety.bmp", 1);
myBullet.push_back(pBullet);
}
}
for (iterPlane = myPlane.begin(); iterPlane != myPlane.end(); iterPlane++)
{
(*iterPlane)->drawPlane();
(*iterPlane)->movePlane();
}
for (iterPlane = myPlane.begin(); iterPlane != myPlane.end(); iterPlane++)
{
if ((*iterPlane)->getY() >= 800)
{
iterPlane=myPlane.erase(iterPlane);
if (iterPlane == myPlane.end())
{
break;
}
}
}
for (iterBullet = myBullet.begin(); iterBullet != myBullet.end(); iterBullet++)
{
(*iterBullet)->drawBullet();
(*iterBullet)->moveBullet();
}
EndBatchDraw();
}
_getch();
closegraph();
return 0;
}
```
以上就是我飞机大战的源码,欢迎大家来讨论。