艺小卤 发表于 2025-4-4 17:56:12

这个代码有问题

import pygame
import sys
import traceback

pygame.init()
pygame.mixer.init()

bg_sire = width, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战 -- Fish C Deno")

background = pygame.image.load("images/background.png").convert()

#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volune(0.2)
bullet_sound = pygame.mixer.music.Sound("sound/use_bullet.wav")
bullet_sound.set_volune(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volune(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volune(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volune(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volune(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volune(0.2)
enemy3_flying_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volune(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volune(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volune(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volune(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volune(0.2)

def main():
    pygame.mixer musice.play(-1)

    clock = pygame.time.Clock()

    running = True

    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

      sceen.blit{background, (0,0)}

      pygame.display.flip()

      clock.tick(60)


if _name_ =="_main_":
    try:
      main()
   except SystemExit:
         pass
   except:
         traceback.print_exc
         pygame.quit()
         input()

FishC 发表于 2025-4-4 17:57:28

代码存在多处拼写错误和语法问题,以下是主要问题和修改建议:

主要错误修正:

[*]1. 变量名拼写错误
bg_sire = width, height = 400, 700 → bg_size = (400, 700)

[*]2. 方法名拼写错误
所有 set_volune → set_volume

[*]3. 音效加载错误
bullet_sound = pygame.mixer.music.Sound(...) → pygame.mixer.Sound(...)

[*]4. 主循环音乐播放错误
pygame.mixer musice.play(-1) → pygame.mixer.music.play(-1)

[*]5. 事件判断缺失模块前缀
if event.type == QUIT: → pygame.QUIT

[*]6. 屏幕绘制语法错误
sceen.blit{...} → screen.blit(...)

[*]7. 主函数入口错误
_name_ → __name__


修正后的核心代码:

# 修正后的初始化部分
bg_size = (400, 700)
screen = pygame.display.set_mode(bg_size)

# 修正后的音效设置(以bullet_sound为例)
bullet_sound = pygame.mixer.Sound("sound/use_bullet.wav")
bullet_sound.set_volume(0.2)

# 修正后的主函数
def main():
    pygame.mixer.music.play(-1)
    while running:
      for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
      screen.blit(background, (0,0))

if __name__ == "__main__":
    try:
      main()
    except SystemExit:
      pass
    except:
      traceback.print_exc()
      pygame.quit()
      input()


其他注意事项:

[*]检查所有音效变量名是否一致(如enemy3_flying_sound和enemy3_fly_sound存在不一致)
[*]确保所有音频文件路径正确
[*]建议使用pygame.quit()前先停止音乐:pygame.mixer.music.stop()


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

player-none 发表于 2025-4-4 20:15:04

1.volume而不是volune;注意到FishCDemo也被写成Deno了
2.main里pygame.mixer.music.play
3.screen.blit()而不是sceen.blit{}
4.traceback.print_exc(),要有括号

你先是缩进没了,又是奇怪的拼写,很好奇地你的键盘长啥样

smallwh 发表于 2025-4-4 21:28:31

import pygame
import sys
import traceback

pygame.init()
pygame.mixer.init()

bg_size = width, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战 -- Fish C Deno")

background = pygame.image.load("images/background.png").convert()

#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/use_bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_flying_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_flying_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)

def main():
    pygame.mixer.music.play(-1)

    clock = pygame.time.Clock()

    running = True

    while running:
      for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

      screen.blit(background,(0,0))
      pygame.display.flip()
      clock.tick(60)


if __name__ == "__main__":
    try:
      main()
    except SystemExit:
      pass
    except:
      traceback.print_exc()
      pygame.quit()
      input()

请确保有相应的图片及音频文件
页: [1]
查看完整版本: 这个代码有问题