记得调成C++11,不定时乱写qwq
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
using namespace std;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
int x;
double y;
char s[50][200], c;
bool gameActive = true;
POINT Windowpos() {
POINT pt;
GetCursorPos(&pt);
HWND h = GetForegroundWindow();
ScreenToClient(h, &pt);
pt.x = (pt.x + 4) / 10 + 0.5;
pt.y = pt.y / 8 + 0.5;
swap(pt.x, pt.y);
return pt;
}
void Color(int a) {
if(a == -2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN);
if(a == -1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(a == 0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(a == 1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(a == 2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN);
if(a == 3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);
if(a == 4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED);
if(a == 5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
if(a >= 6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_BLUE);
}
POINT pt;
float kx, ky;
int ti(int a) {
return ((int)(a * 10 + 5)) / 10;
}
void Setpos(double x, double y) {
COORD pos;
pos.X = ti(y * 4) / 2;
pos.Y = ti(x);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void print(string luogu) {
for(int i = 0; i < luogu.size(); i++) {
cout << luogu[i];
Sleep(40);
}
}
// 机关结构体
struct Mechanism {
int type; // 0:移动平台, 1:尖刺陷阱, 2:消失平台, 3:弹跳板
double x, y;
double width;
int dir;
int timer;
bool active;
};
vector<Mechanism> mechanisms;
// 初始化机关
void initMechanisms(int level) {
mechanisms.clear();
if (level == 2) {
// 移动平台
mechanisms.push_back({0, 25.0, 30.0, 10.0, 1, 0, true});
// 尖刺陷阱
mechanisms.push_back({1, 26.0, 50.0, 5.0, 0, 0, true});
} else if (level == 3) {
// 消失平台
mechanisms.push_back({2, 25.0, 30.0, 5.0, 0, 0, true});
mechanisms.push_back({2, 25.0, 50.0, 5.0, 0, 0, true});
// 弹跳板
mechanisms.push_back({3, 26.0, 70.0, 3.0, 0, 0, true});
} else if (level == 4) {
// 组合机关
mechanisms.push_back({0, 25.0, 20.0, 8.0, 1, 0, true});
mechanisms.push_back({1, 26.0, 50.0, 8.0, 0, 0, true});
mechanisms.push_back({2, 25.0, 70.0, 5.0, 0, 0, true});
mechanisms.push_back({3, 26.0, 90.0, 3.0, 0, 0, true});
}
}
// 更新机关状态
void updateMechanisms() {
for (auto& mech : mechanisms) {
if (mech.type == 0) { // 移动平台
mech.y += 0.3 * mech.dir;
if (mech.y <= 20 || mech.y + mech.width >= 70) {
mech.dir *= -1;
}
} else if (mech.type == 1) { // 尖刺陷阱
mech.timer = (mech.timer + 1) % 20;
} else if (mech.type == 2) { // 消失平台
mech.timer = (mech.timer + 1) % 60;
mech.active = (mech.timer < 40);
}
}
}
// 绘制机关
void drawMechanisms() {
for (const auto& mech : mechanisms) {
if (!mech.active) continue;
if (mech.type == 0) { // 移动平台
Color(3); // 青色
for (int j = 0; j < mech.width; j++) {
Setpos(mech.x, mech.y + j);
cout << "=";
}
Color(0);
} else if (mech.type == 1) { // 尖刺陷阱
if (mech.timer < 10) {
Color(4); // 红色
for (int j = 0; j < mech.width; j++) {
Setpos(mech.x, mech.y + j);
cout << "^";
}
Color(0);
}
} else if (mech.type == 2) { // 消失平台
if (mech.active) {
Color(5); // 黄色
for (int j = 0; j < mech.width; j++) {
Setpos(mech.x, mech.y + j);
cout << "#";
}
Color(0);
}
} else if (mech.type == 3) { // 弹跳板
Color(2); // 绿色
for (int j = 0; j < mech.width; j++) {
Setpos(mech.x, mech.y + j);
cout << "X";
}
Color(0);
}
}
}
// 检查机关碰撞
bool checkMechanismCollision() {
for (const auto& mech : mechanisms) {
if (!mech.active) continue;
// 检查是否站在机关上
bool onMech = false;
if (mech.type == 0 || mech.type == 2) { // 平台类机关
if (x == mech.x - 1 && y >= mech.y && y <= mech.y + mech.width) {
onMech = true;
// 随移动平台移动
if (mech.type == 0) {
y += 0.3 * mech.dir;
}
}
}
// 检查尖刺碰撞
if (mech.type == 1 && mech.timer < 10) { // 尖刺激活时
if (x == mech.x && y >= mech.y && y <= mech.y + mech.width) {
return true; // 碰撞死亡
}
}
// 检查弹跳板
if (mech.type == 3) {
if (x == mech.x && y >= mech.y && y <= mech.y + mech.width) {
// 弹跳
for (int i = 0; i < 5; i++) {
if (s[x-1][(int)(y*2)+1] != '*') {
x--;
Setpos(x, y);
cout << "@";
Sleep(30);
Setpos(x, y);
cout << " ";
}
}
x -= 2; // 额外跳跃高度
}
}
}
return false;
}
bool playLevel(int level) {
gameActive = true;
for(int i = 1; i <= 40; i++) {
for(int j = 1; j <= 156; j++) {
s[i][j] = ' ';
}
}
// 基础地面
for(int i = 60; i <= 150; i++) {
s[27][i] = '*';
}
// 门的位置
s[30][154] = '|', s[30][155] = '|';
// 根据关卡定制地图
if (level == 1) {
for(int i = 92; i <= 95; i++) {
s[27][i] = ' ';
}
for(int i = 31; i <= 40; i++) {
for(int j = 1; j <= 62; j++) {
s[i][j] = '*';
}
}
for(int i = 31; i <= 40; i++) {
for(int j = 153; j <= 156; j++) {
s[i][j] = '*';
}
}
}
else if (level == 2) {
// 添加空隙
for(int i = 100; i <= 110; i++) {
s[27][i] = ' ';
}
// 添加高台
for(int i = 25; i <= 27; i++) {
for(int j = 110; j <= 120; j++) {
s[i][j] = '*';
}
}
}
else if (level == 3) {
// 更多空隙
for(int i = 80; i <= 90; i++) {
s[27][i] = ' ';
}
for(int i = 100; i <= 110; i++) {
s[27][i] = ' ';
}
// 浮空平台
for(int j = 85; j <= 95; j++) {
s[25][j] = '*';
}
for(int j = 105; j <= 115; j++) {
s[23][j] = '*';
}
}
else if (level == 4) {
// 复杂地形
for(int i = 70; i <= 80; i++) {
s[27][i] = ' ';
}
for(int i = 100; i <= 110; i++) {
s[27][i] = ' ';
}
// 多层平台
for(int j = 75; j <= 85; j++) {
s[25][j] = '*';
}
for(int j = 95; j <= 105; j++) {
s[23][j] = '*';
}
for(int j = 115; j <= 125; j++) {
s[21][j] = '*';
}
}
// 初始位置
x = 30, y = 1;
// 绘制地图
system("cls");
for(int i = 1; i <= 40; i++) {
for(int j = 1; j <= 156; j++) {
cout << s[i][j];
}
cout << "\n";
}
// 显示关卡信息
Setpos(1, 1);
cout << "Level " << level;
// 初始化机关
initMechanisms(level);
int up = 0;
while(gameActive) {
// 保存旧位置
int old_x = x;
double old_y = y;
// 更新机关
updateMechanisms();
// 清除旧机关
for (int i = 20; i <= 30; i++) {
for (int j = 1; j <= 100; j++) {
if (s[i][j] != '*') {
Setpos(i, j/2.0);
cout << " ";
}
}
}
// 绘制机关
drawMechanisms();
// 清除旧位置
Setpos(old_x, old_y);
cout << " ";
// 处理输入
if(GetAsyncKeyState(40) & 0x8000) { // 下
if(s[x + 1][(int)(y * 2) + 1] != '*') {
x++;
}
}
if(GetAsyncKeyState(37) & 0x8000) { // 左
if(s[x][(int)(y * 2)] != '*') {
y -= 0.5;
}
}
if(GetAsyncKeyState(39) & 0x8000) { // 右
if(s[x][(int)(y * 2) + 2] != '*') {
y += 0.5;
}
}
if(GetAsyncKeyState(38) & 0x8000) { // 上
if(up < 2) {
for (int i = 0; i < 3; i++) {
if (s[x - 1][(int)(y * 2) + 1] != '*') {
x--;
Setpos(x, y);
cout << "@";
Sleep(30);
Setpos(x, y);
cout << " ";
}
}
up++;
}
}
// 重力
if(x <= 40 && s[x + 1][(int)(y * 2) + 1] != '*') {
x++;
} else {
up = 0;
}
// 边界检查
if(x >= 41) {
gameActive = false;
}
x = max(x, 1);
y = min(y, 78.0), y = max(y, 1.0);
// 机关碰撞检测
if (checkMechanismCollision()) {
gameActive = false;
}
// 胜利条件
if(s[x][(int)(2 * y) + 1] == '|') {
break;
}
// 绘制新位置
Setpos(x, y);
cout << "@";
Sleep(100);
}
if (gameActive) {
// 胜利
system("color 78");
Setpos(23, 39);
Color(2);
cout << "You Win!";
Color(0);
getch();
Sleep(1000);
system("cls");
system("color 07");
return true;
} else {
// 失败
system("color 47");
Setpos(23, 39);
Color(4);
cout << "You Die!";
Color(0);
getch();
Sleep(1000);
system("cls");
system("color 07");
return false;
}
}
int main() {
memset(s, ' ', sizeof(s));
Setpos(5, 10);
cout << "欢迎来到 ";
Color(-2);
cout << "乱世生存";
Color(0);
cout << " !";
Setpos(7, 3);
cout << "作者:koukou蒟蒻";
Setpos(7, 20);
Color(-2);
cout << "【 这里绝不能是拼音输入法! 】";
Setpos(9, 20);
cout << "【 建议全屏游玩! 】";
Setpos(10, 20);
cout << "【 现在,立刻马上调全屏! 】";
Color(0);
Setpos(11, 2);
cout << "按‘y’键继续...";
while(1) {
if(getch() == 'y') {
break;
}
}
system("cls");
Setpos(6, 10);
print("规则:上下左右控制方向,进入门里游戏胜利!");
Setpos(8, 15);
print("游戏中会不定时出现一些");
Color(4);
print("机关");
Color(0);
print("哦!\n");
Setpos(20, 9);
cout << "按‘y’键继续...";
while(1) {
if(getch() == 'y') {
break;
}
}
system("cls");
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
int level = 1;
while (level <= 4) {
if (playLevel(level)) {
level++;
} else {
// 失败后重新开始当前关卡
system("cls");
Setpos(10, 10);
cout << "按任意键重试...";
getch();
}
}
// 通关所有关卡
system("cls");
Setpos(10, 10);
Color(2);
cout << "恭喜你通关所有关卡!";
Color(0);
Setpos(12, 10);
cout << "按任意键退出...";
getch();
return 0;
}