Advertisement
Guest User

Untitled

a guest
Aug 12th, 2024
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | Source Code | 0 0
  1.  
  2. import turtle
  3.  
  4.  
  5. sc = turtle.Screen()
  6. sc.title("Pong game")
  7. sc.bgcolor("white")
  8. sc.setup(width=1000, height=600)
  9.  
  10.  
  11. left_pad = turtle.Turtle()
  12. left_pad.speed(0)
  13. left_pad.shape("square")
  14. left_pad.color("black")
  15. left_pad.shapesize(stretch_wid=6, stretch_len=2)
  16. left_pad.penup()
  17. left_pad.goto(-400, 0)
  18.  
  19.  
  20.  
  21. right_pad = turtle.Turtle()
  22. right_pad.speed(0)
  23. right_pad.shape("square")
  24. right_pad.color("black")
  25. right_pad.shapesize(stretch_wid=6, stretch_len=2)
  26. right_pad.penup()
  27. right_pad.goto(400, 0)
  28.  
  29.  
  30.  
  31. hit_ball = turtle.Turtle()
  32. hit_ball.speed(40)
  33. hit_ball.shape("circle")
  34. hit_ball.color("blue")
  35. hit_ball.penup()
  36. hit_ball.goto(0, 0)
  37. hit_ball.dx = 5
  38. hit_ball.dy = -5
  39.  
  40.  
  41.  
  42. left_player = 0
  43. right_player = 0
  44.  
  45. sketch = turtle.Turtle()
  46. sketch.speed(0)
  47. sketch.color("blue")
  48. sketch.penup()
  49. sketch.hideturtle()
  50. sketch.goto(0, 260)
  51. sketch.write("Left_player : 0    Right_player: 0",
  52.              align="center", font=("Courier", 24, "normal"))
  53.  
  54.  
  55.  
  56. def paddleaup():
  57.     y = left_pad.ycor()
  58.     y += 20
  59.     left_pad.sety(y)
  60.  
  61.  
  62. def paddleadown():
  63.     y = left_pad.ycor()
  64.     y -= 20
  65.     left_pad.sety(y)
  66.  
  67.  
  68. def paddlebup():
  69.     y = right_pad.ycor()
  70.     y += 20
  71.     right_pad.sety(y)
  72.  
  73.  
  74. def paddlebdown():
  75.     y = right_pad.ycor()
  76.     y -= 20
  77.     right_pad.sety(y)
  78.  
  79.  
  80.  
  81. sc.listen()
  82. sc.onkeypress(paddleaup, "e")
  83. sc.onkeypress(paddleadown, "x")
  84. sc.onkeypress(paddlebup, "Up")
  85. sc.onkeypress(paddlebdown, "Down")
  86.  
  87.  
  88. while True:
  89.     sc.update()
  90.  
  91.     hit_ball.setx(hit_ball.xcor()+hit_ball.dx)
  92.     hit_ball.sety(hit_ball.ycor()+hit_ball.dy)
  93.  
  94.     # Checking borders
  95.     if hit_ball.ycor() > 280:
  96.         hit_ball.sety(280)
  97.         hit_ball.dy *= -1
  98.  
  99.     if hit_ball.ycor() < -280:
  100.         hit_ball.sety(-280)
  101.         hit_ball.dy *= -1
  102.  
  103.     if hit_ball.xcor() > 500:
  104.         hit_ball.goto(0, 0)
  105.         hit_ball.dy *= -1
  106.         left_player += 1
  107.         sketch.clear()
  108.         sketch.write("Left_player : {}    Right_player: {}".format(
  109.                       left_player, right_player), align="center",
  110.                       font=("Courier", 24, "normal"))
  111.  
  112.     if hit_ball.xcor() < -500:
  113.         hit_ball.goto(0, 0)
  114.         hit_ball.dy *= -1
  115.         right_player += 1
  116.         sketch.clear()
  117.         sketch.write("Left_player : {}    Right_player: {}".format(
  118.                                  left_player, right_player), align="center",
  119.                                  font=("Courier", 24, "normal"))
  120.  
  121.     if (hit_ball.xcor() > 360 and
  122.                         hit_ball.xcor() < 370) and
  123.                         (hit_ball.ycor() < right_pad.ycor()+40 and
  124.                         hit_ball.ycor() > right_pad.ycor()-40):
  125.         hit_ball.setx(360)
  126.         hit_ball.dx*=-1
  127.        
  128.     if (hit_ball.xcor()<-360 and
  129.                        hit_ball.xcor()>-370) and
  130.                        (hit_ball.ycor()<left_pad.ycor()+40 and
  131.                         hit_ball.ycor()>left_pad.ycor()-40):
  132.         hit_ball.setx(-360)
  133.         hit_ball.dx*=-1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement