求正确解答

来源:3-2 飞机大战项目搭建

Buck_messic

2020-06-06 15:24:18

import sys
import pygame

from contants import BG_MUSIC, BG_IMG


def main(BG_IMAGE=None):
   """游戏入口,main方法"""
   #初始化
   pygame.init()

   width,height =480,852

   #屏幕对象
   screen = pygame.display.set_mode((width,height))

   #设置窗口标题
   pygame.display.set_caption("飞机大战")

   #加载背景图片
   pygame.image.load(BG_IMG)
   pygame.mixer.music.play(-1)#无限循环播放
   pygame.mixer.music.set_volume(0.2)#设置音量

   #加载背景音乐
   bg_music = pygame.mixer.music.load(BG_MUSIC)

   while True:
       #1 监听事件
       for event in pygame.event.get():
           #退出游戏
           if event.type == pygame.QUIT:
               pygame.quit()
               sys.exit()

       #更新游戏的状态
       #绘制
       screen.blit(BG_IMAGE,BG_IMAGE.get_rect())
       pygame.display.flip()

if __name__ == '__main__':
   main()


报错:

D:\untitled\venv\Scripts\python.exe D:/untitled1/cheapter12/main.py

pygame 1.9.6

Hello from the pygame community. https://www.pygame.org/contribute.html

Traceback (most recent call last):

  File "D:/untitled1/cheapter12/main.py", line 42, in <module>

    main()

  File "D:/untitled1/cheapter12/main.py", line 22, in main

    pygame.mixer.music.play(-1)#无限循环播放

pygame.error: music not loaded


Process finished with exit code 1


写回答

1回答

时间,

2020-06-06

同学,你好。加载背景图片应赋值给BG_IMAGE变量,加载背景音乐不需要赋值给变量,要先加载背景音乐,再设置无限循环播放及音量

http://img.mukewang.com/climg/5edb4c500903aeab06690288.jpg

如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~

0

0 学习 · 8160 问题

查看课程

相似问题