Advertisement
KattCode

Pong Game Code

Mar 4th, 2021
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # Simple Pong
  2. # By KattCode
  3.  
  4. import turtle
  5. import os
  6.  
  7. wn = turtle.Screen()
  8. wn.title("Pong")
  9. wn.bgcolor("black")
  10. wn.setup(width=800, height=600)
  11. wn.tracer(0)
  12.  
  13. # Score
  14. score_a = 0
  15. score_b = 0
  16.  
  17. # Paddle A
  18. paddle_a = turtle.Turtle()
  19. paddle_a.speed(0)
  20. paddle_a.shape("square")
  21. paddle_a.color("white")
  22. paddle_a.shapesize(stretch_wid=5,stretch_len=1)
  23. paddle_a.penup()
  24. paddle_a.goto(-350, 0)
  25.  
  26. # Paddle B
  27. paddle_b = turtle.Turtle()
  28. paddle_b.speed(0)
  29. paddle_b.shape("square")
  30. paddle_b.color("white")
  31. paddle_b.shapesize(stretch_wid=5,stretch_len=1)
  32. paddle_b.penup()
  33. paddle_b.goto(350, 0)
  34.  
  35. # Ball
  36. ball = turtle.Turtle()
  37. ball.speed(0)
  38. ball.shape("square")
  39. ball.color("white")
  40. ball.penup()
  41. ball.goto(0, 0)
  42. ball.dx = 2
  43. ball.dy = 2
  44.  
  45. # Pen
  46. pen = turtle.Turtle()
  47. pen.speed(0)
  48. pen.shape("square")
  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. # Functions
  56. def paddle_a_up():
  57.     y = paddle_a.ycor()
  58.     y += 20
  59.     paddle_a.sety(y)
  60.  
  61. def paddle_a_down():
  62.     y = paddle_a.ycor()
  63.     y -= 20
  64.     paddle_a.sety(y)
  65.  
  66. def paddle_b_up():
  67.     y = paddle_b.ycor()
  68.     y += 20
  69.     paddle_b.sety(y)
  70.  
  71. def paddle_b_down():
  72.     y = paddle_b.ycor()
  73.     y -= 20
  74.     paddle_b.sety(y)
  75.  
  76. # Keyboard bindings
  77. wn.listen()
  78. wn.onkeypress(paddle_a_up, "w")
  79. wn.onkeypress(paddle_a_down, "s")
  80. wn.onkeypress(paddle_b_up, "Up")
  81. wn.onkeypress(paddle_b_down, "Down")
  82.  
  83. # Main game loop
  84. while True:
  85.     wn.update()
  86.    
  87.     # Move the ball
  88.     ball.setx(ball.xcor() + ball.dx)
  89.     ball.sety(ball.ycor() + ball.dy)
  90.  
  91.     # Border checking
  92.  
  93.     # Top and bottom
  94.     if ball.ycor() > 290:
  95.         ball.sety(290)
  96.         ball.dy *= -1
  97.         os.system("afplay bounce.wav&")
  98.    
  99.     elif ball.ycor() < -290:
  100.         ball.sety(-290)
  101.         ball.dy *= -1
  102.         os.system("afplay bounce.wav&")
  103.  
  104.     # Left and right
  105.     if ball.xcor() > 350:
  106.         score_a += 1
  107.         pen.clear()
  108.         pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
  109.         ball.goto(0, 0)
  110.         ball.dx *= -1
  111.  
  112.     elif ball.xcor() < -350:
  113.         score_b += 1
  114.         pen.clear()
  115.         pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
  116.         ball.goto(0, 0)
  117.         ball.dx *= -1
  118.  
  119.     # Paddle and ball collisions
  120.     if ball.xcor() < -340 and ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50:
  121.         ball.dx *= -1
  122.         os.system("afplay bounce.wav&")
  123.    
  124.     elif ball.xcor() > 340 and ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50:
  125.         ball.dx *= -1
  126.         os.system("afplay bounce.wav&")
  127.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement