簡體   English   中英

我需要幫助在 Replit(Pygame) 上播放聲音

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

因此,我正在 Pygame 上制作游戲,並且我已經觀看了有關如何使用 Pygame 獲得聲音的教程,但由於某種原因,無論我做什么,它都無法播放。 我在 Replit,我曾嘗試使用 Replit 庫,但我也遇到了問題。 我想知道我是不是寫錯了什么,或者它只是Replit還是它是什么。 另外,如果您對循環有任何提示,以及如何在特定時間播放音樂,那就太好了。 下面的代碼只是一個小代碼,試圖弄清楚如何制作菜單,當我 select 一個顏色時,我打算讓音樂播放。 我剛剛嘗試刪除

pygame.init() 

但是當我這樣做時,一大堆東西就不再起作用了,例如 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()

您無法在repl上播放pygame中的音樂(據我所知),因為repl在虛擬機上運行。 顯然,這個功能是有計划的,會及時添加。 現在,我想說在不同的 IDE 上運行您的代碼,這應該可以。

您還應該看看這個: https://docs.replit.com/misc/playing-audio-replit#playing-audio-on-replit

對於循環音頻,您需要傳遞-1pygame.mixer.music.play(loops = -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM