#include<graphics.h>
#include<conio.h>
#include<stdio.h>
//引用Windows Multimedia API
#pragma comment(lib,"Winmm.lib")
#define high 700 //游戏画面尺寸
#define width 590
IMAGE img_bk; //背景图片
int position_x, position_y; //飞机位置
int bullet_x, bullet_y; //子弹位置
float enemy_x, enemy_y; //敌机位置
IMAGE img_planeNormal1, img_planeNormal2; //飞机图片
IMAGE img_planeExplode1, img_planeExplode2; //飞机爆炸图片
IMAGE img_bullet1, img_bullet2; //子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; //敌机图片
int isExplode = 0; //飞机是否爆炸
int score=0; //得分
void startup()
{
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\game_music.mp3 alias bkmusic"), NULL, 0, NULL); //打开背景音乐
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL); //循环播放
initgraph(width,high);
loadimage(&img_bk, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\background.jpg"));
loadimage(&img_planeNormal1, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\planeNormal_1.jpg"));
loadimage(&img_planeNormal2, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\planeNormal_2.jpg"));
loadimage(&img_bullet1, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\bullet1.jpg"));
loadimage(&img_bullet2, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\bullet2.jpg"));
loadimage(&img_enemyPlane1, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\enemyPlane1.jpg"));
loadimage(&img_enemyPlane2, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\enemyPlane2.jpg"));
loadimage(&img_planeExplode1, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\planeExplode_1.jpg"));
loadimage(&img_planeExplode2, _T("C:\\Users\\asus\\Desktop\\飞机大战素材\\planeExplode_2.jpg"));
position_x = width * 0.7; //初始化飞机位置
position_y = high * 0.5;
bullet_x = position_x; //初始化子弹位置
bullet_y = -85;
enemy_x = width * 0.5;
enemy_y = 10;
BeginBatchDraw();
}
void show()
{
putimage(0, 0, &img_bk); //显示背景
if (isExplode == 0)
{
putimage(position_x - 50, position_y - 60, &img_planeNormal1, NOTSRCERASE); //显示正常飞机
putimage(position_x - 50, position_y - 60, &img_planeNormal2, SRCINVERT);
putimage(bullet_x - 7, bullet_y, &img_bullet1, NOTSRCERASE); //显示子弹
putimage(bullet_x - 7, bullet_y, &img_bullet2, SRCINVERT);
putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE); //显示敌机
putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);
}
else
{
putimage(position_x - 50, position_y - 60, &img_planeExplode1, NOTSRCERASE); //显示爆炸飞机
putimage(position_x - 50, position_y - 60, &img_planeExplode2, SRCINVERT);
outtextxy(width * 0.48, high * 0.35, _T("游戏失败! 按任意键退出游戏"));
}
outtextxy(width * 0.48, high * 0.25, _T("得分:"));
TCHAR s[5];
_stprintf_s(s, _T("%d"), score);
outtextxy(width * 0.55, high * 0.25, s);
FlushBatchDraw();
Sleep(2);
}
void updateWithoutInput()
{
if (bullet_y > -25)
bullet_y -= 3;
if (enemy_y < high - 25)
{
if (score < 10)
enemy_y = enemy_y + 0.5;
else if (score < 20)
enemy_y = enemy_y + 1;
else if (score < 30)
enemy_y = enemy_y + 2;
else if (score < 40)
enemy_y = enemy_y + 3;
else
enemy_y = enemy_y + 4;
}
else
enemy_y = 10;
if ((bullet_x-50 - enemy_x)* (bullet_x-50 - enemy_x) + (bullet_y-50 - enemy_y)* (bullet_y-50 - enemy_y) < 2500)
{
enemy_x = rand()% (width-100)+50;
enemy_y = -40;
bullet_y = -85;
mciSendString(_T("close gemusic"), NULL, 0, NULL); //先关闭前面的音乐
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\gotEnemy.mp3 alias gemusic"), NULL, 0, NULL); //打开音乐
mciSendString(_T("play gemusic"), NULL, 0, NULL); //仅播放一次
score++;
if (score > 0 && score % 5 == 0 && score % 2 != 0)
{
mciSendString(_T("close 5music"), NULL, 0, NULL);
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\5.mp3 alias 5music"), NULL, 0, NULL);
mciSendString(_T("play 5music"), NULL, 0, NULL);
}
if (score % 10 == 0)
{
mciSendString(_T("close 10music"), NULL, 0, NULL);
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\10.mp3 alias 10music"), NULL, 0, NULL);
mciSendString(_T("play 10music"), NULL, 0, NULL);
}
}
if ((position_x-50 - enemy_x)* (position_x-50 - enemy_x) + (position_y-190 - enemy_y)* (position_y - enemy_y) < 500)
{
isExplode = 1;
mciSendString(_T("close exmusic"), NULL, 0, NULL);
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\explode.mp3 alias exmusic"), NULL, 0, NULL);
mciSendString(_T("play exmusic"), NULL, 0, NULL);
}
}
void updateWithInput()
{
MOUSEMSG m; //定义鼠标信息
while (MouseHit()) //检测目前是否有鼠标信息
{
m = GetMouseMsg();
if (m.uMsg == WM_MOUSEMOVE)
{
//飞机的位置等于鼠标所在位置
position_x = m.x;
position_y = m.y;
}
else if (m.uMsg == WM_LBUTTONDOWN)
{
//按下鼠标左键发射子弹
bullet_x = position_x;
bullet_y = position_y - 85;
mciSendString(_T("close fgmusic"), NULL, 0, NULL);
mciSendString(_T("open C:\\Users\\asus\\Desktop\\飞机大战素材\\f_gun.MP3 alias fgmusic"), NULL, 0, NULL);
mciSendString(_T("play fgmusic"), NULL, 0, NULL);
}
}
}
void gameover()
{
EndBatchDraw();
closegraph();
}
int main()
{
startup(); //数据初始化
while (1)
{
show();
if (isExplode == 1)
{
_getch();
break;
}
updateWithoutInput();
updateWithInput();
}
gameover(); //游戏结束,进行后续处理
return 0;
}
飞机大战(easyx版)
最新推荐文章于 2023-06-13 21:02:15 发布