Bläddra i källkod

增加粒子特效氛围感

detopoi 1 år sedan
förälder
incheckning
002740d31b
2 ändrade filer med 75 tillägg och 9 borttagningar
  1. 42 0
      game/ui/menu/component.py
  2. 33 9
      laucher.py

+ 42 - 0
game/ui/menu/component.py

@@ -1,4 +1,5 @@
 import pygame
+import random
 
 from game.sound import sound_player
 
@@ -135,3 +136,44 @@ class PygameButtonBackIMG:
             if self.hovered and self.action:
                 sound_player.play("uiLeave")
                 self.action()
+
+
+class Particle:
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
+        self.radius = random.randint(1, 3)
+        # self.color = (200, 200, 200)
+        self.color = (170, 188, 157)
+        # 垂直速度
+        self.speed_y = random.uniform(0.3, 0.6)
+        # 水平速度,模拟风吹
+        self.speed_x = random.uniform(-0.3, 0.3)
+        # 透明度和生命周期
+        self.alpha = 191
+        self.life = random.randint(100, 600)  # 寿命更长
+        self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)
+
+    def update(self):
+        # 更新位置
+        self.y -= self.speed_y
+        self.x += self.speed_x
+        self.speed_y += random.uniform(0, 0.01)
+        self.speed_x += random.uniform(-0.06, 0.06)
+        # 减缓透明度下降速度
+        self.life -= 1
+        self.alpha = max(0, int(191 * min(1., self.life / 300)))
+        # 重绘粒子
+        self.surface.fill((0,0,0,0))
+        pygame.draw.circle(
+            self.surface,
+            (self.color[0], self.color[1], self.color[2], self.alpha),
+            (self.radius, self.radius),
+            self.radius
+        )
+
+    def draw(self, screen):
+        screen.blit(self.surface, (self.x - self.radius, self.y - self.radius))
+
+    def is_alive(self):
+        return self.life > 0 and self.alpha > 0

+ 33 - 9
laucher.py

@@ -21,7 +21,7 @@ from game.const import window as W, color as C, game as G
 from game.const import rouge as R
 from game.music import music_loader
 from game.sound import sound_player
-from game.ui.menu.component import PygameButton, PygameButtonBackIMG
+from game.ui.menu.component import PygameButton, PygameButtonBackIMG, Particle
 
 current_folder_path = os.getcwd()
 
@@ -55,6 +55,7 @@ class Menu:
     def __init__(self, screen, bg_image_path=None):
         self.screen = screen
         self.buttons = []
+        self.particles = []  # 粒子列表
         self.current_hero = "Fox"
         self.current_difficulty = "Easy"
         self.is_random = False  # 随机选择角色
@@ -324,11 +325,9 @@ class Menu:
             text_c = f"继续游戏 (关卡: {lvl}-{stage}) \n (难度: {self.diff_dic[last['difficulty']]}; 英雄: {self.hero_dic[last['rouge_hero']]})"
             self.buttons.append(PygameButton(self.screen, text_c, 0.5, 0.44, 18, "#e1bd48", action=lambda: self._continue_game(last), is_bold=True, bg_alpha=191))
         # 任务系统
-        self.buttons.append(
-            PygameButton(self.screen, "任务系统", 0.16, 0.16, 24, "#e1bd48", action=self._open_task, is_bold=True, fix_shape=(120,30), bg_alpha=127))
+        self.buttons.append(PygameButton(self.screen, "任务系统", 0.16, 0.16, 24, "#e1bd48", action=self._open_task, is_bold=True, fix_shape=(120,30), bg_alpha=127))
         # 陈列馆
-        self.buttons.append(
-            PygameButton(self.screen, "陈列馆", 0.16, 0.28, 24, "#e1bd48", action=self._open_setting, is_bold=True, fix_shape=(120,30), bg_alpha=127))
+        self.buttons.append(PygameButton(self.screen, "陈列馆", 0.16, 0.28, 24, "#e1bd48", action=self._open_setting, is_bold=True, fix_shape=(120,30), bg_alpha=127))
         # 难度选择
         last_diff = {"Easy": "Easy", "Normal": "Easy", "Hell": "Normal", "Hell+": "Hell"}
         for diff, rel in [("Easy", 0.1), ("Normal", 0.18), ("Hell", 0.26), ("Hell+", 0.34)]:
@@ -433,7 +432,10 @@ class Menu:
 
     def run(self):
         clock = pygame.time.Clock()
+        spawn_timer = 0
         while True:
+            dt = clock.tick(60)
+            spawn_timer += dt
             for event in pygame.event.get():
                 if event.type == pygame.QUIT:
                     pygame.quit()
@@ -444,21 +446,43 @@ class Menu:
                     # 看看有没有按钮自己处理了这次点击
                     if not any(btn.bg_rect.collidepoint(event.pos) for btn in self.buttons):
                         sound_player.play("uiFail")
-            # 先画背景
-            # self.screen.fill((30,30,30))
+            # 随机生成新粒子: 每200ms生成一个
+            if spawn_timer > 200:
+                spawn_timer = 0
+                x = random.randint(0, self.screen.get_width())
+                y = self.screen.get_height() + 10
+                self.particles.append(Particle(x, y))
+            # 更新和过滤粒子
+            new_particles = []
+            for p in self.particles:
+                p.update()
+                if p.is_alive():
+                    new_particles.append(p)
+            self.particles = new_particles
+            # 绘制背景
             if self.bg_image:
                 self.screen.blit(self.bg_image, (0, 0))
             else:
                 self.screen.fill((60, 90, 60))
-            # 再画按钮
+            # 绘制粒子
+            for p in self.particles:
+                p.draw(self.screen)
+            # 绘制按钮
             for btn in self.buttons:
                 btn.draw()
             pygame.display.flip()
-            clock.tick(60)
 
     # 读取上次存档
     def read_continue_data(self, last_data):
         R.continuing_game = True
+        self.current_hero = last_data["rouge_hero"]
+        with open("config.json", "r", encoding="utf-8") as json_file:
+            mapsetdata = json.load(json_file)
+        mapsetdata["map_set"] = "rogueMapSet"
+        mapsetdata["your_hero"] = last_data["rouge_hero"]
+        with open("config.json", "w", encoding="utf-8") as json_file:
+            json.dump(mapsetdata, json_file, indent=4, ensure_ascii=False)
+        # ===============================================================================
         G.CURRENT_LEVEL = last_data["current_level"]
         R.map_json_dic = last_data["map_json_dic"]
         R.central_mapset = last_data["central_mapset"]