Python Turtle Mini Project Pong Game

Introduction

The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.

The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.

Overview of the syntax

A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).

“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.

Basic syntax:

import turtle // start of the program
     //body
    //of the main
   //code
turtle.done() //end of the program

Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.

Methods

A Python method is a label that can be applied to an object and is a piece of code that may be run on that object.

The most frequently, used turtle methods are:

MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
left()angleTurns the turtle counterclockwise
penup()NonePicks up the turtle’s Pen
pendown()NonePuts down the turtle’s Pen
up()NonePicks up the turtle’s Pen
down()NonePuts down the turtle’s Pen
color()Color nameChanges the color of the turtle’s pen
fillcolor()Color nameChanges the color of the turtle will use to fill a polygon
heading()NoneReturns the current heading
position()NoneReturns the current position
goto()x, yMove the turtle to position x,y
begin_fill()NoneRemember the starting point for a filled polygon
end_fill()NoneClose the polygon and fill with the current fill color
dot()NoneLeave the dot at the current position
stamp()NoneLeaves an impression of a turtle shape at the current location
shape()shapenameShould be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Nostalgic Pong Game

The principles governs everything in every subject. They’re the basis on which all incredible exploits are built. To execute acts that appear magical to us simple humans, great scientists and artists equally rely on their most basic skills. Same is the case for python turtle module.

In order to make any games using python turtle, for this instance a nostalgic pong game, the fundamentals are mandatory. If you think about it a pong game is only, two rectangle paddles that can move up and down and repeal a ball once it touches them. The top and bottom of the game window bounces the ball back. Whereas, if the ball goes past the left and the right wall of the game then a point is added to the player.

The following is the code for making a pong game using python turtle module:

import turtle

window = turtle.Screen()
window.title("Pong made by ABK for Follow Tutorials")
window.bgcolor("skyblue")
window.setup(width=800, height=600)
window.tracer(0)

# Scorew
score_a = 0
score_b = 0

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5,stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5,stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.8
ball.dy = 0.8

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("turtle")
pen.color("white")
pen.penup()
pen.hideturtle()
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 += 40
    paddle_a.sety(y)

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

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

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

# Keyboard bindings
window.listen()
window.onkeypress(paddle_a_up, "w")
window.onkeypress(paddle_a_down, "s")
window.onkeypress(paddle_b_up, "Up")
window.onkeypress(paddle_b_down, "Down")

# Main game loop
while True:
    window.update()
    
    # 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.sety(290)
        ball.dy *= -1
        
    
    elif ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1
        

    if ball.xcor() > 350:
        score_a += 1
        pen.clear()
        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.clear()
        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 
        
    
    elif ball.xcor() > 340 and ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50:
        ball.dx *= -1
        
    

Output

The output for the above code is:

SHARE Python Turtle Mini Project Pong Game

You may also like...

Leave a Reply

Your email address will not be published.

Share