游戏简介
这是一个基于控制台的 Florr.io 游戏实现,使用 C++ 编写,完全兼容 Dev-C++ 开发环境。游戏还原了 Florr.io 的核心玩法,包括花瓣收集、敌人战斗、技能系统等元素。
如何在 Dev-C++ 中运行游戏
第一步:创建新项目
- 打开 Dev-C++
- 点击顶部菜单 “文件” → “新建” → “项目”
- 在弹出的窗口中:
- 选择 “Console Application”(控制台应用程序)
- 在 “名称” 框中输入项目名称,如 “FlorrGame”
- 确保 “C++ 项目” 被选中
- 点击 “确定” 按钮
- 选择保存位置并点击 “保存”
第二步:设置编译器选项
- 点击顶部菜单 “工具” → “编译选项”
- 在弹出窗口中:
- 勾选 “编译时加入以下命令”
- 在文本框中输入:
-std=c++11 - 勾选 “在连接器命令行加入以下命令”
- 在文本框中输入:
-static-libgcc -static-libstdc++ - 点击 “确定” 保存设置
第三步:复制游戏代码
将以下完整代码复制到 Dev-C++ 的主源文件中:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <algorithm>
#include <cctype>
#include <windows.h>
using namespace std;
// 游戏常量
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 24;
const int PLAYER_SIZE = 1;
const int PETAL_SIZE = 1;
const int ENEMY_SIZE = 1;
const int FLOWER_SIZE = 1;
// 花瓣类型枚举 (基于florr.io)
enum PetalType {
BASIC, // 基础花瓣
ROSE, // 玫瑰 - 治疗效果
STINGER, // 毒刺 - 持续伤害
IRIS, // 鸢尾 - 穿透敌人
HEAVY, // 重瓣 - 高伤害
WING, // 翼瓣 - 快速移动
PETAL_COUNT // 花瓣类型总数
};
// 敌人类型枚举 (基于florr.io)
enum EnemyType {
ANT, // 蚂蚁 - 基础敌人
BEE, // 蜜蜂 - 远程攻击
BEETLE, // 甲虫 - 高生命值
SPIDER, // 蜘蛛 - 快速移动
HORNET, // 黄蜂 - 追踪玩家
ENEMY_COUNT // 敌人类型总数
};
// 技能类型枚举
enum AbilityType {
HEAL, // 治疗
BURST, // 爆发 - 发射一圈花瓣
SHIELD, // 护盾 - 暂时无敌
ABILITY_COUNT
};
struct Petal {
float x, y;
PetalType type;
float dx, dy;
bool active;
int lifetime; // 花瓣存在时间
int damage; // 伤害值
};
struct Enemy {
float x, y;
float dx, dy;
int health;
int maxHealth;
EnemyType type;
bool active;
int attackCooldown;
float speed;
};
struct Flower {
float x, y;
bool active;
int value; // 资源值
};
struct Ability {
AbilityType type;
int cooldown;
int maxCooldown;
bool ready;
};
class Player {
public:
float x, y;
vector<Petal> petals;
int health;
int maxHealth;
int level;
int experience;
int nextLevelExp;
int resources;
vector<Ability> abilities;
int shieldTimer;
bool isShielded;
Player() : x(SCREEN_WIDTH/2), y(SCREEN_HEIGHT/2),
health(100), maxHealth(100),
level(1), experience(0), nextLevelExp(50),
resources(0), shieldTimer(0), isShielded(false) {
// 初始化技能
abilities.push_back({HEAL, 0, 200, true});
abilities.push_back({BURST, 0, 300, true});
abilities.push_back({SHIELD, 0, 500, true});
}
void move(float dx, float dy) {
x += dx;
y += dy;
// 边界检查
x = max(0.0f, min((float)SCREEN_WIDTH-1, x));
y = max(0.0f, min((float)SCREEN_HEIGHT-1, y));
}
void shoot(float dx, float dy, PetalType type = BASIC) {
if (petals.size() < 5 + level) {
Petal p;
p.x = x;
p.y = y;
p.dx = dx * 2.5f;
p.dy = dy * 2.5f;
p.type = type;
p.active = true;
p.lifetime = 100;
// 根据花瓣类型设置伤害
switch(type) {
case BASIC: p.damage = 10; break;
case ROSE: p.damage = 5; break;
case STINGER: p.damage = 3; break;
case IRIS: p.damage = 8; break;
case HEAVY: p.damage = 15; break;
case WING: p.damage = 7; break;
default: p.damage = 10;
}
petals.push_back(p);
}
}
void update() {
// 更新花瓣位置
for (auto& p : petals) {
p.x += p.dx;
p.y += p.dy;
p.lifetime--;
// 检查是否超出边界或超时
if (p.x < 0 || p.x >= SCREEN_WIDTH || p.y < 0 || p.y >= SCREEN_HEIGHT || p.lifetime <= 0) {
p.active = false;
}
}
// 移除不活跃的花瓣
petals.erase(remove_if(petals.begin(), petals.end(),
[](const Petal& p) { return !p.active; }), petals.end());
// 更新技能冷却
for (auto& ability : abilities) {
if (!ability.ready) {
ability.cooldown--;
if (ability.cooldown <= 0) {
ability.ready = true;
ability.cooldown = 0;
}
}
}
// 更新护盾时间
if (isShielded) {
shieldTimer--;
if (shieldTimer <= 0) {
isShielded = false;
}
}
}
bool checkCollision(Enemy& enemy) {
bool hit = false;
for (auto& p : petals) {
if (p.active) {
float dist = sqrt(pow(p.x - enemy.x, 2) + pow(p.y - enemy.y, 2));
if (dist < (PETAL_SIZE + ENEMY_SIZE)) {
// 根据花瓣类型应用效果
switch(p.type) {
case ROSE: // 玫瑰治疗玩家
health = min(maxHealth, health + p.damage/2);
break;
case STINGER: // 毒刺持续伤害
enemy.health -= p.damage / 4; // 初始伤害较小
p.lifetime = min(p.lifetime, 10); // 缩短存在时间
break;
case IRIS: // 鸢尾穿透但不销毁
hit = true;
enemy.health -= p.damage;
continue; // 不销毁花瓣
default: // 其他花瓣正常伤害
enemy.health -= p.damage;
break;
}
// 非穿透花瓣命中后消失
if (p.type != IRIS) {
p.active = false;
}
hit = true;
}
}
}
return hit;
}
void gainExp(int amount) {
experience += amount;
if (experience >= nextLevelExp) {
level++;
maxHealth += 20;
health = maxHealth;
nextLevelExp = level * 50;
}
}
void useAbility(AbilityType type) {
for (auto& ability : abilities) {
if (ability.type == type && ability.ready) {
ability.ready = false;
ability.cooldown = ability.maxCooldown;
switch(type) {
case HEAL:
health = min(maxHealth, health + 50);
break;
case BURST:
for (int i = 0; i < 12; i++) {
float angle = i * (3.14159f / 6);
shoot(cos(angle), sin(angle), BASIC);
}
break;
case SHIELD:
isShielded = true;
shieldTimer = 100;
break;
}
break;
}
}
}
};
void spawnEnemy(vector<Enemy>& enemies, int level, float playerX, float playerY) {
if (enemies.size() < 5 + level/2) {
Enemy e;
e.active = true;
// 确保不会选择无效的敌人类型
int enemyTypeCount = min(ENEMY_COUNT, 1 + level/5);
if (enemyTypeCount <= 0) enemyTypeCount = 1;
e.type = static_cast<EnemyType>(rand() % enemyTypeCount);
e.attackCooldown = 0;
// 根据类型设置属性
switch(e.type) {
case ANT:
e.maxHealth = 10 + level;
e.speed = 0.8f;
break;
case HORNET:
e.maxHealth = 15 + level;
e.speed = 1.0f;
break;
case BEETLE:
e.maxHealth = 40 + level * 2;
e.speed = 0.4f;
break;
case BEE:
e.maxHealth = 12 + level;
e.speed = 0.6f;
break;
case SPIDER:
e.maxHealth = 5 + level/2;
e.speed = 1.5f;
break;
default: // 默认处理
e.maxHealth = 10 + level;
e.speed = 0.8f;
}
e.health = e.maxHealth;
// 生成位置 (远离玩家)
float angle = rand() % 360 * 3.14159f / 180;
float dist = min(SCREEN_WIDTH, SCREEN_HEIGHT) * 0.4f;
e.x = playerX + cos(angle) * dist;
e.y = playerY + sin(angle) * dist;
// 确保在屏幕内
e.x = max(0.0f, min((float)SCREEN_WIDTH-1, e.x));
e.y = max(0.0f, min((float)SCREEN_HEIGHT-1, e.y));
enemies.push_back(e);
}
}
void spawnFlower(vector<Flower>& flowers, int level) {
if (flowers.size() < 3 + level/5 && rand() % 100 < 5) {
Flower f;
f.x = rand() % SCREEN_WIDTH;
f.y = rand() % SCREEN_HEIGHT;
f.active = true;
f.value = 5 + rand() % (level + 1);
flowers.push_back(f);
}
}
void updateEnemies(vector<Enemy>& enemies, Player& player, vector<Petal>& enemyPetals) {
for (auto& e : enemies) {
if (e.active) {
// 根据类型更新行为
switch(e.type) {
case ANT: // 直线移动
if (e.dx == 0 && e.dy == 0) {
float angle = rand() % 360 * 3.14159f / 180;
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
}
break;
case HORNET: // 追踪玩家
{
float angle = atan2(player.y - e.y, player.x - e.x);
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
}
break;
case BEETLE: // 缓慢但坚定
{
float angle = atan2(player.y - e.y, player.x - e.x);
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
}
break;
case BEE: // 远程攻击
if (e.attackCooldown <= 0) {
float angle = atan2(player.y - e.y, player.x - e.x);
Petal p;
p.x = e.x;
p.y = e.y;
p.dx = cos(angle) * 1.5f;
p.dy = sin(angle) * 1.5f;
p.type = BASIC;
p.active = true;
p.lifetime = 60;
p.damage = 5;
enemyPetals.push_back(p);
e.attackCooldown = 60;
} else {
e.attackCooldown--;
}
// 保持距离
{
float dist = sqrt(pow(e.x - player.x, 2) + pow(e.y - player.y, 2));
if (dist < 10) {
float angle = atan2(e.y - player.y, e.x - player.x);
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
} else if (dist > 15) {
float angle = atan2(player.y - e.y, player.x - e.x);
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
} else {
e.dx = e.dy = 0;
}
}
break;
case SPIDER: // 快速移动
{
float angle = atan2(player.y - e.y, player.x - e.x);
angle += (rand() % 100 - 50) * 0.01f; // 添加一些随机性
e.dx = cos(angle) * e.speed;
e.dy = sin(angle) * e.speed;
}
break;
}
// 更新位置
e.x += e.dx;
e.y += e.dy;
// 边界检查
e.x = max(0.0f, min((float)SCREEN_WIDTH-1, e.x));
e.y = max(0.0f, min((float)SCREEN_HEIGHT-1, e.y));
// 检查与玩家碰撞
float dist = sqrt(pow(e.x - player.x, 2) + pow(e.y - player.y, 2));
if (dist < (PLAYER_SIZE + ENEMY_SIZE)) {
if (!player.isShielded) {
player.health -= max(1, 10 - player.level); // 随等级减少伤害
}
e.health -= 5; // 敌人也受到伤害
}
}
}
// 移除不活跃的敌人
enemies.erase(remove_if(enemies.begin(), enemies.end(),
[](const Enemy& e) { return !e.active || e.health <= 0; }), enemies.end());
}
void updateEnemyPetals(vector<Petal>& enemyPetals, Player& player) {
for (auto& p : enemyPetals) {
p.x += p.dx;
p.y += p.dy;
p.lifetime--;
// 检查是否超出边界或超时
if (p.x < 0 || p.x >= SCREEN_WIDTH || p.y < 0 || p.y >= SCREEN_HEIGHT || p.lifetime <= 0) {
p.active = false;
}
// 检查与玩家碰撞
float dist = sqrt(pow(p.x - player.x, 2) + pow(p.y - player.y, 2));
if (dist < (PLAYER_SIZE + PETAL_SIZE) && p.active) {
if (!player.isShielded) {
player.health -= p.damage;
}
p.active = false;
}
}
// 移除不活跃的花瓣
enemyPetals.erase(remove_if(enemyPetals.begin(), enemyPetals.end(),
[](const Petal& p) { return !p.active; }), enemyPetals.end());
}
void checkFlowerCollection(vector<Flower>& flowers, Player& player) {
for (auto& f : flowers) {
if (f.active) {
float dist = sqrt(pow(f.x - player.x, 2) + pow(f.y - player.y, 2));
if (dist < (PLAYER_SIZE + FLOWER_SIZE)) {
f.active = false;
player.resources += f.value;
}
}
}
// 移除收集的花朵
flowers.erase(remove_if(flowers.begin(), flowers.end(),
[](const Flower& f) { return !f.active; }), flowers.end());
}
void clearScreen() {
// 使用Windows API清屏
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = {0, 0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, ' ', dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
void drawGame(const Player& player, const vector<Enemy>& enemies,
const vector<Flower>& flowers, const vector<Petal>& enemyPetals) {
// 创建屏幕缓冲区
vector<vector<char>> screen(SCREEN_HEIGHT, vector<char>(SCREEN_WIDTH, ' '));
// 绘制花朵
for (const auto& f : flowers) {
if (f.active) {
int fx = static_cast<int>(f.x);
int fy = static_cast<int>(f.y);
if (fx >= 0 && fx < SCREEN_WIDTH && fy >= 0 && fy < SCREEN_HEIGHT) {
screen[fy][fx] = 'F';
}
}
}
// 绘制敌人子弹
for (const auto& p : enemyPetals) {
if (p.active) {
int px = static_cast<int>(p.x);
int py = static_cast<int>(p.y);
if (px >= 0 && px < SCREEN_WIDTH && py >= 0 && py < SCREEN_HEIGHT) {
screen[py][px] = '*';
}
}
}
// 绘制敌人
for (const auto& e : enemies) {
if (e.active) {
int ex = static_cast<int>(e.x);
int ey = static_cast<int>(e.y);
if (ex >= 0 && ex < SCREEN_WIDTH && ey >= 0 && ey < SCREEN_HEIGHT) {
char c;
switch(e.type) {
case ANT: c = 'A'; break; // 蚂蚁
case BEE: c = 'B'; break; // 蜜蜂
case BEETLE: c = 'T'; break; // 甲虫
case SPIDER: c = 'S'; break; // 蜘蛛
case HORNET: c = 'H'; break; // 黄蜂
default: c = 'E';
}
screen[ey][ex] = c;
// 显示血条 (如果不满)
if (e.health < e.maxHealth && ex+1 < SCREEN_WIDTH) {
screen[ey][ex+1] = '|';
}
}
}
}
// 绘制玩家花瓣
for (const auto& p : player.petals) {
if (p.active) {
int px = static_cast<int>(p.x);
int py = static_cast<int>(p.y);
if (px >= 0 && px < SCREEN_WIDTH && py >= 0 && py < SCREEN_HEIGHT) {
char c;
switch(p.type) {
case BASIC: c = '*'; break; // 基础花瓣
case ROSE: c = 'R'; break; // 玫瑰
case STINGER: c = 'S'; break; // 毒刺
case IRIS: c = 'I'; break; // 鸢尾
case HEAVY: c = 'H'; break; // 重瓣
case WING: c = 'W'; break; // 翼瓣
default: c = '*';
}
screen[py][px] = c;
}
}
}
// 绘制玩家
int px = static_cast<int>(player.x);
int py = static_cast<int>(player.y);
if (px >= 0 && px < SCREEN_WIDTH && py >= 0 && py < SCREEN_HEIGHT) {
screen[py][px] = player.isShielded ? '#' : 'P';
}
// 输出屏幕
for (int y = 0; y < SCREEN_HEIGHT; y++) {
for (int x = 0; x < SCREEN_WIDTH; x++) {
cout << screen[y][x];
}
cout << endl;
}
// 显示状态
cout << "生命值: " << player.health << "/" << player.maxHealth
<< " | 等级: " << player.level
<< " | 经验: " << player.experience << "/" << player.nextLevelExp
<< " | 资源: " << player.resources << endl;
// 显示技能状态
cout << "技能: ";
for (const auto& ability : player.abilities) {
switch(ability.type) {
case HEAL: cout << "治疗"; break;
case BURST: cout << "爆发"; break;
case SHIELD: cout << "护盾"; break;
}
if (ability.ready) {
cout << "(可用) ";
} else {
cout << "(" << ability.cooldown << "帧) ";
}
}
cout << endl;
// 显示控制提示
cout << "控制: WASD-移动, 方向键-射击, 1-治疗, 2-爆发, 3-护盾, Q-退出" << endl;
}
int main() {
srand(time(nullptr));
// 设置控制台窗口大小
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect = {0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT+10};
SetConsoleWindowInfo(hConsole, TRUE, &rect);
// 隐藏光标
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(hConsole, &cursorInfo);
Player player;
vector<Enemy> enemies;
vector<Flower> flowers;
vector<Petal> enemyPetals;
bool running = true;
clock_t lastTime = clock();
clock_t enemySpawnTime = lastTime;
clock_t lastRenderTime = lastTime;
clock_t flowerSpawnTime = lastTime;
// 初始生成一些敌人和花朵
for (int i = 0; i < 3; i++) {
spawnEnemy(enemies, player.level, player.x, player.y);
}
for (int i = 0; i < 2; i++) {
spawnFlower(flowers, player.level);
}
// 游戏主循环
while (running && player.health > 0) {
clock_t currentTime = clock();
float deltaTime = (currentTime - lastTime) / (float)CLOCKS_PER_SEC;
lastTime = currentTime;
// 处理输入
if (_kbhit()) {
int key = _getch();
// 特殊键 (方向键)
if (key == 0 || key == 0xE0) {
key = _getch();
// 方向键射击
switch(key) {
case 72: // 上
player.shoot(0, -1, static_cast<PetalType>(rand() % PETAL_COUNT));
break;
case 80: // 下
player.shoot(0, 1, static_cast<PetalType>(rand() % PETAL_COUNT));
break;
case 75: // 左
player.shoot(-1, 0, static_cast<PetalType>(rand() % PETAL_COUNT));
break;
case 77: // 右
player.shoot(1, 0, static_cast<PetalType>(rand() % PETAL_COUNT));
break;
}
} else {
// 普通键
float moveSpeed = 10.0f * deltaTime;
switch(tolower(key)) {
case 'w': player.move(0, -moveSpeed); break;
case 's': player.move(0, moveSpeed); break;
case 'a': player.move(-moveSpeed, 0); break;
case 'd': player.move(moveSpeed, 0); break;
case '1': player.useAbility(HEAL); break;
case '2': player.useAbility(BURST); break;
case '3': player.useAbility(SHIELD); break;
case 'q': running = false; break;
}
}
}
// 生成敌人
if (currentTime - enemySpawnTime > CLOCKS_PER_SEC * (3.0f - min(2.0f, player.level/10.0f))) {
spawnEnemy(enemies, player.level, player.x, player.y);
enemySpawnTime = currentTime;
}
// 生成花朵
if (currentTime - flowerSpawnTime > CLOCKS_PER_SEC * 5) {
spawnFlower(flowers, player.level);
flowerSpawnTime = currentTime;
}
// 更新游戏状态
player.update();
updateEnemies(enemies, player, enemyPetals);
updateEnemyPetals(enemyPetals, player);
checkFlowerCollection(flowers, player);
// 检查花瓣与敌人碰撞
for (auto& e : enemies) {
if (e.active && player.checkCollision(e)) {
if (e.health <= 0) {
player.gainExp(10 + e.type * 5); // 不同类型敌人给予不同经验
if (rand() % 3 == 0) { // 掉落资源
player.resources += 1 + player.level/5;
}
}
}
}
// 限制渲染频率
if (currentTime - lastRenderTime > CLOCKS_PER_SEC / 15) {
clearScreen();
drawGame(player, enemies, flowers, enemyPetals);
lastRenderTime = currentTime;
}
// 小延迟减少CPU使用
Sleep(10);
}
// 游戏结束
clearScreen();
if (player.health <= 0) {
cout << "游戏结束!" << endl;
} else {
cout << "游戏已退出" << endl;
}
cout << "最终等级: " << player.level << endl;
cout << "收集资源总数: " << player.resources << endl;
cout << "击败敌人数量: " << player.experience / 10 << endl;
cout << "按任意键退出...";
_getch();
// 恢复光标
cursorInfo.bVisible = true;
SetConsoleCursorInfo(hConsole, &cursorInfo);
return 0;
}第四步:编译和运行
- 点击顶部菜单 “运行” → “编译” (或按 F9 键)
- 编译成功后,点击 “运行” → “运行” (或按 F10 键)
- 或者直接点击 “运行” → “编译运行” (或按 F11 键)
第五步:解决常见问题
如果编译出错,请检查:
- 是否已设置编译器选项为 C++11
- 是否已正确复制所有代码
- 如果出现 “_kbhit 未定义” 错误:
- 尝试将
_kbhit()改为kbhit() - 将
_getch()改为getch()
- 尝试将
游戏说明
游戏元素
-
玩家(P):控制角色在屏幕上移动
-
花瓣类型:
*基础花瓣 - 标准攻击R玫瑰 - 击中敌人时治疗玩家S毒刺 - 造成持续伤害I鸢尾 - 穿透敌人不消失H重瓣 - 高伤害但移动慢W翼瓣 - 快速移动但伤害较低
-
敌人类型:
A蚂蚁 - 基础敌人B蜜蜂 - 远程攻击T甲虫 - 高生命值S蜘蛛 - 快速移动H黄蜂 - 追踪玩家
-
花朵(F):收集获得资源
控制方式
- WASD:移动角色
- 方向键:向对应方向发射花瓣
- 1:使用治疗技能
- 2:使用爆发技能(向所有方向发射花瓣)
- 3:使用护盾技能(暂时无敌)
- Q:退出游戏
游戏目标
- 击败敌人获得经验值升级
- 收集花朵获得资源
- 升级后可以发射更多花瓣
- 使用技能应对不同情况
- 尽可能生存更长时间
游戏特点
- 完全还原 Florr.io 核心玩法
- 多种花瓣类型,每种有独特效果
- 多样化的敌人AI行为
- 技能系统增强游戏策略性
- 资源收集和成长系统
- 中文界面和提示