Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # Constants
- WIDTH = 800
- HEIGHT = 400
- PADDLE_WIDTH = 10
- PADDLE_HEIGHT = 60
- BALL_RADIUS = 10
- PADDLE_SPEED = 10
- BALL_SPEED_X = 5
- BALL_SPEED_Y = 5
- # Game variables
- score_left = 0
- score_right = 0
- # Initialize the window
- window = tk.Tk()
- window.title("Retro Ping Pong")
- window.geometry(f"{WIDTH}x{HEIGHT}")
- # Create the canvas for drawing
- canvas = tk.Canvas(window, bg="black", width=WIDTH, height=HEIGHT)
- canvas.pack()
- # Create the paddles
- paddle_left = canvas.create_rectangle(
- 50, HEIGHT // 2 - PADDLE_HEIGHT // 2,
- 50 + PADDLE_WIDTH, HEIGHT // 2 + PADDLE_HEIGHT // 2,
- fill="white"
- )
- paddle_right = canvas.create_rectangle(
- WIDTH - 50, HEIGHT // 2 - PADDLE_HEIGHT // 2,
- WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 + PADDLE_HEIGHT // 2,
- fill="white"
- )
- # Create the ball
- ball = canvas.create_oval(
- WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS,
- WIDTH // 2 + BALL_RADIUS, HEIGHT // 2 + BALL_RADIUS,
- fill="white"
- )
- # Add labels to display scores
- score_label = tk.Label(window, text="0 0", font=("Courier", 30), fg="white", bg="black")
- score_label.pack()
- # Initialize ball direction
- ball_speed_x = BALL_SPEED_X
- ball_speed_y = BALL_SPEED_Y
- # Function to move the paddles
- def move_paddle(event):
- key = event.keysym
- paddle_speed = PADDLE_SPEED
- # Move the left paddle
- if key == "w":
- canvas.move(paddle_left, 0, -paddle_speed)
- elif key == "s":
- canvas.move(paddle_left, 0, paddle_speed)
- # Move the right paddle
- if key == "Up":
- canvas.move(paddle_right, 0, -paddle_speed)
- elif key == "Down":
- canvas.move(paddle_right, 0, paddle_speed)
- # Bind keyboard events to the paddle movement function
- window.bind("<KeyPress>", move_paddle)
- # Function to reset ball and paddle positions
- def reset_positions():
- canvas.move(ball, WIDTH // 2 - canvas.coords(ball)[0], HEIGHT // 2 - canvas.coords(ball)[1])
- canvas.move(paddle_left, 0, HEIGHT // 2 - canvas.coords(paddle_left)[1])
- canvas.move(paddle_right, 0, HEIGHT // 2 - canvas.coords(paddle_right)[1])
- # Function to update the game
- def update():
- global ball_speed_x, ball_speed_y, score_left, score_right
- # Move the ball
- canvas.move(ball, ball_speed_x, ball_speed_y)
- # Retrieve the ball's current position
- ball_pos = canvas.coords(ball)
- # Detect collision with paddles
- paddles = canvas.find_overlapping(*ball_pos)
- if paddle_left in paddles or paddle_right in paddles:
- ball_speed_x *= -1
- # Detect collision with walls
- if ball_pos[0] <= 0:
- score_right += 1
- score_label.config(text=f"{score_left} {score_right}")
- ball_speed_x = BALL_SPEED_X
- ball_speed_y = BALL_SPEED_Y
- reset_positions()
- elif ball_pos[2] >= WIDTH:
- score_left += 1
- score_label.config(text=f"{score_left} {score_right}")
- ball_speed_x = -BALL_SPEED_X
- ball_speed_y = -BALL_SPEED_Y
- reset_positions()
- if ball_pos[1] <= 0 or ball_pos[3] >= HEIGHT:
- ball_speed_y *= -1
- # Check for game over
- if score_left >= 10 or score_right >= 10:
- canvas.unbind("<KeyPress>")
- game_over_label = tk.Label(window, text="Game Over!", font=("Courier", 40), fg="white", bg="black")
- game_over_label.pack()
- # Schedule the next update
- window.after(30, update)
- # Start the game
- window.after(0, update)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement