这个Demo会展示基础的人物控制、跳跃、攀爬和攻击等核心技能。
```cpp
// SimpleOpenClawDemo.cpp
// 一个简单的OpenClaw风格游戏技能演示
// 使用SFML图形库
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cmath>
const float GRAVITY = 0.5f;
const float JUMP_FORCE = -12.0f;
const float MOVE_SPEED = 3.0f;
const float CLAW_ATTACK_DURATION = 20;
class Player {
public:
sf::RectangleShape shape;
sf::Vector2f velocity;
bool isGrounded;
bool isOnWall;
bool isAttacking;
int attackTimer;
int health;
int clawsCount;
Player() {
shape.setSize(sf::Vector2f(30, 40));
shape.setFillColor(sf::Color::Red);
shape.setPosition(100, 300);
velocity = sf::Vector2f(0, 0);
isGrounded = false;
isOnWall = false;
isAttacking = false;
attackTimer = 0;
health = 100;
clawsCount = 3; // 初始爪子数量
}
void update() {
// 重力
if (!isGrounded && !isOnWall) {
velocity.y += GRAVITY;
}
// 限制最大下落速度
if (velocity.y > 15) velocity.y = 15;
// 移动
shape.move(velocity);
// 攻击计时器
if (isAttacking) {
attackTimer--;
if (attackTimer <= 0) {
isAttacking = false;
shape.setFillColor(sf::Color::Red);
}
}
}
void jump() {
if (isGrounded) {
velocity.y = JUMP_FORCE;
isGrounded = false;
}
else if (isOnWall) {
// 墙壁跳跃
velocity.y = JUMP_FORCE * 0.8f;
velocity.x = (shape.getPosition().x < 400) ? -MOVE_SPEED * 2 : MOVE_SPEED * 2;
isOnWall = false;
}
}
void attack() {
if (clawsCount > 0 && !isAttacking) {
isAttacking = true;
attackTimer = CLAW_ATTACK_DURATION;
clawsCount--;
shape.setFillColor(sf::Color::Yellow);
// 攻击判定会在碰撞检测中处理
}
}
void moveLeft() {
velocity.x = -MOVE_SPEED;
}
void moveRight() {
velocity.x = MOVE_SPEED;
}
void stop() {
velocity.x = 0;
}
void collectClaw() {
clawsCount++;
if (clawsCount > 5) clawsCount = 5; // 最多5个爪子
}
};
class Platform {
public:
sf::RectangleShape shape;
Platform(float x, float y, float width, float height, sf::Color color = sf::Color::Green) {
shape.setSize(sf::Vector2f(width, height));
shape.setPosition(x, y);
shape.setFillColor(color);
}
};
class Enemy {
public:
sf::CircleShape shape;
sf::Vector2f position;
float speed;
int health;
bool movingRight;
Enemy(float x, float y) {
shape.setRadius(15);
shape.setFillColor(sf::Color::Magenta);
shape.setPosition(x, y);
position = sf::Vector2f(x, y);
speed = 1.5f;
health = 30;
movingRight = true;
}
void update() {
// 简单巡逻移动
if (movingRight) {
position.x += speed;
if (position.x > 700) movingRight = false;
} else {
position.x -= speed;
if (position.x < 100) movingRight = true;
}
shape.setPosition(position);
}
void takeDamage(int damage) {
health -= damage;
if (health < 0) health = 0;
shape.setFillColor(sf::Color(255, 0, 255, 100)); // 闪烁效果
}
};
class ClawItem {
public:
sf::RectangleShape shape;
bool collected;
ClawItem(float x, float y) {
shape.setSize(sf::Vector2f(20, 20));
shape.setPosition(x, y);
shape.setFillColor(sf::Color::Cyan);
collected = false;
}
};
class Game {
private:
sf::RenderWindow window;
std::vector<Platform> platforms;
std::vector<Enemy> enemies;
std::vector<ClawItem> clawItems;
Player player;
sf::Font font;
sf::Text healthText;
sf::Text clawText;
bool gameRunning;
public:
Game() : window(sf::VideoMode(800, 600), "Simple OpenClaw Skill Demo") {
setupLevel();
setupUI();
gameRunning = true;
}
void setupLevel() {
// 地面
platforms.push_back(Platform(0, 550, 800, 50, sf::Color(100, 100, 100)));
// 平台
platforms.push_back(Platform(200, 450, 150, 20, sf::Color(150, 150, 150)));
platforms.push_back(Platform(500, 400, 150, 20, sf::Color(150, 150, 150)));
platforms.push_back(Platform(300, 300, 120, 20, sf::Color(150, 150, 150)));
// 墙壁(用于攀爬)
platforms.push_back(Platform(50, 400, 20, 150, sf::Color(200, 100, 100)));
platforms.push_back(Platform(730, 350, 20, 200, sf::Color(200, 100, 100)));
// 敌人
enemies.push_back(Enemy(300, 520));
enemies.push_back(Enemy(600, 370));
// 爪子收集品
clawItems.push_back(ClawItem(250, 520));
clawItems.push_back(ClawItem(550, 370));
clawItems.push_back(ClawItem(350, 270));
}
void setupUI() {
if (!font.loadFromFile("arial.ttf")) {
// 如果找不到字体,使用默认字体
std::cout << "Warning: Could not load font" << std::endl;
}
healthText.setFont(font);
healthText.setCharacterSize(20);
healthText.setFillColor(sf::Color::White);
healthText.setPosition(10, 10);
clawText.setFont(font);
clawText.setCharacterSize(20);
clawText.setFillColor(sf::Color::Cyan);
clawText.setPosition(10, 40);
}
void handleInput() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Space) {
player.jump();
}
if (event.key.code == sf::Keyboard::X) {
player.attack();
}
if (event.key.code == sf::Keyboard::R) {
resetGame();
}
}
}
// 持续按键处理
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
player.moveLeft();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
player.moveRight();
}
else {
player.stop();
}
}
void update() {
player.update();
// 碰撞检测
checkCollisions();
// 更新敌人
for (auto& enemy : enemies) {
enemy.update();
// 敌人与玩家的碰撞
if (enemy.health > 0 &&
player.shape.getGlobalBounds().intersects(enemy.shape.getGlobal
夜雨聆风