Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import winsound
- import time
- # main title screen
- wn = turtle.Screen()
- wn.title("Pong")
- wn.bgcolor("Black")
- wn.setup(width=800, height=600)
- wn.tracer(0)
- # score
- lscore = 0
- rscore = 0
- playing = True
- # ball
- ball = turtle.Turtle()
- ball.speed(0)
- ball.shape("square")
- ball.color("red")
- ball.penup()
- ball.goto(0, 0)
- ball.dx = 0.2
- ball.dy = 0.2
- # left paddle
- lpaddle = turtle.Turtle()
- lpaddle.speed(0)
- lpaddle.shape("square")
- lpaddle.color("purple")
- lpaddle.shapesize(stretch_wid=5, stretch_len=1)
- lpaddle.penup()
- lpaddle.goto(-350, 0)
- # right paddle
- rpaddle = turtle.Turtle()
- rpaddle.speed(0)
- rpaddle.shape("square")
- rpaddle.color("yellow")
- rpaddle.shapesize(stretch_wid=5, stretch_len=1)
- rpaddle.penup()
- rpaddle.goto(350, 0)
- # pen
- pen = turtle.Turtle()
- pen.speed(0)
- pen.color("white")
- pen.penup()
- pen.hideturtle()
- pen.goto(0, 260)
- pen.write("Player A: 0 Player B: 0", align="center", font=("Courier", 24, "normal"))
- # functions
- def l_paddle_up():
- y = lpaddle.ycor()
- y += 20
- if y > 250:
- y = 250
- lpaddle.sety(y)
- def l_paddle_down():
- y = lpaddle.ycor()
- y -= 20
- if y < -250:
- y = -250
- lpaddle.sety(y)
- def r_paddle_up():
- y = rpaddle.ycor()
- y += 20
- if y > 250:
- y = 250
- rpaddle.sety(y)
- def r_paddle_down():
- y = rpaddle.ycor()
- y -= 20
- if y < -250:
- y = -250
- rpaddle.sety(y)
- # Key Bindings
- wn.listen()
- wn.onkeypress(l_paddle_up, "w")
- wn.onkeypress(l_paddle_down, "s")
- wn.onkeypress(r_paddle_up, "Up")
- wn.onkeypress(r_paddle_down, "Down")
- while playing:
- wn.update()
- ball.setx(ball.xcor() + ball.dx)
- ball.sety(ball.ycor() + ball.dy)
- if ball.ycor() > 290:
- winsound.PlaySound("bounce.wav",winsound.SND_ASYNC)
- ball.sety(290)
- ball.dy *= -1
- if ball.ycor() < -290:
- winsound.PlaySound("bounce.wav",winsound.SND_ASYNC)
- ball.sety(-290)
- ball.dy *= -1
- if ball.xcor() > 390:
- winsound.PlaySound("score.wav", winsound.SND_ASYNC)
- lscore += 1
- ball.goto(0, 0)
- ball.dx *= -1
- pen.clear()
- pen.write("Player A: {} Player B: {}".format(lscore, rscore), align="center", font=("Courier", 24, "normal"))
- if ball.xcor() < -390:
- winsound.PlaySound("score.wav", winsound.SND_NOWAIT)
- rscore += 1
- ball.goto(0, 0)
- ball.dx *= -1
- pen.clear()
- pen.write("Player A: {} Player B: {}".format(lscore, rscore), align="center", font=("Courier", 24, "normal"))
- # paddle check
- if 330 < ball.xcor() < 340 and (rpaddle.ycor() + 60 > ball.ycor() > rpaddle.ycor() - 60):
- winsound.PlaySound("bounce.wav", winsound.SND_NOWAIT)
- ball.setx(330)
- ball.dx *= -1
- if -330 > ball.xcor() > -340 and (lpaddle.ycor() + 60 > ball.ycor() > lpaddle.ycor() - 60):
- winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
- ball.setx(-330)
- ball.dx *= -1
- if lscore >= 10:
- pen.clear()
- pen.write("left player wins", align="center", font=("Courier", 24, "normal"))
- time.sleep(10)
- playing = False
- if rscore >= 10:
- pen.clear()
- pen.write("right player wins", align="center", font=("Courier", 24, "normal"))
- time.sleep(10)
- playing = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement