import pygame
import random
import math
import sys
# 初始化pygame
pygame.init()
# 游戏常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PINK = (255, 182, 193)
BLUE = (135, 206, 235)
GREEN = (144, 238, 144)
RED = (255, 99, 71)
PURPLE = (221, 160, 221)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 40
self.height = 40
self.speed = 5
self.color = PINK
def move(self, keys):
if keys[pygame.K_LEFT] and self.x > 0:
self.x -= self.speed
if keys[pygame.K_RIGHT] and self.x < SCREEN_WIDTH - self.width:
self.x += self.speed
if keys[pygame.K_UP] and self.y > 0:
self.y -= self.speed
if keys[pygame.K_DOWN] and self.y < SCREEN_HEIGHT - self.height:
self.y += self.speed
def draw(self, screen):
# 绘制可爱的Labubu玩家角色
# 主体
pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.width, self.height))
# 耳朵
pygame.draw.ellipse(screen, self.color, (self.x - 5, self.y - 10, 15, 20))
pygame.draw.ellipse(screen, self.color, (self.x + 30, self.y - 10, 15, 20))
# 眼睛
pygame.draw.circle(screen, BLACK, (self.x + 12, self.y + 15), 3)
pygame.draw.circle(screen, BLACK, (self.x + 28, self.y + 15), 3)
# 嘴巴
pygame.draw.arc(screen, BLACK, (self.x + 15, self.y + 20, 10, 8), 0, math.pi, 2)
def get_rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Labubu:
def __init__(self):
self.x = random.randint(0, SCREEN_WIDTH - 30)
self.y = random.randint(0, SCREEN_HEIGHT - 30)
self.width = 30
self.height = 30
self.colors = [PURPLE, BLUE, GREEN, YELLOW, ORANGE]
self.color = random.choice(self.colors)
self.bounce_offset = 0
self.bounce_speed = 0.1
def update(self):
# 让Labubu有弹跳效果
self.bounce_offset += self.bounce_speed
def draw(self, screen):
bounce_y = self.y + math.sin(self.bounce_offset) * 3
# 绘制可收集的小Labubu
pygame.draw.ellipse(screen, self.color, (self.x, bounce_y, self.width, self.height))
# 小耳朵
pygame.draw.ellipse(screen, self.color, (self.x - 3, bounce_y - 8, 10, 15))
pygame.draw.ellipse(screen, self.color, (self.x + 23, bounce_y - 8, 10, 15))
# 眼睛
pygame.draw.circle(screen, BLACK, (int(self.x + 8), int(bounce_y + 12)), 2)
pygame.draw.circle(screen, BLACK, (int(self.x + 22), int(bounce_y + 12)), 2)
# 闪闪发光效果
for i in range(3):
star_x = self.x + random.randint(-10, 40)
star_y = bounce_y + random.randint(-10, 40)
if 0 <= star_x <= SCREEN_WIDTH and 0 <= star_y <= SCREEN_HEIGHT:
pygame.draw.circle(screen, YELLOW, (int(star_x), int(star_y)), 1)
def get_rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Obstacle:
def __init__(self):
self.x = random.randint(0, SCREEN_WIDTH - 50)
self.y = random.randint(0, SCREEN_HEIGHT - 50)
self.width = 50
self.height = 50
self.speed_x = random.choice([-2, -1, 1, 2])
self.speed_y = random.choice([-2, -1, 1, 2])
def update(self):
self.x += self.speed_x
self.y += self.speed_y
# 边界反弹
if self.x <= 0 or self.x >= SCREEN_WIDTH - self.width:
self.speed_x = -self.speed_x
if self.y <= 0 or self.y >= SCREEN_HEIGHT - self.height:
self.speed_y = -self.speed_y
def draw(self, screen):
# 绘制移动的障碍物(看起来像危险的刺球)
pygame.draw.circle(screen, RED, (self.x + 25, self.y + 25), 25)
# 绘制刺
for angle in range(0, 360, 45):
rad = math.radians(angle)
start_x = self.x + 25 + math.cos(rad) * 20
start_y = self.y + 25 + math.sin(rad) * 20
end_x = self.x + 25 + math.cos(rad) * 30
end_y = self.y + 25 + math.sin(rad) * 30
pygame.draw.line(screen, BLACK, (start_x, start_y), (end_x, end_y), 3)
def get_rect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Labubu收集冒险 - 用方向键移动!")
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 36)
self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
self.labubus = []
self.obstacles = []
self.score = 0
self.game_over = False
self.level = 1
# 生成初始Labubu和障碍物
self.spawn_labubus(5)
self.spawn_obstacles(3)
def spawn_labubus(self, count):
for _ in range(count):
labubu = Labubu()
# 确保不与玩家重叠
while labubu.get_rect().colliderect(self.player.get_rect()):
labubu = Labubu()
self.labubus.append(labubu)
def spawn_obstacles(self, count):
for _ in range(count):
obstacle = Obstacle()
# 确保不与玩家重叠
while obstacle.get_rect().colliderect(self.player.get_rect()):
obstacle = Obstacle()
self.obstacles.append(obstacle)
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r and self.game_over:
self.restart_game()
return True
def restart_game(self):
self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
self.labubus = []
self.obstacles = []
self.score = 0
self.game_over = False
self.level = 1
self.spawn_labubus(5)
self.spawn_obstacles(3)
def update(self):
if self.game_over:
return
keys = pygame.key.get_pressed()
self.player.move(keys)
# 更新Labubu
for labubu in self.labubus:
labubu.update()
# 更新障碍物
for obstacle in self.obstacles:
obstacle.update()
# 检查收集Labubu
for labubu in self.labubus[:]:
if self.player.get_rect().colliderect(labubu.get_rect()):
self.labubus.remove(labubu)
self.score += 10
# 检查与障碍物碰撞
for obstacle in self.obstacles:
if self.player.get_rect().colliderect(obstacle.get_rect()):
self.game_over = True
# 所有Labubu被收集完毕,进入下一关
if not self.labubus:
self.level += 1
labubu_count = min(5 + self.level, 10)
obstacle_count = min(3 + self.level // 2, 8)
self.spawn_labubus(labubu_count)
self.spawn_obstacles(obstacle_count)
def draw(self):
# 绘制渐变背景
for y in range(SCREEN_HEIGHT):
color_ratio = y / SCREEN_HEIGHT
r = int(135 + (255 - 135) * color_ratio)
g = int(206 + (182 - 206) * color_ratio)
b = int(235 + (193 - 235) * color_ratio)
pygame.draw.line(self.screen, (r, g, b), (0, y), (SCREEN_WIDTH, y))
# 绘制游戏对象
for labubu in self.labubus:
labubu.draw(self.screen)
for obstacle in self.obstacles:
obstacle.draw(self.screen)
self.player.draw(self.screen)
# 绘制UI
score_text = self.font.render(f"分数: {self.score}", True, BLACK)
level_text = self.font.render(f"关卡: {self.level}", True, BLACK)
labubu_count_text = self.font.render(f"剩余Labubu: {len(self.labubus)}", True, BLACK)
self.screen.blit(score_text, (10, 10))
self.screen.blit(level_text, (10, 50))
self.screen.blit(labubu_count_text, (10, 90))
if self.game_over:
game_over_text = self.font.render("游戏结束!按R重新开始", True, RED)
text_rect = game_over_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
pygame.draw.rect(self.screen, WHITE, text_rect.inflate(20, 20))
pygame.draw.rect(self.screen, BLACK, text_rect.inflate(20, 20), 2)
self.screen.blit(game_over_text, text_rect)
# 绘制说明
if not self.game_over and self.score == 0:
instruction = self.font.render("用方向键移动,收集所有Labubu,避开红色刺球!", True, BLACK)
self.screen.blit(instruction, (SCREEN_WIDTH//2 - 200, SCREEN_HEIGHT - 40))
def run(self):
running = True
while running:
running = self.handle_events()
self.update()
self.draw()
pygame.display.flip()
self.clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
game = Game()
game.run()