用python生成一个可以运行的解压小游戏
- User
用python生成一个可以运行的解压小游戏
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
用python生成一个可以运行的解压小游戏
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我来为你创建一个简单有趣的解压小游戏。这个游戏包含多种解压元素:
import pygame
import random
import math
import sys
# 初始化pygame
pygame.init()
# 设置窗口
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("解压小游戏")
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (100, 150, 255)
GREEN = (100, 255, 100)
RED = (255, 100, 100)
YELLOW = (255, 255, 100)
PURPLE = (200, 100, 255)
ORANGE = (255, 150, 100)
# 气泡类
class Bubble:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = random.randint(20, 50)
self.color = random.choice([BLUE, GREEN, RED, YELLOW, PURPLE, ORANGE])
self.popped = False
self.pop_animation = 0
def draw(self, screen):
if not self.popped:
# 绘制气泡
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.radius, 3)
# 高光效果
pygame.draw.circle(screen, WHITE, (int(self.x - self.radius//3), int(self.y - self.radius//3)), self.radius//4)
else:
# 爆炸动画
if self.pop_animation < 20:
for i in range(8):
angle = i * math.pi / 4
x = self.x + math.cos(angle) * self.pop_animation * 2
y = self.y + math.sin(angle) * self.pop_animation * 2
pygame.draw.circle(screen, self.color, (int(x), int(y)), max(1, 5 - self.pop_animation//4))
self.pop_animation += 1
def check_click(self, mouse_pos):
if not self.popped:
distance = math.sqrt((mouse_pos[0] - self.x)**2 + (mouse_pos[1] - self.y)**2)
if distance <= self.radius:
self.popped = True
return True
return False
# 粒子类(用于点击效果)
class Particle:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random.uniform(-5, 5)
self.vy = random.uniform(-5, 5)
self.life = 30
self.color = random.choice([BLUE, GREEN, RED, YELLOW, PURPLE])
def update(self):
self.x += self.vx
self.y += self.vy
self.vy += 0.2 # 重力
self.life -= 1
def draw(self, screen):
if self.life > 0:
alpha = self.life / 30
size = max(1, int(3 * alpha))
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), size)
# 游戏主类
class RelaxGame:
def __init__(self):
self.bubbles = []
self.particles = []
self.score = 0
self.font = pygame.font.Font(None, 36)
self.mode = "bubble" # bubble, sand, wave
self.sand_particles = []
self.wave_points = []
self.init_sand()
self.init_wave()
def init_sand(self):
# 初始化沙粒
for _ in range(200):
x = random.randint(0, WIDTH)
y = random.randint(HEIGHT//2, HEIGHT)
self.sand_particles.append([x, y, random.choice([YELLOW, ORANGE, (255, 200, 100)])])
def init_wave(self):
# 初始化波浪点
for i in range(0, WIDTH, 5):
self.wave_points.append([i, HEIGHT//2])
def spawn_bubble(self):
if len(self.bubbles) < 15:
x = random.randint(50, WIDTH - 50)
y = random.randint(50, HEIGHT - 50)
self.bubbles.append(Bubble(x, y))
def handle_click(self, mouse_pos):
if self.mode == "bubble":
for bubble in self.bubbles:
if bubble.check_click(mouse_pos):
self.score += 10
# 添加粒子效果
for _ in range(10):
self.particles.append(Particle(bubble.x, bubble.y))
break
elif self.mode == "sand":
# 沙粒效果
for _ in range(20):
x = mouse_pos[0] + random.randint(-30, 30)
y = mouse_pos[1] + random.randint(-30, 30)
if 0 <= x < WIDTH and 0 <= y < HEIGHT:
color = random.choice([YELLOW, ORANGE, (255, 200, 100)])
self.sand_particles.append([x, y, color])
elif self.mode == "wave":
# 波浪效果
for i, point in enumerate(self.wave_points):
distance = abs(point[0] - mouse_pos[0])
if distance < 100:
amplitude = 50 * (1 - distance / 100)
point[1] = HEIGHT//2 - amplitude
def update(self):
# 更新气泡
self.bubbles = [b for b in self.bubbles if not b.popped or b.pop_animation < 20]
for bubble in self.bubbles:
bubble.draw(screen)
# 更新粒子
for particle in self.particles[:]:
particle.update()
if particle.life <= 0:
self.particles.remove(particle)
# 更新沙粒(重力效果)
for particle in self.sand_particles:
if particle[1] < HEIGHT - 5:
particle[1] += 1
# 更新波浪(回弹效果)
for point in self.wave_points:
if point[1] < HEIGHT//2:
point[1] += 2
elif point[1] > HEIGHT//2:
point[1] = HEIGHT//2
# 随机生成新气泡
if self.mode == "bubble" and random.randint(1, 60) == 1:
self.spawn_bubble()
def draw(self, screen):
screen.fill(WHITE)
if self.mode == "bubble":
# 绘制气泡模式
for bubble in self.bubbles:
bubble.draw(screen)
for particle in self.particles:
particle.draw(screen)
elif self.mode == "sand":
# 绘制沙粒模式
for particle in self.sand_particles:
pygame.draw.circle(screen, particle[2], (int(particle[0]), int(particle[1])), 2)
elif self.mode == "wave":
# 绘制波浪模式
if len(self.wave_points) > 1:
pygame.draw.lines(screen, BLUE, False, [(p[0], p[1]) for p in self.wave_points], 3)
# 绘制UI
score_text = self.font.render(f"分数: {self.score}", True, BLACK)
screen.blit(score_text, (10, 10))
mode_text = self.font.render(f"模式: {self.mode} (按空格切换)", True, BLACK)
screen.blit(mode_text, (10, 50))
instruction = self.font.render("点击屏幕进行解压!", True, BLACK)
screen.blit(instruction, (10, HEIGHT - 40))
# 主游戏循环
def main():
clock = pygame.time.Clock()
game = RelaxGame()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
game.handle_click(event.pos)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# 切换模式
modes = ["bubble", "sand", "wave"]
current_index = modes.index(game.mode)
game.mode = modes[(current_index + 1) % len(modes)]
game.update()
game.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
这个解压小游戏包含三种模式:
气泡模式(bubble):点击屏幕上的彩色气泡,气泡会爆炸并产生粒子效果
沙粒模式(sand):点击屏幕会产生沙粒效果,沙粒会受重力影响下落
波浪模式(wave):点击屏幕会产生波浪效果,波浪会逐渐平复
操作说明:
运行要求:
需要安装pygame库:pip install pygame
这个游戏设计简单但有趣,不同的模式提供了不同的解压体验,适合在工作学习间隙放松心情。