DEV Community

Palak Hirave
Palak Hirave

Posted on

Day 22 of 100

I'm coming back to this series after quite a long time. It's nice to get back to programming.

Today I build the Ping Pong game with a simple UI. Here's the code -

main.py

from turtle import Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time

screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)

r_paddle = Paddle(350,0)
l_paddle = Paddle(-350,0)
ball = Ball()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(r_paddle.up, "Up")
screen.onkey(r_paddle.down, "Down")
screen.onkey(l_paddle.up, "w")
screen.onkey(l_paddle.down, "s")

game_is_on = True

while game_is_on:
    time.sleep(ball.faster)
    screen.update()
    ball.move()

    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce_y()

    if ball.distance(r_paddle) < 55 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
        ball.bounce_x()

    if ball.xcor() > 380:
        ball.reset_pos()
        scoreboard.l_point()

    if ball.xcor() < -380:
        ball.reset_pos()
        scoreboard.r_point()


screen.exitonclick()

Enter fullscreen mode Exit fullscreen mode

ball.py

from turtle import Turtle

class Ball(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.color("white")
        self.penup()
        self.x_new = 10
        self.y_new = 10
        self.faster = 0.1

    def move(self):
        new_x = self.xcor() + self.x_new
        new_y = self.ycor() + self.y_new
        self.goto(new_x, new_y)

    def bounce_y(self):
        self.y_new *= -1

    def bounce_x(self):
        self.x_new *= -1
        self.faster *= 0.7

    def reset_pos(self):
        self.goto(0,0)
        self.faster = 0.1
        self.bounce_x()

Enter fullscreen mode Exit fullscreen mode

paddle.py

import turtle

class Paddle(turtle.Turtle):

    def __init__(self, xcord, ycord):
        super().__init__()
        self.color("white")
        self.shape("square")
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.penup()
        self.x_cord = xcord
        self.y_cord = ycord
        self.setpos(self.x_cord, self.y_cord)
        self.jump = 20

    def up(self):
        self.goto(self.x_cord, self.y_cord + self.jump)
        self.y_cord += self.jump

    def down(self):
        self.goto(self.x_cord, self.y_cord - self.jump)
        self.y_cord -= self.jump

Enter fullscreen mode Exit fullscreen mode

scoreboard.py

import turtle

class Scoreboard(turtle.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", 70, "normal"))
        self.goto(100, 200)
        self.write(self.r_score, align="center", font=("Arial", 70, "normal"))

    def l_point(self):
        self.l_score += 1
        self.update_score()

    def r_point(self):
        self.r_score += 1
        self.update_score()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)