[创客活动与软文] Labubu收集冒险游戏

[复制链接]
 楼主| hbzjt2011 发表于 2025-6-20 19:06 | 显示全部楼层 |阅读模式
#申请原创#  @21小跑堂 游戏特色:
  • 可爱的Labubu角色:玩家控制一个粉色的Labubu,有耳朵、眼睛和微笑
  • 收集系统:收集不同颜色的小Labubu,它们会发光并有弹跳效果
  • 动态障碍物:红色刺球会在屏幕上移动并反弹
  • 关卡进阶:每收集完所有Labubu就进入下一关,难度递增
  • 精美视觉:渐变背景、动画效果和粒子特效
游戏**:
  • 使用方向键控制Labubu移动
  • 收集所有发光的小Labubu来获得分数
  • 避开移动的红色刺球障碍物
  • 收集完所有Labubu后自动进入下一关
  • 游戏结束后按R键重新开始
运行要求:
需要安装pygame库
  1. pip install pygame
  1. import pygame
  2. import random
  3. import math
  4. import sys

  5. # 初始化pygame
  6. pygame.init()

  7. # 游戏常量
  8. SCREEN_WIDTH = 800
  9. SCREEN_HEIGHT = 600
  10. FPS = 60

  11. # 颜色定义
  12. WHITE = (255, 255, 255)
  13. BLACK = (0, 0, 0)
  14. PINK = (255, 182, 193)
  15. BLUE = (135, 206, 235)
  16. GREEN = (144, 238, 144)
  17. RED = (255, 99, 71)
  18. PURPLE = (221, 160, 221)
  19. YELLOW = (255, 255, 0)
  20. ORANGE = (255, 165, 0)

  21. class Player:
  22.     def __init__(self, x, y):
  23.         self.x = x
  24.         self.y = y
  25.         self.width = 40
  26.         self.height = 40
  27.         self.speed = 5
  28.         self.color = PINK
  29.         
  30.     def move(self, keys):
  31.         if keys[pygame.K_LEFT] and self.x > 0:
  32.             self.x -= self.speed
  33.         if keys[pygame.K_RIGHT] and self.x < SCREEN_WIDTH - self.width:
  34.             self.x += self.speed
  35.         if keys[pygame.K_UP] and self.y > 0:
  36.             self.y -= self.speed
  37.         if keys[pygame.K_DOWN] and self.y < SCREEN_HEIGHT - self.height:
  38.             self.y += self.speed
  39.             
  40.     def draw(self, screen):
  41.         # 绘制可爱的Labubu玩家角色
  42.         # 主体
  43.         pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.width, self.height))
  44.         # 耳朵
  45.         pygame.draw.ellipse(screen, self.color, (self.x - 5, self.y - 10, 15, 20))
  46.         pygame.draw.ellipse(screen, self.color, (self.x + 30, self.y - 10, 15, 20))
  47.         # 眼睛
  48.         pygame.draw.circle(screen, BLACK, (self.x + 12, self.y + 15), 3)
  49.         pygame.draw.circle(screen, BLACK, (self.x + 28, self.y + 15), 3)
  50.         # 嘴巴
  51.         pygame.draw.arc(screen, BLACK, (self.x + 15, self.y + 20, 10, 8), 0, math.pi, 2)
  52.         
  53.     def get_rect(self):
  54.         return pygame.Rect(self.x, self.y, self.width, self.height)

  55. class Labubu:
  56.     def __init__(self):
  57.         self.x = random.randint(0, SCREEN_WIDTH - 30)
  58.         self.y = random.randint(0, SCREEN_HEIGHT - 30)
  59.         self.width = 30
  60.         self.height = 30
  61.         self.colors = [PURPLE, BLUE, GREEN, YELLOW, ORANGE]
  62.         self.color = random.choice(self.colors)
  63.         self.bounce_offset = 0
  64.         self.bounce_speed = 0.1
  65.         
  66.     def update(self):
  67.         # 让Labubu有弹跳效果
  68.         self.bounce_offset += self.bounce_speed
  69.         
  70.     def draw(self, screen):
  71.         bounce_y = self.y + math.sin(self.bounce_offset) * 3
  72.         # 绘制可收集的小Labubu
  73.         pygame.draw.ellipse(screen, self.color, (self.x, bounce_y, self.width, self.height))
  74.         # 小耳朵
  75.         pygame.draw.ellipse(screen, self.color, (self.x - 3, bounce_y - 8, 10, 15))
  76.         pygame.draw.ellipse(screen, self.color, (self.x + 23, bounce_y - 8, 10, 15))
  77.         # 眼睛
  78.         pygame.draw.circle(screen, BLACK, (int(self.x + 8), int(bounce_y + 12)), 2)
  79.         pygame.draw.circle(screen, BLACK, (int(self.x + 22), int(bounce_y + 12)), 2)
  80.         # 闪闪发光效果
  81.         for i in range(3):
  82.             star_x = self.x + random.randint(-10, 40)
  83.             star_y = bounce_y + random.randint(-10, 40)
  84.             if 0 <= star_x <= SCREEN_WIDTH and 0 <= star_y <= SCREEN_HEIGHT:
  85.                 pygame.draw.circle(screen, YELLOW, (int(star_x), int(star_y)), 1)
  86.         
  87.     def get_rect(self):
  88.         return pygame.Rect(self.x, self.y, self.width, self.height)

  89. class Obstacle:
  90.     def __init__(self):
  91.         self.x = random.randint(0, SCREEN_WIDTH - 50)
  92.         self.y = random.randint(0, SCREEN_HEIGHT - 50)
  93.         self.width = 50
  94.         self.height = 50
  95.         self.speed_x = random.choice([-2, -1, 1, 2])
  96.         self.speed_y = random.choice([-2, -1, 1, 2])
  97.         
  98.     def update(self):
  99.         self.x += self.speed_x
  100.         self.y += self.speed_y
  101.         
  102.         # 边界反弹
  103.         if self.x <= 0 or self.x >= SCREEN_WIDTH - self.width:
  104.             self.speed_x = -self.speed_x
  105.         if self.y <= 0 or self.y >= SCREEN_HEIGHT - self.height:
  106.             self.speed_y = -self.speed_y
  107.             
  108.     def draw(self, screen):
  109.         # 绘制移动的障碍物(看起来像危险的刺球)
  110.         pygame.draw.circle(screen, RED, (self.x + 25, self.y + 25), 25)
  111.         # 绘制刺
  112.         for angle in range(0, 360, 45):
  113.             rad = math.radians(angle)
  114.             start_x = self.x + 25 + math.cos(rad) * 20
  115.             start_y = self.y + 25 + math.sin(rad) * 20
  116.             end_x = self.x + 25 + math.cos(rad) * 30
  117.             end_y = self.y + 25 + math.sin(rad) * 30
  118.             pygame.draw.line(screen, BLACK, (start_x, start_y), (end_x, end_y), 3)
  119.             
  120.     def get_rect(self):
  121.         return pygame.Rect(self.x, self.y, self.width, self.height)

  122. class Game:
  123.     def __init__(self):
  124.         self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  125.         pygame.display.set_caption("Labubu收集冒险 - 用方向键移动!")
  126.         self.clock = pygame.time.Clock()
  127.         self.font = pygame.font.Font(None, 36)
  128.         
  129.         self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
  130.         self.labubus = []
  131.         self.obstacles = []
  132.         self.score = 0
  133.         self.game_over = False
  134.         self.level = 1
  135.         
  136.         # 生成初始Labubu和障碍物
  137.         self.spawn_labubus(5)
  138.         self.spawn_obstacles(3)
  139.         
  140.     def spawn_labubus(self, count):
  141.         for _ in range(count):
  142.             labubu = Labubu()
  143.             # 确保不与玩家重叠
  144.             while labubu.get_rect().colliderect(self.player.get_rect()):
  145.                 labubu = Labubu()
  146.             self.labubus.append(labubu)
  147.             
  148.     def spawn_obstacles(self, count):
  149.         for _ in range(count):
  150.             obstacle = Obstacle()
  151.             # 确保不与玩家重叠
  152.             while obstacle.get_rect().colliderect(self.player.get_rect()):
  153.                 obstacle = Obstacle()
  154.             self.obstacles.append(obstacle)
  155.    
  156.     def handle_events(self):
  157.         for event in pygame.event.get():
  158.             if event.type == pygame.QUIT:
  159.                 return False
  160.             if event.type == pygame.KEYDOWN:
  161.                 if event.key == pygame.K_r and self.game_over:
  162.                     self.restart_game()
  163.         return True
  164.    
  165.     def restart_game(self):
  166.         self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
  167.         self.labubus = []
  168.         self.obstacles = []
  169.         self.score = 0
  170.         self.game_over = False
  171.         self.level = 1
  172.         self.spawn_labubus(5)
  173.         self.spawn_obstacles(3)
  174.    
  175.     def update(self):
  176.         if self.game_over:
  177.             return
  178.             
  179.         keys = pygame.key.get_pressed()
  180.         self.player.move(keys)
  181.         
  182.         # 更新Labubu
  183.         for labubu in self.labubus:
  184.             labubu.update()
  185.             
  186.         # 更新障碍物
  187.         for obstacle in self.obstacles:
  188.             obstacle.update()
  189.             
  190.         # 检查收集Labubu
  191.         for labubu in self.labubus[:]:
  192.             if self.player.get_rect().colliderect(labubu.get_rect()):
  193.                 self.labubus.remove(labubu)
  194.                 self.score += 10
  195.                
  196.         # 检查与障碍物碰撞
  197.         for obstacle in self.obstacles:
  198.             if self.player.get_rect().colliderect(obstacle.get_rect()):
  199.                 self.game_over = True
  200.                
  201.         # 所有Labubu被收集完毕,进入下一关
  202.         if not self.labubus:
  203.             self.level += 1
  204.             labubu_count = min(5 + self.level, 10)
  205.             obstacle_count = min(3 + self.level // 2, 8)
  206.             self.spawn_labubus(labubu_count)
  207.             self.spawn_obstacles(obstacle_count)
  208.    
  209.     def draw(self):
  210.         # 绘制渐变背景
  211.         for y in range(SCREEN_HEIGHT):
  212.             color_ratio = y / SCREEN_HEIGHT
  213.             r = int(135 + (255 - 135) * color_ratio)
  214.             g = int(206 + (182 - 206) * color_ratio)
  215.             b = int(235 + (193 - 235) * color_ratio)
  216.             pygame.draw.line(self.screen, (r, g, b), (0, y), (SCREEN_WIDTH, y))
  217.         
  218.         # 绘制游戏对象
  219.         for labubu in self.labubus:
  220.             labubu.draw(self.screen)
  221.             
  222.         for obstacle in self.obstacles:
  223.             obstacle.draw(self.screen)
  224.             
  225.         self.player.draw(self.screen)
  226.         
  227.         # 绘制UI
  228.         score_text = self.font.render(f"分数: {self.score}", True, BLACK)
  229.         level_text = self.font.render(f"关卡: {self.level}", True, BLACK)
  230.         labubu_count_text = self.font.render(f"剩余Labubu: {len(self.labubus)}", True, BLACK)
  231.         
  232.         self.screen.blit(score_text, (10, 10))
  233.         self.screen.blit(level_text, (10, 50))
  234.         self.screen.blit(labubu_count_text, (10, 90))
  235.         
  236.         if self.game_over:
  237.             game_over_text = self.font.render("游戏结束!按R重新开始", True, RED)
  238.             text_rect = game_over_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
  239.             pygame.draw.rect(self.screen, WHITE, text_rect.inflate(20, 20))
  240.             pygame.draw.rect(self.screen, BLACK, text_rect.inflate(20, 20), 2)
  241.             self.screen.blit(game_over_text, text_rect)
  242.         
  243.         # 绘制说明
  244.         if not self.game_over and self.score == 0:
  245.             instruction = self.font.render("用方向键移动,收集所有Labubu,避开红色刺球!", True, BLACK)
  246.             self.screen.blit(instruction, (SCREEN_WIDTH//2 - 200, SCREEN_HEIGHT - 40))
  247.    
  248.     def run(self):
  249.         running = True
  250.         while running:
  251.             running = self.handle_events()
  252.             self.update()
  253.             self.draw()
  254.             pygame.display.flip()
  255.             self.clock.tick(FPS)
  256.         
  257.         pygame.quit()
  258.         sys.exit()

  259. if __name__ == "__main__":
  260.     game = Game()
  261.     game.run()


评论

感谢分享,未达原创审核标准不予审核,活动详情可查看https://bbs.21ic.com/icview-3431776-1-1.html  发表于 2025-6-23 10:14
cooldog123pp 发表于 2025-6-25 08:29 | 显示全部楼层
代码很全啊,但是有没有实际运行效果可以看看,或者视频。
真的问题不大 发表于 2025-6-26 18:21 | 显示全部楼层
同问,lz能不能贴个效果看看
MythWeaver 发表于 2025-6-28 22:10 | 显示全部楼层
真的问题不大 发表于 2025-6-26 18:21
同问,lz能不能贴个效果看看

是的要看看效果如何啊,就贴了一大串代码啊
codingtuzi 发表于 2025-7-1 09:50 | 显示全部楼层
蹭热度的方向倒是没有问题,我感觉这个小形象的热度扛不到您这成品做出来。
LiuDW091 发表于 2025-7-3 15:55 | 显示全部楼层
拉布布
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

256

主题

2827

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

256

主题

2827

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部