Advertisement
naser2345

Game 2 Pong 2 Player

Aug 8th, 2018 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. # Imports
  2. """
  3. MADE BY Naser Al-Shaer
  4. """
  5. import turtle
  6. import os
  7.  
  8.  
  9. # Setup the screen
  10. wn = turtle.Screen()
  11. wn.title("Game By @Naser2018")
  12. wn.bgcolor("black")
  13. wn.setup(width=800, height=600)
  14. wn.tracer(0)
  15.  
  16.  
  17. # Score
  18. score_a = 0
  19. score_b = 0
  20.  
  21. #paddles width
  22. paddlesWidth = 100
  23.  
  24.  
  25. # Paddle A
  26. paddle_a = turtle.Turtle()
  27. paddle_a.speed(0)
  28. paddle_a.shape("square")
  29. paddle_a.shapesize(stretch_wid=paddlesWidth/20, stretch_len=1)
  30. paddle_a.color("white")
  31. paddle_a.penup()
  32. paddle_a.goto(-350, 0)
  33.  
  34.  
  35. # Paddle B
  36. paddle_b = turtle.Turtle()
  37. paddle_b.speed(0)
  38. paddle_b.shape("square")
  39. paddle_b.shapesize(stretch_wid=paddlesWidth/20, stretch_len=1)
  40. paddle_b.color("white")
  41. paddle_b.penup()
  42. paddle_b.goto(350, 0)
  43.  
  44.  
  45. # Ball
  46. ball = turtle.Turtle()
  47. ball.speed(0)
  48. ball.shape("circle")
  49. ball.color("white")
  50. ball.penup()
  51. ball.goto(0, 0)
  52. ball.dx = .08
  53. ball.dy = .08
  54.  
  55. # Pen
  56. pen = turtle.Turtle()
  57. pen.color("white")
  58. pen.speed(0)
  59. pen.penup()
  60. pen.hideturtle()
  61. pen.goto(0,260)
  62. pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
  63.  
  64. # Moves Functions
  65. def paddle_a_up():
  66.     if paddle_a.ycor() + 50 < 300:
  67.         paddle_a.sety(paddle_a.ycor() + paddlesWidth / 2)
  68. def paddle_a_down():
  69.     if paddle_a.ycor() - 50 > -300:
  70.         paddle_a.sety(paddle_a.ycor() - paddlesWidth / 2)
  71. def paddle_b_up():
  72.     if paddle_b.ycor() + 50 < 300:
  73.         paddle_b.sety(paddle_b.ycor() + paddlesWidth / 2)
  74. def paddle_b_down():
  75.     if paddle_b.ycor() - 50 > -300:
  76.         paddle_b.sety(paddle_b.ycor() - paddlesWidth / 2)
  77.  
  78. # Keyboards Binding
  79. wn.listen()
  80. wn.onkey(paddle_a_up, "w")
  81. wn.onkey(paddle_a_down, "s")
  82. wn.onkey(paddle_b_up, "Up")
  83. wn.onkey(paddle_b_down, "Down")
  84.  
  85.  
  86. # Main game loop
  87. while True:
  88.     wn.update()
  89.    
  90.  
  91.     # Ball moves
  92.     ball.setx(ball.xcor() + ball.dx)
  93.     ball.sety(ball.ycor() + ball.dy)
  94.  
  95.     # Border Check
  96.     if ball.ycor() > 290:
  97.         ball.sety(290)
  98.         ball.dy *= -1
  99.         os.system("aplay bounce.wav&")
  100.     if ball.ycor() < -290:
  101.         ball.sety(-290)
  102.         ball.dy *= -1
  103.         os.system("aplay bounce.wav&")
  104.  
  105.     if ball.xcor() > 390:
  106.         ball.goto(0,0)
  107.         ball.dx *= -1
  108.         score_a += 1
  109.         pen.clear()
  110.         pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
  111.         os.system("aplay score.wav")
  112.     if ball.xcor() < -390:
  113.         ball.goto(0,0)
  114.         ball.dx *= -1
  115.         score_b += 1
  116.         pen.clear()
  117.         pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
  118.         os.system("aplay score.wav")
  119.     # Paddles and ball collisions
  120.     if (ball.xcor() > 340 and ball.xcor() < 350 ) and (ball.ycor() > paddle_b.ycor() - 50 and ball.ycor() < paddle_b.ycor() + 50):   # 50 is 100/2 = paddleWidth / 2
  121.         ball.setx(340)
  122.         ball.dx *= -1
  123.         os.system("aplay bounce.wav&")
  124.     if (ball.xcor() < -340 and ball.xcor() > -350 ) and (ball.ycor() > paddle_a.ycor() - 50 and ball.ycor() < paddle_a.ycor() + 50):   # 50 is 100/2 = paddleWidth / 2
  125.         ball.setx(-340)
  126.         ball.dx *= -1
  127.         os.system("aplay bounce.wav&")
  128.  
  129. # Game Finished V0.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement