Explorar o código

增加菜单音效,禁用preload

detopoi hai 1 ano
pai
achega
c2777cc446
Modificáronse 5 ficheiros con 88 adicións e 39 borrados
  1. 1 1
      game/const/window.py
  2. 19 11
      game/music/music_loader.py
  3. 29 7
      game/sound/sound_player.py
  4. 5 2
      game/sprite/player.py
  5. 34 18
      laucher.py

+ 1 - 1
game/const/window.py

@@ -1,3 +1,3 @@
 WINDOW_SIZE = WINDOW_W, WINDOW_H = (800, 600)
-WINDOW_CAPTION = "QQTRogue(底层逻辑:永恒; 地牢算法开发与设计:100の雨女; 唯一官方群756422783免费下载; 擅自售卖皆属剽窃!)"
+WINDOW_CAPTION = "QQTRogue2.3.2(底层逻辑:永恒; 地牢算法开发与设计:100の雨女; 唯一官方群756422783免费下载; 擅自售卖皆属剽窃!)"
 WINDOW_SCROLL_PADDING = 200

+ 19 - 11
game/music/music_loader.py

@@ -10,10 +10,14 @@ def play(name, volume):
     global current_music
     if current_music == name:
         return
-    pygame.mixer.music.load(G.GAME_PATH + "\\" + "res/music/" + name)
-    pygame.mixer.music.set_volume(volume)
-    pygame.mixer.music.play(loops=-1, fade_ms=1000)
-    current_music = name
+    try:
+        pygame.mixer.music.load(G.GAME_PATH + "\\" + "res/music/" + name)
+        pygame.mixer.music.set_volume(volume)
+        pygame.mixer.music.play(loops=-1, fade_ms=1000)
+        current_music = name
+    except Exception as e:
+        print(e)
+
 
 def play_ambience(name, volume):
     global current_ambience, ambience_sounds
@@ -22,13 +26,17 @@ def play_ambience(name, volume):
     # 停止当前正在播放的环境音效
     stop_ambience()
     # 加载并播放新的环境音效
-    sound_path = "res/ambience/" + name
-    ambience_sound = pygame.mixer.Sound(sound_path)
-    ambience_sound.set_volume(volume)
-    ambience_sound.play(loops=-1)
-    ambience_sounds[name] = ambience_sound  # 更新字典
-    current_ambience = name
-    # print(f"Playing ambience: {name}")
+    try:
+        sound_path = "res/ambience/" + name
+        ambience_sound = pygame.mixer.Sound(sound_path)
+        ambience_sound.set_volume(volume)
+        ambience_sound.play(loops=-1)
+        ambience_sounds[name] = ambience_sound  # 更新字典
+        current_ambience = name
+        # print(f"Playing ambience: {name}")
+    except Exception as e:
+        print(e)
+
 
 def stop_ambience():
     global current_ambience, ambience_sounds

+ 29 - 7
game/sound/sound_player.py

@@ -1,18 +1,40 @@
 import pygame
+import time
 from game.const import game as G
 
-pygame.mixer.init()
+try:
+    pygame.mixer.init()
+except Exception as e:
+    print(e)
 PATH = "res/sound"
 
 
+sounds_dic = {}
+
+
 def play(name):
-    sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + PATH + '/' + name + ".wav")
-    sound.set_volume(G.SOUND_VOLUME)
-    sound.play()
+    global sounds_dic
+    last_time = sounds_dic.get(name)
+    try:
+        if not last_time:
+            sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + PATH + '/' + name + ".wav")
+            sound.set_volume(G.SOUND_VOLUME)
+            sound.play()
+        else:
+            if time.time() - last_time >= 0.1:
+                sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + PATH + '/' + name + ".wav")
+                sound.set_volume(G.SOUND_VOLUME)
+                sound.play()
+        sounds_dic[name] = time.time()
+    except Exception as e:
+        print(e)
 
 
 def playWin(name):
-    sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + "res/music" + '/' + name + ".ogg")
-    sound.set_volume(G.SOUND_VOLUME)
-    sound.play()
+    try:
+        sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + "res/music" + '/' + name + ".ogg")
+        sound.set_volume(G.SOUND_VOLUME)
+        sound.play()
+    except Exception as e:
+        print(e)
 

+ 5 - 2
game/sprite/player.py

@@ -974,8 +974,11 @@ class Player(updatable.Updatable):
                     frame = self.character["LOSE"][component][self.character_frame_idxs[component]]
                     screen.blit(frame.image, (frame.cx + self.x_pos + self.cx, frame.cy + self.y_pos + self.cy))
         # 显示前景特效
-        for s in self.effects_front:
-            s.draw(screen)
+        try:
+            for s in self.effects_front:
+                s.draw(screen)
+        except Exception as e:
+            print(f"Fatal Error: (line 979) player.py: {e}.")
         # 显示文字
         if pygame.time.get_ticks() - self.text_init_time < self.text_duration:
             screen.blit(self.text_img, (self.x_pos - G.HALF_GAME_SQUARE, self.y_pos - G.GAME_SQUARE - G.GAME_SQUARE))

+ 34 - 18
laucher.py

@@ -20,6 +20,8 @@ from game.algo import utils as util
 from game.algo import rougeGen as RG
 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
 
 
 current_folder_path = os.getcwd()
@@ -50,11 +52,11 @@ def check_rouge_ob_data(data):
 
 
 class PygameButton:
-    def __init__(self, screen, text, relx, rely, font_size, bg_color, fg_color=(255,255,255), action=None, anchor='center'):
+    def __init__(self, screen, text, relx, rely, font_size, bg_color, fg_color=(0,0,0), action=None, anchor='center', is_bold=False):
         self.screen = screen
         self.text = text
         self.lines = text.split('\n')
-        self.font = pygame.font.SysFont("Microsoft YaHei", font_size)
+        self.font = pygame.font.SysFont("Microsoft YaHei", font_size, bold=is_bold)
         self.bg_color_normal = pygame.Color(bg_color)
         # 计算 hover 颜色:加深 20%
         h = self.bg_color_normal
@@ -105,8 +107,10 @@ class PygameButton:
         if event.type == pygame.MOUSEMOTION:
             self.hovered = self.bg_rect.collidepoint(event.pos)
         elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
-            if self.hovered and self.action:
-                self.action()
+            if self.hovered:
+                if self.action:
+                    self.action()
+                sound_player.play("uiLeave")
 
 
 class Menu:
@@ -335,35 +339,39 @@ class Menu:
         # 主界面按钮
         self.buttons.clear()
         # "进入游戏"
-        text = f"进入游戏 \n (当前难度: {R.difficulty}; 当前英雄: {R.cursor_skin})"
-        self.buttons.append(PygameButton(self.screen, text, 0.5, 0.29, 20, "#C68E17", action=self._start_game))
+        text = f"进入游戏 \n (当前难度: {self.diff_dic[R.difficulty]}; 当前英雄: {self.hero_dic[R.rouge_hero]})"
+        self.buttons.append(PygameButton(self.screen, text, 0.5, 0.29, 20, "#C68E17", action=self._start_game, is_bold=True))
         # "继续游戏"
         if os.path.exists(os.path.join(os.path.expanduser("~\\Documents"), "QQTPVE", "rougeOBLastGame.rougedata")):
             last = load_rouge_data(os.path.join(os.path.expanduser("~\\Documents"), "QQTPVE", "rougeOBLastGame.rougedata"))
             lvl = ((last["current_level"]+2)//2 -1)//5+1
             stage = ((last["current_level"]+2)//2 -1)%5+1
             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, "#C68E17", action=lambda: self._continue_game(last)))
+            self.buttons.append(PygameButton(self.screen, text_c, 0.5, 0.44, 18, "#C68E17", action=lambda: self._continue_game(last), is_bold=True))
         # 任务系统
-        self.buttons.append(PygameButton(self.screen, "任务系统", 0.16, 0.16, 24, "#C68E17", action=self._open_task))
+        self.buttons.append(PygameButton(self.screen, "任务系统", 0.16, 0.16, 24, "#C68E17", action=self._open_task, is_bold=True))
         # 陈列馆
-        self.buttons.append(PygameButton(self.screen, "陈列馆", 0.16, 0.28, 18, "#C68E17", action=self._open_setting))
+        self.buttons.append(PygameButton(self.screen, "陈列馆", 0.16, 0.28, 18, "#C68E17", action=self._open_setting, is_bold=True))
         # 难度选择
         for diff, rel in [("Easy", 0.1), ("Normal", 0.18), ("Hell", 0.26), ("Hell+", 0.34)]:
             enabled = R.pass_status.get(diff, 0) > 0
-            text = f"● 选择{self.diff_dic[diff]}难度" if enabled else f"○ {self.diff_dic[diff]}难度(未解锁)"
-            color = "#C47451" if enabled else "#614051"
+            if diff == "Easy":
+                text = f"● 选择{self.diff_dic[diff]}难度"
+                color = "#C47451"
+            else:
+                text = f"● 选择{self.diff_dic[diff]}难度" if enabled else f"○ {self.diff_dic[diff]}难度(未解锁)"
+                color = "#C47451" if enabled else "#614051"
             action = lambda d=diff: self._choose_diff(d) if enabled else None
-            self.buttons.append(PygameButton(self.screen, text, 0.75, rel, 12, color, action=action))
+            self.buttons.append(PygameButton(self.screen, text, 0.75, rel, 13, color, action=action, anchor="topleft", is_bold=False))
         # ================================= 英雄选择 =================================
         heroes = ["Fox", "Boboli", "Naruto_old", "Asha", "Maria", "Surprise"]
         x_position = 0.07
         y_positions = [0.64, 0.70, 0.76, 0.82, 0.88, 0.49]
         for hero, y in zip(heroes, y_positions):
             text = f"● 选择{self.hero_dic[hero]}"
-            self.buttons.append(PygameButton(self.screen, text, x_position, y, 12, "#C47451", action=lambda h=hero: self._choose_hero(h), anchor="topleft"))
+            self.buttons.append(PygameButton(self.screen, text, x_position, y, 12, "#C47451", action=lambda h=hero: self._choose_hero(h), anchor="topleft", is_bold=True))
         # 随机开局
-        self.buttons.append(PygameButton(self.screen, "● 随机开局", x_position, 0.94, 12, "#C47451", action=lambda: self._choose_hero("Random"), anchor="topleft"))
+        self.buttons.append(PygameButton(self.screen, "● 随机开局", x_position, 0.94, 12, "#C47451", action=lambda: self._choose_hero("Random"), anchor="topleft", is_bold=True))
         # 英雄信息提示
         infos = [getattr(self, attr) for attr in ['Fox_info','Boboli_info','Naruto_old_info','Asha_info','Maria_info']]
         for info, y in zip(infos, y_positions):
@@ -402,7 +410,7 @@ class Menu:
     def _choose_hero(self, hero):
         if hero == "Random":
             hero = random.choice(["Fox","Boboli","Naruto_old","Asha","Maria","Surprise"])
-        R.cursor_skin = hero
+        R.rouge_hero = hero
         self._create_buttons()
 
     def run(self):
@@ -414,11 +422,15 @@ class Menu:
                     sys.exit()
                 for btn in self.buttons:
                     btn.handle_event(event)
+                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
+                    # 看看有没有按钮自己处理了这次点击
+                    if not any(btn.bg_rect.collidepoint(event.pos) for btn in self.buttons):
+                        sound_player.play("uiFail")
             self.screen.fill((30,30,30))
             for btn in self.buttons:
                 btn.draw()
             pygame.display.flip()
-            clock.tick(30)
+            clock.tick(60)
 
     # 读取上次存档
     def button_continue(self, last_data, last_path):
@@ -520,11 +532,14 @@ class Launcher:
         self.screen = pygame.display.set_mode(W.WINDOW_SIZE)
         pygame.display.set_caption(W.WINDOW_CAPTION)
         self.cfg_json = {}
-        self.display_flags = None
+        self.music_volume = 1
+        self.display_flags = 0
         self.init_config()
+        self.init_window()
         # 预加载资源
-        self.preload_resources()
+        # self.preload_resources()
         # 启动菜单
+        music_loader.play("M10.ogg", self.music_volume)
         self.menu = Menu(self.screen)
         self.menu.run()
 
@@ -535,6 +550,7 @@ class Launcher:
         except json.JSONDecodeError:
             with open("config.json", encoding="utf-8_sig") as f:
                 self.cfg_json = json.load(f)
+        self.music_volume = self.cfg_json["music_volume"]
         if not self.cfg_json["frame_screen"]:
             self.display_flags = pygame.NOFRAME
         if self.cfg_json["full_screen"]: