我正在做飞机大战的游戏,背景输出了,背景声也有,但是飞机和子弹却没有,不知道怎么回事,有人能帮助一下吗?
#define _CRT_SECURE_NO_WARNINGS
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")
#define High 579 // 游戏画面尺寸
#define Width 512
IMAGE img_bk; // 背景图片
float position_x, position_y; // 飞机位置
float bullet_x, bullet_y; // 子弹位置
IMAGE img_planeNormal1, img_planeNormal2; // 正常飞机图片
IMAGE img_bullet1, img_bullet2; // 子弹图片
void startup()
{
mciSendString("open .\\game_music.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL); // 循环播放
initgraph(Width, High);
loadimage(&img_bk, ".\\background.jpg");
loadimage(&img_planeNormal1, ".\\planeNormal_1.jpg");
loadimage(&img_planeNormal2, ".\\planeNormal_2.jpg");
loadimage(&img_bullet1, ".\\bullet1.jpg");
loadimage(&img_bullet2, ".\\bullet2.jpg");
position_x = Width * 0.5;
position_y = High * 0.8;
bullet_x = position_x;
bullet_y = -25;
BeginBatchDraw();
}
void show()
{
putimage(0, 0, &img_bk); // 显示背景
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);
FlushBatchDraw();
Sleep(2);
}
void updateWithoutInput()
{
if (bullet_y > -25)
bullet_y = bullet_y - 3;
}
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("stop fgmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close fgmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open .\\f_gun.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play fgmusic", NULL, 0, NULL); // 仅播放一次
}
}
}
void gameover()
{
EndBatchDraw();
_getch();
closegraph();
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
gameover(); // 游戏结束、后续处理
return 0;
}