зацикленная музыка в игре
при проигрыше музыка повторяется миллион раз помогите
введите сюда код
import pygame
pygame.init()
screen = pygame.display.set_mode([900,750])
flag = True
print(pygame.font.get_fonts())
pic = pygame.image.load("CrazySmile.bmp")
picx =0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock()
speedx =5
speedy =5
WHITE = (255,255,255)
paddulew=200
padduleh=30
padduley=700
paddulex=300
picw=100
pich=100
point= 0
lives=3
font = pygame.font.SysFont("microsoftyaheitruetypemicrosoftyaheiuibold",24)
pop = pygame.mixer.Sound("pop.wav")
gameover=pygame.mixer.Sound("gameover.wav")
while flag:
for event in pygame.event.get():
if event.type==pygame.QUIT:
flag = False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_F1:
point=0
lives=5
picx=0
picy=0
speedx=5
speedy=5
picx+=speedx
picy+=speedy
if picx<=0 or picx+pic.get_width()>= 900:
speedx=-int(speedx*1.1)
if picy <=0:
speedy=-speedy
if picy>=650:
lives-=1
speedy=-speedy
screen.fill(BLACK)
paddulex=pygame.mouse.get_pos()[0]
paddulex-=paddulew//2
pygame.draw.rect(screen,WHITE,(paddulex,padduley,paddulew,padduleh))
if picy+pich>=padduley and picy+pich<=padduley+padduleh and speedy>0:
if picx+picw/2>=paddulex and picx +picw/2<=paddulex+paddulew:
point+=1
pop.play()
speedy=-speedy
string =" Жизни:"+str(lives)+" очки:"+str(point)
if lives<1:
speedx=speedy=0
string="Игра окончена.Вы набрали очков: "+str(point)
string+=" f1=продолжение(нажмите)"
gameover.play()
text=font.render(string,True,WHITE)
rec=text.get_rect()
rec.y=10
screen.blit(text,rec)
rec.centerx=screen.get_rect().centerx
screen.blit(pic,(picx,picy))
pygame.display.update()
timer.tick(90)
pygame.quit()
Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
Ну можно как-нибудь так. Заведите переменную gameoverstate = False и потом проверяйте её. Только не забудьте по F1 её тоже сбрасывать, как и остальное состояние. Таким образом этот блок кода, где играет финальная музыка, будет выполняться только один раз, когда кончатся жизни:
if lives<1 and not gameoverstate:
speedx=speedy=0
string="Игра окончена.Вы набрали очков: "+str(point)
string+=" f1=продолжение(нажмите)"
gameover.play()
gameoverstate = True