Pygame声音无法正确导入

时间:2016-03-24 23:45:13

标签: python audio pygame mixer

因此,对于我的游戏,当我越过边界或(稍后在开发中)击中敌人时,我希望有一个碰撞声效果。我遇到的问题始于声音的初始化部分。 python / pygame的详细错误给了我"(unicode错误)' unicodeescape'编解码器无法解码位置2-3中的字节:截断/ UXXXXXXXX转义。 如果我没有将文件放在正确的位置,这是一个错误吗? 有关修复此问题的任何建议吗?谢谢 至于可能的重复,我尝试了使用的方法,它修复了我上面的错误,但现在说"无法打开文件'

import pygame
import random

# basic setting
global inventory

window_name = "Need for coinage: 2016"
display_width = 1200
display_height = 800

pygame.init()

错误区域

crahs_sound = pygame.mixer.Sound('C:\Users\Ayden\AppData\Local\Programs\Python\Python35-32\sounds')

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
purple = (160,32,240)
neon_blue = (10, 20, 238)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption(window_name)

pygame.display.update()
# game loop

def message_display(text,x,y):
    mediumText = pygame.font.Font('freesansbold.ttf', 30)
    TextSurf, TextRect = text_objects(text, mediumText)
    TextRect.center = ((x), (y))
    gameDisplay.blit(TextSurf, TextRect)

def text_objects(text, font):
    textSurface = font.render(text,True,black)
    return textSurface, textSurface.get_rect()

def speed_display():
    message_display(("Speed: " + str(speed) +"(" + str(speed_boost_reserve) + ")"), (display_width /2), (display_height - 785))

def coin_display():
    message_display("Coins: " + str(coin), (display_width -1125), (display_height - 785))

def lifes_display():
    message_display(("Lifes: " + str(lifes) + "(" + str(life_reserve) + ")"), (display_width -100), (display_height - 785))

def player(x,y,width,height,color):
    pygame.draw.rect(gameDisplay, color, [x,y,width,height])

def speed_up(x,y,width,height,color):
    pygame.draw.rect(gameDisplay, color, [x,y,width,height])

def coins(x,y,width,height,color):
    pygame.draw.rect(gameDisplay, color, [x,y,width,height])

def lifes1(x,y,width,height,color):
    pygame.draw.rect(gameDisplay, color, [x,y,width,height])

def coin_xy_update():
    global coin_x
    global coin_y

    coin_x = random.randrange(0,display_width - coin_width)
    coin_y = random.randrange(75, display_height - coin_height)

def lifes_xy_update():
    global life_x
    global life_y

    life_x = random.randrange(0,display_width - coin_width)
    life_y = random.randrange(75, display_height - coin_height)

def speed_boost():
    pygame.time.set_timer(pygame.USEREVENT+1, 1000)


clock = pygame.time.Clock()
inventory = []
def game_loop():
    #globals
    global speed
    global speed_boost_reserve
    global time
    global coin
    global coin_width
    global coin_height
    global lifes
    global life_reserve
    time = 5
    speed = 10
    gameExit = False
    lead_x = (display_width / 2)
    lead_y = (display_height / 2)
    player_x_change = 0
    player_y_change = 0
    # player dimensions
    height = 20
    width = 20
    player_color = neon_blue
    # end
    # speed
    speed_x = 200
    speed_y = 300
    speed_width = 40
    speed_height = 40
    speed_collected = 0
    speed_boost_reserve = 0
    speed_boost_amount = 7
    #coin
    coin_width = 25
    coin_height = 25
    coin_color = yellow
    coin = 0
    coin_xy_update()
    # lifes
    lifes = 3
    life_width = 40
    life_height = 40
    life_color = purple
    life_reserve = 0
    lifes_xy_update()

    pygame.display.update

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if lead_x > 0 and lead_x < display_width and lead_y > 0 and lead_y < display_height:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        player_y_change = (speed * -1)
                    elif event.key == pygame.K_s:
                        player_y_change = speed
                    elif event.key == pygame.K_a:
                        player_x_change = (speed * -1)
                    elif event.key == pygame.K_d:
                        player_x_change = speed
                    elif event.key == pygame.K_q:
                        if speed_boost_reserve > 0:
                            speed_boost()
                            inventory.remove('speed boost')
                            speed_boost_reserve -= 1

                    elif event.key == pygame.K_e:
                       if life_reserve > 0:
                           lifes += 1
                       if life_reserve > 0:
                           inventory.remove('life')
                       if life_reserve > 0:
                           life_reserve -= 1
            else:
                lead_x = display_width/2
                lead_y = display_height/2
                if lifes < 1:
                    gameExit = True
                if lifes > 0:
                    lifes -= 1
            if event.type == pygame.KEYUP:
                player_y_change = 0
                player_x_change = 0

            if event.type == pygame.USEREVENT+1:
                if speed_collected == 1 and time == 5:
                    speed += speed_boost_amount
                if speed_collected == 1 and speed > 10:
                    time -= 1
            if time == 0 and speed > 10:
                speed -= speed_boost_amount

        lead_y += player_y_change
        lead_x += player_x_change
        #physics

        #speed
        if lead_x > speed_x and lead_x < speed_x + speed_width and lead_y + height > speed_y and lead_y + height < speed_y + speed_height:          
            if speed_collected == 0:
                inventory.append('speed boost')
                speed_collected += 1
        if lead_x > coin_x and lead_x < coin_x + coin_width and lead_y + height > coin_y and lead_y + height < coin_y + coin_height:
            coin += 1
            coin_xy_update()
        if lead_x > life_x and lead_x < life_x + life_width and lead_y + height > life_y and lead_y + height < life_y + life_height:
            if life_reserve < 1:
                inventory.append('life')
                lifes_xy_update()

        for x in inventory:
            if x == 'life' and life_reserve < 1:
                life_reserve += 1
        for x in inventory:
            if x == 'speed boost' and speed_boost_reserve < 1:
                speed_boost_reserve += 1
        gameDisplay.fill(green)
        if speed_collected != 1:
            speed_up(speed_x,speed_y, speed_width,speed_height, red)
        player(lead_x, lead_y, width, height, player_color)
        speed_display()
        coin_display()
        lifes_display()

        coins(coin_x, coin_y, coin_width, coin_height, coin_color)
        if life_reserve < 1:
            lifes1(life_x, life_y, life_width, life_height, life_color)
        pygame.display.update()
        clock.tick(45)

game_loop()
pygame.quit()
quit()

0 个答案:

没有答案