• 切水果游戏开发1


    多数无益,上代码

    1. import pygame
    2. import random
    3. # 初始化pygame
    4. pygame.init()
    5. # 设置窗口尺寸
    6. window_width = 800
    7. window_height = 600
    8. window_size = (window_width, window_height)
    9. window = pygame.display.set_mode(window_size)
    10. # 设置窗口标题
    11. pygame.display.set_caption("切水果游戏")
    12. # 加载背景音乐和切水果音效
    13. # pygame.mixer.music.load("background_music.mp3")
    14. slice_sound = pygame.mixer.Sound("music.mp3")
    15. fruit_images = {
    16. "apple": pygame.transform.scale(pygame.image.load("apple.png"), (100, 100)),
    17. "banana": pygame.transform.scale(pygame.image.load("banana.png"), (100, 100)),
    18. "orange": pygame.transform.scale(pygame.image.load("orange.png"), (100, 100)),
    19. "watermelon": pygame.transform.scale(pygame.image.load("watermelon.png"), (100, 100)),
    20. "strawberry": pygame.transform.scale(pygame.image.load("strawberry.png"), (100, 100))
    21. }
    22. # 定义水果颜色
    23. fruit_colors = {
    24. "apple": (255, 0, 0), # 红色
    25. "banana": (255, 255, 0), # 黄色
    26. "orange": (255, 165, 0), # 橙色
    27. "watermelon": (0, 255, 0), # 绿色
    28. "strawberry": (255, 0, 255) # 紫色
    29. }
    30. # 定义粒子颜色
    31. particle_colors = {
    32. "apple": (255, 0, 0), # 红色
    33. "banana": (255, 255, 0), # 黄色
    34. "orange": (255, 165, 0), # 橙色
    35. "watermelon": (0, 255, 0), # 绿色
    36. "strawberry": (255, 0, 255) # 紫色
    37. }
    38. # 定义水果初始位置和速度
    39. fruit_initial_x = []
    40. fruit_initial_y = []
    41. fruit_speed = []
    42. for i in range(5):
    43. fruit_initial_x.append(random.randint(50, window_width - 50))
    44. fruit_initial_y.append(-100)
    45. fruit_speed.append(random.randint(1, 5))
    46. # 定义切水果得分
    47. score = 0
    48. # 定义粒子系统
    49. particles = []
    50. class Particle:
    51. def __init__(self, x, y, color):
    52. self.x = x
    53. self.y = y
    54. self.color = color
    55. self.radius = 5
    56. self.speed_x = random.randint(-5, 5)
    57. self.speed_y = random.randint(-15, -5)
    58. self.alpha = 255
    59. def update(self):
    60. self.x += self.speed_x
    61. self.y += self.speed_y
    62. self.alpha -= 10
    63. def draw(self):
    64. color = tuple(max(0, min(c, 255)) for c in self.color)
    65. alpha = max(0, min(self.alpha, 255))
    66. pygame.draw.circle(window, color + (alpha,), (int(self.x), int(self.y)), self.radius)
    67. # 游戏主循环
    68. running = True
    69. clock = pygame.time.Clock()
    70. # pygame.mixer.music.play(-1)
    71. while running:
    72. # 填充窗口背景色
    73. window.fill((0, 0, 0))
    74. # 处理事件
    75. for event in pygame.event.get():
    76. if event.type == pygame.QUIT:
    77. running = False
    78. elif event.type == pygame.MOUSEBUTTONDOWN:
    79. # 获取鼠标位置
    80. mouse_x, mouse_y = pygame.mouse.get_pos()
    81. # 判断鼠标是否划过水果
    82. for i in range(5):
    83. if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
    84. # 播放切水果音效
    85. slice_sound.play()
    86. # 增加得分
    87. score += 1
    88. # 生成粒子效果
    89. for _ in range(20):
    90. particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
    91. # 重新生成水果位置和速度
    92. fruit_initial_x[i] = random.randint(50, window_width - 50)
    93. fruit_initial_y[i] = -100
    94. fruit_speed[i] = random.randint(1, 5)
    95. for event in pygame.event.get():
    96. if event.type == pygame.QUIT:
    97. running = False
    98. elif event.type == pygame.MOUSEBUTTONDOWN:
    99. # 获取鼠标位置
    100. mouse_x, mouse_y = pygame.mouse.get_pos()
    101. # 判断鼠标是否在窗口范围内
    102. if 0 <= mouse_x <= window_width and 0 <= mouse_y <= window_height:
    103. # 判断鼠标是否划过水果
    104. for i in range(5):
    105. if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
    106. # 播放切水果音效
    107. slice_sound.play()
    108. # 增加得分
    109. score += 1
    110. # 生成粒子效果
    111. for _ in range(20):
    112. particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
    113. # 重新生成水果位置和速度
    114. fruit_initial_x[i] = random.randint(50, window_width - 50)
    115. fruit_initial_y[i] = -100
    116. fruit_speed[i] = random.randint(1, 5)
    117. # 获取鼠标位置
    118. mouse_x, mouse_y = pygame.mouse.get_pos()
    119. # 绘制水果
    120. for i in range(5):
    121. fruit_initial_y[i] += fruit_speed[i]
    122. window.blit(fruit_images[list(fruit_images.keys())[i]], (fruit_initial_x[i], fruit_initial_y[i]))
    123. # 判断鼠标是否划过水果
    124. if fruit_initial_x[i] <= mouse_x <= fruit_initial_x[i] + 100 and fruit_initial_y[i] <= mouse_y <= fruit_initial_y[i] + 100:
    125. # 播放切水果音效
    126. slice_sound.play()
    127. # 增加得分
    128. score += 1
    129. # 生成粒子效果
    130. for _ in range(20):
    131. particles.append(Particle(fruit_initial_x[i] + 50, fruit_initial_y[i] + 50, particle_colors[list(fruit_images.keys())[i]]))
    132. # 重新生成水果位置和速度
    133. fruit_initial_x[i] = random.randint(50, window_width - 50)
    134. fruit_initial_y[i] = -100
    135. fruit_speed[i] = random.randint(1, 5)
    136. # 更新和绘制粒子效果
    137. for particle in particles:
    138. particle.update()
    139. particle.draw()
    140. if particle.alpha <= 0:
    141. particles.remove(particle)
    142. # pass
    143. # 绘制得分
    144. font = pygame.font.Font(None, 36)
    145. text = font.render("Score: " + str(score), True, (255, 255, 255))
    146. window.blit(text, (10, 10))
    147. # 更新窗口
    148. pygame.display.flip()
    149. # 控制帧率
    150. clock.tick(60)
    151. # 退出游戏
    152. pygame.quit()

    工程目录:

    效果:

    遗留问题:

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

    2,没背景音乐

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

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

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

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

  • 相关阅读:
    nodejs+vue健身房课程预约评分系统
    Python多进程 multiprocessing
    Python 及 Pycharm 的安装 2023.8
    mybatis-plus 根据任意字段saveOrUpdateBatch
    【贝叶斯回归】【第 1 部分】--pyro库应用
    聊聊Java 序列化和反序列化为什么要实现 Serializable 接口?
    全球生物气候产品2.5m和30s分辨率
    java基于ssm的农村留守儿童社会支持信息系统#毕业设计
    为 Visual Paradigm 的应用程序收集 JVM 线程转储
    Houdini HDA参数无法删除
  • 原文地址:https://blog.csdn.net/k1419197516/article/details/133936994