按空格键时音乐无法播放

时间:2019-02-04 01:03:10

标签: python pygame

这里是新功能,所以无论如何我对论坛站点都不太满意,我在加载屏幕时遇到了这个问题,它显示了我的标题屏幕。接下来,我按下空格键,然后它崩溃了。我的歌曲在同一目录中。我的代码也带有__init__'s ...我也可以在python / pygame缩进中使用帮助,因为我在控制台中正确了,但是将其复制并粘贴到论坛上将无法正常工作。编辑:粘贴代码突出显示,然后按我不知道的..

#2/2/2019
#My first game jam!
#Started at 3:03 AM

import pygame
import os
import random
from pygame.locals import *

pygame.init
pygame.mixer.init(44100, -16,2,2048)
pygame.display.init

#Checks to see if on the title screen
isTitle = True
isOP = False

#Character
isAlive = False
isLeft = False
isRight = False
isJumping = False
isThrowing = False

#Items
hasKey = False

#Projectile
hasGrap = False
hasDart = False
hasKuni = False

#Abilities
hasArmor = False
hasBoots = False
hasCrawl = False

#Window info
FPS = pygame.time.Clock()
sWidth = 512
sHeight = 512
isRunning = True
SCREEN = pygame.display.set_mode((sWidth, sHeight))
pygame.display.set_caption("Golden Dreamer: Evocation")

#Sets icon for window
GDE = pygame.image.load('GD_icon.png').convert()
pygame.display.set_icon(GDE)

#Loads all sprites here
GDT = pygame.image.load('GD_title.png').convert()
PLR_IDLE = pygame.image.load('Plr_idle.png').convert()
OP_1 = pygame.image.load('OP_1.png').convert()

#TITLE SCREEN
if (isTitle == True):
    SCREEN.blit(GDT, (-8, 0))
    GDTM = pygame.mixer.music.load("GD_title_m.ogg")
    pygame.mixer.music.play(-1)

while isRunning:
    for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                isRunning = False 

    keys = pygame.key.get_pressed()            
    if (keys[pygame.K_a]):
        xPos -= mSpd

    if (keys[pygame.K_d]):
        xPos += mSpd

    if (keys[pygame.K_SPACE] and isTitle == True):
        isOP = True
        isTitle = False

    if (isOP == True):
        pygame.mixer.stop()
        SCREEN.blit(OP_1, (-8, 0))
        OP_M = pygame.mixer.music.load("OP_m.ogg")

    FPS.tick(60)
    pygame.display.flip()
    pygame.display.update()
    print(FPS, ' FPS')

pygame.quit()

1 个答案:

答案 0 :(得分:0)

该问题似乎是由于在主循环中测试了isOP造成的。

按下[Space]将设置isOP标志,该标志将开始播放新声音文件的加载。但是,由于对此标志的检查位于您的主事件循环中,因此一旦触发,将指示混音器一遍又一遍地重新加载声音文件。

isOP设置为false应该可以解决此问题。

if (keys[pygame.K_SPACE] and isTitle == True):
    isOP = True
    isTitle = False

if (isOP == True):
    pygame.mixer.stop()
    SCREEN.blit(OP_1, (-8, 0))
    OP_M = pygame.mixer.music.load("OP_m.ogg")
    isOP = False     # DON'T RELOAD AGAIN