o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
python写一个2D版的合成大西瓜,可直接运行 1.暂时用不同颜色的圆代替水果,发射间隔可以是0,可调。 2.碰撞体积等于水果图形体积,做好碰撞反弹。…
- User
python写一个2D版的合成大西瓜,可直接运行 1.暂时用不同颜色的圆代替水果,发射间隔可以是0,可调。 2.碰撞体积等于水果图形体积,做好碰撞反弹。 3.发射时横坐标取决于鼠标点击位置
- Assistant
python写一个2D版的合成大西瓜,可直接运行 1.暂时用不同颜色的圆代替水果,发射间隔可以是0,可调。 2.碰撞体积等于水果图形体积,做好碰撞反弹。 3.发射时横坐标取决于鼠标点击位置
以下是一个使用 Pygame 和 Pymunk 编写的 2D 版“合成大西瓜”游戏的完整代码。该程序可以直接运行,实现了您提出的要求:
要运行此代码,请确保已安装 pygame 和 pymunk 库。您可以使用以下命令安装:
pip install pygame pymunk
下面是完整代码:
import pygame
import pymunk
import pymunk.pygame_util
import random
# 初始化 Pygame
pygame.init()
# 设置屏幕尺寸
width, height = 600, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("合成大西瓜 - 2D版")
clock = pygame.time.Clock()
# 创建 Pymunk 空间
space = pymunk.Space()
space.gravity = (0, 900) # 设置重力加速度
draw_options = pymunk.pygame_util.DrawOptions(screen)
# 定义水果等级和对应的半径、颜色
fruit_levels = [
{"radius": 15, "color": (255, 0, 0)}, # 等级 0 - 红色
{"radius": 20, "color": (0, 255, 0)}, # 等级 1 - 绿色
{"radius": 25, "color": (0, 0, 255)}, # 等级 2 - 蓝色
{"radius": 30, "color": (255, 255, 0)}, # 等级 3 - 黄色
{"radius": 35, "color": (255, 165, 0)}, # 等级 4 - 橙色
{"radius": 40, "color": (128, 0, 128)}, # 等级 5 - 紫色
# 您可以根据需要添加更多等级
]
class Fruit:
def __init__(self, x, y, level):
self.level = level
self.radius = fruit_levels[level]["radius"]
self.color = fruit_levels[level]["color"]
mass = self.radius # 质量与半径成正比
moment = pymunk.moment_for_circle(mass, 0, self.radius)
self.body = pymunk.Body(mass, moment)
self.body.position = x, y
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.6
self.shape.friction = 0.5
# 将 Fruit 实例附加到形状,用于回调访问
self.shape.fruit = self
space.add(self.body, self.shape)
def spawn_fruit(x, level=0):
return Fruit(x, 50, level)
def fruit_collision(arbiter, space, data):
# 获取碰撞的两个形状
shapeA, shapeB = arbiter.shapes
fruitA = shapeA.fruit
fruitB = shapeB.fruit
# 检查两个水果是否为同一等级
if fruitA.level == fruitB.level:
# 移除旧的水果
space.remove(fruitA.body, fruitA.shape)
space.remove(fruitB.body, fruitB.shape)
# 计算新水果的位置(两个水果位置的平均值)
new_x = (fruitA.body.position.x + fruitB.body.position.x) / 2
new_y = (fruitA.body.position.y + fruitB.body.position.y) / 2
# 创建新等级的水果
new_level = min(fruitA.level + 1, len(fruit_levels) -1)
new_fruit = Fruit(new_x, new_y, new_level)
return True # 返回 True 以继续处理碰撞
# 为水果添加碰撞处理器
fruit_collision_handler = space.add_collision_handler(0, 0)
fruit_collision_handler.begin = fruit_collision
# 创建静态边界线(屏幕底部和两侧)
static_lines = [
pymunk.Segment(space.static_body, (0, height), (width, height), 0.0),
pymunk.Segment(space.static_body, (0, 0), (0, height), 0.0),
pymunk.Segment(space.static_body, (width, 0), (width, height), 0.0)
]
for line in static_lines:
line.elasticity = 0.8
line.friction = 1.0
space.add(static_lines)
running = True
fire_interval = 0 # 发射间隔(毫秒)
last_fire_time = 0 # 上一次发射时间
while running:
dt = 1 / 60.0 # 60 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标点击位置
x, y = pygame.mouse.get_pos()
current_time = pygame.time.get_ticks()
if current_time - last_fire_time >= fire_interval:
spawn_fruit(x)
last_fire_time = current_time
# 更新物理引擎
space.step(dt)
# 清屏
screen.fill((255, 255, 255))
# 绘制水果
for shape in space.shapes:
if isinstance(shape, pymunk.Circle):
body = shape.body
x, y = body.position
radius = shape.radius
fruit = shape.fruit
color = fruit.color
pygame.draw.circle(screen, color, (int(x), int(y)), int(radius))
pygame.display.flip()
clock.tick(60)
pygame.quit()
代码说明:
y=50)。fire_interval 变量来调整发射间隔,单位为毫秒。例如,将其设置为 500,则每隔 0.5 秒才能发射一个水果。运行效果:
注意事项:
fruit_levels 列表中添加更多等级的水果。希望这个代码符合您的要求,祝您游戏愉快!
Not yet included in a published release.