chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
帮我制作一个小兔快跑的游戏
- User
帮我制作一个小兔快跑的游戏
- Assistant
当然可以!以下是一个简单的“小兔快跑”游戏的实现,我们使用 Python 和
pygame库来制作这个 2D 横版小游戏。游戏目标:小兔子左右移动、跳跃,躲避障碍物并收集胡萝卜!
🐰 游戏功能简介:
- 小兔子可以左右移动、跳跃;
- 有落地检测;
- 胡萝卜可收集,增加得分;
- 障碍物接触会减少生命并重新开始;
- 游戏界面简洁可爱。
🧾 第一步:准备工作
📦 需要安装的库:
pip install pygame
🧩 第二步:游戏代码(完整)
import pygame import random import sys # 初始化 pygame.init() # 屏幕设置 WIDTH, HEIGHT = 800, 600 win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("小兔快跑 🐰") # 时钟 clock = pygame.time.Clock() FPS = 60 # 颜色 WHITE = (255, 255, 255) GREEN = (34, 177, 76) BLACK = (0, 0, 0) ORANGE = (255, 128, 0) RED = (255, 0, 0) # 字体 font = pygame.font.SysFont("Arial", 28) # 小兔子参数 player_width, player_height = 50, 50 player_x = WIDTH // 2 player_y = HEIGHT - player_height - 60 player_vel = 5 jumping = False jump_vel = 10 gravity = 0.5 fall_speed = 0 lives = 3 score = 0 # 胡萝卜 carrot_width, carrot_height = 30, 30 carrot_list = [] # 障碍物 obstacle_width, obstacle_height = 50, 50 obstacle_list = [] # 地面 ground_y = HEIGHT - 50 # 加载图片(你可以换成实际图片路径) # player_img = pygame.image.load("rabbit.png") # carrot_img = pygame.image.load("carrot.png") def draw_player(x, y): pygame.draw.rect(win, WHITE, (x, y, player_width, player_height)) def draw_carrot(x, y): pygame.draw.ellipse(win, ORANGE, (x, y, carrot_width, carrot_height)) def draw_obstacle(x, y): pygame.draw.rect(win, RED, (x, y, obstacle_width, obstacle_height)) # 生成新的胡萝卜 def spawn_carrot(): x = random.randint(0, WIDTH - carrot_width) y = random.randint(0, ground_y - 100) carrot_list.append([x, y]) # 生成新的障碍物 def spawn_obstacle(): x = random.randint(0, WIDTH - obstacle_width) obstacle_list.append([x, ground_y - obstacle_height]) # 主游戏循环 running = True spawn_timer = 0 obstacle_timer = 0 while running: clock.tick(FPS) win.fill(GREEN) pygame.draw.rect(win, BLACK, (0, ground_y, WIDTH, 50)) # 地面 # 事件检测 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 按键检测 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x - player_vel > 0: player_x -= player_vel if keys[pygame.K_RIGHT] and player_x + player_vel < WIDTH - player_width: player_x += player_vel if keys[pygame.K_SPACE]: if not jumping: jumping = True fall_speed = -jump_vel # 跳跃 & 重力 if jumping: player_y += fall_speed fall_speed += gravity if player_y >= ground_y - player_height: player_y = ground_y - player_height jumping = False # 生成胡萝卜 spawn_timer += 1 if spawn_timer > 90: spawn_carrot() spawn_timer = 0 # 胡萝卜移动 & 检测碰撞 for carrot in carrot_list[:]: if player_x < carrot[0] + carrot_width and player_x + player_width > carrot[0] \ and player_y < carrot[1] + carrot_height and player_y + player_height > carrot[1]: carrot_list.remove(carrot) score += 1 else: draw_carrot(*carrot) # 生成障碍物 obstacle_timer += 1 if obstacle_timer > 100: spawn_obstacle() obstacle_timer = 0 for obstacle in obstacle_list[:]: obstacle[0] -= 3 if obstacle[0] < -obstacle_width: obstacle_list.remove(obstacle) elif player_x < obstacle[0] + obstacle_width and player_x + player_width > obstacle[0] \ and player_y + player_height > obstacle[1]: obstacle_list.clear() carrot_list.clear() player_x = WIDTH // 2 lives -= 1 if lives <= 0: pygame.quit() sys.exit() else: draw_obstacle(*obstacle) # 绘制角色 draw_player(player_x, player_y) # 显示分数和生命 score_text = font.render(f"分数:{score}", True, BLACK) life_text = font.render(f"生命:{lives}", True, RED) win.blit(score_text, (10, 10)) win.blit(life_text, (10, 40)) pygame.display.update() pygame.quit()
📁 第三步:美术资源说明(可选)
如果你有小兔子、胡萝卜、障碍物的 PNG 图片,可以替换
draw_函数中的绘图代码为图片贴图:rabbit_img = pygame.image.load("rabbit.png") win.blit(rabbit_img, (x, y))
✨ 如果你希望更高级怎么办?
- 加背景音乐、跳跃音效;
- 添加关卡系统;
- 使用 Tiled 配置地图;
- 添加排行榜和存档功能。
如果你希望用 HTML5 + JS 写浏览器版本的小兔快跑,我也可以帮你写一个版本哦!
你要不要试试网页版本?或者继续增强 Python 版?👀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.