Untitled
unknown
plain_text
2 years ago
2.3 kB
7
Indexable
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 left paddle left_paddle = turtle.Turtle() left_paddle.speed(0) left_paddle.shape("square") left_paddle.color("white") left_paddle.shapesize(stretch_wid=5, stretch_len=1) left_paddle.penup() left_paddle.goto(-250, 0) # Create the right paddle right_paddle = turtle.Turtle() right_paddle.speed(0) right_paddle.shape("square") right_paddle.color("white") right_paddle.shapesize(stretch_wid=5, stretch_len=1) right_paddle.penup() right_paddle.goto(250, 0) # 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 def paddle_a_up(): y = left_paddle.ycor() y += 20 left_paddle.sety(y) # Move the left paddle down def paddle_a_down(): y = left_paddle.ycor() y -= 20 left_paddle.sety(y) # Move the right paddle up def paddle_b_up(): y = right_paddle.ycor() y += 20 right_paddle.sety(y) # Move the right paddle down def paddle_b_down(): y = right_paddle.ycor() y -= 20 right_paddle.sety(y) # Bind the keys to the paddles win.listen() win.onkeypress(paddle_a_up, "w") win.onkeypress(paddle_a_down, "s") win.onkeypress(paddle_b_up, "Up") win.onkeypress(paddle_b_down, "Down") # 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
Editor is loading...