Advertisement
dronkowitz

Pong.py

Mar 23rd, 2022 (edited)
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import turtle
  2. import winsound
  3. import time
  4.  
  5. # main title screen
  6. wn = turtle.Screen()
  7. wn.title("Pong")
  8. wn.bgcolor("Black")
  9. wn.setup(width=800, height=600)
  10. wn.tracer(0)
  11.  
  12. # score
  13. lscore = 0
  14. rscore = 0
  15.  
  16. playing = True
  17.  
  18. # ball
  19. ball = turtle.Turtle()
  20. ball.speed(0)
  21. ball.shape("square")
  22. ball.color("red")
  23. ball.penup()
  24. ball.goto(0, 0)
  25. ball.dx = 0.2
  26. ball.dy = 0.2
  27.  
  28. # left paddle
  29. lpaddle = turtle.Turtle()
  30. lpaddle.speed(0)
  31. lpaddle.shape("square")
  32. lpaddle.color("purple")
  33. lpaddle.shapesize(stretch_wid=5, stretch_len=1)
  34. lpaddle.penup()
  35. lpaddle.goto(-350, 0)
  36.  
  37. # right paddle
  38. rpaddle = turtle.Turtle()
  39. rpaddle.speed(0)
  40. rpaddle.shape("square")
  41. rpaddle.color("yellow")
  42. rpaddle.shapesize(stretch_wid=5, stretch_len=1)
  43. rpaddle.penup()
  44. rpaddle.goto(350, 0)
  45.  
  46. # pen
  47. pen = turtle.Turtle()
  48. pen.speed(0)
  49. pen.color("white")
  50. pen.penup()
  51. pen.hideturtle()
  52. pen.goto(0, 260)
  53. pen.write("Player A: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))
  54.  
  55.  
  56. # functions
  57. def l_paddle_up():
  58.     y = lpaddle.ycor()
  59.     y += 20
  60.     if y > 250:
  61.         y = 250
  62.     lpaddle.sety(y)
  63.  
  64.  
  65. def l_paddle_down():
  66.     y = lpaddle.ycor()
  67.     y -= 20
  68.     if y < -250:
  69.         y = -250
  70.     lpaddle.sety(y)
  71.  
  72.  
  73. def r_paddle_up():
  74.     y = rpaddle.ycor()
  75.     y += 20
  76.     if y > 250:
  77.         y = 250
  78.     rpaddle.sety(y)
  79.  
  80.  
  81. def r_paddle_down():
  82.     y = rpaddle.ycor()
  83.     y -= 20
  84.     if y < -250:
  85.         y = -250
  86.     rpaddle.sety(y)
  87.  
  88.  
  89. # Key Bindings
  90. wn.listen()
  91. wn.onkeypress(l_paddle_up, "w")
  92. wn.onkeypress(l_paddle_down, "s")
  93. wn.onkeypress(r_paddle_up, "Up")
  94. wn.onkeypress(r_paddle_down, "Down")
  95.  
  96. while playing:
  97.     wn.update()
  98.  
  99.     ball.setx(ball.xcor() + ball.dx)
  100.     ball.sety(ball.ycor() + ball.dy)
  101.  
  102.     if ball.ycor() > 290:
  103.         winsound.PlaySound("bounce.wav",winsound.SND_ASYNC)
  104.         ball.sety(290)
  105.         ball.dy *= -1
  106.  
  107.     if ball.ycor() < -290:
  108.         winsound.PlaySound("bounce.wav",winsound.SND_ASYNC)
  109.         ball.sety(-290)
  110.         ball.dy *= -1
  111.  
  112.     if ball.xcor() > 390:
  113.         winsound.PlaySound("score.wav", winsound.SND_ASYNC)
  114.         lscore += 1
  115.         ball.goto(0, 0)
  116.         ball.dx *= -1
  117.         pen.clear()
  118.         pen.write("Player A: {}  Player B: {}".format(lscore, rscore), align="center", font=("Courier", 24, "normal"))
  119.  
  120.     if ball.xcor() < -390:
  121.         winsound.PlaySound("score.wav", winsound.SND_NOWAIT)
  122.         rscore += 1
  123.         ball.goto(0, 0)
  124.         ball.dx *= -1
  125.         pen.clear()
  126.         pen.write("Player A: {}  Player B: {}".format(lscore, rscore), align="center", font=("Courier", 24, "normal"))
  127.  
  128.     # paddle check
  129.     if 330 < ball.xcor() < 340 and (rpaddle.ycor() + 60 > ball.ycor() > rpaddle.ycor() - 60):
  130.         winsound.PlaySound("bounce.wav", winsound.SND_NOWAIT)
  131.         ball.setx(330)
  132.         ball.dx *= -1
  133.  
  134.     if -330 > ball.xcor() > -340 and (lpaddle.ycor() + 60 > ball.ycor() > lpaddle.ycor() - 60):
  135.         winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
  136.         ball.setx(-330)
  137.         ball.dx *= -1
  138.  
  139.     if lscore >= 10:
  140.         pen.clear()
  141.         pen.write("left player wins", align="center", font=("Courier", 24, "normal"))
  142.         time.sleep(10)
  143.         playing = False
  144.  
  145.     if rscore >= 10:
  146.         pen.clear()
  147.         pen.write("right player wins", align="center", font=("Courier", 24, "normal"))
  148.         time.sleep(10)
  149.         playing = False
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement