ardw
2026-11-05 16:19:47
发布于:浙江
#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#include <iostream>
#include <random>
#include <map>
// 常量定义
const int TILE_SIZE = 32;
const int WORLD_WIDTH = 200; // 世界宽度(方块数)
const int WORLD_HEIGHT = 64; // 世界高度(方块数)
const float GRAVITY = 0.5f;
const float JUMP_FORCE = -10.0f;
const float MOVE_SPEED = 5.0f;
// 方块类型枚举
enum BlockType {
AIR = 0,
DIRT = 1,
GRASS = 2,
STONE = 3,
BEDROCK = 4
};
// 颜色映射
sfColor getBlockColor(BlockType type) {
switch (type) {
case DIRT: return sfColor(101, 67, 33);
case GRASS: return sfColor(50, 205, 50);
case STONE: return sfColor(128, 128, 128);
case BEDROCK: return sfColor(30, 30, 30);
default: return sfColor::Transparent;
}
}
class World {
public:
stdvector<stdvector<BlockType>> map;
int width, height;
World(int w, int h) : width(w), height(h) {
map.resize(height, std::vector<BlockType>(width, AIR));
generateTerrain();
}
void generateTerrain() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0.0, 1.0);
// 简单的高度图生成
std::vector<int> heightMap(width);
int currentHeight = height / 2;
for (int x = 0; x < width; ++x) {
// 随机改变高度,模拟地形起伏
if (dis(gen) > 0.5) {
currentHeight += (dis(gen) > 0.5 ? 1 : -1);
}
// 限制高度范围
if (currentHeight < 10) currentHeight = 10;
if (currentHeight > height - 5) currentHeight = height - 5;
heightMap[x] = currentHeight;
}
// 填充方块
for (int x = 0; x < width; ++x) {
int h = heightMap[x];
for (int y = 0; y < height; ++y) {
if (y == height - 1) {
map[y][x] = BEDROCK;
} else if (y > h + 3) {
map[y][x] = STONE;
} else if (y > h) {
map[y][x] = DIRT;
} else if (y == h) {
map[y][x] = GRASS;
} else {
map[y][x] = AIR;
}
}
}
}
BlockType getBlock(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return BEDROCK; // 边界视为基岩
return map[y][x];
}
void setBlock(int x, int y, BlockType type) {
if (x >= 0 && x < width && y >= 0 && y < height) {
map[y][x] = type;
}
}
};
class Player {
public:
float x, y;
float vx, vy;
int width, height; // 像素尺寸
bool onGround;
Player(float startX, float startY) : x(startX), y(startY), vx(0), vy(0), width(20), height(56), onGround(false) {}
void update(World& world) {
// 应用重力
vy += GRAVITY;
// 水平移动
x += vx;
handleCollision(world, true);
// 垂直移动
y += vy;
onGround = false;
handleCollision(world, false);
// 摩擦力
vx *= 0.8f;
}
void jump() {
if (onGround) {
vy = JUMP_FORCE;
onGround = false;
}
}
void moveLeft() { vx -= 1.0f; }
void moveRight() { vx += 1.0f; }
private:
void handleCollision(World& world, bool horizontal) {
// 计算玩家占据的网格坐标范围
int startX = static_cast<int>(x) / TILE_SIZE;
int endX = static_cast<int>(x + width) / TILE_SIZE;
int startY = static_cast<int>(y) / TILE_SIZE;
int endY = static_cast<int>(y + height) / TILE_SIZE;
for (int cy = startY; cy <= endY; ++cy) {
for (int cx = startX; cx <= endX; ++cx) {
if (world.getBlock(cx, cy) != AIR) {
if (horizontal) {
if (vx > 0) { // 向右撞
x = cx * TILE_SIZE - width - 0.01f;
} else if (vx < 0) { // 向左撞
x = (cx + 1) * TILE_SIZE + 0.01f;
}
vx = 0;
} else {
if (vy > 0) { // 向下落
y = cy * TILE_SIZE - height - 0.01f;
onGround = true;
vy = 0;
} else if (vy < 0) { // 向上顶
y = (cy + 1) * TILE_SIZE + 0.01f;
vy = 0;
}
}
return; // 一次只处理一个方向的碰撞简化逻辑
}
}
}
}
};
int main() {
// 创建窗口
sfRenderWindow window(sfVideoMode(800, 600), "2D Minecraft C++");
window.setFramerateLimit(60);
// 初始化世界和玩家
World world(WORLD_WIDTH, WORLD_HEIGHT);
Player player(100, 100);
// 相机偏移
sf::View view = window.getDefaultView();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
// 鼠标交互:挖掘或放置
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
// 计算鼠标在世界中的坐标
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f worldPos = window.mapPixelToCoords(mousePos, view);
int tileX = static_cast<int>(worldPos.x) / TILE_SIZE;
int tileY = static_cast<int>(worldPos.y) / TILE_SIZE;
// 挖掘(设置为空气)
if (world.getBlock(tileX, tileY) != BEDROCK) {
world.setBlock(tileX, tileY, AIR);
}
} else if (event.mouseButton.button == sf::Mouse::Right) {
// 放置(设置为泥土,简单起见)
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f worldPos = window.mapPixelToCoords(mousePos, view);
int tileX = static_cast<int>(worldPos.x) / TILE_SIZE;
int tileY = static_cast<int>(worldPos.y) / TILE_SIZE;
// 防止卡在方块里
sf::FloatRect playerRect(player.x, player.y, player.width, player.height);
sf::FloatRect blockRect(tileX * TILE_SIZE, tileY * TILE_SIZE, TILE_SIZE, TILE_SIZE);
if (!playerRect.intersects(blockRect) && world.getBlock(tileX, tileY) == AIR) {
world.setBlock(tileX, tileY, DIRT);
}
}
}
}
// 键盘输入
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player.moveLeft();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
player.moveRight();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
player.jump();
}
// 更新物理
player.update(world);
// 更新相机跟随玩家
view.setCenter(player.x + player.width / 2, player.y + player.height / 2);
window.setView(view);
// 渲染
window.clear(sf::Color(135, 206, 235)); // 天空蓝
// 优化:只渲染视野内的方块
sf::Vector2f viewCenter = view.getCenter();
sf::Vector2u viewSize = window.getSize();
int startCol = std::max(0, static_cast<int>((viewCenter.x - viewSize.x / 2) / TILE_SIZE) - 1);
int endCol = std::min(WORLD_WIDTH, static_cast<int>((viewCenter.x + viewSize.x / 2) / TILE_SIZE) + 2);
int startRow = std::max(0, static_cast<int>((viewCenter.y - viewSize.y / 2) / TILE_SIZE) - 1);
int endRow = std::min(WORLD_HEIGHT, static_cast<int>((viewCenter.y + viewSize.y / 2) / TILE_SIZE) + 2);
for (int y = startRow; y < endRow; ++y) {
for (int x = startCol; x < endCol; ++x) {
BlockType type = world.getBlock(x, y);
if (type != AIR) {
sf::RectangleShape block(sf::Vector2f(TILE_SIZE, TILE_SIZE));
block.setPosition(x * TILE_SIZE, y * TILE_SIZE);
block.setFillColor(getBlockColor(type));
// 添加边框让方块更清晰
block.setOutlineThickness(1.f);
block.setOutlineColor(sf::Color(0, 0, 0, 50));
window.draw(block);
}
}
}
// 绘制玩家
sf::RectangleShape playerShape(sf::Vector2f(player.width, player.height));
playerShape.setPosition(player.x, player.y);
playerShape.setFillColor(sf::Color::Red);
window.draw(playerShape);
window.display();
}
return 0;
}
这里空空如也


















有帮助,赞一个