Advertisement
Guest User

Untitled

a guest
Jan 5th, 2021
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # Ping-Pong game with turtle module.
  2. # Done by Sri Manikanta Palakollu.
  3. # Version - 3.7.0
  4.  
  5. import turtle as t
  6. import json
  7. import socket
  8.  
  9. ball_xy = [0, 0]
  10.  
  11.  
  12. win = t.Screen()    # creating a window
  13. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14. s.connect(("айпишка сервака", 10082))
  15. win.title("Ping-Pong Game") # Giving name to the game.
  16. win.bgcolor('black')    # providing color to the HomeScreen
  17. win.setup(width=1920, height=1080) # Size of the game panel
  18. win.tracer(0)   # which speed up's the game.
  19.  
  20. # Creating a right paddle for the game
  21.  
  22. paddle_right = t.Turtle()
  23. paddle_right.speed(0)
  24. paddle_right.shape('square')
  25. paddle_right.shapesize(stretch_wid=5,stretch_len=1)
  26. paddle_right.color('red')
  27. paddle_right.penup()
  28. paddle_right.goto(900, 0)
  29.  
  30. # Creating a pong ball for the game
  31.  
  32. ball = t.Turtle()
  33. ball.speed(0)
  34. ball.shape('circle')
  35. ball.color('yellow')
  36. ball.penup()
  37. ball.goto(0, 0)
  38. ball_dx = 5  # Setting up the pixels for the ball movement.
  39. ball_dy = 5
  40.  
  41.  
  42.  
  43. def paddle_right_up():
  44.     y = paddle_right.ycor()
  45.     y = y + 15
  46.     paddle_right.sety(y)
  47.  
  48. # Moving right paddle down
  49.  
  50. def paddle_right_down():
  51.     y = paddle_right.ycor()
  52.     y = y - 15
  53.     paddle_right.sety(y)
  54.  
  55.  
  56. win.onkeypress(paddle_right_up,"Up")
  57. win.onkeypress(paddle_right_down,"Down")
  58.  
  59.  
  60.  
  61.  
  62. # Main Game Loop
  63.  
  64. while True:
  65.     win.update() # This methods is mandatory to run any game
  66.  
  67.     # Moving the ball
  68.     ball.setx(ball.xcor() + ball_dx)
  69.     ball.sety(ball.ycor() + ball_dy)
  70.  
  71.     # setting up the border
  72.  
  73.     if ball.ycor() > 540:   # Right top paddle Border
  74.         ball.sety(540)
  75.         ball_dy = ball_dy * -1
  76.  
  77.  
  78.     if ball.ycor() < -540:  # Left top paddle Border
  79.         ball.sety(-540)
  80.         ball_dy = ball_dy * -1
  81.  
  82.  
  83.     if ball.xcor() > 960:   # right width paddle Border
  84.         ball.setx(960)
  85.         ball_dx = ball_dx * -1
  86.  
  87.  
  88.     # Handling the collisions with paddles.
  89.  
  90.     if(ball.xcor() > 890) and (ball.xcor() < 900) and (ball.ycor() < paddle_right.ycor() + 40 and ball.ycor() > paddle_right.ycor() - 40):
  91.         ball.setx(890)
  92.         ball_dx = ball_dx * -1
  93.  
  94.     ball_xy = [ball.xcor(), ball.ycor()]
  95.     ready_data = json.dumps(ball_xy)
  96.     s.send(ready_data.encode())
  97.     ball_xy = []
  98.     ready_data = ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement