#include <iostream>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
// ================= 常量设置 =================
const int SCREEN_WIDTH = 70;
const int SCREEN_HEIGHT = 18;
const int GROUND_Y = 15;
const int MAX_HP = 100;
const int GRAVITY = 1;
// 枚举定义
enum ItemType { WEP_SWORD, WEP_SPEAR, WEP_GUN, ARM_IRON, POT_HEALTH, BOMB };
enum SkillType { SKILL_BLINK, SKILL_SHIELD, SKILL_STOMP, SKILL_NONE };
enum EntityType { ENT_PLAYER, ENT_CPU };
// ================= 基础结构体 =================
struct Item {
int x, y; ItemType type; char symbol; bool active;
};
struct Obstacle {
int x, y, w, h; // x,y是左上角,w,h是宽高
};
struct Projectile {
int x, y, vx; // vx: 速度方向 (-1 或 1)
int owner; // 0:P1, 1:P2
bool active;
};
// ================= 玩家类 =================
class Fighter {
public:
int x, y; // 中心坐标,y是脚底
int velY; // Y轴速度 (用于跳跃)
int hp, maxHp;
bool isAttacking, isJumping, isSliding;
int attackTimer, slideTimer;
bool facingRight;
private:
void setChar(char screen[][SCREEN_WIDTH], int cx, int cy, char c) {
if (cx >= 0 && cx < SCREEN_WIDTH && cy >= 0 && cy < SCREEN_HEIGHT) {
screen[cy][cx] = c;
}
}
};
// ================= 全局变量 =================
Fighter p1, p2;
stdvector<Item> items;
stdvector<Obstacle> obstacles;
stdvector<Projectile> bullets;
bool gameOver = false;
stdstring winner = "";
int frameCount = 0;
int aiCooldown = 0;
bool isPvEMode = false;
// ================= 辅助函数 =================
stdstring intToString(int val) { stdostringstream oss; oss << val; return oss.str(); }
void printLine(const stdstring& text) {
stdcout << text;
if (text.length() < SCREEN_WIDTH) stdcout << stdstring(SCREEN_WIDTH - text.length(), ' ');
std::cout << "\n";
}
void gotoxy(int x, int y) {
COORD coord; coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hideCursor() {
CONSOLE_CURSOR_INFO info; GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
info.bVisible = false; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}
void clearScreen() { system("cls"); }
// 全局障碍物碰撞检测实现
bool Fighter::checkObstacleCollision(int nextX, int nextY) {
for (size_t i = 0; i < obstacles.size(); ++i) {
int ox = obstacles[i].x, oy = obstacles[i].y;
int ow = obstacles[i].w, oh = obstacles[i].h;
// 玩家碰撞箱简化为 宽3 高3 (x-1 到 x+1, y-2 到 y)
if (nextX + 1 >= ox && nextX - 1 <= ox + ow - 1 &&
nextY >= oy && nextY - 2 <= oy + oh - 1) {
return true;
}
}
return false;
}
// ================= 菜单与初始化 =================
int showMainMenu() {
clearScreen();
gotoxy(20, 3); stdcout << "================================" << stdendl;
gotoxy(20, 4); stdcout << " C++ PLATFORM FIGHTER v3.0 " << stdendl;
gotoxy(20, 5); stdcout << "================================" << stdendl;
gotoxy(23, 8); stdcout << "1. Player vs Player (PvP)" << stdendl;
gotoxy(23, 10); stdcout << "2. Player vs CPU (PvE)" << stdendl;
gotoxy(23, 12); stdcout << "3. Exit Game" << stdendl;
gotoxy(20, 16); std::cout << "Select (1-3): ";
}
SkillType selectSkillMenu(const stdstring& playerName) {
clearScreen();
gotoxy(15, 3); stdcout << "=== Select Skill for " << playerName << " ===" << stdendl;
gotoxy(15, 6); stdcout << "1. Blink (Teleport short distance, CD 4s)" << stdendl;
gotoxy(15, 8); stdcout << "2. Shield (50% Damage reduction for 3s, CD 8s)" << stdendl;
gotoxy(15, 10); stdcout << "3. Stomp (AOE damage around you, CD 6s)" << stdendl;
gotoxy(15, 14); stdcout << "Choose (1-3): ";
}
void generateTerrain() {
obstacles.clear();
// 随机生成 2-4 个障碍物
int numObs = 2 + rand() % 3;
for (int i = 0; i < numObs; ++i) {
Obstacle obs;
obs.w = 2; obs.h = 2; // 2x2 的箱子
obs.x = 10 + rand() % (SCREEN_WIDTH - 20);
obs.y = GROUND_Y - obs.h + 1; // 放在地上
// 避免生成在玩家初始位置
if (abs(obs.x - 15) < 5 || abs(obs.x - 55) < 5) {
obs.x += 10;
}
obstacles.push_back(obs);
}
}
void resetGame() {
p1.reset(15, true);
p2.reset(55, false);
items.clear(); bullets.clear();
gameOver = false; winner = ""; frameCount = 0; aiCooldown = 0;
generateTerrain();
}
// ================= 游戏核心逻辑 =================
void spawnItem() {
if (items.size() < 3 && frameCount % 150 == 0) {
Item newItem;
newItem.x = 5 + rand() % (SCREEN_WIDTH - 10);
newItem.y = GROUND_Y;
}
void updateAI(Fighter& cpu, Fighter& player) {
if (aiCooldown > 0) { aiCooldown--; return; }
aiCooldown = 8 + rand() % 12;
}
void handleInput() {
if (_kbhit()) {
int key = _getch();
if (key == 224 || key == 0) { // 方向键
key = _getch();
if (!gameOver && !isPvEMode) {
switch (key) {
case 75: p2.moveLeft(); break; // Left
case 77: p2.moveRight(); break; // Right
case 72: p2.jump(); break; // Up
case 80: p2.slide(); break; // Down
}
}
} else {
if (key == 27) exit(0); // ESC
if (!gameOver) {
// P1 控制: A/D, W, S, J, K
switch (key) {
case 'a': case 'A': p1.moveLeft(); break;
case 'd': case 'D': p1.moveRight(); break;
case 'w': case 'W': p1.jump(); break;
case 's': case 'S': p1.slide(); break;
case 'j': case 'J': p1.attack(); break;
case 'k': case 'K': p1.useSkill(p2); break;
}
// P2 控制: 1(攻击), 2(技能)
if (!isPvEMode) {
if (key == '1') p2.attack();
if (key == '2') p2.useSkill(p1);
}
}
}
}
}
void updateGame() {
if (gameOver) return;
frameCount++;
p1.update(); p2.update();
if (isPvEMode) updateAI(p2, p1);
}
void draw() {
gotoxy(0, 0);
std::string title = isPvEMode ? "=== PLATFORM FIGHTER (PvE) =" : "= PLATFORM FIGHTER (PvP) ===";
printLine(title);
}
void startGame(bool pve) {
isPvEMode = pve;
resetGame();
}
int main() {
srand(static_cast<unsigned int>(time(0)));
hideCursor();
while (true) {
int choice = showMainMenu();
if (choice == 1) startGame(false);
else if (choice == 2) startGame(true);
else if (choice == 3) { clearScreen(); std::cout << "Goodbye!\n"; break; }
}
MessageBoxA(NULL, "正版光荣,盗版可耻,改编跟作者说一声", "防伪标签",MB_ICONWARNING);
return 0;
}
英文版的,单词不懂自己搜,此处省略78字