在 pygame 中播放音频的问题

问题描述

这是我的代码

import pygame
import random
import time

pygame.init()
backX = 1000
backY = 600
screen = pygame.display.set_mode((backX,backY))
score = 0
white = (255,255,255)
green = (0,0)
blue = (0,128)
timespent = 0
pygame.display.set_caption('Monkey Simulator')
pygame.font.init()  # you have to call this at the start,# if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS',30)
textsurface = myfont.render('score: ' + str(score),False,(255,255))
pygame.mixer.init()
pygame.mixer.music.load('background1.mp3')
pygame.mixer.music.play()
text = myfont.render('score: ' + str(score),True,white,blue)

# create a rectangular object for the
# text surface object
textRect = text.get_rect()

# set the center of the rectangular object.
textRect.center = (400 // 2,400 // 2)

pre_background = pygame.image.load('background.jpeg')

background = pygame.transform.scale(pre_background,(backX,backY))
clock = pygame.time.Clock()
FPS = 60
vel = 6.5
BLACK = (0,0)
monkeyimg = pygame.image.load('monkey.png')
playerX = 410
playerY = 435
bananveLocity = 4
monkey = pygame.transform.scale(monkeyimg,(100,120))
prebanana = pygame.image.load('banana.png')
bananaXList = []

for i in range(30):
    value = random.randint(10,980)
    bananaXList.append(value)
valuenumber = random.randint(1,30)
bananaX = bananaXList[valuenumber - 1]
bananaY = 0
banana = pygame.transform.scale(prebanana,(50,50))

banana_rect = banana.get_rect(topleft=(bananaX,bananaY))
monkey_rect = monkey.get_rect(topleft=(playerX,playerY))
run = True

while run:
    clock.tick(FPS)
    screen.fill(BLACK)
    screen.blit(background,(0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    bananaY = bananaY + bananveLocity
    timespent = int(pygame.time.get_ticks() / 1000)
    bananveLocity = 4 + (timespent * 0.1)
    vel = 6.5 + (timespent * 0.05)

    if bananaY > 510:
        bananaX = -50
        bananaY = -50
        bananveLocity = 0
        gameover = pygame.image.load("gameover.jpg")
        background = pygame.transform.scale(gameover,backY))

    if banana_rect.colliderect(monkey_rect):
        valuenumber = random.randint(1,30)

        bananaX = bananaXList[valuenumber - 1]

        bananaY = -25

        score += 1
    if keys[pygame.K_LEFT] and playerX > 0:
        playerX = playerX - vel
    if keys[pygame.K_RIGHT] and playerX < 930:
        playerX = playerX + vel
    screen.blit(monkey,(playerX,playerY))
    screen.blit(banana,(bananaX,bananaY))
    banana_rect = banana.get_rect(topleft=(bananaX,bananaY))
    monkey_rect = monkey.get_rect(topleft=(playerX,playerY))
    pygame.draw.rect(screen,(150,75,0),pygame.Rect(0,534,1000,20))
    screen.blit(textsurface,(30,0))
    textsurface = myfont.render('score: ' + str(score),255))

    pygame.display.update()

目前,每当我运行此代码时,它都应该启动游戏,然后播放音乐。但是我只是得到这个error message

我很确定我拥有所有必需的文件。但是,也许我没有将它们放在正确的位置或其他什么地方。我在互联网上查过这个问题,但没有发现任何可以解决的问题,因为大多数时候人们都提到了海报代码的问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)