飞机大战游戏设计
首先创建飞机/子弹结构:
cpp
struct Plane
{
int x;
int y;
bool live;
int width;
int height;
int type;
int hp;
}player,bul[BUL_NUM],enemy[ENE_NUM];
你需要加载图片:
cpp
void ImageLoad()
{
//背景
loadimage(&bg[0], "./image/飞机大战bg.png");
loadimage(&bg[1], "./image/Background.png");
loadimage(&bg[2], "./image/restart.png");
//飞机
loadimage(&img_player[0], "./image/Plane1.png");
loadimage(&img_player[1], "./image/Plane2.png");
//子弹
loadimage(&img_bullet[0], "./image/Bullet1.png");
loadimage(&img_bullet[1], "./image/Bullet2.png");
//敌机
loadimage(&img_enemy[0][0], "./image/enemy_small1.png");
loadimage(&img_enemy[0][1], "./image/enemy_small2.png");
loadimage(&img_enemy[1][0], "./image/enemy_big1.png");
loadimage(&img_enemy[1][1], "./image/enemy_big2.png");
}
游戏初始化设置:
cpp
void GameInit()
{
//创建一个窗口,控制窗口台是自动创建的,图形窗口是需要自己手动创建的(后加 SHOWCONSOLE)
initgraph(WIDTH, HEIGHT );
//飞机初始状态
player.x = WIDTH / 2-53;
player.y = HEIGHT - 130;
player.live = true;
//子弹初始状态
for (int i = 0; i < BUL_NUM; i++)
{
bul[i].live = false;
}
//敌机初始状态
for (int i = 0; i < ENE_NUM; i++)
{
enemy[i].live = false;
EnemyType(i);
}
//游戏初始界面
ImageLoad();
putimage(0, 0, &bg[0]);
setbkmode(TRANSPARENT);//设置透明背景
settextcolor(YELLOW);
settextstyle(60, 0, "黑体");
char begin[] = { "打飞机" };
outtextxy(WIDTH / 3, HEIGHT / 4, begin);
settextstyle(20, 0, "黑体");
char b[] = { "按"B"开始游戏" };
outtextxy(WIDTH / 3, HEIGHT / 2+50, b);
while (!GetAsyncKeyState('B'));
}
游戏界面加载:
cpp
游戏界面加载
void GameDraw()
{
//双缓冲绘图
BeginBatchDraw();
//加载图片
ImageLoad();
//背景
putimage(0, 0, &bg[1]);
//飞机
putimage(player.x, player.y , &img_player[0],NOTSRCERASE);//先贴透明图
putimage(player.x, player.y, &img_player[1],SRCINVERT);
//子弹
for (int i = 0; i < BUL_NUM; i++)
{
if (bul[i].live)
{
putimage(bul[i].x, bul[i].y, &img_bullet[0], NOTSRCERASE);//先贴透明图
putimage(bul[i].x, bul[i].y, &img_bullet[1], SRCINVERT);
}
}
//敌机
for (int i = 0; i < ENE_NUM; i++)
{
if (enemy[i].live)
{
if (enemy[i].type == BIG)
{
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][0], NOTSRCERASE);//先贴透明图
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][1], SRCINVERT);
}
else //if (enemy[i].type == SMALL)
{
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][0], NOTSRCERASE);//先贴透明图
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][1], SRCINVERT);
}
}
}
//分数
settextstyle(20, 0, "黑体");
char str[] = { "当前分数:" };
outtextxy(WIDTH-150, 10, str);
char t[500];
sprintf_s(t, "%d", score);
outtextxy(WIDTH - 30, 10, t);
EndBatchDraw();
}
随机一定比例地分配敌机类型:
cpp
void EnemyType(int i)
{
int flag = rand() % 10;
if (flag>=0&&flag<2)
{
enemy[i].type = BIG;
enemy[i].hp = 3;
enemy[i].width = 86;
enemy[i].height = 125;
}
else
{
enemy[i].type = SMALL;
enemy[i].hp = 1;
enemy[i].width = 71;
enemy[i].height = 52;
}
}
子弹发射配置:
cpp
void BulletCreate()
{
for (int i = 0; i < BUL_NUM; i++)
{
if (!bul[i].live)
{
bul[i].x = player.x + 41;
bul[i].y = player.y;
bul[i].live = true;
break;
}
}
}
子弹移动设置:
cpp
void BulletMove(int speed)
{
for (int i = 0; i < BUL_NUM; i++)
{
if (bul[i].live)
{
bul[i].y -= speed;
if (bul[i].y < 0)
{
bul[i].live = false;
}
}
}
}
飞机移动和子弹发射控制:
cpp
void PlayerMove(int speed)
{
#if 0
if (_kbhit()) //有按键按下返回真
{
char key = _getch(); //阻塞函数,不按下停住不执行
switch (key)
{
case 'w':
case 'W':
case 72:
player.y -= speed;
break;
case 's':
case 'S':
case 80:
player.y += speed;
break;
case 'a':
case 'A':
case 75:
player.x -= speed;
break;
case 'd':
case 'D':
case 77:
player.x += speed;
break;
}
}
#elif 1
//Windows函数, 非阻塞
//按键大写可检测到大小写,小写都检测不到
if (GetAsyncKeyState(VK_UP)|| GetAsyncKeyState('W'))
{
if (player.y > 0)
{
player.y -= speed;
}
}
if (GetAsyncKeyState(VK_DOWN) | GetAsyncKeyState('S'))
{
if (player.y < HEIGHT - 124)
{
player.y += speed;
}
}
if (GetAsyncKeyState(VK_LEFT) | GetAsyncKeyState('A'))
{
if (player.x > 0)
{
player.x -= speed;
}
}
if (GetAsyncKeyState(VK_RIGHT) | GetAsyncKeyState('D'))
{
if (player.x < WIDTH - 106)
{
player.x += speed;
}
}
#endif // 0
if (GetAsyncKeyState(VK_SPACE)&&Timer(100,0))
{
BulletCreate();
}
}
随机位置产生一个敌机(敌机会重叠,未优化。可以利用坐标判断飞机之间是否重叠,不重叠则可以创建敌机,否则重新生成坐标进行循环):
cpp
void EnemyCreate()
{
for (int i = 0; i < ENE_NUM; i++)
{
if (!enemy[i].live)
{
enemy[i].x = rand() % (WIDTH - 86);
enemy[i].y = rand()%20;
enemy[i].live = true;
EnemyType(i);
//printf("pos(%d,%d) %d %d\n", enemy[i].x, enemy[i].y, enemy[i].live, enemy[i].hp); //检测敌机状态,用于调试
break;
}
}
}
敌机移动设置:
cpp
void EnemyMove(int speed)
{
for (int i = 0; i < ENE_NUM; i++)
{
if (enemy[i].live)
{
enemy[i].y +=speed;
if(enemy[i].y>HEIGHT)
{
enemy[i].live = false;
}
}
}
}
敌机与子弹碰撞检测设置:
cpp
bool Bump()
{
for (int i = 0; i < ENE_NUM; i++)
{
if (!enemy[i].live)
continue;
for (int j = 0; j < BUL_NUM; j++)
{
if (!bul[j].live)
continue;
if (bul[j].x + 12 > enemy[i].x && bul[j].x + 12 < enemy[i].x + enemy[i].width/2
&& bul[j].y > enemy[i].y && bul[j].y < enemy[i].y + enemy[i].height/2)
{
bul[j].live = false;
enemy[i].hp--;
}
}
if (enemy[i].hp <= 0)
{
enemy[i].live = false;
if (enemy[i].type == BIG)
{
score += 3;
}
else if (enemy[i].type == SMALL)
{
score++;
}
}
if (player.x + 106/2 > enemy[i].x && player.x < enemy[i].x + enemy[i].width/2
&& player.y > enemy[i].y && player.y < enemy[i].y + enemy[i].height/2)
{
return false;
}
}
return true; // 默认true,可不写
}
由于文件执行速度较快,需要加一定的延时,以便肉眼可见。
再着需要一个定时器,定时发射敌机:
cpp
bool Timer(int ms, int id)
{
static DWORD t[TimerNum];
if (clock() - t[id] > ms)
{
t[id] = clock();
return true;
}
return false;
}
执行主函数:
cpp
int main()
{
srand((unsigned int)time(NULL)); //让随机数每次进入时都不同,只调用一次!!!
GameInit();
while (1)
{
while (1)
{
//Sleep(1); //可以让速度更慢
GameDraw();
PlayerMove(3);
BulletMove(5);
if (Timer(300, 1))
{
EnemyCreate();
}
if (Timer(10, 2))
{
EnemyMove(2);
}
FlushBatchDraw();
if (!Bump())
break;
//printf("无人能挡,所向披靡"); //用于阻塞测试
}
settextcolor(RED);
settextstyle(70, 0, "黑体");
char end[] = { "GAME OVER!" };
outtextxy(WIDTH / 6, HEIGHT / 3, end);
settextstyle(40, 0, "黑体");
char b[] = { "按"V"重新开始游戏" };
outtextxy(WIDTH / 6, HEIGHT / 2, b);
while (!GetAsyncKeyState('V'));
GameInit();
}
return 0;
}
示例视频:
由视频可见,一个基本的飞机大战游戏已经成功设计了,不过在画面上可以进行进一步的优化,玩法上可以自己增加机制提高游戏的趣味性。