pong

 avatar
unknown
python
13 days ago
5.2 kB
4
Indexable
import turtle  # Import the turtle module to create graphics
from time import sleep  # Importing sleep function to pause the game when needed

# Set up the game screen
screen = turtle.Screen()  # Create the game window
screen.title("Pong")  # Set the title of the game window
screen.bgcolor("black")  # Make the background black
screen.setup(width=800, height=600)  # Set the window size
screen.tracer(0)  # Disable automatic screen updates to manually control rendering

# Create the left paddle
left_paddle = turtle.Turtle()  # Create a new turtle object for the left paddle
left_paddle.speed(0)  # Set the speed of the paddle to the fastest
left_paddle.shape("square")  # Set the shape of the paddle to a square
left_paddle.color("white")  # Make the paddle white
left_paddle.shapesize(stretch_wid=6, stretch_len=1)  # Stretch the shape to look like a paddle
left_paddle.penup()  # Prevent drawing lines when moving
left_paddle.goto(-350, 0)  # Position the paddle on the left side of the screen

# 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=6, stretch_len=1)
right_paddle.penup()
right_paddle.goto(350, 0)  # Position the paddle on the right side of the screen

# Create the ball
ball = turtle.Turtle()
ball.speed(1)  # Set the initial speed of the ball
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)  # Start the ball in the center
ball.dx = 0.5  # Move the ball in the x direction
ball.dy = 0.5  # Move the ball in the y direction

# Function to move paddles up and down
def move_paddle(i,j):
    if i == j:
        return
    elif i == 1:  # If input is positive, move paddles up
        if right_paddle.ycor() < 250:  # Check if paddle is within the top boundary
            right_paddle.sety(right_paddle.ycor() + 1)
        if left_paddle.ycor() < 250:
            left_paddle.sety(left_paddle.ycor() + 1)
    elif j == 1:  # If input is negative, move paddles down
        if right_paddle.ycor() > -240:  # Check if paddle is within the bottom boundary
            right_paddle.sety(right_paddle.ycor() - 1)
        if left_paddle.ycor() > -240:
            left_paddle.sety(left_paddle.ycor() - 1)

# Function to make paddles follow the ball automatically
def auto_move_paddle():
    right_paddle.sety(ball.ycor())
    left_paddle.sety(ball.ycor())

# Class to track player's input and score
class total:
    def __init__(self):
        self.in_y = 0  # Track movement input
        self.in_y2 = 0;
        self.max = 0  # Track highest score

    def reset(self):
        if self.in_y > self.max:  # Update max score if necessary
            self.max = self.in_y
        self.in_y = 0  # Reset current score

    def add(self, i):
        self.in_y += i  # Increase or decrease input movement

    def sety(self, i):
        self.in_y = i # sets the value of in_y to the param

    def sety2(self, i):
        self.in_y2 = i  # sets the value of in_y to the param

# Create instances to handle input and leaderboard
input_handler = total()
leaderboard = total()
auto_play = total()

# Set up keyboard controls
screen.listen()  # Enable keyboard input
screen.onkeypress(lambda: auto_play.sety(1), "a")  # Press 'a' to activate auto-play
screen.onkeypress(lambda: input_handler.sety(1), "Up")  # Move up when 'Up' key is pressed
screen.onkeypress(lambda: input_handler.sety2(1), "Down")  # Move down when 'Down' key is pressed
screen.onkeyrelease(lambda: auto_play.sety(0), "a")  # Release 'a' to stop auto-play
screen.onkeyrelease(lambda: input_handler.sety(0), "Up")  # Stop moving up when key is released
screen.onkeyrelease(lambda: input_handler.sety2(0), "Down")  # Stop moving down when key is released

# Main game loop
while True:
    screen.update()  # Refresh the screen
    if auto_play.in_y != 1:
        move_paddle(input_handler.in_y , input_handler.in_y2)  # Move paddles manually
    else:
        auto_move_paddle()  # Move paddles automatically

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

    # Ball collision with top and bottom walls
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1  # Reverse ball direction
    if ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1

    # Ball collision with paddles
    if (ball.xcor() > 340 and ball.xcor() < 350) and (
            right_paddle.ycor() - 50 < ball.ycor() < right_paddle.ycor() + 50):
        ball.setx(340)
        ball.dx *= -1  # Reverse ball direction
        leaderboard.in_y += 1  # Increase score

    if (ball.xcor() < -340 and ball.xcor() > -350) and (
            left_paddle.ycor() - 50 < ball.ycor() < left_paddle.ycor() + 50):
        ball.setx(-340)
        ball.dx *= -1
        leaderboard.in_y += 1

    # Ball goes out of bounds (missed by paddles)
    if ball.xcor() > 390 or ball.xcor() < -390:
        sleep(1)  # Pause before restarting
        ball.goto(0, 0)  # Reset ball to center
        ball.dx *= -1  # Reverse direction
        leaderboard.reset()  # Reset score

    # Print score details
    print("Current score:", leaderboard.in_y)
    print("Max score:", leaderboard.max)
Editor is loading...
Leave a Comment