Как добавить фоновую песню в эту игру понг?Использование библиотеки 'winsound' для Python3 - PullRequest
0 голосов
/ 10 марта 2019

В этой игре в понг я использую библиотеку 'winsound' для python3. Когда мяч ударяется о весло, он издает звук отскока. Я также хочу добавить файл .wav, чтобы песня могла играть на протяжении всего игрового процесса. Где и как мне добавить строку кода?

Это будет строка кода:

winsound.PlaySound ("D: \ Python \ Pong_game_turtle \ one_more_time_daft_punk_8BitUniverse.wav", winsound.SND_ASYNC)

import turtle
import winsound

#window creation
wn = turtle.Screen()
wn.title("Classic Pong")
wn.bgcolor("blue")
wn.setup(width=800, height=600) # size in pixels
wn.tracer(0) #stops auto-update of winodw to speed up the game
#Score 
score_a = 0
score_b = 0

#Arena
arena = turtle.Turtle()
arena.shape("square")
arena.color("orange")
arena.shapesize(stretch_wid=30, stretch_len=0.1)
arena.penup()
arena.goto(0, 0)

# Paddle A
paddle_a=turtle.Turtle() #turtle object
paddle_a.speed(0) #speed of animation
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup() #prevents the paddle from making a trace as it moves
paddle_a.goto(-350, 0)

#Paddle B
paddle_b=turtle.Turtle() #turtle object
paddle_b.speed(0) #speed of animation
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup() #prevents the paddle from making a trace as it moves
paddle_b.goto(350, 0)

#Ball
ball=turtle.Turtle() #turtle object
ball.speed(0) #speed of animation
ball.shape("square")
ball.color("white")
ball.penup() #prevents the paddle from making a trace as it moves
ball.goto(0, 0)
ball.dx = 0.2 # delta x (the ball will move 1 position)
ball.dy = -0.2

#Pen 

pen = turtle.Turtle()
pen.speed(0)
pen.color("Orange")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player A: 0  Player B: 0", align="center", font=("Fixedsys", 24, "normal"))

# Functions 
def paddle_a_up():
    y = paddle_a.ycor() #ycor returns y coordinate
    y += 20
    paddle_a.sety(y) #set y to the new y

def paddle_a_down():
    y = paddle_a.ycor()
    y -= 20
    paddle_a.sety(y) 

def paddle_b_up():
    y = paddle_b.ycor()
    y += 20
    paddle_b.sety(y) 

def paddle_b_down():
    y = paddle_b.ycor() 
    y -= 20
    paddle_b.sety(y) 

#Keyboard binding 

wn.listen()   #listen for keyboard input
wn.onkeypress(paddle_a_up, "w") #when w is pressed paddle_a_up() is called
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up") #up key
wn.onkeypress(paddle_b_down, "Down") #down key

# Main game loop 
while True:
    wn.update()  #everytime the loop runs it updates the screen

    #Moving the ball

    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    #Border checking

    #Top & bottom
    if ball.ycor() > 290:  
        ball.sety(290)
        ball.dy *=-1  #reverses direction
        winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC)

    elif ball.ycor() < -290:  
        ball.sety(-290)
        ball.dy *=-1  
        winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC)

    #Left and right
    if ball.xcor() > 390:
        ball.goto(0, 0)
        ball.dx *=-1
        score_a +=1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Fixedsys", 24, "normal"))

    elif ball.xcor() < -390:
        ball.goto(0, 0)
        ball.dx *=-1
        score_b +=1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Fixedsys", 24, "normal"))

    #Paddle and ball collisions 

    #Right paddle
    #Checking if the ball is in the area in front of the paddle, right at the paddle's surface
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
        ball.setx(340)
        ball.dx *= -1
        winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC)

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
        ball.setx(-340)
        ball.dx *= -1
        winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC)
...