Untitled

unknown
plain_text
a year ago
2.3 kB
2
Indexable
Never
```import turtle
import time

# Set up the game window
win = turtle.Screen()
win.title("Ping Pong Game")
win.bgcolor("black")
win.setup(width=600, height=400)

# Create the ball
ball = turtle.Turtle()
ball.speed(40)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 3
ball.dy = 3

# Move the left paddle up
y += 20

# Move the left paddle down
y -= 20

# Move the right paddle up
y += 20

# Move the right paddle down
y -= 20

# Bind the keys to the paddles
win.listen()

# Main game loop
while True:
win.update()

# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)

# Check the borders
if ball.ycor() > 190:
ball.sety(190)
ball.dy *= -1

if ball.ycor() < -190:
ball.sety(-190)
ball.dy *= -1

if ball.xcor() > 290:
ball.goto(0, 0)
ball.dy *= -1
time.sleep(1)

if ball.xcor() < -290:
ball.goto(0, 0)
ball.dy *= -1
time.sleep(1)

# Check the collisions with the paddles
if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < right_paddle.ycor() + 50 and ball.ycor() > right_paddle.ycor() - 50):
ball.setx(240)
ball.dx *= -1

if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < left_paddle.ycor() + 50 and ball.ycor() > left_paddle.ycor() - 50):
ball.setx(-240)
ball.dx *= -1
```