Pygame实现种树游戏:种树、合成与买卖乐趣多
以下是一个使用 Pygame 实现种树游戏的示例代码,该游戏包含种树场和家园两个场景,实现了种树、果实合成、买卖等功能。 ```python import pygame import random # 初始化 Pygame pygame.init() # 游戏窗口设置 WIDTH, HEIGHT = 800, 600 win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("种树游戏") # 颜色定义 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) BROWN = (139, 69, 19) # 果实等级信息 FRUIT_LEVELS = [ "葡萄", "草莓", "桃子", "苹果", "香蕉", "西瓜", "椰子", "榴莲", "大西瓜", "大榴莲", "星星糖" ] # 种子价格 SEED_PRICE = 10 # 肥料价格 FERTILIZER_PRICE = 20 # 水的价格 WATER_PRICE = 15 # 初始金钱 money = 100 # 树的初始高度 tree_height = 0 # 树的叶子数量 leaf_count = 0 # 树的果实列表 fruits = [] # 家园中的果实列表 home_fruits = [] # 场景切换标志 is_home = False # 绘制树 def draw_tree(): trunk_width = 20 trunk_x = WIDTH // 2 - trunk_width // 2 trunk_y = HEIGHT - tree_height pygame.draw.rect(win, BROWN, (trunk_x, trunk_y, trunk_width, tree_height)) # 绘制叶子 leaf_size = 20 for i in range(leaf_count): leaf_x = trunk_x - leaf_size // 2 + random.randint(-10, 10) leaf_y = HEIGHT - (i + 1) * 5 * 10 - leaf_size // 2 pygame.draw.ellipse(win, GREEN, (leaf_x, leaf_y, leaf_size, leaf_size)) # 绘制果实 fruit_size = 30 for fruit in fruits: fruit_x = trunk_x - fruit_size // 2 + random.randint(-20, 20) fruit_y = HEIGHT - fruit[1] - fruit_size // 2 font = pygame.font.Font(None, 24) text = font.render(FRUIT_LEVELS[fruit[0]], True, (255, 0, 0)) win.blit(text, (fruit_x, fruit_y)) # 绘制家园 def draw_home(): font = pygame.font.Font(None, 24) text = font.render("家园", True, BLACK) win.blit(text, (WIDTH // 2 - 20, 20)) # 绘制家园中的果实 fruit_size = 30 for i, fruit in enumerate(home_fruits): fruit_x = 50 + (i % 5) * 100 fruit_y = 100 + (i // 5) * 50 text = font.render(FRUIT_LEVELS[fruit], True, (255, 0, 0)) win.blit(text, (fruit_x, fruit_y)) # 绘制界面信息 def draw_info(): font = pygame.font.Font(None, 24) money_text = font.render(f"金钱: {money}", True, BLACK) win.blit(money_text, (20, 20)) if not is_home: tree_height_text = font.render(f"树高: {tree_height}mm", True, BLACK) win.blit(tree_height_text, (20, 50)) # 购买种子 def buy_seed(): global money if money >= SEED_PRICE: money -= SEED_PRICE return True return False # 购买肥料 def buy_fertilizer(): global money, tree_height, leaf_count if money >= FERTILIZER_PRICE: money -= FERTILIZER_PRICE growth = random.randint(2, 6) tree_height += growth check_growth() return True return False # 购买水 def buy_water(): global money, tree_height, leaf_count if money >= WATER_PRICE: money -= WATER_PRICE growth = random.randint(2, 3) tree_height += growth check_growth() return True return False # 检查树的生长情况 def check_growth(): global leaf_count, fruits new_leaf_count = tree_height // 5 if new_leaf_count > leaf_count: leaf_count = new_leaf_count new_fruit_count = tree_height // 10 while len(fruits) < new_fruit_count: fruit_level = min(len(FRUIT_LEVELS) - 1, tree_height // 10 - 1) fruits.append((fruit_level, tree_height)) # 采摘果实 def pick_fruit(): global fruits, home_fruits if fruits: fruit = fruits.pop() home_fruits.append(fruit[0]) # 合成果实 def synthesize_fruits(): global home_fruits for i in range(len(home_fruits)): for j in range(i + 1, len(home_fruits)): if home_fruits[i] == home_fruits[j] and home_fruits[i] < len(FRUIT_LEVELS) - 1: new_level = min(home_fruits[i] + 1, len(FRUIT_LEVELS) - 1) home_fruits.pop(j) home_fruits.pop(i) home_fruits.append(new_level) return True return False # 出售果实 def sell_fruits(): global money, home_fruits if home_fruits: fruit = home_fruits.pop() price = (fruit + 1) * 10 money += price # 主循环 clock = pygame.time.Clock() running = True while running: clock.tick(60) win.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if not is_home: if event.key == pygame.K_s: if buy_seed(): tree_height = 0 leaf_count = 0 fruits = [] elif event.key == pygame.K_f: buy_fertilizer() elif event.key == pygame.K_w: buy_water() elif event.key == pygame.K_p: pick_fruit() elif event.key == pygame.K_h: is_home = True else: if event.key == pygame.K_s: sell_fruits() elif event.key == pygame.K_c: synthesize_fruits() elif event.key == pygame.K_b: is_home = False if is_home: draw_home() else: draw_tree() draw_info() pygame.display.update() pygame.quit() ``` ### 🔧 准备工作 - **依赖安装**:确保你已经安装了 Pygame 库,可以使用以下命令进行安装: ```bash pip install pygame ``` - **版本要求**:Pygame 建议使用最新稳定版本。 ### 📚 代码解释 1. **初始化部分**:初始化 Pygame 并设置游戏窗口的大小和,定义颜色、果实等级信息、价格等常量。 2. **绘制函数**:`draw_tree` 函数用于绘制树的树干、叶子和果实;`draw_home` 函数用于绘制家园界面;`draw_info` 函数用于绘制界面信息,如金钱、树高。 3. **购买和生长函数**:`buy_seed`、`buy_fertilizer` 和 `buy_water` 函数分别用于购买种子、肥料和水,并更新树的高度;`check_growth` 函数用于检查树的生长情况,根据树的高度生成叶子和果实。 4. **采摘、合成和出售函数**:`pick_fruit` 函数用于采摘果实并将其放入家园;`synthesize_fruits` 函数用于合成家园中的果实;`sell_fruits` 函数用于出售家园中的果实并获得金钱。 5. **主循环**:处理用户的输入事件,根据用户的操作调用相应的函数,并更新游戏界面。 ### 💡 开发建议 - **代码优化**:可以将一些重复的代码封装成函数,提高代码的可读性和可维护性。 - **界面美化**:可以使用图片代替绘制的图形,使游戏界面更加美观。 - ********:可以增加更多的道具和玩法,如不同类型的种子、特殊的肥料等,丰富游戏内容。 ######[AI写代码神器 | 2138点数解答 | 2026-06-08 15:39:01]
- 修正代码实现“3D爱心粒子跳动”,附运行错误及逻辑注意点(字节豆包 | 1111点数解答 | 2025-07-28 22:34:46)172
- Python 实现 3D 爱心粒子跳动特效:代码解析与注意要点(字节豆包 | 1142点数解答 | 2025-07-28 22:36:37)178
- Python生日祝福爱心粒子特效代码优化与开发建议(字节豆包 | 2223点数解答 | 2026-03-21 14:32:57)36
- 超燃年会来袭!诗舞歌戏轮番上阵,共赴欢乐盛宴! (字节豆包 | 206点数解答 | 2025-01-08 10:59:43)381
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)418
- Python 报错:for 语句后缩进缺失,附修正示例与注意要点(字节豆包 | 170点数解答 | 2025-03-19 21:49:55)220
- Python Pygame库介绍与简单示例代码(字节豆包 | 583点数解答 | 2026-02-15 10:09:00)40
- Pygame种树游戏中按键检测代码含义及开发建议(字节豆包 | 676点数解答 | 2026-06-08 15:43:10)5
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)376
- 探秘各类游戏:多样玩法带来不同乐趣!(字节豆包 | 77点数解答 | 2025-05-04 01:10:38)203
- Python代码实现:带窗口、滚轴、重力、惯性与怪物的类马里奥游戏(字节豆包 | 964点数解答 | 2025-06-13 19:11:12)233
- 游戏大观:从类型到平台,追溯其发展与魅力!(字节豆包 | 268点数解答 | 2025-07-10 17:57:48)214