切水果游戏开发1
- 其他
- 2025-08-16 12:51:01

多数无益,上代码:
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