Hello
In pong game I have a problem with adding the sound effect on windows as the sound that I dowanloaded isnot the sound that comes out and iam sure it is the same name.
# border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
winsound.PlaySound("bounce.wav" , winsound.SND_ASYNC)
and this is the complete code:
import turtle
import winsound
wn =turtle.Screen()
wn.title("ping pong")
wn.bgcolor("blue")
wn.setup(width=800, height=600)
wn.tracer(0)
#paddle A
paddle_A = turtle.Turtle()
paddle_A.speed(0)
paddle_A.shape("square")
paddle_A.color("white")
paddle_A.shapesize(stretch_wid=5,stretch_len=1)
paddle_A.penup()
paddle_A.goto(-350, 0)
#paddle B
paddle_B = turtle.Turtle()
paddle_B.speed(0)
paddle_B.shape("square")
paddle_B.color("white")
paddle_B.shapesize(stretch_wid=5,stretch_len=1)
paddle_B.penup()
paddle_B.goto(350, 0)
#ball
ball =turtle.Turtle()
ball.shape("circle")
ball.color("white")
ball.speed(0)
ball.penup()
ball.goto(0, 0)
ball.shapesize(stretch_wid=1.5)
ball.dx = 0.2
ball.dy = 0.2
#pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.goto(0,260)
pen.hideturtle()
pen.write("player A:0 player B:0", align="center", font=("corier", 24 ,"normal"))
#score
score_a = 0
score_b = 0
#movement
def paddle_A_up():
y = paddle_A.ycor()
y += 20
paddle_A.sety(y)
def paddle_A_down():
y = paddle_A.ycor()
y -= 20
paddle_A.sety(y)
#paddle_B_
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()
wn.onkeypress(paddle_A_up, "w")
wn.onkeypress(paddle_A_down, "s")
wn.onkeypress(paddle_B_up, "Up")
wn.onkeypress(paddle_B_down, "Down")
while True:
wn.update()
# move the ball.
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
winsound.PlaySound("bounce.wav" , winsound.SND_ASYNC)
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
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=("Courier", 24,"normal"))
if 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=("Courier", 24,"normal"))
# paddle and pall collisions
if ball.xcor() > 340 and ball.ycor() < paddle_B.ycor() + 50 and ball.ycor() > paddle_B.ycor() - 50:
ball.setx(340)
ball.dx *= -1
if ball.xcor() < -340 and ball.ycor() < paddle_A.ycor() + 50 and ball.ycor()> paddle_A.ycor() -50:
ball.setx(-340)
ball.dx *= -1
repost