Pong Game in Python

We all must be familiar with a game called ping pong from our childhood. It is a game where it gives an essence of virtual way of playing a table tennis. The only condition is that the user should not miss the ball. This game which I created has tap sound when the ball is being tapped on the bar or bouncing on the screen.

In order to create a Ping Pong Game everything has to be acquired though few steps, keeping in mind about the features that we would like to have in our game we can vary the code accordingly but the steps remain the same: -

1. Import the packages like turtle and winsound (For the sound effects)

2. Initial set up of the background colors as it acts like a pillar for our code.

3. Paddle A and B (This particular code will concentrate on the speed, shape, color of the paddle)

4. Ball (the shape of the ball, speed and these kind of preferences will be defined in this code)

5.  Score count feature code

6. Keyboard code (As this would be a 2 player game there has to be 2 sets of controls that needs to be assigned for the game so that both the players playing the game will be able to play the game)

7. Loops

              These will be fixed main steps for the Ping pong game that we will be creating with sounds, the visuals can always change according to the preferences of the user or the coder.

The code goes like this: -

Python
import turtle
import winsound


wn = turtle.Screen()
wn.title('Pong Game - Prathyusha')
wn.bgcolor('turquoise')
wn.setup(width=800, height=600)
wn.tracer(0)


# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape('square')
paddle_a.color('grey')
paddle_a.penup()
paddle_a.goto(-350, 0)
paddle_a.shapesize(5, 1)


# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape('square')
paddle_b.color('grey')
paddle_b.penup()
paddle_b.goto(350, 0)
paddle_b.shapesize(5, 1)


This above code will be explaining the importance of the visual effects and here in the game i have chosen to have a turquoise background. Now let's see the further steps. these further codes will now define the ball shape, pen, score etc.


Python
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape('circle')
ball.color('red')
ball.penup()
ball.dx = 0.4
ball.dy = 0.4

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color('grey')
pen.penup()
pen.goto(0, 260)
pen.write("Player A: 0  Player B: 0", align='center', font=('Courier', 24, 'bold'))
pen.hideturtle()

# Score
score_a = 0
score_b = 0

def paddle_a_up():
    y = paddle_a.ycor()
    y += 20
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y += 20
    paddle_b.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y += -20
    paddle_a.sety(y)

def paddle_b_down():
    y = paddle_b.ycor()
    y += -20
    paddle_b.sety(y)


We have now constructed the base of the game now we need to jump into the functionality of the game by introducing keyboard controls and loops to the game.


Python
# Keyboard binding
wn.listen()
wn.onkeypress(paddle_a_up, 'w')
wn.onkeypress(paddle_a_down, 's')
wn.onkeypress(paddle_b_up, 'Up')
wn.onkeypress(paddle_b_down, 'Down')
  
  
  # Main game loop
while True:
    wn.update()

    # Moving Ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border checking
    if ball.ycor() > 290 or ball.ycor() < -290:
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)
        ball.dy *= -1

    if ball.xcor() > 390:
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)
        ball.goto(0, 0)
        ball.dx *= -1
        score_a += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align='center', font=('Courier', 24, 'bold'))

    if ball.xcor() < -390:
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)
        ball.goto(0, 0)

        ball.dx *= -1
        score_b += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align='center', font=('Courier', 24, 'bold'))

    # Paddle and ball collisions
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 60 and ball.ycor() > paddle_b.ycor() -60):
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)
        ball.setx(340)
        ball.dx *= -1

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 60 and ball.ycor() > paddle_a.ycor() -60):
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)
        ball.setx(-340)
        ball.dx *= -1



Output: -

Once we run all the above code there will be a new window that will pop up. This code is different as this provides game along with sound effects while the ball is either bouncing or even when it touches the paddles.








Comments

  1. This worked! I just made this one, it was so innovative. winsound was a new concept that i came across after your blog!

    ReplyDelete

Post a Comment

Popular posts from this blog

Basic command lines for the developers

Sentimental Analysis: 2 min read

Ethical Web Scraping