2D Minecraft
#include <iostream>
#include <vector>
#include <string>
#include <conio.h>
#include <windows.h>
#include <ctime>
using namespace std;
// ==================== 游戏配置 ====================
const int BLOCK_SIZE = 4; // 每个方块占用的字符数
const int WORLD_WIDTH = 120; // 世界宽度
const int WORLD_HEIGHT = 50; // 世界高度
// 屏幕大小(字符单位)
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 24;
// ==================== 方块类型 ====================
enum BlockType {
AIR,
GRASS,
DIRT,
STONE,
WOOD,
LEAF,
PLAYER
};
// 方块属性
struct BlockInfo {
char symbol;
int color; // 0-15
};
// ==================== 玩家结构 ====================
struct Player {
float x, y; // 位置
float vx, vy; // 速度
int width; // 宽度(字符)
int height; // 高度(字符)
bool onGround; // 是否在地面上
BlockType selectedBlock; // 选中的方块
};
// ==================== 全局变量 ====================
vector<vector<BlockType>> world(WORLD_HEIGHT, vector<BlockType>(WORLD_WIDTH, AIR));
Player player;
// ==================== 控制台工具函数 ====================
void gotoxy(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void hideCursor() {
CONSOLE_CURSOR_INFO info = {1, FALSE};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}
void clearScreen() {
system("cls");
}
// ==================== 方块信息 ====================
BlockInfo GetBlockInfo(BlockType type) {
switch(type) {
case GRASS: return {'#', 10}; // 绿色
case DIRT: return {'#', 6}; // 棕色
case STONE: return {'#', 8}; // 灰色
case WOOD: return {'|', 14}; // 黄色
case LEAF: return {'*', 2}; // 深绿色
case PLAYER: return {'P', 12}; // 红色
default: return {' ', 7}; // 空格
}
}
// ==================== 世界生成 ====================
void InitWorld() {
srand((unsigned)time(0));
}
// ==================== 方块操作 ====================
BlockType GetBlock(int x, int y) {
if (x >= 0 && x < WORLD_WIDTH && y >= 0 && y < WORLD_HEIGHT) {
return world[y][x];
}
return STONE;
}
void SetBlock(int x, int y, BlockType type) {
if (x >= 0 && x < WORLD_WIDTH && y >= 0 && y < WORLD_HEIGHT) {
world[y][x] = type;
}
}
// ==================== 物理更新 ====================
void UpdatePlayer() {
// 输入处理
if (GetAsyncKeyState('A') & 0x8000) {
player.vx = -0.3f;
} else if (GetAsyncKeyState('D') & 0x8000) {
player.vx = 0.3f;
} else {
player.vx *= 0.8f;
}
}
// ==================== 输入处理 ====================
void HandleInput() {
// 数字键选择方块
if (GetAsyncKeyState('1') & 0x8000) player.selectedBlock = GRASS;
if (GetAsyncKeyState('2') & 0x8000) player.selectedBlock = DIRT;
if (GetAsyncKeyState('3') & 0x8000) player.selectedBlock = STONE;
if (GetAsyncKeyState('4') & 0x8000) player.selectedBlock = WOOD;
if (GetAsyncKeyState('5') & 0x8000) player.selectedBlock = LEAF;
}
// ==================== 渲染 ====================
void Render() {
// 计算相机位置
int cameraX = floor(player.x) - SCREEN_WIDTH / 2;
int cameraY = floor(player.y) - SCREEN_HEIGHT / 2;
}
// ==================== 主函数 ====================
int main() {
// 初始化
hideCursor();
clearScreen();
}