前言
这是大一期间C语言课的大作业,游戏类型就是比较容易实现的飞机大战系列。由于代码是我大一时期写的,所以可能看起来不太规范,游戏本身可能比较粗糙,所以本篇博客仅供参考,希望该博客可以帮助到一些同学完成大学期间的C语言课大作业。(需要源码和素材,请私信)
游戏效果预览
游戏故事概要:
游戏分为剧情模式和无尽模式
主要剧情
剧情模式:地球遭遇未知生命体入侵,难以与强敌抗衡,无奈之下派出星际战机迎敌。敌方的进攻并没有想象中的这么强大,但是由于他们能力特殊,可以不断对时间进行重置,一旦他们被击败,他们将时间倒流重新开始进攻,并且每次进攻都变得越来越猛烈。人类终于知道自己不过是一群虫子,根本翻不起大浪,但是总需要有人仰望星空,成为新的希望。在第一波防守中,人类取得了初步胜利,就在某些人认为敌人不过如此的时候,人马座旋臂出现大量敌军以接近光的速度向坐落在猎户座旋臂上的太阳系袭来,在接近太阳系的过程中,速度不断减慢,在太阳系边界减到了1/10光速,人类见到了三体探测器的影像探,探测器的大小与预想的差不多,长三点五米,观察员看到它时,产生了与其他人一样的印象:一滴水银。探测器呈完美的水滴形状,头部浑圆,尾部很尖,表面是极其光滑的全反射镜面,银河系在它的表面映成一片流畅的光纹,使得这滴水银看上去纯洁而唯美。它的液滴外形是那么栩栩如生,以至于观察员有时真以为它就是液态的,根本不可能有内部机械结构。一些乐观的人以为这是求和的象征。
事实上真的是这样么?
在接近人类战舰的时候水滴周围直接爆炸,水滴速度猛增,直接将前几排的战舰一一穿破,只剩下4艘战舰往四个不同的方向逃跑。玩家作为其中逃亡成功的一艘战舰的舰长正驾驶着战舰往银河中心逃亡。中心遇到了虫洞效应,战舰直接接近银河中心,很多人预测在银河中心的黑洞可能可以扭转局面,但是进入黑洞到底是被撕裂成渣?还是像星际穿越一样进入五维空间,强行改变过去呢?未知生命体知道银河中心是战略要地,所以布置重军把守,而我方需要打破防守,强行进入黑洞,试图扭转局面。星际大战,一触即发!
无尽模式:玩家将遇到无限波次的敌军,需要尽可能地击败敌军,不断地进行闯关。
游戏关卡设计
剧情模式关卡:前五关是在太阳系周围与敌军战斗,每一关卡可能会随机出现Boss级别的敌机,前五关通关后,出现剧情—水滴袭来,我方舰队溃败,无力对抗。玩家进入虫洞,空间跳跃来到了银河系中心地带,试图强行进去黑洞,遇上了敌方的防守舰队,以少敌方,背水一战,银河中心,终极之战,一触即发!
无尽模式关卡:在这一模式中,将有无数关卡,玩家需要不断击败敌方来让自己存活下去。
游戏操控设计:
键盘操作:
w、a、s、d作为方向键进行操控,空格键为射击,ESC可以暂停游戏并且进入空中商店购买补给:1-炸弹、2-修复、3-存档。q可以引爆炸弹,摧毁除了Boss之外的所有敌军。
鼠标操控: 鼠标移动来控制战机的移动,左键射击,ESC可以暂停游戏并且进入空中商店购买补给:1-炸弹、2-修复、3-存档。q可以引爆炸弹,摧毁除了Boss之外的所有敌军。
图形框图设计:
游戏音频的加载、文字加载、图片处理都是右EasyX库来处理的,该产品不需要数据库与网络服务的支持,布署在本地后即可开发使用。子弹与战机表面接触就相当于战机被击中了,框图内下方会依次显示:当前波次、金币、生命值、敌军的数量、boss的生命值。
游戏的代码设计
图片的加载、音频的播放、文字由EasyX库的相关函数进行实现。,键盘操控、鼠标移动也由相关函数实现。子弹的移动根据for循环来对数组中的所有子弹的坐标进行变换,再以图片的行房加载出来。然后由两点间的距离公式来判断子弹与飞机之间的距离来判断是否击中了飞机,若我方飞机与敌方飞机相撞也会进行相关处理。打到中间也可以选择存档,下一次进入游戏后不需要选择新游戏,直接选择加载档案即可接着上一次的游戏继续。
全局变量声明
#pragma once
#include "config.hpp"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <conio.h>
#include <Windows.h>
#include <graphics.h>
#include <ctime>
using namespace std;
extern IMAGE img_fbk;
//前景1
extern IMAGE img_fbk2;
//前景2
extern IMAGE img_bk;
extern IMAGE img_pauseground;
//补给时的背景
extern IMAGE img_game1;
//level关卡的背景
extern IMAGE img_game2;
//level关卡的背景
extern IMAGE img_plane1_1, img_plane1_2;
//我方飞机1
extern IMAGE img_bullet1, img_bullet2;
//我方飞机的子弹
extern IMAGE img_plane2_1, img_plane2_2;
//我方飞机2
extern IMAGE img_plane3_1, img_plane3_2;
//我方敌机3
extern IMAGE img_boss_bullet1, img_boss_bullet2;
//敌方boss的子弹
extern IMAGE img_iron_man1, img_iron_man2;
//我方开启技能:iron-man辅助攻击
extern IMAGE img_PlaneChoice1_1, img_PlaneChoice1_2;
//飞机库中飞机1:歼20
extern IMAGE img_PlaneChoice2_1, img_PlaneChoice2_2;
//飞机库中飞机2:歼11
extern IMAGE img_PlaneChoice3_1, img_PlaneChoice3_2;
//飞机库中飞机3:暗箭
extern IMAGE img_enemy1_1, img_enemy1_2;
//敌机1
extern IMAGE img_enemy2_1, img_enemy2_2;
//敌机2
extern IMAGE img_enemy3_1, img_enemy3_2;
//敌机3
extern IMAGE img_boss1_1, img_boss1_2;
//boss1
extern IMAGE img_boss2_1, img_boss2_2;
//boss2
extern IMAGE img_explode1, img_explode2;
//爆炸图
extern IMAGE img_enemy1_explode1, img_enemy1_explode2;
//敌机1的爆炸图
extern IMAGE img_enemy2_explode1, img_enemy2_explode2;
//敌机2的爆炸图
extern IMAGE img_Start_Up1;
//开始情景图1
extern IMAGE img_Start_Up2;
//开始情景图2
extern IMAGE img_enemy1_bullet1, img_enemy1_bullet2;
//敌机1的子弹
extern IMAGE img_enemy2_bullet1, img_enemy2_bullet2;
//敌机2的子弹
extern IMAGE img_enemy3_bullet1, img_enemy3_bullet2;
//敌机3的子弹
extern IMAGE img_boom1, img_boom2;
//道具:炸弹
extern IMAGE img_bloodhealth1, img_bloodhealth2;
//道具:加血
extern IMAGE img_boomexplode1, img_boomexplode2;
//氢弹爆炸
extern IMAGE img_boss1_bullet1, img_boss1_bullet2;
//boss1的子弹
extern IMAGE img_boss2_bullet1, img_boss2_bullet2;
//boss2的子弹
extern IMAGE XM1, XM2, XM3, XM4, XM5, XM6, XM7, XM2_1, XM2_2, XM2_3, XM2_4, XM2_5, XM2_6, XM2_7;
//序幕的图片
extern int GameStatus;
//记录当前游戏状态:
//0--初次进入游戏;1--正在游戏;2--暂停游戏;3--结束游戏;4--购买补给
extern int isExplode;
//飞机是否爆炸
//0--正常 ; 1--boom;
extern double position_x, position_y;
//飞机坐标;
extern double bullet_x[100005], bullet_y[100005];
//本飞机子弹的位置
extern int bullet_tot;
//弹药的数量;
//第一种弹药每次伤害血量为 1
extern int bullet1;
//子弹的号数
extern int s_bullet1;
//由于射出界面的子弹不再管
extern int money;
//飞机击落敌机后可以得到相应的金钱,金钱可以用来购买补给
extern int score;
//玩家的得分
extern int health;
//飞机自身的血量
extern int enemy1_blood[20005];
//敌机1的血量
extern int enemy2_blood[20005];
//敌机2的血量
extern int enemy3_blood[20005];
//敌机3的血量
extern double enemy1_x[20005], enemy1_y[20005];
//敌机1的位置
extern double enemy2_x[20005], enemy2_y[20005];
//敌机2的位置
extern double enemy3_x[20005], enemy3_y[20005];
//敌机3的位置
extern int enemytot1;
//这一轮敌机1的数量
extern int enemytot2;
//这一轮敌机2的数量
extern int enemytot3;
//这一轮敌机3的数量
extern int alivetot1;
//敌机1还剩下的数量
extern int alivetot2;
//敌机2还剩下的数量
extern int alivetot3;
//敌机3还剩下的数量
extern int boss_blood1;
//boss1的血量
extern int boss_blood2;
//boss2的血量
extern double boss1_x, boss1_y;
//boss1的位置
extern double boss2_x, boss2_y;
//boss2的位置
extern int levelsign;
//levelsign==1,指关卡1 levelsign==2,指关卡2 levelsign==3,指关卡3;
extern int level1sign;
//关卡1的波次
extern int level2sign;
//关卡2的波次
extern int level3sign;
//关卡3的波次
extern int wusign;
//无尽模式的波次
extern int plane_sign;
//飞机选择:1-歼20,2-歼11,3-暗箭;
extern int explodesign1[20005];
//敌机1是否爆炸
extern int explodesign2[20005];
//敌机2是否爆炸
extern int explodesign3[20005];
//敌机3是否爆炸
extern double enemy1_bullet_x[20005], enemy1_bullet_y[20005];
//敌机1的子弹
extern double enemy2_bullet_x[20005], enemy2_bullet_y[20005];
//敌机2的子弹
extern double enemy3_bullet_x[20005], enemy3_bullet_y[20005];
//敌机3的子弹
extern double boss1_bullet_x, boss1_bullet_y;
//boss1的子弹
extern double boss2_bullet_x, boss2_bullet_y;
//boss2的子弹
extern int enemy1_turn[2005];
//敌机1移动方向,1--左移,2--右移
extern int enemy2_turn[2005];
//敌机2移动方向,1--左移,2--右移
extern int enemy3_turn[2005];
//敌机3移动方向,1--左移,2--右移
extern int GameSign;
//GameSign==1表示的是剧情模式,GameSign==2表示的是无尽模式
extern int explodeboss1;
//判断boss1是否爆炸 0--未爆炸,1爆炸
extern int explodeboss2;
//判断boss1是否爆炸 0--未爆炸,1爆炸
extern int signboss1;
//判断boss1是否出现
extern int signboss2;
//判断boss2是否出现
extern int cnt1;
//boss1爆炸时间计数器
extern int cnt2;
//boss1爆炸时间计数器
extern int boss1_turn;
//boss1移动 1为左,2为右
extern int boss2_turn;
//boss2移动 1为左,2为右
extern int boomtot;
// 氢弹的数量
extern int bossexplodesign1;
//boss1爆炸图
extern int bossexplodesign2;
//boss2爆炸图
extern int bosscnt;
函数声明
#pragma once
#pragma comment(lib,"Winmm.lib")
#pragma warning(disable:4996)
#include "global.hpp"
void ReadRecordFile();
void WriteRecordFile();
void InitGame();
void GameStart();
void XuMu();
void XuMu2();
void Show();
void UpdateInput();
void GameChoice1();
void GameChoice2();
主函数导入
#include "define.hpp"
int main()
{
InitGame();
GameStart();
return 0;
}
游戏初始化
#include "define.hpp"
IMAGE img_fbk;
IMAGE img_fbk2;
IMAGE img_bk;
IMAGE img_pauseground;
IMAGE img_game1;
IMAGE img_game2;
IMAGE img_plane1_1, img_plane1_2;
IMAGE img_bullet1, img_bullet2;
IMAGE img_plane2_1, img_plane2_2;
IMAGE img_plane3_1, img_plane3_2;
IMAGE img_boss_bullet1, img_boss_bullet2;
IMAGE img_iron_man1, img_iron_man2;
IMAGE img_PlaneChoice1_1, img_PlaneChoice1_2;
IMAGE img_PlaneChoice2_1, img_PlaneChoice2_2;
IMAGE img_PlaneChoice3_1, img_PlaneChoice3_2;
IMAGE img_enemy1_1, img_enemy1_2;
IMAGE img_enemy2_1, img_enemy2_2;
IMAGE img_enemy3_1, img_enemy3_2;
IMAGE img_boss1_1, img_boss1_2;
IMAGE img_boss2_1, img_boss2_2;
IMAGE img_explode1, img_explode2;
IMAGE img_enemy1_explode1, img_enemy1_explode2;
IMAGE img_enemy2_explode1, img_enemy2_explode2;
IMAGE img_Start_Up1;
IMAGE img_Start_Up2;
IMAGE img_enemy1_bullet1, img_enemy1_bullet2;
IMAGE img_enemy2_bullet1, img_enemy2_bullet2;
IMAGE img_enemy3_bullet1, img_enemy3_bullet2;
IMAGE img_boom1, img_boom2;
IMAGE img_bloodhealth1, img_bloodhealth2;
IMAGE img_boomexplode1, img_boomexplode2;
IMAGE img_boss1_bullet1, img_boss1_bullet2;
IMAGE img_boss2_bullet1, img_boss2_bullet2;
IMAGE XM1, XM2, XM3, XM4, XM5, XM6, XM7, XM2_1, XM2_2, XM2_3, XM2_4, XM2_5, XM2_6, XM2_7;
int GameStatus;
int isExplode;
double position_x, position_y;
double bullet_x[100005], bullet_y[100005];
int bullet_tot;
int bullet1;
int s_bullet1;
int money = 300;
int score;
int health = 100;
int enemy1_blood[20005];
int enemy2_blood[20005];
int enemy3_blood[20005];
double enemy1_x[20005], enemy1_y[20005];
double enemy2_x[20005], enemy2_y[20005];
double enemy3_x[20005], enemy3_y[20005];
int enemytot1;
int enemytot2;
int enemytot3;
int alivetot1;
int alivetot2;
int alivetot3;
int boss_blood1;
int boss_blood2;
double boss1_x, boss1_y;
double boss2_x, boss2_y;
int levelsign;
int level1sign;
int level2sign;
int level3sign;
int wusign = 0;
int plane_sign;
int explodesign1[20005];
int explodesign2[20005];
int explodesign3[20005];
double enemy1_bullet_x[20005], enemy1_bullet_y[20005];
double enemy2_bullet_x[20005], enemy2_bullet_y[20005];
double enemy3_bullet_x[20005], enemy3_bullet_y[20005];
double boss1_bullet_x, boss1_bullet_y;
double boss2_bullet_x, boss2_bullet_y;
int enemy1_turn[2005];
int enemy2_turn[2005];
int enemy3_turn[2005];
int GameSign;
int explodeboss1;
int explodeboss2;
int signboss1;
int signboss2;
int cnt1;
int cnt2;
int boss1_turn;
int boss2_turn;
int boomtot;
int bossexplodesign1;
int bossexplodesign2;
int bosscnt = 0;
void Gotoxy(int x, int y);
void HideCursor(); // 用于隐藏光标
void LoadImage();
void PlayMusic();
void StartMenu();
void InitGame();
void InitGame()
{
srand((unsigned)time(NULL));
LoadImage();
PlayMusic();
bullet_tot = 0x7f7f7f7f;
//第一种弹药无限
}
void Gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void LoadImage()
{
initgraph(width, high);
loadimage(&img_fbk, "image/frontground.jpg");
loadimage(&img_fbk2, "image/frontground2.jpg");
loadimage(&img_pauseground, "image/pauseground.jpg");
loadimage(&img_game1, "image/game1.jpg");
loadimage(&img_game2, "image/game2.jpg");
loadimage(&img_plane1_1, "image/plane1_1.jpg");
loadimage(&img_plane1_2, "image/plane1_2.jpg");
loadimage(&img_plane2_1, "image/plane2_1.jpg");
loadimage(&img_plane2_2, "image/plane2_2.jpg");
loadimage(&img_plane3_1, "image/plane3_1.jpg");
loadimage(&img_plane3_2, "image/plane3_2.jpg");
loadimage(&img_bullet1, "image/bullet1.jpg");
loadimage(&img_bullet2, "image/bullet2.jpg");
loadimage(&img_boss1_bullet1, "image/boss_bullet1.jpg");
loadimage(&img_boss1_bullet2, "image/boss_bullet2.jpg");
loadimage(&img_boss2_bullet1, "image/boss_bullet1.jpg");
loadimage(&img_boss2_bullet2, "image/boss_bullet2.jpg");
loadimage(&img_PlaneChoice1_1, "image/PlaneChoice1_1.jpg");
loadimage(&img_PlaneChoice1_2, "image/PlaneChoice1_2.jpg");
loadimage(&img_PlaneChoice2_1, "image/PlaneChoice2_1.jpg");
loadimage(&img_PlaneChoice2_2, "image/PlaneChoice2_2.jpg");
loadimage(&img_PlaneChoice3_1, "image/PlaneChoice3_1.jpg");
loadimage(&img_PlaneChoice3_2, "image/PlaneChoice3_2.jpg");
loadimage(&img_iron_man1, "image/man1.jpg");
loadimage(&img_iron_man2, "image/man2.jpg");
loadimage(&img_enemy1_1, "image/enemy1_1.jpg");
loadimage(&img_enemy1_2, "image/enemy1_2.jpg");
loadimage(&img_enemy2_1, "image/enemy2_1.jpg");
loadimage(&img_enemy2_2, "image/enemy2_2.jpg");
loadimage(&img_enemy3_1, "image/enemy3_1.jpg");
loadimage(&img_enemy3_2, "image/enemy3_2.jpg");
loadimage(&img_boss1_1, "image/boss1_1.jpg");
loadimage(&img_boss1_2, "image/boss1_2.jpg");
loadimage(&img_boss2_1, "image/boss2_1.jpg");
loadimage(&img_boss2_2, "image/boss2_2.jpg");
loadimage(&img_explode1, "image/explode1.jpg");
loadimage(&img_explode2, "image/explode2.jpg");
loadimage(&img_enemy1_explode1, "image/enemy1_explode1.jpg");
loadimage(&img_enemy1_explode2, "image/enemy1_explode2.jpg");
loadimage(&img_enemy2_explode1, "image/enemy2_explode1.jpg");
loadimage(&img_enemy2_explode2, "image/enemy2_explode2.jpg");
loadimage(&img_enemy1_bullet1, "image/enemy1_bullet1.jpg");
loadimage(&img_enemy1_bullet2, "image/enemy1_bullet2.jpg");
loadimage(&img_enemy2_bullet1, "image/enemy2_bullet1.jpg");
loadimage(&img_enemy2_bullet2, "image/enemy2_bullet2.jpg");
loadimage(&img_enemy3_bullet1, "image/enemy3_bullet1.jpg");
loadimage(&img_enemy3_bullet2, "image/enemy3_bullet2.jpg");
loadimage(&img_Start_Up1, "image/Start_Up1.jpg");
loadimage(&img_Start_Up2, "image/Start_Up2.jpg");
loadimage(&img_boom1, "image/boom1.jpg");
loadimage(&img_boom2, "image/boom2.jpg");
loadimage(&img_bloodhealth1, "image/bloodhealth1.jpg");
loadimage(&img_bloodhealth2, "image/bloodhealth2.jpg");
loadimage(&img_boomexplode1, "image/boomexplode1.jpg");
loadimage(&img_boomexplode2, "image/boomexplode2.jpg");
loadimage(&XM1, "image/XM1.jpg");
loadimage(&XM2, "image/XM2.jpg");
loadimage(&XM3, "image/XM3.jpg");
loadimage(&XM4, "image/XM4.jpg");
loadimage(&XM5, "image/XM5.jpg");
loadimage(&XM6, "image/XM6.jpg");
loadimage(&XM7, "image/XM7.jpg");
loadimage(&XM2_1, "image/XM2_1.jpg");
loadimage(&XM2_2, "image/XM2_2.jpg");
loadimage(&XM2_3, "image/XM2_3.jpg");
loadimage(&XM2_4, "image/XM2_4.jpg");
loadimage(&XM2_5, "image/XM2_5.jpg");
loadimage(&XM2_6, "image/XM2_6.jpg");
loadimage(&XM2_7, "image/XM2_7.jpg");
position_x = width / 2;
position_y = high * 0.8;
boss1_x = 100000;
boss2_x = 100000;
boss1_y = 100000;
boss2_y = 100000;
boss1_bullet_x = 100000;
boss1_bullet_y = 100000;
boss2_bullet_x = 100000;
boss2_bullet_y = 100000;
for (int i = 0; i < 100000; i++)
{
bullet_x[i] = -0x3f3f3f3f;
bullet_y[i] = -0x3f3f3f3f;
}
HideCursor();
//隐藏光标.
BeginBatchDraw();
//优化清屏.
while (GameStatus == 0)
{
StartMenu();
}
}
void PlayMusic()
{
mciSendString("pause PlaneChoice", NULL, 0, NULL);
mciSendString("stop PlaneChoice", NULL, 0, NULL);
mciSendString("open music/fly.mp3 alias fly", NULL, 0, NULL);
mciSendString("play fly", NULL, 0, NULL);
Sleep(1000);
mciSendString("pause fly", NULL, 0, NULL);
mciSendString("stop fly", NULL, 0, NULL);
}
void StartMenu()
{
putimage(0, 0, &img_fbk);
if (GameStatus == 0)
{
mciSendString("open music/frontmusic1.mp3 alias frontmusic1", NULL, 0, NULL);
mciSendString("play frontmusic1 repeat", NULL, 0, NULL);
}
else
{
mciSendString("pause frontmusic1", NULL, 0, NULL);
mciSendString("stop frontmusic1", NULL, 0, NULL);
}
setbkmode(TRANSPARENT);
//设置使背景透明
settextcolor(BLACK);
//设置字体的颜色
settextstyle(60, 0, _T("宋体"));
//设置字体大小为50,从左至右,字体为黑体
outtextxy(width * 0.4, high * 0.1, "星际大战");
settextcolor(RED);
//设置字体的颜色
settextstyle(40, 0, _T("黑体"));
//设置字体大小为50,从左至右,字体为黑体
outtextxy(width * 0.4, high * 0.3, "1 剧情模式");
outtextxy(width * 0.4, high * 0.4, "2 血战长空");
outtextxy(width * 0.4, high * 0.5, "3 退出游戏");
FlushBatchDraw();
if (_kbhit())
{
char input;
input = _getch();
if (input == '1')
{
//玩家选择了剧情模式
//即将转入GameChoice1选择新游戏或者读取档案
GameStatus = 2;
GameSign = 1;
//切换音乐前先关闭frontmusic1
mciSendString("pause frontmusic1", NULL, 0, NULL);
mciSendString("stop frontmusic1", NULL, 0, NULL);
while (GameStatus == 2)
{
GameChoice1();
}
}
else if (input == '2')
{
//玩家选择了血战长空即无尽模式
//即将转入GameChoice2选择新游戏或者读取档案
GameStatus = 2;
GameSign = 2;
//切换音乐前先关闭frontmusic1
mciSendString("pause frontmusic1", NULL, 0, NULL);
mciSendString("stop frontmusic1", NULL, 0, NULL);
while (GameStatus == 2)
{
GameChoice2();
}
}
else if (input == '3')
{
GameStatus = 3;
exit(0);
}
}
}
游戏模式选择
#include "define.hpp"
void GameChoice1();
void GameChoice2();
void PlaneChoice();
void GameChoice1()
{
putimage(0, 0, &img_fbk2);
mciSendString("open music/frontmusic2.mp3 alias frontmusic2", NULL, 0, NULL);
mciSendString("play frontmusic2 repeat", NULL, 0, NULL);
setbkmode(TRANSPARENT);
settextcolor(RED);
settextstyle(30, 0, _T("黑体"));
outtextxy(width * 0.05, high * 0.1, "1 新游戏");
outtextxy(width * 0.05, high * 0.2, "2 读取档案");
FlushBatchDraw();
if (_kbhit())
{
char input;
input = _getch();
if (input == '1')
{
//切换音乐前先关闭frontmusic2
mciSendString("pause frontmusic2", NULL, 0, NULL);
mciSendString("stop frontmusic2", NULL, 0, NULL);
while (GameStatus == 2)
{
PlaneChoice();
}
return;
}
else if (input == '2')
{
//切换音乐前先关闭frontmusic2
mciSendString("pause frontmusic2", NULL, 0, NULL);
mciSendString("stop frontmusic2", NULL, 0, NULL);
ReadRecordFile();
GameStatus = 1;
return;
}
}
}
//GameChoice2表示血战长空模式的选择:
//新游戏or读取档案
void GameChoice2()
{
putimage(0, 0, &img_fbk2);
mciSendString("open music/frontmusic2.mp3 alias frontmusic2", NULL, 0, NULL);
mciSendString("play frontmusic2 repeat", NULL, 0, NULL);
setbkmode(TRANSPARENT);
settextcolor(RED);
settextstyle(30, 0, _T("黑体"));
outtextxy(width * 0.05, high * 0.1, "1 新游戏");
outtextxy(width * 0.05, high * 0.2, "2 读取档案");
FlushBatchDraw();
if (_kbhit())
{
char input;
input = _getch();
if (input == '1')
{
//切换音乐前先关闭frontmusic2
mciSendString("pause frontmusic2", NULL, 0, NULL);
mciSendString("stop frontmusic2", NULL, 0, NULL);
while (GameStatus == 2)
{
PlaneChoice();
}
return;
}
else if (input == '2')
{
//切换音乐前先关闭frontmusic2
mciSendString("pause frontmusic2", NULL, 0, NULL);
mciSendString("stop frontmusic2", NULL, 0, NULL);
ReadRecordFile();
GameStatus = 1;
return;
}
}
}
void PlaneChoice()
{
putimage(0, 0, &img_pauseground);
mciSendString("open music/PlaneChoice.mp3 alias PlaneChoice", NULL, 0, NULL);
mciSendString("play PlaneChoice repeat", NULL, 0, NULL);
setbkmode(TRANSPARENT);
settextcolor(RED);
settextstyle(50, 0, _T("宋体"));
outtextxy(width * 0.20, high * 0.1, "1 歼20");
outtextxy(width * 0.77, high * 0.1, "2 歼11");
putimage(200, high * 0.25, &img_PlaneChoice1_1, NOTSRCERASE);
putimage(200, high * 0.25, &img_PlaneChoice1_2, SRCINVERT);
putimage(900, high * 0.45, &img_PlaneChoice2_1, NOTSRCERASE);
putimage(900, high * 0.45, &img_PlaneChoice2_2, SRCINVERT);
if (_kbhit())
{
char input;
input = _getch();
if (input == '1')
{
plane_sign = 1;
}
else if (input == '2')
{
plane_sign = 2;
}
else if (input == '3')
{
plane_sign = 3;
}
GameStatus = 1;
}
FlushBatchDraw();
}
序幕设计
#include "define.hpp"
void XuMu();
void XuMu()
{
time_t start = 0, end = 0;
start = time(&start);
// Chapter one
while (1)
{
mciSendString("open music/XM1.mp3 alias XM", NULL, 0, NULL);
mciSendString("play XM", NULL, 0, NULL);
end = time(&end);
putimage(0, -30, &XM1);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM1);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM2);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM2);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM3);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM3);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM4);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM4);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM5);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM5);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM6);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM6);
Sleep(100);
FlushBatchDraw();
}
putimage(0, -30, &XM7);
for (int i = 0; i <= 60; i++)
{
cleardevice();
putimage(0, i - 30, &XM7);
Sleep(100);
FlushBatchDraw();
}
FlushBatchDraw();
mciSendString("pause XM", NULL, 0, NULL);
mciSendString("stop XM", NULL, 0, NULL);
break;
}
}
void XuMu2()
{
// Chapter Two
while (1)
{
mciSendString("open music/XM1.mp3 alias XM", NULL, 0, NULL);
mciSendString("play XM", NULL, 0, NULL);
putimage(0, -25, &XM2_1);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_1);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_2);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_2);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_3);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_3);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_4);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_4);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_5);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_5);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_6);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_6);
Sleep(200);
FlushBatchDraw();
}
putimage(0, -25, &XM2_7);
for (int i = 0; i <= 50; i++)
{
cleardevice();
putimage(0, i - 25, &XM2_7);
Sleep(200);
FlushBatchDraw();
}
FlushBatchDraw();
mciSendString("pause XM", NULL, 0, NULL);
mciSendString("stop XM", NULL, 0, NULL);
break;
}
}
游戏开始
#include "define.hpp"
void Check();
void PlaneUpdate();
void GameStart();
void GameStart()
{
if (GameSign == 1)
{
//剧情模式
XuMu();
//游戏序幕
while (1)
{
Check();
//判断是否开始新的波次
Show();
//显示画面
UpdateInput();
}
}
else if (GameSign == 2)
{
//无尽模式
while (1)
{
Check();
//判断是否开始新的波次
Show();
//显示画面
UpdateInput();
}
}
}
void Check()
{
if (alivetot1 <= 0 && alivetot2 <= 0 && alivetot3 <= 0 && signboss2 == 0 && signboss1 == 0)
{
PlaneUpdate();
}
}
void PlaneUpdate()
{
wusign++;
if (wusign % 10 == 7)
{
enemytot1 = enemytot2 = enemytot3 = 3;
}
else if (wusign % 10 == 0)
{
enemytot1 = enemytot2 = enemytot3 = 3;
}
else
{
enemytot1 = 3 * wusign;
enemytot2 = 2 * wusign;
enemytot3 = 1 * wusign;
}
alivetot1 = enemytot1;
alivetot2 = enemytot2;
alivetot3 = enemytot3;
memset(enemy1_x, 0x3f3f3f3f, sizeof(enemy1_x));
memset(enemy1_y, 0x3f3f3f3f, sizeof(enemy1_y));
memset(enemy2_x, 0x3f3f3f3f, sizeof(enemy2_x));
memset(enemy2_y, 0x3f3f3f3f, sizeof(enemy2_y));
memset(enemy3_x, 0x3f3f3f3f, sizeof(enemy3_x));
memset(enemy3_y, 0x3f3f3f3f, sizeof(enemy3_y));
for (int i = 1; i <= enemytot1; i++)
{
enemy1_x[i] = rand() % width;
enemy1_y[i] = 5;
enemy1_blood[i] = 2;
explodesign1[i] = 0;
if (i % 2)
{
enemy1_turn[i] = 1;
}
else
{
enemy1_turn[i] = 2;
}
if (i > 2)
{
enemy1_y[i] -= i * 25;
}
enemy1_bullet_x[i] = enemy1_x[i];
enemy1_bullet_y[i] = enemy1_y[i];
}
for (int i = 1; i <= enemytot2; i++)
{
enemy2_x[i] = rand() % width;
enemy2_y[i] = 10;
enemy2_blood[i] = 2;
explodesign2[i] = 0;
if (i % 2)
{
enemy2_turn[i] = 1;
}
else
{
enemy2_turn[i] = 2;
}
if (i > 2)
{
enemy2_y[i] -= i * 25;
}
enemy2_bullet_x[i] = enemy2_x[i];
enemy2_bullet_y[i] = enemy2_y[i];
}
for (int i = 1; i <= enemytot3; i++)
{
enemy3_x[i] = rand() % width;
enemy3_y[i] = 15;
enemy3_blood[i] = 2;
explodesign3[i] = 0;
if (i % 2)
{
enemy3_turn[i] = 1;
}
else
{
enemy3_turn[i] = 2;
}
if (i > 2)
{
enemy3_y[i] -= i * 25;
}
enemy3_bullet_x[i] = enemy3_x[i];
enemy3_bullet_y[i] = enemy3_y[i];
}
if (wusign % 3 == 0)
{
boss1_x = width / 2;
boss1_y = 30;
boss1_bullet_x = boss1_x;
boss1_bullet_y = boss1_y;
boss_blood1 = 30;
signboss1 = 1;
explodeboss1 = 0;
int f = rand() % 2;
if (f == 0)boss1_turn = 1;
else if (f == 1)boss1_turn = 2;
cnt1 = 0;
}
else if (wusign % 4 == 0)
{
boss2_x = width / 2;
boss2_y = 30;
boss2_bullet_x = boss2_x;
boss2_bullet_y = boss2_y;
boss_blood2 = 50;
signboss2 = 1;
explodeboss2 = 0;
int f = rand() % 2;
if (f == 0)boss2_turn = 1;
else if (f == 1)boss2_turn = 2;
cnt2 = 0;
}
}
游戏界面显示
#include "define.hpp"
void PauseMenu();
void Show();
void Show()
{
while (GameStatus == 4)
{
PauseMenu();
}
if (GameSign == 2)
{
putimage(0, 0, &img_game1);
mciSendString("open music/background1.mp3 alias background1", NULL, 0, NULL);
mciSendString("play background1 repeat", NULL, 0, NULL);
}
else
{
if (wusign <= 5)
{
putimage(0, 0, &img_game1);
mciSendString("open music/background1.mp3 alias background1", NULL, 0, NULL);
mciSendString("play background1", NULL, 0, NULL);
}
else
{
if (wusign == 6)
{
mciSendString("stop background1", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close background1", NULL, 0, NULL);
// 先把前面一次的音乐关闭
wusign++;
XuMu2();
}
putimage(0, 0, &img_game2);
mciSendString("open music/background2.mp3 alias background2", NULL, 0, NULL);
mciSendString("play background2", NULL, 0, NULL);
}
}
settextcolor(RED);
settextstyle(20, 0, _T("黑体"));
outtextxy(width * 0.45, high * 0.95, "生命值:");
char Health[5];
sprintf(Health, "%d", health);
outtextxy(width * 0.55, high * 0.95, Health);
char Score[5];
sprintf(Score, "%d", score);
outtextxy(width * 0.10, high * 0.95, "得分:");
outtextxy(width * 0.15, high * 0.95, Score);
char Money[5];
sprintf(Money, "%d", money);
outtextxy(width * 0.25, high * 0.95, "金钱:");
outtextxy(width * 0.3, high * 0.95, Money);
char BoomTot[5];
sprintf(BoomTot, "%d", boomtot);
outtextxy(width * 0.35, high * 0.95, "氢弹:");
outtextxy(width * 0.4, high * 0.95, BoomTot);
char Bossblood1[5];
sprintf(Bossblood1, "%d", boss_blood1);
outtextxy(width * 0.70, high * 0.85, "boss1:");
outtextxy(width * 0.75, high * 0.85, Bossblood1);
char Bossblood2[5];
sprintf(Bossblood2, "%d", boss_blood2);
outtextxy(width * 0.8, high * 0.85, "boss2:");
outtextxy(width * 0.85, high * 0.85, Bossblood2);
char BoCi[5];
sprintf(BoCi, "%d", wusign);
outtextxy(width * 0.1, high * 0.85, "波次:");
outtextxy(width * 0.15, high * 0.85, BoCi);
char Num1[5], Num2[5], Num3[5];
sprintf(Num1, "%d", alivetot1);
sprintf(Num2, "%d", alivetot2);
sprintf(Num3, "%d", alivetot3);
outtextxy(width * 0.70, high * 0.95, "数量1:");
outtextxy(width * 0.75, high * 0.95, Num1);
outtextxy(width * 0.8, high * 0.95, "数量2:");
outtextxy(width * 0.85, high * 0.95, Num2);
outtextxy(width * 0.9, high * 0.95, "数量3:");
outtextxy(width * 0.95, high * 0.95, Num3);
if (isExplode == 0)
{
if (plane_sign == 1)
{
putimage(position_x - 25, position_y - 50, &img_plane1_1, NOTSRCERASE);
putimage(position_x - 25, position_y - 50, &img_plane1_2, SRCINVERT);
// 飞机1
}
else if (plane_sign == 2)
{
putimage(position_x - 25, position_y - 50, &img_plane2_1, NOTSRCERASE);
putimage(position_x - 25, position_y - 50, &img_plane2_2, SRCINVERT);
// 飞机2
}
else
{
putimage(position_x - 25, position_y - 50, &img_plane3_1, NOTSRCERASE);
putimage(position_x - 25, position_y - 50, &img_plane3_2, SRCINVERT);
// 飞机3
}
for (int i = s_bullet1; i <= bullet1; i++)
{
putimage(bullet_x[i] - 7, bullet_y[i], &img_bullet1, NOTSRCERASE);
putimage(bullet_x[i] - 7, bullet_y[i], &img_bullet2, SRCINVERT);
}
// 显示子弹
for (int i = 1; i <= enemytot1; i++)
{
if (enemy1_y[i] <= high - 25)
{
putimage(enemy1_x[i] - 25, enemy1_y[i] - 50, &img_enemy1_1, NOTSRCERASE);
putimage(enemy1_x[i] - 25, enemy1_y[i] - 50, &img_enemy1_2, SRCINVERT);
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (enemy2_y[i] <= high - 25)
{
putimage(enemy2_x[i] - 25, enemy2_y[i] - 50, &img_enemy2_1, NOTSRCERASE);
putimage(enemy2_x[i] - 25, enemy2_y[i] - 50, &img_enemy2_2, SRCINVERT);
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (enemy3_y[i] <= high - 25)
{
putimage(enemy3_x[i] - 25, enemy3_y[i] - 50, &img_enemy3_1, NOTSRCERASE);
putimage(enemy3_x[i] - 25, enemy3_y[i] - 50, &img_enemy3_2, SRCINVERT);
}
}
for (int i = 1; i <= enemytot1; i++)
{
if (enemy1_bullet_y[i] <= high - 25 && explodesign1[i] == 0)
{
putimage(enemy1_bullet_x[i] - 20, enemy1_bullet_y[i] - 30, &img_enemy1_bullet1, NOTSRCERASE);
putimage(enemy1_bullet_x[i] - 20, enemy1_bullet_y[i] - 30, &img_enemy1_bullet2, SRCINVERT);
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (enemy2_bullet_y[i] <= high - 25 && explodesign2[i] == 0)
{
putimage(enemy2_bullet_x[i] - 20, enemy2_bullet_y[i] - 30, &img_enemy2_bullet1, NOTSRCERASE);
putimage(enemy2_bullet_x[i] - 20, enemy2_bullet_y[i] - 30, &img_enemy2_bullet2, SRCINVERT);
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (enemy3_bullet_y[i] <= high - 25 && explodesign3[i] == 0)
{
putimage(enemy3_bullet_x[i] - 20, enemy3_bullet_y[i] - 30, &img_enemy3_bullet1, NOTSRCERASE);
putimage(enemy3_bullet_x[i] - 20, enemy3_bullet_y[i] - 30, &img_enemy3_bullet2, SRCINVERT);
}
}
if (boss1_bullet_y <= high - 25 && bossexplodesign1 == 0)
{
putimage(boss1_bullet_x - 20, boss1_bullet_y - 30, &img_boss1_bullet1, NOTSRCERASE);
putimage(boss1_bullet_x - 20, boss1_bullet_y - 30, &img_boss1_bullet2, SRCINVERT);
}
if (boss2_bullet_y <= high - 25 && bossexplodesign2 == 0)
{
putimage(boss2_bullet_x - 20, boss2_bullet_y - 30, &img_boss2_bullet1, NOTSRCERASE);
putimage(boss2_bullet_x - 20, boss2_bullet_y - 30, &img_boss2_bullet2, SRCINVERT);
}
if (signboss1 == 1)
{
putimage(boss1_x - 25, boss1_y - 50, &img_boss1_1, NOTSRCERASE);
putimage(boss1_x - 25, boss1_y - 50, &img_boss1_2, SRCINVERT);
}
if (signboss2 == 1)
{
putimage(boss2_x - 25, boss2_y - 50, &img_boss2_1, NOTSRCERASE);
putimage(boss2_x - 25, boss2_y - 50, &img_boss2_2, SRCINVERT);
}
if (bossexplodesign1 == 1 && bosscnt != 0)
{
bosscnt++;
putimage(boss1_x - 25, boss1_y - 50, &img_boomexplode1, NOTSRCERASE);
putimage(boss1_x - 25, boss1_y - 50, &img_boomexplode2, SRCINVERT);
}
else if (bossexplodesign2 == 1 && bosscnt != 0)
{
bosscnt++;
putimage(boss2_x - 25, boss2_y - 50, &img_boomexplode1, NOTSRCERASE);
putimage(boss2_x - 25, boss2_y - 50, &img_boomexplode2, SRCINVERT);
}
if (bosscnt > 100)
{
bosscnt = 0;
bossexplodesign1 = 0;
bossexplodesign2 = 0;
}
}
else
{
putimage(position_x - 25, position_y - 50, &img_explode1, NOTSRCERASE);
putimage(position_x - 25, position_y - 50, &img_explode2, SRCINVERT);
}
FlushBatchDraw();
}
//中途暂停ESC购买补给,用money
void PauseMenu()
{
putimage(0, 0, &img_pauseground);
setbkmode(TRANSPARENT);
settextcolor(BROWN);
settextstyle(30, 0, _T("黑体"));
outtextxy(width * 0.48, high * 0.05, "空中补给");
settextcolor(RED);
settextstyle(30, 0, _T("黑体"));
outtextxy(width * 0.2, high * 0.3, "1 氢弹(100金币):");
outtextxy(width * 0.2, high * 0.35, "全屏幕轰炸,");
outtextxy(width * 0.2, high * 0.4, "但对Boss无效");
putimage(width * 0.2, high * 0.45, &img_boom1, NOTSRCERASE);
putimage(width * 0.2, high * 0.45, &img_boom2, SRCINVERT);
putimage(width * 0.4, high * 0.45, &img_bloodhealth1, NOTSRCERASE);
putimage(width * 0.4, high * 0.45, &img_bloodhealth2, SRCINVERT);
outtextxy(width * 0.4, high * 0.3, "2 修复(200金币):");
outtextxy(width * 0.4, high * 0.35, "加血20");
outtextxy(width * 0.6, high * 0.3, "3 存档");
outtextxy(width * 0.8, high * 0.3, "Esc 返回游戏");
char BoomTot[5];
sprintf(BoomTot, "%d", boomtot);
outtextxy(width * 0.15, high * 0.05, BoomTot);
outtextxy(width * 0.05, high * 0.05, "氢弹数:");
char Health[5];
sprintf(Health, "%d", health);
outtextxy(width * 0.8, high * 0.05, Health);
outtextxy(width * 0.73, high * 0.05, "血量:");
char Money[5];
sprintf(Money, "%d", money);
outtextxy(width * 0.35, high * 0.05, Money);
outtextxy(width * 0.28, high * 0.05, "金钱:");
if (_kbhit())
{
int input = _getch();
if (input == '1')
{
if (money >= 100)
{
boomtot++;
money -= 100;
}
}
else if (input == '2')
{
if (money >= 200 && health < 100)
{
if (health + 20 < 100)
health += 20;
else
health = 100;
money -= 200;
}
}
else if (input == '3')
{
WriteRecordFile();
GameStatus = 1;
}
else if (input == 27)
{
GameStatus = 1;
}
}
FlushBatchDraw();
}
数据更新:飞机移动和子弹移动
#include "define.hpp"
void UpdateWithoutInput();
void UpdateWithInput();
void UpdateMouseInput();
void UpdateInput();
void UpdateInput()
{
UpdateWithoutInput();
//与用户操作无关的更新
UpdateWithInput();
//与用户键盘操作有关的更新
UpdateMouseInput();
//与用户鼠标操作有关的更新
}
//画面数据自动迭代更新
void UpdateWithoutInput()
{
if (isExplode == 0)
{
if (boss1_y >= 0 && boss1_y <= high && explodeboss1 == 0)
{
if (boss1_turn == 1)
{
boss1_x -= 0.1;
}
else
{
boss1_x += 0.1;
}
if (boss1_x > width * 0.8)
{
boss1_turn = 1;
}
else if (boss1_x < width * 0.2)
{
boss1_turn = 2;
}
if (boss1_bullet_y < high + 55)
{
if (wusign >= 15)
{
boss1_bullet_y = boss1_bullet_y + 2.0;
}
else
{
boss1_bullet_y = boss1_bullet_y + 1.0;
}
}
else if (explodeboss1 == 0 && boss1_bullet_y >= high + 25)
{
boss1_bullet_y = boss1_y;
boss1_bullet_x = boss1_x;
}
else if (boss1_bullet_y > high + 25 && explodeboss1 == 1)
{
boss1_bullet_y = 100000;
}
}
if (boss2_y >= 0 && boss2_y <= high && explodeboss2 == 0)
{
if (boss2_turn == 1)
{
boss2_x -= 0.1;
}
else
{
boss2_x += 0.1;
}
if (boss2_x > width * 0.8)
{
boss2_turn = 1;
}
else if (boss2_x < width * 0.2)
{
boss2_turn = 2;
}
if (boss2_bullet_y < high + 55)
{
if (wusign >= 15)
{
boss2_bullet_y = boss2_bullet_y + 2.0;
}
else
{
boss2_bullet_y = boss2_bullet_y + 1.0;
}
}
else if (explodeboss2 == 0 && boss2_bullet_y >= high + 25)
{
boss2_bullet_y = boss2_y;
boss2_bullet_x = boss2_x;
}
else if (boss2_bullet_y > high + 25 && explodeboss2 == 1)
{
boss2_bullet_y = 100000;
}
}
for (int i = s_bullet1; i <= bullet1; i++)
{
if (bullet_y[i] > -25)
{
bullet_y[i] = bullet_y[i] - 2;
}
else s_bullet1++;
}
for (int i = 1; i <= enemytot1; i++)
{
if (explodesign1[i] == 0 && enemy1_bullet_y[i] < high + 55)
{
if (wusign >= 15)
{
enemy1_bullet_y[i] = enemy1_bullet_y[i] + 0.7;
}
else
{
enemy1_bullet_y[i] = enemy1_bullet_y[i] + 0.5;
}
}
else if (explodesign1[i] == 0 && enemy1_bullet_y[i] >= high + 25)
{
enemy1_bullet_y[i] = enemy1_y[i];
enemy1_bullet_x[i] = enemy1_x[i];
}
else if (enemy1_bullet_y[i] > high + 25 && explodesign1[i] == 1)
{
enemy1_bullet_y[i] = 100000;
}
if (enemy1_bullet_x[i] < position_x)
{
enemy1_bullet_x[i] += 0.6;
}
else
{
enemy1_bullet_x[i] -= 0.6;
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (explodesign2[i] == 0 && enemy2_bullet_y[i] < high + 55)
{
if (wusign >= 15)
{
enemy2_bullet_y[i] = enemy2_bullet_y[i] + 0.8;
}
else
{
enemy2_bullet_y[i] = enemy2_bullet_y[i] + 0.6;
}
}
else if (explodesign2[i] == 0 && enemy2_bullet_y[i] >= high + 25)
{
enemy2_bullet_y[i] = enemy2_y[i];
enemy2_bullet_x[i] = enemy2_x[i];
}
else if (enemy2_bullet_y[i] > high + 25 && explodesign2[i] == 1)
{
enemy2_bullet_y[i] = 100000;
}
if (enemy2_bullet_x[i] < position_x)
{
enemy2_bullet_x[i] += 0.6;
}
else
{
enemy2_bullet_x[i] -= 0.6;
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (explodesign3[i] == 0 && enemy3_bullet_y[i] < high + 55)
{
if (wusign >= 15)
{
enemy3_bullet_y[i] = enemy3_bullet_y[i] + 0.9;
}
else
{
enemy3_bullet_y[i] = enemy3_bullet_y[i] + 0.7;
}
}
else if (explodesign3[i] == 0 && enemy3_bullet_y[i] >= high + 25)
{
enemy3_bullet_y[i] = enemy3_y[i];
enemy3_bullet_x[i] = enemy3_x[i];
}
else if (enemy3_bullet_y[i] > high + 25 && explodesign3[i] == 1)
{
enemy3_bullet_y[i] = 100000;
}
if (enemy3_bullet_x[i] < position_x)
{
enemy3_bullet_x[i] += 0.6;
}
else
{
enemy3_bullet_x[i] -= 0.6;
}
}
for (int i = 1; i <= enemytot1; i++)
{
if (enemy1_y[i] < high + 25)
{
enemy1_y[i] += 0.16;
}
if (enemy1_y[i] > high && explodesign1[i] == 0 && enemy1_y[i] != 0x3f3f3f3f)
{
alivetot1--;
explodesign1[i] = 1;
continue;
}
if (enemy1_turn[i] == 1)
{
//左移
enemy1_x[i] -= 0.16;
if (enemy1_x[i] <= 0)
{
enemy1_turn[i] = 2;
}
}
else if (enemy1_turn[i] == 2)
{
//右移
enemy1_x[i] += 0.16;
if (enemy1_x[i] >= width - 15)
{
enemy1_turn[i] = 1;
}
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (enemy2_y[i] < high + 25)
{
enemy2_y[i] += 0.13;
}
if (enemy2_y[i] > high && explodesign2[i] == 0 && enemy2_y[i] != 0x3f3f3f3f)
{
alivetot2--;
explodesign2[i] = 1;
continue;
}
if (enemy2_turn[i] == 1)
{
//左移
enemy2_x[i] -= 0.16;
if (enemy2_x[i] <= 0)
{
enemy2_turn[i] = 2;
}
}
else if (enemy2_turn[i] == 2)
{
//右移
enemy2_x[i] += 0.16;
if (enemy2_x[i] >= width - 15)
{
enemy2_turn[i] = 1;
}
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (enemy3_y[i] < high + 25)
{
enemy3_y[i] += 0.13;
}
if (enemy3_y[i] > high && explodesign3[i] == 0 && enemy3_y[i] != 0x3f3f3f3f)
{
alivetot3--;
explodesign3[i] = 1;
continue;
}
if (enemy3_turn[i] == 1)
{
//左移
enemy3_x[i] -= 0.16;
if (enemy3_x[i] <= 0)
{
enemy3_turn[i] = 2;
}
}
else if (enemy3_turn[i] == 2)
{
//右移
enemy3_x[i] += 0.16;
if (enemy3_x[i] >= width - 15)
{
enemy3_turn[i] = 1;
}
}
}
for (int i = s_bullet1; i <= bullet1; i++)
{
for (int j = 1; j <= enemytot1; j++)
{
if (fabs(bullet_x[i] - enemy1_x[j]) + fabs(bullet_y[i] - enemy1_y[j]) < 30 && explodesign1[i] == 0)
{
//子弹击中了敌机
enemy1_blood[i] -= 1;
bullet_y[i] = 0x3f3f3f3f;
if (enemy1_blood[i] <= 0)
{
double x = enemy1_x[j]; double y = enemy1_y[j];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy1_x[j] = 0x3f3f3f3f;
enemy1_y[j] = 0x3f3f3f3f;
alivetot1--;
score = score + 1;
explodesign1[i] = 1;
FlushBatchDraw();
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
for (int j = 1; j <= enemytot2; j++)
{
if (fabs(bullet_x[i] - enemy2_x[j]) + fabs(bullet_y[i] - enemy2_y[j]) < 30 && explodesign2[i] == 0)
{
//子弹击中了敌机
enemy2_blood[i] -= 1;
bullet_y[i] = 0x3f3f3f3f;
if (enemy2_blood[i] <= 0)
{
double x = enemy2_x[j]; double y = enemy2_y[j];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy2_x[j] = 0x3f3f3f3f;
enemy2_y[j] = 0x3f3f3f3f;
alivetot2--;
score = score + 3;
explodesign2[i] = 1;
FlushBatchDraw();
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
for (int j = 1; j <= enemytot3; j++)
{
if (fabs(bullet_x[i] - enemy3_x[j]) + fabs(bullet_y[i] - enemy3_y[j]) < 30 && explodesign3[i] == 0)
{
//子弹击中了敌机
enemy3_blood[i] -= 1;
bullet_y[i] = 0x3f3f3f3f;
if (enemy3_blood[i] <= 0)
{
double x = enemy3_x[j]; double y = enemy3_y[j];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy3_x[j] = 0x3f3f3f3f;
enemy3_y[j] = 0x3f3f3f3f;
alivetot3--;
score = score + 5;
explodesign3[i] = 1;
FlushBatchDraw();
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
}
for (int i = 1; i <= enemytot1; i++)
{
if (fabs(position_x - enemy1_x[i]) + fabs(position_y - enemy1_y[i]) < 30)
{
//飞机撞上了敌机
health -= 2;
score = score + 1;
if (health <= 0)
{
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
explodesign1[i] = 1;
enemy1_x[i] = 0x3f3f3f3f;
enemy1_y[i] = 0x3f3f3f3f;
alivetot1--;
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (fabs(position_x - enemy2_x[i]) + fabs(position_y - enemy2_y[i]) < 30)
{
//飞机撞上了敌机
health -= 5;
score = score + 3;
if (health <= 0)
{
isExplode = 1;
GameStatus = 3;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
explodesign2[i] = 1;
enemy2_x[i] = 0x3f3f3f3f;
enemy2_y[i] = 0x3f3f3f3f;
alivetot2--;
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (fabs(position_x - enemy3_x[i]) + fabs(position_y - enemy3_y[i]) < 30)
{
//飞机撞上了敌机
health -= 10;
score = score + 5;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
explodesign3[i] = 1;
enemy3_x[i] = 0x3f3f3f3f;
enemy3_y[i] = 0x3f3f3f3f;
alivetot3--;
}
}
for (int i = 1; i <= enemytot1; i++)
{
if (fabs(position_x - enemy1_bullet_x[i]) + fabs(position_y - enemy1_bullet_y[i]) < 30)
{
//敌机子弹击中了飞机
health -= 2;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
enemy1_bullet_y[i] = enemy1_y[i];
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (fabs(position_x - enemy2_bullet_x[i]) + fabs(position_y - enemy2_bullet_y[i]) < 30)
{
//敌机子弹击中了飞机
health -= 2;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
enemy2_bullet_y[i] = enemy2_y[i];
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (fabs(position_x - enemy3_bullet_x[i]) + fabs(position_y - enemy3_bullet_y[i]) < 30)
{
//敌机子弹击中了飞机
health -= 2;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
enemy3_bullet_y[i] = enemy3_y[i];
}
}
if (fabs(position_x - boss1_bullet_x) + fabs(position_y - boss1_bullet_y) < 30)
{
//敌机子弹击中了飞机
health -= 8;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
boss1_bullet_y = boss1_y;
}
if (fabs(position_x - boss2_bullet_x) + fabs(position_y - boss2_bullet_y) < 30)
{
//敌机子弹击中了飞机
health -= 8;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
boss2_bullet_y = boss2_y;
}
if (signboss1 == 1)
{
for (int i = s_bullet1; i <= bullet1; i++)
{
if (((bullet_x[i] - boss2_x >= -60 && bullet_x[i] - boss1_x <= 0) || (bullet_x[i] - boss1_x >= 0 && bullet_x[i] - boss1_x <= 100)) && fabs(bullet_y[i] - boss1_y) < 50)
{
//子弹击中了敌机
boss_blood1 -= 1;
bullet_y[i] = 100000;
if (boss_blood1 <= 0)
{
// boss1_x = 0x3f3f3f3f;
// boss1_y = 0x3f3f3f3f;
score = score + 100;
money = money + 100;
boss1_bullet_x = 100000;
boss1_bullet_y = 100000;
explodeboss1 = 1;
signboss1 = 0;
bossexplodesign1 = 1;
bosscnt = 1;
putimage(boss1_x - 25, boss1_y - 50, &img_boomexplode1, NOTSRCERASE);
putimage(boss1_x - 25, boss1_y - 50, &img_boomexplode2, SRCINVERT);
break;
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
}
if (signboss2 == 1)
{
for (int i = s_bullet1; i <= bullet1; i++)
{
if (((bullet_x[i] - boss2_x >= -60 && bullet_x[i] - boss2_x <= 0) || (bullet_x[i] - boss2_x >= 0 && bullet_x[i] - boss2_x <= 100)) && fabs(bullet_y[i] - boss2_y) < 50)
{
//子弹击中了敌机
boss_blood2 -= 1;
bullet_y[i] = 100000;
if (boss_blood2 <= 0)
{
// boss2_x = 0x3f3f3f3f;
// boss2_y = 0x3f3f3f3f;
boss2_bullet_x = 100000;
boss2_bullet_y = 100000;
score = score + 100;
money = money + 100;
explodeboss2 = 1;
signboss2 = 0;
bossexplodesign2 = 1;
bosscnt = 1;
putimage(boss2_x - 25, boss2_y - 50, &img_boomexplode1, NOTSRCERASE);
putimage(boss2_x - 25, boss2_y - 50, &img_boomexplode2, SRCINVERT);
break;
}
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
}
if (fabs(position_x - boss1_x) + fabs(position_y - boss1_y) < 30)
{
//飞机撞上了敌机
health -= 30;
if (health <= 0)
{
isExplode = 1;
GameStatus = 3;
mciSendString("stop backmusic", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close backmusic", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
boss_blood1 -= 20;
if (boss_blood1 <= 0)
{
// boss1_x = 0x3f3f3f3f;
// boss1_y = 0x3f3f3f3f;
score = score + 100;
money = money + 100;
boss1_bullet_x = 100000;
boss1_bullet_y = 100000;
explodeboss1 = 1;
signboss1 = 0;
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
if (fabs(position_x - boss2_x) + fabs(position_y - boss2_y) < 30)
{
//飞机撞上了敌机
health -= 30;
if (health <= 0)
{
GameStatus = 3;
isExplode = 1;
mciSendString("stop explode", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close explode", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
}
boss_blood2 -= 20;
if (boss_blood2 <= 0)
{
// boss2_x = 0x3f3f3f3f;
// boss2_y = 0x3f3f3f3f;
score = score + 100;
money = money + 100;
boss2_bullet_x = 100000;
boss2_bullet_y = 100000;
explodeboss2 = 1;
signboss2 = 0;
mciSendString("stop getscore", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close getscore", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/getscore.mp3 alias getscore", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play getscore", NULL, 0, NULL);
// 仅播放一次
}
}
}
}
//键盘操作
void UpdateWithInput()
{
char input;
//键盘操作;
if (GetAsyncKeyState(0x57) & 0x8000)
{
//向上移动
position_y--;
}
else if ((GetAsyncKeyState(0x53) & 0x8000))
{
//向下移动
position_y++;
}
else if ((GetAsyncKeyState(0x41) & 0x8000))
{
//向左移动
position_x--;
}
else if ((GetAsyncKeyState(0x44) & 0x8000))
{
//向右移动
position_x++;
}
/* else if ((GetAsyncKeyState(VK_SPACE) & 0x8000))
{
bullet_x[++bullet1] = position_x;
bullet_y[bullet1] = position_y - 1;
}
*/
else if (_kbhit())
{
int input = _getch();
if (input == 27)
{
GameStatus = 4;
}
if (input == 32)
{
bullet_x[++bullet1] = position_x;
bullet_y[bullet1] = position_y - 1;
}
if (input == 'q')
{
if (boomtot >= 1)
{
mciSendString("stop explode", NULL, 0, NULL);
// 先把前面一次的音乐停止
mciSendString("close explode", NULL, 0, NULL);
// 先把前面一次的音乐关闭
mciSendString("open music/explode.mp3 alias explode", NULL, 0, NULL);
// 打开跳动音乐
mciSendString("play explode", NULL, 0, NULL);
// 仅播放一次
boomtot--;
for (int i = 1; i <= enemytot1; i++)
{
if (explodesign1[i] == 0)
{
double x = enemy1_x[i]; double y = enemy1_y[i];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy1_x[i] = 0x3f3f3f3f;
enemy1_y[i] = 0x3f3f3f3f;
alivetot1 = 0;
score = score + 1;
money = money + 1;
explodesign1[i] = 1;
FlushBatchDraw();
}
}
for (int i = 1; i <= enemytot2; i++)
{
if (explodesign2[i] == 0)
{
double x = enemy2_x[i]; double y = enemy2_y[i];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy2_x[i] = 0x3f3f3f3f;
enemy2_y[i] = 0x3f3f3f3f;
alivetot2 = 0;
score = score + 3;
money = money + 3;
explodesign2[i] = 1;
FlushBatchDraw();
}
}
for (int i = 1; i <= enemytot3; i++)
{
if (explodesign3[i] == 0)
{
double x = enemy3_x[i]; double y = enemy3_y[i];
putimage(x - 25, y - 50, &img_enemy1_explode1, NOTSRCERASE);
putimage(x - 25, y - 50, &img_enemy1_explode2, SRCINVERT);
enemy3_x[i] = 0x3f3f3f3f;
enemy3_y[i] = 0x3f3f3f3f;
alivetot3 = 0;
score = score + 5;
money = money + 5;
explodesign3[i] = 1;
FlushBatchDraw();
}
}
}
}
}
}
//鼠标操作
void UpdateMouseInput()
{
if (isExplode == 0)
{
MOUSEMSG m;
//表示鼠标的消息
while (MouseHit())
{
//MouseHit这个函数用于检测是否有鼠标的消息
m = GetMouseMsg();
if (m.uMsg == WM_MOUSEMOVE)
{
//飞机的位置等于鼠标所在的位置
position_x = m.x;
position_y = m.y;
}
else if (m.uMsg == WM_LBUTTONDOWN)
{
bullet_x[++bullet1] = position_x;
bullet_y[bullet1] = position_y - 85;
}
}
if (_kbhit())
{
int input = _getch();
if (input == 27)
GameStatus = 4;
}
}
}
}
游戏存档
#include "define.hpp"
void ReadRecordFile();
void WriteRecordFile();
void ReadRecordFile()
{
FILE* fp;
//文档的指针;
fp = fopen(".\\GameRecord.dat", "r");
fscanf(fp, "%lf %lf", &position_x, &position_y);
fscanf(fp, "%d", &isExplode);
for (int i = 0; i < 100000; i++)
{
fscanf(fp, "%lf %lf", &bullet_x[i], &bullet_y[i]);
}
fscanf(fp, "%d", &bullet_tot);
fscanf(fp, "%d", &bullet1);
fscanf(fp, "%d", &s_bullet1);
fscanf(fp, "%d", &money);
fscanf(fp, "%d", &score);
fscanf(fp, "%d", &health);
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy1_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy2_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy3_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy1_x[i], &enemy1_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy2_x[i], &enemy2_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy3_x[i], &enemy3_y[i]);
}
fscanf(fp, "%d %d %d", &enemytot1, &enemytot2, &enemytot3);
fscanf(fp, "%d %d %d", &alivetot1, &alivetot2, &alivetot3);
fscanf(fp, "%d %d", &boss_blood1, &boss_blood2);
fscanf(fp, "%lf %lf %lf %lf", &boss1_x, &boss1_y, &boss2_x, &boss2_y);
fscanf(fp, "%d %d %d %d", &levelsign, &level1sign, &level2sign, &level3sign);
fscanf(fp, "%d %d", &wusign, &plane_sign);
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &explodesign1[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &explodesign2[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &explodesign3[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy1_bullet_x[i], &enemy1_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy2_bullet_x[i], &enemy2_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%lf %lf", &enemy3_bullet_x[i], &enemy3_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy1_turn[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy2_turn[i]);
}
for (int i = 0; i < 10005; i++)
{
fscanf(fp, "%d", &enemy3_turn[i]);
}
fscanf(fp, "%d %d %d %d %d", &GameSign, &explodeboss1, &explodeboss2, &signboss1, &signboss2);
fscanf(fp, "%d %d", &cnt1, &cnt2);
fscanf(fp, "%d %d", &boss1_turn, &boss2_turn);
fscanf(fp, "%d", &boomtot);
fclose(fp);
}
//存储游戏档案
void WriteRecordFile()
{
FILE* fp;
fp = fopen(".\\GameRecord.dat", "w");
fprintf(fp, "%lf %lf\n", position_x, position_y);
fprintf(fp, "%d\n", isExplode);
for (int i = 0; i < 100000; i++)
{
fprintf(fp, "%lf %lf\n", bullet_x[i], bullet_y[i]);
}
fprintf(fp, "%d\n", bullet_tot);
fprintf(fp, "%d\n", bullet1);
fprintf(fp, "%d\n", s_bullet1);
fprintf(fp, "%d\n", money);
fprintf(fp, "%d\n", score);
fprintf(fp, "%d\n", health);
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy1_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy2_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy3_blood[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy1_x[i], enemy1_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy2_x[i], enemy2_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy3_x[i], enemy3_y[i]);
}
fprintf(fp, "%d %d %d\n", enemytot1, enemytot2, enemytot3);
fprintf(fp, "%d %d %d\n", alivetot1, alivetot2, alivetot3);
fprintf(fp, "%d %d\n", boss_blood1, boss_blood2);
fprintf(fp, "%lf %lf %lf %lf\n", boss1_x, boss1_y, boss2_x, boss2_y);
fprintf(fp, "%d %d %d %d\n", levelsign, level1sign, level2sign, level3sign);
fprintf(fp, "%d %d\n", wusign, plane_sign);
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", explodesign1[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", explodesign2[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", explodesign3[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy1_bullet_x[i], enemy1_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy2_bullet_x[i], enemy2_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%lf %lf\n", enemy3_bullet_x[i], enemy3_bullet_y[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy1_turn[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy2_turn[i]);
}
for (int i = 0; i < 10005; i++)
{
fprintf(fp, "%d\n", enemy3_turn[i]);
}
fprintf(fp, "%d %d %d %d %d\n", GameSign, explodeboss1, explodeboss2, signboss1, signboss2);
fprintf(fp, "%d %d\n", cnt1, cnt2);
fprintf(fp, "%d %d\n", boss1_turn, boss2_turn);
fprintf(fp, "%d\n", boomtot);
fclose(fp);
}
代码设计需求
对于图片的载入需要用的loadimage()、putimage()函数,对于音乐则需要mciSenString()函数,文字输出则需要用settextcolor()函数设置颜色,settextstyle()设置字体大小和字体类型,最后用outtextxy()对文字进行输出。
键盘操控用到_kbhit()函数来得到键盘的按键情况,从而控制飞机,GetAsyncKeyState()函数可以实现多个按键同时识别,由此飞机可以一边移动一边射击。
另外需要一个函数来专门对所有的敌机和子弹进行更新,并且进行图片载入。后储存档案和载入档案分别需要fprintf()和fscanf()函数。对于具体的控制和子弹的攻击、炸弹的轰炸需要看具体的代码,这里就不一一赘述。
具体的代码需要分多个部分:键盘操作的更新、鼠标操作的更新、游戏内部数据的更新、显示画面、检测当前的关卡。
UpdateWithoutInput()
函数就是更新游戏内部的数据,比如子弹的移动,敌机的移动,Boss的移动等等。
UpdateWithInput()
函数就是键盘操作,主要是系统自带的库函数
UpdateMouseInput()
函数就是鼠标操作,主要也是系统自带的库函数。这两个函数都是用来控制飞机移动和射击的,还有道具的选择,和暂停游戏的选项。
Check()
函数用来判断是否开启新的一轮战斗。
即如果敌机数量为0就开启新的战斗。
PlaneUpdate()
函数用来定义这轮战斗敌机的数量,以及决定这一轮战斗是否有Boss参与战斗。
Show()
函数用来显示游戏画面,根据当前各个游戏数据的坐标点来铺制相关的图片比如我方飞机、敌机、子弹、背景等等。这里同时用到了outtextxy()
函数来进行文字输出。同时使用sprintf
来进行数据写入。
然后将相关的图片铺制在程序框中。
为了玩家游戏的方便,增强游戏的可玩性,还增添了读档和存档的设置。玩家在游戏的过程中可以按ESC来进入暂停菜单,然后按数字3选择存档。在再次进入游戏的时候可以选择读档重新上一次的游戏。
存档函数WriteRecordFile()
的实现方式是文件输出,将相关的游戏数据写入到GameRecord.dat
里面。
读档函数ReadRecordFile()
方法也类似,通过读入文件,得到之前的所有游戏数据,然后开始游戏。
以上就是游戏代码实现的主要过程,更多的细节还需要查看代码来了解。
图片的实现
先在网上找到符合星际大战系列的图片、战机、敌机、Boss以及各种子弹,然后运用PS制作图片。然后在代码里面先载入图片,定义相关类型的图片变量,然后载入图片。
音乐的实现
在QQ音乐上下载了一系列的纯音乐作为背景音乐,这些音乐都是各大型游戏和电影的背景音乐。然后放进资源文件夹中,用mciSendString()函数进行调用。
道具的实现
道具设置了加血来让玩家的血量回升,以及氢弹来直接毁灭敌方所有飞机。
更新:2022.3.17(写完这篇博客,真心觉得当初写的这个游戏项目是真的烂,还请大家轻喷)