Advertisement
CodeCrusader

Classic Retro Ping Pong Game

Jun 10th, 2023 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | Software | 0 0
  1. import tkinter as tk
  2.  
  3. # Constants
  4. WIDTH = 800
  5. HEIGHT = 400
  6. PADDLE_WIDTH = 10
  7. PADDLE_HEIGHT = 60
  8. BALL_RADIUS = 10
  9. PADDLE_SPEED = 10
  10. BALL_SPEED_X = 5
  11. BALL_SPEED_Y = 5
  12.  
  13. # Game variables
  14. score_left = 0
  15. score_right = 0
  16.  
  17. # Initialize the window
  18. window = tk.Tk()
  19. window.title("Retro Ping Pong")
  20. window.geometry(f"{WIDTH}x{HEIGHT}")
  21.  
  22. # Create the canvas for drawing
  23. canvas = tk.Canvas(window, bg="black", width=WIDTH, height=HEIGHT)
  24. canvas.pack()
  25.  
  26. # Create the paddles
  27. paddle_left = canvas.create_rectangle(
  28.     50, HEIGHT // 2 - PADDLE_HEIGHT // 2,
  29.     50 + PADDLE_WIDTH, HEIGHT // 2 + PADDLE_HEIGHT // 2,
  30.     fill="white"
  31. )
  32. paddle_right = canvas.create_rectangle(
  33.     WIDTH - 50, HEIGHT // 2 - PADDLE_HEIGHT // 2,
  34.     WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 + PADDLE_HEIGHT // 2,
  35.     fill="white"
  36. )
  37.  
  38. # Create the ball
  39. ball = canvas.create_oval(
  40.     WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS,
  41.     WIDTH // 2 + BALL_RADIUS, HEIGHT // 2 + BALL_RADIUS,
  42.     fill="white"
  43. )
  44.  
  45. # Add labels to display scores
  46. score_label = tk.Label(window, text="0   0", font=("Courier", 30), fg="white", bg="black")
  47. score_label.pack()
  48.  
  49. # Initialize ball direction
  50. ball_speed_x = BALL_SPEED_X
  51. ball_speed_y = BALL_SPEED_Y
  52.  
  53. # Function to move the paddles
  54. def move_paddle(event):
  55.     key = event.keysym
  56.     paddle_speed = PADDLE_SPEED
  57.  
  58.     # Move the left paddle
  59.     if key == "w":
  60.         canvas.move(paddle_left, 0, -paddle_speed)
  61.     elif key == "s":
  62.         canvas.move(paddle_left, 0, paddle_speed)
  63.  
  64.     # Move the right paddle
  65.     if key == "Up":
  66.         canvas.move(paddle_right, 0, -paddle_speed)
  67.     elif key == "Down":
  68.         canvas.move(paddle_right, 0, paddle_speed)
  69.  
  70. # Bind keyboard events to the paddle movement function
  71. window.bind("<KeyPress>", move_paddle)
  72.  
  73. # Function to reset ball and paddle positions
  74. def reset_positions():
  75.     canvas.move(ball, WIDTH // 2 - canvas.coords(ball)[0], HEIGHT // 2 - canvas.coords(ball)[1])
  76.     canvas.move(paddle_left, 0, HEIGHT // 2 - canvas.coords(paddle_left)[1])
  77.     canvas.move(paddle_right, 0, HEIGHT // 2 - canvas.coords(paddle_right)[1])
  78.  
  79. # Function to update the game
  80. def update():
  81.     global ball_speed_x, ball_speed_y, score_left, score_right
  82.  
  83.     # Move the ball
  84.     canvas.move(ball, ball_speed_x, ball_speed_y)
  85.  
  86.     # Retrieve the ball's current position
  87.     ball_pos = canvas.coords(ball)
  88.  
  89.     # Detect collision with paddles
  90.     paddles = canvas.find_overlapping(*ball_pos)
  91.     if paddle_left in paddles or paddle_right in paddles:
  92.         ball_speed_x *= -1
  93.  
  94.     # Detect collision with walls
  95.     if ball_pos[0] <= 0:
  96.         score_right += 1
  97.         score_label.config(text=f"{score_left}   {score_right}")
  98.         ball_speed_x = BALL_SPEED_X
  99.         ball_speed_y = BALL_SPEED_Y
  100.         reset_positions()
  101.     elif ball_pos[2] >= WIDTH:
  102.         score_left += 1
  103.         score_label.config(text=f"{score_left}   {score_right}")
  104.         ball_speed_x = -BALL_SPEED_X
  105.         ball_speed_y = -BALL_SPEED_Y
  106.         reset_positions()
  107.  
  108.     if ball_pos[1] <= 0 or ball_pos[3] >= HEIGHT:
  109.         ball_speed_y *= -1
  110.  
  111.     # Check for game over
  112.     if score_left >= 10 or score_right >= 10:
  113.         canvas.unbind("<KeyPress>")
  114.         game_over_label = tk.Label(window, text="Game Over!", font=("Courier", 40), fg="white", bg="black")
  115.         game_over_label.pack()
  116.  
  117.     # Schedule the next update
  118.     window.after(30, update)
  119.  
  120. # Start the game
  121. window.after(0, update)
  122. window.mainloop()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement