Long before Pacman there was Pong game that tested your shooting skills. It is one of the most loved and played game in the history of gaming. The game was released in 1972. Pong paved the way for Golden Era of the Arcade games in 1980, the era in which it all started. As time has passed, this game has become a lost art. As you’ve previously liked our posts on helicopter and car racing games with python. So, we are here to recreate Python Pong game with Pygame.

Pong Game with pygame

And in this tutorial, we are going to tell you how you can recreate Python Pong game on your device using pygame as its simple still contains enough challenges for you to work your way through making more complex and fun games.

We are using python 3 gaming and core python as back-end programming to create this game.

Python Pong Game Project overview :

Usage of basic python concepts:

·   Firstly, Loops,

·   Secondly, Functions,

·   Lastly, Conditional statements

Here we will not be using any official frameworks we are using core python. We will be using loops specifically I have used while loops use can also use other loops and for loops for multiple level. This will use conditional statements. Also, we have use if else and else if statements We will be using built in functions as well as user defined functions.

Python Libraries: Pygame (pip3 install pygame)

The pygame library is an open-source module for the Python programming language specifically designed to help you make games and other multimedia applications. Built on top of the highly portable SDL (Simple Direct Media Layer) development library, pygame can be used across many platforms and operating systems. You can focus on the logic and graphics of your games without having to think about the backend complexities required for working with video and audio.

By far we came to know on why we are going for pygame now coming to how to use it so first we will set up our environment and then we will code a sample game. To know more about library on games visit this (link)

Stretch goals:

·    Firstly, Multiple levels

·    Secondly, Timed condition

·    Thirdly, Multiple lives

·    Lastly, Multiplayer support

So, it can pretty be a daunting task to complete all of the programming steps at once so here we are going to break it in simple steps for you to follow:

1: Generate a blank screen

2: Now create the arena, paddles and the ball

3: Try to move the ball

4: Examine collision on all edges

5: Try to move the paddles

6: Check for Collision of balls with paddles

7: Add a scoring system

 Python Pong Game Transcript :

# Simple Pong in Python 3 for Beginners
# By @TokyoEdTech

import turtle
import os

wn = turtle.Screen()
wn.setup(width=800, height=600)

# Score
score_a = 0
score_b = 0

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.goto(0, 0)
ball.dx = 2
ball.dy = 2

# Pen
pen = turtle.Turtle()
pen.goto(0, 260)
pen.write("Player A: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))

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

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

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

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

# Keyboard bindings
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:
    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border checking

    # Top and bottom
    if ball.ycor() > 290:
        ball.dy *= -1
        os.system("afplay bounce.wav&")
    elif ball.ycor() < -290:
        ball.dy *= -1
        os.system("afplay bounce.wav&")

    # Left and right
    if ball.xcor() > 350:
        score_a += 1
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
        ball.goto(0, 0)
        ball.dx *= -1

    elif ball.xcor() < -350:
        score_b += 1
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))
        ball.goto(0, 0)
        ball.dx *= -1

    # Paddle and ball collisions
    if ball.xcor() < -340 and ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50:
        ball.dx *= -1 
        os.system("afplay bounce.wav&")
    elif ball.xcor() > 340 and ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50:
        ball.dx *= -1
        os.system("afplay bounce.wav&")

For more detailed guide visit GithubCode. To give other games a go with python here is a blog you can refer too.

Categorized in:

Tagged in: