Advertisement
Guest User

Untitled

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