简体   繁体   English

我需要帮助在 Replit(Pygame) 上播放声音

[英]I need help playing sound on Replit(Pygame)

So I'm making a game on Pygame, and I have watched tutorials on how to use Pygame for sound but for some reason no matter what I do, it doesn't play.因此,我正在 Pygame 上制作游戏,并且我已经观看了有关如何使用 Pygame 获得声音的教程,但由于某种原因,无论我做什么,它都无法播放。 I am on Replit, and I have tried to use the Replit Library to but I had issues with that too.我在 Replit,我曾尝试使用 Replit 库,但我也遇到了问题。 I was wondering if I was writing something wrong or if it's just Replit or what it is.我想知道我是不是写错了什么,或者它只是Replit还是它是什么。 Also If you have any tips on looping, and how to play the music at certain times that would be great.另外,如果您对循环有任何提示,以及如何在特定时间播放音乐,那就太好了。 The code below is just a little code to try to figure out how to make menus, and I was going to make the music play when I select a color.下面的代码只是一个小代码,试图弄清楚如何制作菜单,当我 select 一个颜色时,我打算让音乐播放。 I just tried removing我刚刚尝试删除

pygame.init() 

but when I do, A whole bunch of stuff just does not work anymore such as fonts and it also says the mixer is not initialized.但是当我这样做时,一大堆东西就不再起作用了,例如 fonts 并且它还说混音器没有初始化。

import pygame
import random
import time

pygame.init() 

font = pygame.font.SysFont("arial", 15, True)

compwin = pygame.display.set_mode((512,512))

pygame.display.set_caption("attack")

clock = pygame.time.Clock()
music = pygame.mixer.music.load("space music.wav")
pygame.mixer.music.play(-1)
 
x1 = 128
y1 = 256
x2 = 256
y2 = 256
x3 = 384
y3 = 256
x4 = 251
y4 = 251
def picked():
  while run:
    pygame.time.delay(30)
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        run == False
    if white.colliderect(red):
      compwin.fill((255,0,0))
    if white.colliderect(green):
      compwin.fill((0,255,0))
    if white.colliderect(blue):
      compwin.fill((0,0,255))
      
    pygame.display.update()
    
run = True

while run:
  
  pygame.time.delay(30)
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
 
  keys = pygame.key.get_pressed()
  if keys[pygame.K_1]:
    x4 = x1 - 5
    y4 = y1 - 5
    
  elif keys[pygame.K_2]:
    x4 = x2 - 5
    y4 = y2 - 5
  elif keys[pygame.K_3]:
    x4 = x3 - 5
    y4 = y3 - 5
  compwin.fill((0,0,0))
  
  white = pygame.draw.rect(compwin,(255,255,255), (x4,y4,42,42) )
  blue = pygame.draw.rect(compwin,(0,0,255), (x2,y2,32,32) )
  green = pygame.draw.rect(compwin,(0,255,0), (x3,y3,32,32) )
  red = pygame.draw.rect(compwin,(255,0,0), (x1,y1,32,32) )
  
  keys = pygame.key.get_pressed()
  if keys[pygame.K_RETURN]:
    picked()

  pygame.display.update()
    
pygame.quit()

You can't play music in pygame on repl (as far as I know) because repl runs on a virtual machine.您无法在repl上播放pygame中的音乐(据我所知),因为repl在虚拟机上运行。 Apparently, this feature is planned and will be added in time.显然,这个功能是有计划的,会及时添加。 For now, I'd say to run your code on a different IDE, and that should work.现在,我想说在不同的 IDE 上运行您的代码,这应该可以。

You should also look at this: https://docs.replit.com/misc/playing-audio-replit#playing-audio-on-replit您还应该看看这个: https://docs.replit.com/misc/playing-audio-replit#playing-audio-on-replit

For looping audio you need to pass a -1 : pygame.mixer.music.play(loops = -1)对于循环音频,您需要传递-1pygame.mixer.music.play(loops = -1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM