跳到正文

休闲·娱乐

game-雷霆战机

Luna_From_Starry约 33 分钟
原文
文章信息与版本

文章信息

发布
2025年9月1日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 33 分钟

版本

  1. v2当前
  2. v1
C++
#include "tools.h"
#include "keyboard.h"

#include <list>
#include <map>
#include <time.h>
#include <conio.h>
#include <unistd.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <math.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

using namespace std;

#define D (Mine.level < 4 ? 2 : 9)

struct Node1 {
	int row, col, HP, level, kill, cnt;
	double timer;
};
struct Node2 {
	int type, col, point, speed2;
	double timer, speed;
};
struct Node3 {
    int HP;
    double timer;
};
struct Node4 {
	double cooling, timer;
	int cnt;
};

const double fps = 1.0 / 23;
const int ROWS = 9;
const int COLS = 60;
Node1 Mine;
list<Node2> PlaneList[ROWS], BulletList[ROWS];
Node3 Boss;
clock_t last, now;
bool pause;
double HordeTimeCount;
map<string, double> Sentences;
const int LevelUpgrade[5] = {0, 40, 200, 400, 1000};
bool music = false, coding = false, difficult = false;
fstream file;
Node4 Skill[3];

const double SkillTime[3] = {7.5, 10.0, 5.0};
const double SkillCooling[3] = {45.0, 60.0, 30.0};
double BgmTimeCount;
double GameTimeCount;
const double PlaneCreateStart[4] = {0.0, 15.0, 40.0, 150.0};
double PlaneCreateCount[4], BulletCreateCount;
const double PlaneCreateTime[4] = {1.2, 4.7, 13.5, 45.0};
const int PlaneHealthPoint[4] = {3, 6, 15, 100};
const double BulletCreateTime[5] = {0.2, 0.13, 0.13, 0.09, 0.065};
const int BulletDamagePoint[5] = {1, 1, 2, 2, 4};

//调试
void Debug(const char* s) {
	color(0xf);
	show(0, ROWS * 2 + 4, "               ");
	show(0, ROWS * 2 + 4, s);
}

//初始化窗口
void InitWindow() {
    SetConsoleTitle("雷霆战机3.0");
    HideConsoleCursor();
    SetSize(16, 2);

    Sleep(200);
    keybd_event(VK_SHIFT, 0, 0, 0);
    Sleep(20);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
}

//游戏教程
void Tutorial() {
	while(KeyDown(VK_2));

	SetSize(27, 14);
	system("cls");
	gotoxy(0, 0);
	cout << "游戏操作:\n";
    cout << "w↑:上 a←:左 s↓:下 d→:右\n";
	cout << "j:技能① (攻速提升40%)\n";
	cout << "k:技能② (开启穿透子弹)\n";
	cout << "l:技能③ (停止敌人前进)\n";
	cout << "注:每使用一次技能该技能下次多消耗20kill\n\n";
	cout << "其他:\n";
    cout << "space:暂停\n";
    cout << "n:游戏存档 m:游戏读档\n";
	cout << "q:战机升级 e:打开设置菜单\n";
    cout << "ctrl+shift+x:老板键\n";
    cout << "按(1).返回";

    while(!KeyDown(VK_1));
	while(KeyDown(VK_1));
}

//保存设置
void SaveSetUp() {
	file.open("data/雷霆战机设置.dat", ios::out | ios::binary);
	file.write((char*)&music, sizeof(music));
	file.write((char*)&difficult, sizeof(difficult));
	file.close();
}

//获取设置
void LoadSetUp() {
	if(access("data/雷霆战机设置.dat", F_OK)) return ;

	file.open("data/雷霆战机设置.dat", ios::in | ios::binary);
	file.read((char*)&music, sizeof(music));
	file.read((char*)&difficult, sizeof(difficult));
	file.close();
}

//游戏设置菜单
void SetUpMenu() {
	while(KeyDown(VK_3));

	LoadSetUp();
	while(!KeyDown(VK_3)) {
		SetSize(15, 3);
		color(0xf);
		cout << "按(1)." << (music ? "开启" : "关闭") << "音乐\n";
		cout << "按(2)." << (difficult ? "困难" : "简单") << "模式\n";
		cout << "按(3).返回";

		while(!KeyDown(VK_1) && !KeyDown(VK_2) && !KeyDown(VK_3));
		if(KeyDown(VK_1)) {
			while(KeyDown(VK_1));
			music = !music;
		}else if(KeyDown(VK_2)) {
			while(KeyDown(VK_2));
			difficult = !difficult;
		}
		SaveSetUp();
	}
	while(KeyDown(VK_3));
}

//开始界面
void StartUI() {
    bool flag = false;
	do {
		if(flag) {
			if(KeyDown(VK_1)) return ;
			else if(KeyDown(VK_2)) Tutorial();
			else if(KeyDown(VK_3)) SetUpMenu();
		}
        SetSize(15, 3);
        cout << "按(1).开始游戏\n";
		cout << "按(2).游戏教程\n";
		cout << "按(3).游戏设置";
        while(!KeyDown(VK_1) && !KeyDown(VK_2) && !KeyDown(VK_3));
		flag = true;
    } while(true);
}

//游戏存档
void SaveGame() {
    file.open("data/雷霆战机存档.dat", ios::out | ios::binary);

    file.write((char*)&Mine, sizeof(Mine));

    for(int i = 0; i < ROWS; i++) {
		int n1 = PlaneList[i].size();
        file.write((char*)&n1, sizeof(n1));
        for(auto& tmp : PlaneList[i]) {
	        file.write((char*)&tmp, sizeof(tmp));
		}

		int n2 = BulletList[i].size();
        file.write((char*)&n2, sizeof(n2));
		for(auto& tmp : BulletList[i]) {
            file.write((char*)&tmp, sizeof(tmp));
        }
    }

    file.write((char*)&GameTimeCount, sizeof(GameTimeCount));
    for(int i = 0; i < 4; i++) {
	    file.write((char*)&PlaneCreateCount[i], sizeof(PlaneCreateCount[i]));
	}
    file.write((char*)&BulletCreateCount, sizeof(BulletCreateCount));

	for(int i = 0; i < 3; i++) {
		file.write((char*)&Skill[i], sizeof(Skill[i]));
	}

    file.close();
}

//游戏读档
bool LoadGame() {
	if(access("data/雷霆战机存档.dat", F_OK)) return false;

    file.open("data/雷霆战机存档.dat", ios::in | ios::binary);

    file.read((char*)&Mine, sizeof(Mine));

    for(int i = 0; i < ROWS; i++) {
        PlaneList[i].clear();
        int n1;
		file.read((char*)&n1, sizeof(n1));
        for(int j = 0; j < n1; j++) {
            Node2 tmp;
            file.read((char*)&tmp, sizeof(tmp));
            PlaneList[i].push_back(tmp);
        }

        BulletList[i].clear();
        int n2;
		file.read((char*)&n2, sizeof(n2));
        for(int j = 0; j < n2; j++) {
            Node2 tmp;
            file.read((char*)&tmp, sizeof(tmp));
            BulletList[i].push_back(tmp);
        }
    }

    file.read((char*)&GameTimeCount, sizeof(GameTimeCount));
    for(int i = 0; i < 4; i++) {
        file.read((char*)&PlaneCreateCount[i], sizeof(PlaneCreateCount[i]));
    }
    file.read((char*)&BulletCreateCount, sizeof(BulletCreateCount));

	for(int i = 0; i < 3; i++) {
		file.read((char*)&Skill[i], sizeof(Skill[i]));
	}


    file.close();

    last = now = clock();

	return true;
}

//初始化游戏
void InitGame() {
	SetSize(COLS + 10, ROWS * 2 + 5 + coding);

	Mine = (Node1) {(ROWS - 1) / 2, 1, 3, 0, 0, 0, 0.0};
	for(int i = 0; i < ROWS; i++) {
		PlaneList[i].clear();
		BulletList[i].clear();
	}

	GameTimeCount = 0.0;
	for(int i = 0; i < 4; i++) {
		PlaneCreateCount[i] = 0.0;
	}
	BulletCreateCount = 0.0;

	//初始化技能
	for(int i = 0; i < 3; i++) {
		Skill[i] = (Node4) {!coding * SkillCooling[i], 0.0, 0};
	}

	//初始化BGM
	mciSendString(TEXT("open 雷霆战机.mp3 type mpegvideo alias bgm"), NULL, 0, 0);
	mciSendString(TEXT("play bgm repeat"), NULL, 0, 0);
	mciSendString(TEXT("stop bgm"), NULL, 0, 0);

    if(coding) {
        cout << "请输入KILL:";
        cin >> Mine.kill;
        cout << "请输入TIME:";
        cin >> GameTimeCount;
    }

    Boss.HP = 0;

    last = clock();
}

//人机互动
void UserClick(double delta) {
	if(!pause) {
		double t;
		if(!Mine.cnt) t = 0.0;
		else if(Mine.cnt < 2) t = 0.2;
		else if(Mine.cnt < 3) t = 0.075;
		else t = 0.025;

		if((Mine.timer += delta) >= t) {
			if(KeyDown(VK_W) || KeyDown(VK_UP)) {
				//上
				Mine.cnt++, Mine.timer = 0.0;
				Mine.row = max(Mine.row - 1, 0);
			}else if(KeyDown(VK_A) || KeyDown(VK_LEFT)) {
				//左
				Mine.cnt++, Mine.timer = 0.0;
				Mine.col = max(Mine.col - 1, 0);
			}else if(KeyDown(VK_S) || KeyDown(VK_DOWN)) {
				//下
				Mine.cnt++, Mine.timer = 0.0;
				Mine.row = min(Mine.row + 1, ROWS - 1);
			}else if(KeyDown(VK_D) || KeyDown(VK_RIGHT)) {
				//右
				Mine.cnt++, Mine.timer = 0.0;
				Mine.col = min(Mine.col + 1, COLS / 2);
			}else Mine.cnt = 0;
		}

		//技能
		if(KeyDown(VK_J) && !Skill[0].cooling && Mine.kill >= Skill[0].cnt * 20) {
			Skill[0].cooling = SkillCooling[0];
			Skill[0].timer = SkillTime[0];
			Mine.kill -= Skill[0].cnt++ * 20;
		}
		if(KeyDown(VK_K) && !Skill[1].cooling && Mine.kill >= Skill[1].cnt * 20) {
			Skill[1].cooling = SkillCooling[1];
			Skill[1].timer = SkillTime[1];
			Mine.kill -= Skill[1].cnt++ * 20;
		}
		if(KeyDown(VK_L) && !Skill[2].cooling && Mine.kill >= Skill[2].cnt * 20) {
			Skill[2].cooling = SkillCooling[2];
			Skill[2].timer = SkillTime[2];
			Mine.kill -= Skill[2].cnt++ * 20;
		}

		//升级
		if(KeyDown(VK_Q) && Mine.level < 4 && Mine.kill >= LevelUpgrade[Mine.level + 1]) {
			if(!Sentences.count("恭喜升级!")) {
				Mine.kill -= LevelUpgrade[Mine.level + 1];
				Mine.level++;
				Sentences[(string) "恭喜升级!"] = 2.5;
			}
		}
	}

	//存档
	if(KeyDown(VK_N) || KeyDown(VK_NUM2)) {
		SaveGame();
		Sentences[(string)"存档成功!            "] = 2.5;
	}
	//读档
	if(KeyDown(VK_M) || KeyDown(VK_NUM3)) {
		if(LoadGame()) {
			Sentences[(string)"读档成功!            "] = 2.5;
		}
	}
	//暂停
	if(KeyDown(VK_SPACE)) {
		while(KeyDown(VK_SPACE));
		if((pause = !pause)) {
			if(music) mciSendString("stop bgm", NULL, 0, NULL);
		}else {
			if(music) mciSendString("resume bgm", NULL, 0, NULL);
		}
	}

	if(KeyDown(VK_E)) {
		while(KeyDown(VK_E));
		bool tmp = pause;
		pause = true;
		if(music) mciSendString("stop bgm", NULL, 0, NULL);
		SetUpMenu();
		pause = tmp;
		now = last = clock();
		SetSize(COLS + 10, ROWS * 2 + 4 + coding);
		if(music) mciSendString("resume bgm", NULL, 0, NULL);
	}

	//老板键
    if(!(KeyDown(VK_CONTROL) && KeyDown(VK_X))) return ;
	Mine.timer = 0.0;
    while(KeyDown(VK_CONTROL) && KeyDown(VK_X));
    if(music) mciSendString("stop bgm", NULL, 0, NULL);
    HideConsole(false);
    while(!(KeyDown(VK_CONTROL) && KeyDown(VK_X)));
    while(KeyDown(VK_CONTROL) && KeyDown(VK_X));
    if(music) mciSendString("resume bgm", NULL, 0, NULL);
    HideConsole(true);
    now = last = clock();
}

//更新游戏数据
bool UpdataGame(double delta) {
	map<string, double>::iterator it;
    for(it = Sentences.begin(); it != Sentences.end(); it++) {
        if((it->second -= delta) <= 0.0) {
            map<string, double>::iterator next = ++it;
            Sentences.erase(--it);
            if((it = next) == Sentences.begin()) break;
            it--;
        }
    }

	if(pause) return true;

	//更新尸潮
	if(HordeTimeCount > 0.0) {
		HordeTimeCount -= delta;
	}else if(ceil(GameTimeCount) <= GameTimeCount + delta && rand() % 100 == 114) {
		HordeTimeCount = rand() % 5000 / 1000.0;
		Sentences[(string) "尸潮来袭!"] = 2.5;
	}

	//更新技能
	for(int i = 0; i < 3; i++) {
		Skill[i].cooling = max(Skill[i].cooling - !coding * delta, 0.0);
		Skill[i].timer = max(Skill[i].timer - delta, 0.0);
	}

	GameTimeCount += delta;

	for(int i = 0; i < ROWS; i++) {
		list<Node2>::iterator it1, it2;

		//碰撞判断
		for(it1 = PlaneList[i].begin(); it1 != PlaneList[i].end();) {
			bool flag = false;
			for(it2 = BulletList[i].begin(); it2 != BulletList[i].end();) {
				Node2& plane = *it1, bullet = *it2;
				if(plane.col - bullet.col >= 0 && plane.col - bullet.col <= 3) {
					list<Node2>::iterator next2 = ++it2;
					if(Skill[1].timer <= 0.0) BulletList[i].erase(--it2);
					it2 = next2;

					if((plane.point -= bullet.point) <= 0) {
						flag = true;
						break;
					}
				}else it2++;
			}

			if(flag) {
				Mine.kill++;

				list<Node2>::iterator next1 = ++it1;
				PlaneList[i].erase(--it1);
				it1 = next1;
			}else it1++;
		}

		//更新敌人
		for(it1 = PlaneList[i].begin(); it1 != PlaneList[i].end();) {
			Node2 &plane = *it1;
			if((plane.timer += !(Skill[2].timer > 0.0) * delta) >= plane.speed) {
				plane.timer -= plane.speed;
				plane.col -= plane.speed2;
				if(plane.col < 0 || i == Mine.row && plane.col - Mine.col >= 0 && plane.col - Mine.col <= D) {
					Mine.HP--;

					list<Node2>::iterator next1 = ++it1;
					PlaneList[i].erase(--it1);
					it1 = next1;
				}else it1++;
			}else it1++;
		}

		//更新子弹
		for(it2 = BulletList[i].begin(); it2 != BulletList[i].end();) {
			Node2 &bullet = *it2;
			if((bullet.timer += delta) >= bullet.speed) {
				bullet.timer -= bullet.speed;
				bullet.col += bullet.speed2;
				if(bullet.col > COLS) {
					list<Node2>::iterator next2 = ++it2;
					BulletList[i].erase(--it2);
					it2 = next2;
				}else it2++;
			}else it2++;
		}
	}

	//刷新敌人
	for(int i = 0; i < 4; i++) {
		if(GameTimeCount < PlaneCreateStart[i]) continue;
		double f = max(6.0 - (Mine.level - i), 3.5) + (!difficult - (HordeTimeCount > 0.0) * 1.5) * 2;
		double t = PlaneCreateTime[i] / 10 * f;
		if((PlaneCreateCount[i] += delta) >= t) {
			PlaneCreateCount[i] -= t;
			int row = rand() % ROWS;
			PlaneList[row].push_back((Node2) {i, COLS, PlaneHealthPoint[i], 1, 0.0, 0.2 * ((i == 3 && Mine.level < 4) + 1)});
		}
	}

	//刷新子弹
	double t = BulletCreateTime[Mine.level] / 5 * (5 - (Skill[0].timer > 0.0) * 2);
	if((BulletCreateCount += delta) >= t) {
		BulletCreateCount -= t;
		BulletList[Mine.row].push_back((Node2) {Mine.level, Mine.col + D, BulletDamagePoint[Mine.level], 2, 0.0, fps});
	}

	return Mine.HP > 0;
}

//显示游戏画面
void DrawGame() {
	if(music) mciSendString(TEXT("resume bgm"), NULL, 0, 0);

    gotoxy(0, 0);
    for(int i = 1; i <= (COLS - 3) / 2; i++) cout << "-";
    cout << " 雷霆战机3.0 ";
    for(int i = 1; i <= (COLS - 2) / 2; i++) cout << "-";

	for(int i = 0; i < ROWS; i++) {
		for(auto& plane : PlaneList[i]) {
			if(plane.type == 0) {
				color(Skill[2].timer > 0.0 ? 0x7 : 0x5);
				show(plane.col, i * 2 + 2, "<");
			}else if(plane.type == 1) {
				color(Skill[2].timer > 0.0 ? 0x7 : 0xd);
				show(plane.col, i * 2 + 2, "<@");
			}else if(plane.type == 2) {
				color(Skill[2].timer > 0.0 ? 0x7 : 0x4);
				show(plane.col, i * 2 + 2, "<]]");
			}else {
				color(Skill[2].timer > 0.0 ? 0x7 : 0xc);
				show(plane.col + 1, i * 2 + 1, "///-----");
				show(plane.col, i * 2 + 2, "<{{{[][]");
				show(plane.col + 1, i * 2 + 3, "\\\\\\-----");
			}
		}

		color(Skill[1].timer > 0.0 ? 0x6 : 0x8);
		for(auto& bullet : BulletList[i]) {
			show(bullet.col, i * 2 + 2, "·");
		}
	}

	if(Mine.level < 4) {
    	color(0xb);
    	show(Mine.col, Mine.row * 2 + 1, " \\");
    	show(Mine.col, Mine.row * 2 + 2, "*=");
		color(Skill[0].timer > 0.0 || Skill[1].timer > 0.0 ? 0x7 : 0xb);
		show(Mine.col + 2, Mine.row * 2 + 2, "》");
		color(0xb);
    	show(Mine.col, Mine.row * 2 + 3, " /");
	}else {
	    color(0xc);
	    show(Mine.col, Mine.row * 2 + 1, " --");
	    color(0xb);
	    show(Mine.col + 3, Mine.row * 2 + 1, "*O=++");
	    color(0x4);
	    show(Mine.col, Mine.row * 2 + 2, "----");
	    color(0xb);
	    show(Mine.col + 4, Mine.row * 2 + 2, "*");
	    color(0xe);
	    show(Mine.col + 5, Mine.row * 2 + 2, "[=]");
	    color(Skill[0].timer > 0.0 || Skill[1].timer > 0.0 ? 0x7 : 0xb);
	    show(Mine.col + 8, Mine.row * 2 + 2, ")》");
	    color(0xc);
	    show(Mine.col, Mine.row * 2 + 3, " --");
	    color(0xb);
	    show(Mine.col + 3, Mine.row * 2 + 3, "*O=++");
	}

	if(Boss.HP) {
	    color(0xc);
	    show(COLS - 1, 2, "<----------");
	    show(COLS - 2, 4, "//--::::::::");
	    show(COLS - 5, 6, "-///-:::/------");
	    show(COLS - 5, 8, "|[  ----+---   ");
	    show(COLS - 6, 10, "<|======== (·)|");
	    show(COLS - 5, 12, "|[  ---+----   ");
	    show(COLS - 5, 14, "-\\\\\\-:::\\------");
	    show(COLS - 2, 16, "\\\\--::::::::");
	    show(COLS - 1, 18, "<----------");
	}

	gotoxy(0, ROWS * 2 + 2);
	cout.setf(ios::fixed);
	color(0x1f);
	cout << "skill:";

	color(0x1f);
	cout << "①";
	color(!Skill[0].cooling && Mine.kill >= Skill[0].cnt * 20 ? 0x1e : 0x17);
	cout << setw(5) << setprecision(2) << Skill[0].cooling << "  ";

	color(0x1f);
	printf("②");
	color(!Skill[1].cooling && Mine.kill >= Skill[1].cnt * 20 ? 0x1e : 0x17);
	cout << setw(5) << setprecision(2) << Skill[1].cooling << "  ";

	color(0x1f);
	printf("③");
	color(!Skill[2].cooling && Mine.kill >= Skill[2].cnt * 20 ? 0x1e : 0x17);
	cout << setw(5) << setprecision(2) << Skill[2].cooling << "  ";

	color(0x1f);
	puts("upgrade");

	color(0x17);
	cout << "cost: ";
	cout << setw(7) << Skill[0].cnt * 20 << "  ";
	cout << setw(7) << Skill[1].cnt * 20 << "  ";
	cout << setw(7) << Skill[2].cnt * 20 << "  ";

	color(Mine.level < 4 && Mine.kill >= LevelUpgrade[Mine.level + 1] && !Sentences.count("恭喜升级!") ? 0x1e : 0x17);
	if(Mine.level < 4) cout << setw(7) << max(LevelUpgrade[Mine.level + 1] - Mine.kill, 0) << "\n";
	else puts("no");

	color(0x1f);
	for(int i = 1; i <= 40; i++) cout << " ";
	gotoxy(0, ROWS * 2 + 4);
	cout << "kill:" << Mine.kill << "  ";
	cout << "HP:" << Mine.HP << "  ";
	cout << "level:" << Mine.level << "/5  ";
	cout << "time:" << setprecision(2) << GameTimeCount;

	cout.unsetf(ios::fixed);

	for(auto& tmp : Sentences) {
		color(tmp.first == "尸潮来袭!" ? 0x4 : 0xe);
		show(COLS + 10 - tmp.first.size(), ROWS * 2 + 3, tmp.first.c_str());
	}
	color(0xf);
}

//游戏结束
bool GameOver() {
    mciSendString("close bgm", NULL, 0, NULL);

	color(0x6);
	cout << "Game Over\n";
	Sleep(2000);
	cout << "kry again? (y/n)\n";

	while(!KeyDown(VK_Y) && !KeyDown(VK_N));
	return KeyDown(VK_Y);
}

//主函数
int main() {
    InitWindow();
    StartUI();

GAME:
	InitGame();

	while(true) {
		now = clock();
		if((now - last) * 1.0 / CLOCKS_PER_SEC < fps) {
			continue;
		}

		UserClick((now - last) * 1.0 / CLOCKS_PER_SEC);
		system("cls");

		if(coding) Debug("begin updata");
		if(!UpdataGame((now - last) * 1.0 / CLOCKS_PER_SEC)) {
			if(GameOver()) goto GAME;
			break;
		}
		if(coding) Debug("end updata");

		if(coding) Debug("begin draw");
		DrawGame();
		if(coding) Debug("end draw");

		last = now;
	}

	color(0xf);
	return 0;
}
原文