当前位置: 首页 > news >正文

切水果游戏开发1

多数无益,上代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置窗口尺寸
window_width = 800
window_height = 600
window_size = (window_width, window_height)
window = pygame.display.set_mode(window_size)

# 设置窗口标题
pygame.display.set_caption("切水果游戏")

# 加载背景音乐和切水果音效
# pygame.mixer.music.load("background_music.mp3")
slice_sound = pygame.mixer.Sound("music.mp3")


fruit_images = {
    "apple": pygame.transform.scale(pygame.image.load("apple.png"), (100, 100)),
    "banana": pygame.transform.scale(pygame.image.load("banana.png"), (100, 100)),
    "orange": pygame.transform.scale(pygame.image.load("orange.png"), (100, 100)),
    "watermelon": pygame.transform.scale(pygame.image.load("watermelon.png"), (100, 100)),
    "strawberry": pygame.transform.scale(pygame.image.load("strawberry.png"), (100, 100))
}

# 定义水果颜色
fruit_colors = {
    "apple": (255, 0, 0),       # 红色
    "banana": (255, 255, 0),    # 黄色
    "orange": (255, 165, 0),    # 橙色
    "watermelon": (0, 255, 0),  # 绿色
    "strawberry": (255, 0, 255) # 紫色
}

# 定义粒子颜色
particle_colors = {
    "apple": (255, 0, 0),       # 红色
    "banana": (255, 255, 0),    # 黄色
    "orange": (255, 165, 0),    # 橙色
    "watermelon": (0, 255, 0),  # 绿色
    "strawberry": (255, 0, 255) # 紫色
}

# 定义水果初始位置和速度
fruit_initial_x = []
fruit_initial_y = []
fruit_speed = []
for i in range(5):
    fruit_initial_x.append(random.randint(50, window_width - 50))
    fruit_initial_y.append(-100)
    fruit_speed.append(random.randint(1, 5))

# 定义切水果得分
score = 0

# 定义粒子系统
particles = []

class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.radius = 5
        self.speed_x = random.randint(-5, 5)
        self.speed_y = random.randint(-15, -5)
        self.alpha = 255

    def update(self):
        self.x += self.speed_x
        self.y += self.speed_y
        self.alpha -= 10

    def draw(self):
        color = tuple(max(0, min(c, 255)) for c in self.color)
        alpha = max(0, min(self.alpha, 255))
        pygame.draw.circle(window, color + (alpha,), (int(self.x), int(self.y)), self.radius)

# 游戏主循环
running = True
clock = pygame.time.Clock()
# pygame.mixer.music.play(-1)

while running:
    # 填充窗口背景色
    window.fill((0, 0, 0))

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 获取鼠标位置
            mouse_x, mouse_y = pygame.mouse.get_pos()
            # 判断鼠标是否划过水果
            for i in range(5):
                if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
                    # 播放切水果音效
                    slice_sound.play()
                    # 增加得分
                    score += 1
                    # 生成粒子效果
                    for _ in range(20):
                        particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
                    # 重新生成水果位置和速度
                    fruit_initial_x[i] = random.randint(50, window_width - 50)
                    fruit_initial_y[i] = -100
                    fruit_speed[i] = random.randint(1, 5)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 获取鼠标位置
            mouse_x, mouse_y = pygame.mouse.get_pos()
            # 判断鼠标是否在窗口范围内
            if 0 <= mouse_x <= window_width and 0 <= mouse_y <= window_height:
                # 判断鼠标是否划过水果
                for i in range(5):
                    if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
                        # 播放切水果音效
                        slice_sound.play()
                        # 增加得分
                        score += 1
                        # 生成粒子效果
                        for _ in range(20):
                            particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
                        # 重新生成水果位置和速度
                        fruit_initial_x[i] = random.randint(50, window_width - 50)
                        fruit_initial_y[i] = -100
                        fruit_speed[i] = random.randint(1, 5)


    # 获取鼠标位置
    mouse_x, mouse_y = pygame.mouse.get_pos()

    # 绘制水果
    for i in range(5):
        fruit_initial_y[i] += fruit_speed[i]
        window.blit(fruit_images[list(fruit_images.keys())[i]], (fruit_initial_x[i], fruit_initial_y[i]))

        # 判断鼠标是否划过水果
        if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
            # 播放切水果音效
            slice_sound.play()
            # 增加得分
            score += 1
            # 生成粒子效果
            for _ in range(20):
                particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
            # 重新生成水果位置和速度
            fruit_initial_x[i] = random.randint(50, window_width - 50)
            fruit_initial_y[i] = -100
            fruit_speed[i] = random.randint(1, 5)

    # 更新和绘制粒子效果
    for particle in particles:
        particle.update()
        particle.draw()
        if particle.alpha <= 0:
            particles.remove(particle)
        # pass

    # 绘制得分
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, (255, 255, 255))
    window.blit(text, (10, 10))

    # 更新窗口
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出游戏
pygame.quit()

工程目录:

效果:

遗留问题:

1,碰到就切碎了,不少,要点击+碰到好些

2,没背景音乐

3,没搞个地雷出来,切中就减分或结束的那种

4,没排名,应该搞个排名出来

5,速度应该要递增,错过要减分的那种

工程地址:game: 游戏仓库 - Gitee.com

相关文章:

  • 关于电路的输入阻抗与输出阻抗的理解
  • 面试算法32:有效的变位词
  • 【论文阅读】基于卷积神经的端到端无监督变形图像配准
  • SQLAlchemy学习-12.查询之 order_by 按desc 降序排序
  • 竞赛选题 深度学习YOLO安检管制物品识别与检测 - python opencv
  • STM32内部flash闪存的总结
  • 数字图像处理实验记录四(图像的空间域增强-平滑处理)
  • 云计算认证有哪些?认证考了有什么用?
  • 华为智选SF5,AITO问界的车怎么样
  • 使用Gitlab构建简单流水线CI/CD
  • 【数据结构】顺序表
  • 由Django-Session配置引发的反序列化安全问题
  • 留学教育咨询机构如何通过软文强势突围
  • langchain sql agent 案例
  • RUST持续学习 一点borrow问题的心得记录
  • GitHub-使用 Git工具 创建密钥id_rsa.pub
  • Redis 主从复制,哨兵,集群——(2)哨兵篇
  • 如何在Android项目中制作和使用三方包(jar文件)
  • Kylin麒麟操作系统(基于Linux)下的环境变量配置,重启生效
  • 【网络协议】聊聊拓扑网络结构与原理
  • 专家分析丨乌美签署矿产协议,展现美外交困境下的无奈
  • 国务院安委办、应急管理部进一步调度部署“五一”假期安全防范工作
  • 零食连锁鸣鸣很忙递表港交所:去年营收393亿元,门店超1.4万家,净利润率2.1%
  • 总书记考察的上海“模速空间”,要打造什么样的“全球最大”?
  • TAE联手加州大学开发出新型核聚变装置:功率提升百倍,成本减半
  • “杭州六小龙”的招聘迷局