

代码:
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#pragma warning(disable:4996)
#pragma comment(lib,"Winmm.lib")
#define High 864
#define Width 591 //游戏画面尺寸
IMAGE img_bk; //背景图片
IMAGE img_planeNormal1, img_planeNormal2; //正常飞机照片
IMAGE img_planeExplode1, img_planeExplode2;//爆炸飞机图片
IMAGE img_bullet1, img_bullet2;// 子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; //敌机照片
float position_x, position_y; //飞机位置
float bullet_x, bullet_y; //子弹位置
float enemy_x, enemy_y; //敌机位置
int isExpolde = 0; //飞机是否爆炸
int score = 0; //得分
int gameStatus = 0;// 游戏状态,0为初始界面 ,1为正常游戏,2为结束游戏,3为游戏暂停
void staryMenu(); //菜单
void pauseMenu();//暂停菜单
void startup(); //数据初始化
void show(); //显示画面
void updateWithoutInput();//与用户输入无关的更新
void updateWithInput(); //与用户输入有关的更新
void gameover(); //游戏结束,之后的处理
void readRecordFile();//读取游戏存档
void writeRecordFile();//存储游戏存档
void staryMenu() //菜单
{
putimage(0, 0, &img_bk); //显示背景
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(50, 0, _T("黑体"));
outtextxy(Width*0.3, High*0.2, "1新游戏");
outtextxy(Width*0.3, High*0.3, "2读取游戏存档");
outtextxy(Width*0.3, High*0.4, "3退出");
settextcolor(BLACK);
settextstyle(30, 0, _T("黑体"));
outtextxy(Width*0.25, High*0.55, "鼠标控制飞机移动");
outtextxy(Width*0.25, High*0.6, "左键发射子弹");
outtextxy(Width * 025, High*0.65, "esc暂停游戏");
outtextxy(Width * 025, High*0.7, "撞击后按任意键重新开始");
FlushBatchDraw();
Sleep(2);
char input;
if (kbhit())
{
input = getch();
if (input == '1')
gameStatus = 1;
else if (input == '2')
{
writeRecordFile();
gameStatus = 1;
}
else if (input == '3');
{
gameStatus = 2;
exit(0);
}
}
}
void pauseMenu()//暂停菜单
{
putimage(0, 0, &img_bk); //显示背景
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(50, 0, _T("黑体"));
outtextxy(Width*0.3, High*0.2, "1新游戏");
outtextxy(Width*0.3, High*0.3, "2读取游戏存档");
outtextxy(Width*0.3, High*0.4, "3退出");
settextcolor(BLUE);
settextstyle(30, 0, _T("黑体"));
outtextxy(Width*0.25, High*0.55, "鼠标控制飞机移动");
outtextxy(Width*0.25, High*0.6, "左键发射子弹");
outtextxy(Width * 025, High*0.65, "esc暂停游戏");
outtextxy(Width * 025, High*0.7, "撞击后按任意键重新开始");
FlushBatchDraw();
Sleep(2);
char input;
if (kbhit())
{
input = getch();
if (input == '1')
gameStatus = 1;
else if (input == '2')
{
readRecordFile();
gameStatus = 1;
}
else if (input == '3');
{
gameStatus = 2;
exit(0);
}
}
}
void readRecordFile()//读取游戏存档
{
FILE *fp;
fp = fopen("..\\ttt", "r");
fscanf(fp, "%f %f %f %f %f %f %d %d", &position_x, &position_y, &bullet_x, &bullet_y, &enemy_x, &enemy_y, &isExpolde, &score);
fclose(fp);
}
void writeRecordFile()//读取游戏存档
{
FILE *fp;
fp = fopen("..\\ttt", "w");
fscanf(fp, "%f %f %f %f %f %f %d %d", &position_x, &position_y, &bullet_x, &bullet_y, &enemy_x, &enemy_y, &isExpolde, &score);
fclose(fp);
}
void startup()//数据初始化
{
mciSendString("open ..\\images\\123.mp3 alias bkmusic", 0, 0, 0);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL);//循环播放
initgraph(Width, High);
//获取窗口句柄
HWND hwnd = GetHWnd();
//设置窗口标题文字
SetWindowText(hwnd, "飞机大战1.0");
loadimage(&img_bk, "..\\images\\background.jpg"); //背景
loadimage(&img_planeNormal1, "..\\images\\planeNormal_1.jpg"); //飞机
loadimage(&img_planeNormal2, "..\\images\planeNormal_2.jpg");
loadimage(&img_bullet1, "..\\images\\bullet1.jpg"); //子弹
loadimage(&img_bullet2, "..\\images\\buttet2.jpg");
loadimage(&img_enemyPlane1, "..\\images\\enemy_1.jpg"); //敌机
loadimage(&img_enemyPlane2, "..\\images\\enemy_2.jpg");
loadimage(&img_planeExplode1, "..\\images\\planeBoom_1.jpg"); //
loadimage(&img_planeExplode2, "..\\images\\planeBoom_1.jpg");
position_x = Width * 0.5;
position_y = High * 0.7;
bullet_x = position_x;
bullet_y = -85;
enemy_x = Width * 0.5;
enemy_y = 10;
BeginBatchDraw();
while (gameStatus == 0)
staryMenu();
}
void show()
{
while (gameStatus == 3)
pauseMenu();
putimage(0, 0, &img_bk); //显示背景
if (isExpolde == 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_planeExplode1, SRCINVERT);
}
settextcolor(RED);
settextstyle(20, 0, _T("黑体"));
outtextxy(Width*0.48, High*0.95, "得分:");
char s[5];
sprintf(s, "%d", score);
outtextxy(Width*0.55, High*0.95, s);
FlushBatchDraw();
Sleep(2);
}
void updateWithoutInput() //与用户输入无关的更新
{
if (isExpolde == 0)
{
if (bullet_y > -25)
bullet_y = bullet_y - 2;
if (enemy_y < High - 25)
enemy_y = enemy_y + 0.5;
else
enemy_y = 10;
if (float(bullet_x - enemy_x) + float(bullet_y - enemy_y) < 80) //子弹击中敌机
{
enemy_x = rand() % Width;
enemy_y = -40;
bullet_y = -85;
mciSendString("stop gemusic", NULL, 0, NULL);
mciSendString("close gemusic", NULL, 0, NULL);
mciSendString("open ..\\images\\123.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString("play gemusic", NULL, 0, NULL);
score++;
if (score > 0 && score % 5 == 0 && score % 2 != 0)
{
mciSendString("stop gemusic", NULL, 0, NULL);
mciSendString("close gemusic", NULL, 0, NULL);
mciSendString("open ..\\images\\123.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString("play gemusic", NULL, 0, NULL);
}
if (score % 10 == 0)
{
mciSendString("stop gemusic", NULL, 0, NULL);
mciSendString("close gemusic", NULL, 0, NULL);
mciSendString("open ..\\images\\123.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString("play gemusic", NULL, 0, NULL);
}
}
if (float(position_x - enemy_x) + float(position_y - enemy_y) < 150) //敌机击中我机
{
isExpolde = 1;
mciSendString("stop gemusic", NULL, 0, NULL);
mciSendString("close gemusic", NULL, 0, NULL);
mciSendString("open ..\\images\\123.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString("play gemusic", NULL, 0, NULL);
}
}
}
void updateWithInput() //与用户输入有关的更新
{
if (isExpolde == 0)
{
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;
}
}
}
char input;
if (kbhit())
{
input = getch();
if (input == 27)
{
gameStatus = 3;
}
}
}
void gameover()
{
EndBatchDraw();
getch();
closegraph();
}
int main()
{
startup();
while (1)
{
show();
void updateWithoutInput(); //与用户输入无关的更新
void updateWithInput(); //与用户输入有关的更新
}
gameover();
return 0;
}