Skip to content

Instantly share code, notes, and snippets.

@Jithender5913
Last active February 16, 2025 06:58
Show Gist options
  • Select an option

  • Save Jithender5913/f645455caba9c94e07f7bc7bceca9b91 to your computer and use it in GitHub Desktop.

Select an option

Save Jithender5913/f645455caba9c94e07f7bc7bceca9b91 to your computer and use it in GitHub Desktop.
Pong Game using Python OOP
from turtle import Screen
from paddle import Paddle
from ball import Ball
import time
from scoreboard import ScoreBoard
# Step 1 - Create the screen
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong Game")
screen.tracer(0)
# Step 2 - Create the paddle and move it
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")
# Step 3 - Write the Ball Class and Make the Ball Move
ball = Ball()
# Step 4 - Add the Ball Bouncing Logic
# Step 9 - Score Keeping and Changing the Ball Speed
scoreboard = ScoreBoard()
game_is_on = True
while game_is_on:
screen.update()
time.sleep(ball.move_speed)
ball.move()
# Step 5 - detect collision with wall
if ball.ycor() > 270 or ball.ycor() < -270:
ball.bounce_y()
# Step 6 - detect collision with r_paddle
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.bounce_x()
# Step 7 - detect collision when ball misses the r_paddle
if ball.xcor() > 380:
ball.reset_position()
scoreboard.l_point()
# Step 8 - detect collision when ball misses the l_paddle
if ball.xcor() < - 380:
ball.reset_position()
scoreboard.r_point()
screen.exitonclick()
# ball class
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("white")
self.penup()
self.shapesize(stretch_wid=0.7, stretch_len=0.7)
self.x_move = 10
self.y_move = 10
self.move_speed = 0.1
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
def bounce_y(self):
self.y_move *= -1
def bounce_x(self):
self.x_move *= -1
self.move_speed *= 0.9
def reset_position(self):
self.goto(0, 0)
self.move_speed = 0.1
self.bounce_x()
# Scoreboard class
from turtle import Turtle
class ScoreBoard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.penup()
self.hideturtle()
self.l_score = 0
self.r_score = 0
self.update_score()
def update_score(self):
self.clear()
self.goto(-100, 200)
self.write(self.l_score, align="center", font=("arial", 80, "normal"))
self.goto(100, 200)
self.write(self.r_score, align="center", font=("arial", 80, "normal"))
def l_point(self):
self.l_score += 1
self.update_score()
def r_point(self):
self.r_score += 1
self.update_score()
# paddle class
from turtle import Turtle, Screen
screen = Screen()
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape("square")
self.color("white")
self.shapesize(stretch_wid=5, stretch_len=1)
self.penup()
self.goto(position)
def go_up(self):
new_y = self.ycor() + 20
self.goto(self.xcor(), new_y)
def go_down(self):
new_y = self.ycor() - 20
self.goto(self.xcor(), new_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment