這是我自己寫的一個飛機大戰(zhàn)的源碼,目前還沒有完善划鸽,只寫了我方飛機和敵方飛機输莺,通過上下左右四個箭頭控制我方飛機行動,按空格鍵裸诽,飛機發(fā)射子彈嫂用。可自行加入其它功能丈冬,比如子彈敵機碰撞之后爆炸嘱函。下面是我寫了的項目源碼。
? 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坐標(biāo)
{
return x;
}
int& Plane::getY() //得到飛機的y坐標(biāo)
{
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坐標(biāo)
{
return x;
}
int& Bullet::getY() //得到飛機的y坐標(biāo)
{
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.創(chuàng)建窗口
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 畫圖函數(shù)()
{
}
int main()
{
int? 整數(shù) = 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; //創(chuàng)建鏈表
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;
}
```
以上就是我飛機大戰(zhàn)的源碼埂蕊,歡迎大家來討論往弓。