酷代码 AI
菜单
服务商
DeepSeek字节豆包
更多选项

Python Pygame 我的世界简易版:方块放置与移动玩法实现

好的!我可以帮你创建一个简化版的“我的世界”(Minecraft)风格的游戏雏形,使用 **Python + Pygame** 实现基本的方块放置、移动和3D透视效果(伪3D或直接2D俯视图)。 这里我为你提供一个 **2D俯视图版本**(类似“我的世界”经典俯视图的简化版),包含: - 玩家移动(WASD) - 放置和破坏方块(鼠标点击) - 简单的世界生成(草地、石头、树木) --- ## 🔧 准备工作 ### 1. 安装依赖 ```bash pip install pygame ``` ### 2. 运行环境 - Python 3.7+ - 推荐使用虚拟环境 --- ## 🧱 核心代码 ```python import pygame import random # 初始化 Pygame pygame.init() # 屏幕设置 WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("我的世界 - 简易版") # 颜色定义 COLORS = { "sky": (135, 206, 235), "grass": (34, 139, 34), "dirt": (139, 90, 43), "stone": (128, 128, 128), "wood": (139, 90, 43), "leaf": (50, 205, 50), "player": (255, 0, 0), "highlight": (255, 255, 255, 100) } # 方块大小 BLOCK_SIZE = 40 # 玩家设置 player_pos = [WIDTH // 2, HEIGHT // 2] player_speed = 5 # 世界数据(简单生成) world = {} for x in range(-10, 11): for y in range(-10, 11): # 基础地形:草地+石头混合 if random.random() < 0.1: world[(x, y)] = "stone" elif random.random() < 0.05: world[(x, y)] = "wood" # 树干 elif random.random() < 0.03: world[(x, y)] = "leaf" # 树叶 else: world[(x, y)] = "grass" # 当前选中的方块类型(放置时使用) current_block = "grass" # 游戏主循环 running = True clock = pygame.time.Clock() def draw_world(offset_x, offset_y): """绘制所有方块""" for (bx, by), block_type in world.items(): screen_x = bx * BLOCK_SIZE + offset_x screen_y = by * BLOCK_SIZE + offset_y if -BLOCK_SIZE <= screen_x <= WIDTH and -BLOCK_SIZE <= screen_y <= HEIGHT: pygame.draw.rect(screen, COLORS[block_type], (screen_x, screen_y, BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(screen, (0, 0, 0), (screen_x, screen_y, BLOCK_SIZE, BLOCK_SIZE), 1) def draw_player(offset_x, offset_y): """绘制玩家""" px = player_pos[0] + offset_x py = player_pos[1] + offset_y pygame.draw.circle(screen, COLORS["player"], (int(px), int(py)), 10) def get_block_at_mouse(mouse_pos, offset_x, offset_y): """获取鼠标所在的世界方块坐标""" mx, my = mouse_pos bx = (mx - offset_x) // BLOCK_SIZE by = (my - offset_y) // BLOCK_SIZE return (bx, by) while running: screen.fill(COLORS["sky"]) # 计算世界偏移(使玩家始终居中) offset_x = WIDTH // 2 - player_pos[0] offset_y = HEIGHT // 2 - player_pos[1] # 事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 鼠标点击事件 if event.type == pygame.MOUSEBUTTONDOWN: block_pos = get_block_at_mouse(pygame.mouse.get_pos(), offset_x, offset_y) if event.button == 1: # 左键破坏 if block_pos in world: del world[block_pos] elif event.button == 3: # 右键放置 world[block_pos] = current_block # 按键切换方块类型 if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: current_block = "grass" elif event.key == pygame.K_2: current_block = "stone" elif event.key == pygame.K_3: current_block = "wood" elif event.key == pygame.K_4: current_block = "leaf" # 玩家移动 keys = pygame.key.get_pressed() if keys[pygame.K_w]: player_pos[1] -= player_speed if keys[pygame.K_s]: player_pos[1] += player_speed if keys[pygame.K_a]: player_pos[0] -= player_speed if keys[pygame.K_d]: player_pos[0] += player_speed # 绘制世界 draw_world(offset_x, offset_y) # 绘制玩家 draw_player(offset_x, offset_y) # 显示当前选择的方块 font = pygame.font.Font(None, 36) text = font.render(f"方块: {current_block} (1-草 2-石 3-木 4-叶)", True, (0, 0, 0)) screen.blit(text, (10, 10)) # 鼠标高亮 if 0 <= pygame.mouse.get_pos()[0] <= WIDTH and 0 <= pygame.mouse.get_pos()[1] <= HEIGHT: block_pos = get_block_at_mouse(pygame.mouse.get_pos(), offset_x, offset_y) highlight_x = block_pos[0] * BLOCK_SIZE + offset_x highlight_y = block_pos[1] * BLOCK_SIZE + offset_y highlight_surf = pygame.Surface((BLOCK_SIZE, BLOCK_SIZE), pygame.SRCALPHA) highlight_surf.fill(COLORS["highlight"]) screen.blit(highlight_surf, (highlight_x, highlight_y)) pygame.display.flip() clock.tick(60) pygame.quit() ``` --- ## 🎮 操作说明 | 按键/鼠标 | 功能 | |-----------|------| | WASD | 移动玩家 | | 鼠标左键 | 破坏方块 | | 鼠标右键 | 放置方块 | | 1 | 切换草地 | | 2 | 切换石头 | | 3 | 切换木头 | | 4 | 切换树叶 | --- ## 🔍 进阶开发建议 1. **增加3D效果**:可以使用 `pygame` 的 3D 投影(或直接集成 `PyOpenGL`)。 2. **保存/加载世界**:将 `world` 字典序列化为 JSON 或 pickle 文件。 3. **生物与物品系统**:添加怪物、树木砍伐、矿石采集等机制。 4. **光照系统**:实现简单的光照衰减,增加沉浸感。 5. **多人联网**:使用 `socket` 或 `asyncio` 实现局域网联机。 --- ## 📚 学习资源推荐 - [Pygame官方文档](https://www.pygame.org/docs/) - [Minecraft Wiki - 生成与地形](https://minecraft.fandom.com/zh/wiki/%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%96%E7%95%8C) - [Python 游戏开发入门 - 菜鸟教程](https://www.runoob.com/python/python-game.html) --- ## ⚠️ 常见问题 **Q: 为什么方块不显示?** A: 检查 offset 计算是否正确,建议在 draw_world 函数中添加 `print(block_pos)` 调试。 **Q: 如何增加更多方块类型?** A: 在 `COLORS` 字典中添加新颜色,并在 `world` 生成或按键切换时使用新类型。 **Q: 世界生成太随机怎么办?** A: 使用 Perlin 噪声或单纯形噪声算法生成更自然的地形。 --- ######[AI写代码神器 | 1911点数解答 | 2026-07-12 17:15:26]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]