Python Pinball Game – The Retro Game You’ll Love!

A Python Pinball Game is one in which we hit the bouncing ball with a paddle. Every time we hit the ball, the score rises. And the game is over when we miss the hit.

About Python Pinball Game

To develop the project, we will use the turtle and random modules. The turtle module contributes to the creation of the game window and its components, as well as controlling the game. To begin the game, we use the random module to place the ball in a random position.

Prerequisites for Pinball Game using Python

  • Basic knowledge of the Python programming language and how functions are defined in it.
  • How the game window is made using Turtle
  • How to work with the random library

Download Python Pinball Game Project

Please download the source code of Python Pinball Game Project from the following link: Python Pinball Game Project Code

Steps to Create Pinball Game using Python

Following are the steps for developing the Python Pinball Game Project:

Step 1: Importing the necessary modules

Step 2: Making a window for our project

Step 3: Functions

Step 4: The main game loop

Step 1: Importing the necessary modules

To use Tkinter, we need to import the turtle module to make our game. We are also going to import the random module.

Code

#import packages
import turtle
import random

Step 2: Making a window for our project

  • This code sets the title of the window as ‘TechVidvan Pinball’, and sets its dimensions using .setup().
  • The ball is then made with a circle shape. It begins at the top of the screen and moves to a random place along the horizontal axis. The variables dx and dy determine the speed of the ball, whose position is constantly updated by these values.
  • Now we’ll make a square paddle that moves just when we press the keyboard.When the player pushes the left/right keyboard buttons, it goes in the respective direction.
  • Now we make the scoreboard, with the score changing every time the paddle strikes the ball. This is at the top of the screen.

Code

# Set up the window
display = turtle.Screen()
display.title('TechVidvan Pinball')
display.bgcolor('Grey')
display.setup(width=610, height=610)


# Set up the window
display = turtle.Screen()
display.title('TechVidvan Pinball')
display.bgcolor('Grey')
display.setup(width=610, height=610)


# Set up the ball
ball = turtle.Turtle()
ball.shape('circle')
ball.color('red')
ball.penup()
ball.speed(0)
ball.goto(0, 250)
ball.dy = -7
ball.dx = random.randint(-5, 5)


# Set up the paddle
paddle = turtle.Turtle()
paddle.shape('square')
paddle.color('Lightblue')
paddle.shapesize(stretch_wid=1, stretch_len=5)
paddle.penup()
paddle.speed(0)
paddle.goto(0, -250)


# Set up the score
score = 0
score_pen = turtle.Turtle()
score_pen.hideturtle()
score_pen.penup()
score_pen.goto(-290, 270)
score_pen.color('white')
score_pen.write(f'Score: {score}', align='left', font=('Arial', 14, 'normal'))

Step 3: Functions

Then we write the functions movepaddle_right() and movepaddle_left(), which move the paddle to the right and left by 20 units each time the user pushes the right or left key.
The listen() function ensures that keyboard inputs are taken into account.

We set the boundaries for the paddle so that it doesn’t go beyond the screen when pressed left or right.

Code

# Move the paddle left and right
def movepaddle_left():
   x = paddle.xcor()
   x -= 20
   paddle.setx(x)
   if paddle.xcor() <= -240:
       paddle.setx(-240)




def movepaddle_right():
   x = paddle.xcor()
   x += 20
   paddle.setx(x)
   if paddle.xcor() >= 240:
       paddle.setx(240)
# Keyboard bindings
display.listen()
display.onkeypress(movepaddle_left, 'Left')
display.onkeypress(movepaddle_right, 'Right')

Step 4: The main game loop

The entire game is run in an unending while loop.(while True:)

We begin by updating the screen and setting the ball coordinates. Then we check to see if the ball strikes the screen’s right, left, or top edges, and we bounce by inverting the sign of the dx or dy. ( ball.dx *= -1, ball.dy *= -1)

Following that, we check to see if the ball strikes the bottom border of the screen, indicating that the paddle missed the hit. If this condition is met, we pause the game and display the score.( score_pen.write)

Code

# Main game loop
while True:
   display.update()


   # Move the ball
   ball.setx(ball.xcor() + ball.dx)
   ball.sety(ball.ycor() + ball.dy)


   # collision with the walls
   if ball.xcor() > 290 or ball.xcor() < -290:
       ball.dx *= -1


   # collision with the paddle
   if ball.ycor() < -223 and (paddle.xcor() - 50 < ball.xcor() < paddle.xcor() + 50):
       ball.dy *= -1
       score += 10
       score_pen.clear()
       score_pen.write(f'Score: {score}', align='left', font=('Courier', 16, 'normal'))


   # Check for a collision with the top wall
   if ball.ycor() > 270:
       ball.dy *= -1


   # Check for a game over
   if ball.ycor() < -290:
       score_pen.clear()
       score_pen.goto(0, 0)
       # Game over
       score_pen.write('Game over!-Score : {}'.format(score), align='center', font=('Courier', 24, 'normal'))
       break


display.mainloop()

Full Code

import turtle
import random


# Set up the window
display = turtle.Screen()
display.title('TechVidvan Pinball')
display.bgcolor('Grey')
display.setup(width=610, height=610)


# Set up the ball
ball = turtle.Turtle()
ball.shape('circle')
ball.color('red')
ball.penup()
ball.speed(0)
ball.goto(0, 250)
ball.dy = -7
ball.dx = random.randint(-5, 5)


# Set up the paddle
paddle = turtle.Turtle()
paddle.shape('square')
paddle.color('Lightblue')
paddle.shapesize(stretch_wid=1, stretch_len=5)
paddle.penup()
paddle.speed(0)
paddle.goto(0, -250)


# Set up the score
score = 0
score_pen = turtle.Turtle()
score_pen.hideturtle()
score_pen.penup()
score_pen.goto(-290, 270)
score_pen.color('white')
score_pen.write(f'Score: {score}', align='left', font=('Arial', 14, 'normal'))




# Move the paddle left and right
def movepaddle_left():
   x = paddle.xcor()
   x -= 20
   paddle.setx(x)
   if paddle.xcor() <= -240:
       paddle.setx(-240)




def movepaddle_right():
   x = paddle.xcor()
   x += 20
   paddle.setx(x)
   if paddle.xcor() >= 240:
       paddle.setx(240)




# Keyboard bindings
display.listen()
display.onkeypress(movepaddle_left, 'Left')
display.onkeypress(movepaddle_right, 'Right')


# Main game loop
while True:
   display.update()


   # Move the ball
   ball.setx(ball.xcor() + ball.dx)
   ball.sety(ball.ycor() + ball.dy)


   # collision with the walls
   if ball.xcor() > 290 or ball.xcor() < -290:
       ball.dx *= -1


   # collision with the paddle
   if ball.ycor() < -223 and (paddle.xcor() - 50 < ball.xcor() < paddle.xcor() + 50):
       ball.dy *= -1
       score += 10
       score_pen.clear()
       score_pen.write(f'Score: {score}', align='left', font=('Courier', 16, 'normal'))


   # Check for a collision with the top wall
   if ball.ycor() > 270:
       ball.dy *= -1


   # Check for a game over
   if ball.ycor() < -290:
       score_pen.clear()
       score_pen.goto(0, 0)
       # Game over
       score_pen.write('Game over!-Score : {}'.format(score), align='center', font=('Courier', 24, 'normal'))
       break


display.mainloop()

Python Pinball Game Output

python pinball outputpinball game output

Summary

Using the Trtle and Random Python libraries, we were able to build the Pinball game project effectively. We have completed the Python Pinball game. I hope you had fun building with us.