How to Make Bouncing Ball Game in Python with Source Code
Bouncing Ball Game is developed using Python Programming Language . The Bouncing Ball Game in Python is design using tkinter and graphical user interface(GUI) and it is a desktop application. This Bouncing Ball Game use Canvas for drawing objects and random module in Python. Canvas is a rectangular area intended for drawing pictures or other complex layouts. We can place graphics, text, widgets or frames on Canvas.
This Bouncing Ball Game in Python could be very simple to control for the users. There is one bar at the lowest of game window which may be moved left or right key for the proper usage of the buttons which are in the game window.
The goal of the Bouncing Ball Game is to hit all targets as many times as you can to earn the highest score. Every time the ball hits the Target you earn 1 points, and if it is hits 2 targets you earn 2 points, respectively. If the player fail to catch the ball on the bar then it will appear the game over on the game window.
Anyway if you want level up your knowledge in programming especially games in python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code
Before you start on How to Make Bouncing Ball Game in Python, make sure that you have PyCharm IDE installed in your computer.
By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.
This are the steps on How to Make Bouncing Ball Game in Python
How to Make Bouncing Ball Game in Python With Source Code
- Step 1: Create a project name
First when you finished installed the Pycharm IDE in your computer, open it and then create a “project name” after creating a project name click the “create” button.
- Step 2: Create a python file.
Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“.
- Step 4: The actual code
This is the actual coding on How to Make Bouncing Ball Game in Python, and you are free to copy this code and download the full source code given below.
Importing Tkinter Module
from tkinter import *
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications.(Bouncing Ball Game in Python)
Importing Random Module
import random
Among these is the random() function, which generates random numbers between 0 and 1.(Bouncing Ball Game in Python)
Designing the Main Window
root = Tk() root.title("Bounce Ball Game") root.geometry("500x570") root.resizable(0, 0) root.wm_attributes("-topmost", 1) canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0, highlightbackground="Red", bg="Black") canvas.pack(padx=10, pady=10) score = Label(height=50, width=80, text="Score: 00", font="Calibri 14 italic") score.pack(side="left") root.update()
In the code above , which is first of all you have to design the main screen window.(Bouncing Ball Game in Python)
Module for Starting a Game
def start_game(event): global playing if playing is False: playing = True score.configure(text="Score: 00") canvas.delete("all") BALL_COLOR = ["blue", "green", "violet"] STONE_COLOR = ["green", "dark blue", "red", "pink", "violet", "yellow", "orange", "gray", "brown", "white", "blue", "yellow green", "navajo white", "dark gray", "violet red", "powder blue", "blue violet"] random.shuffle(BALL_COLOR) pole = Pole(canvas, "yellow") stones = [] for i in range(0, 5): b = [] for j in range(0, 19): random.shuffle(STONE_COLOR) tmp = Stone(canvas, STONE_COLOR[0]) b.append(tmp) stones.append(b) for i in range(0, 5): for j in range(0, 19): canvas.move(stones[i][j].id, 25 * j, 25 * i) ball = Ball(canvas, BALL_COLOR[0], pole, stones, score) root.update_idletasks() root.update() time.sleep(1) while 1: if pole.pauseSeconds != 1: try: canvas.delete(m) del m except: pass if not ball.bottom_hit: ball.draw() pole.draw() root.update_idletasks() root.update() time.sleep(0.01) if ball.hit == 95: canvas.create_text(250, 250, text="YOU WON !!", fill="yellow", font="Calibri 24 ") root.update_idletasks() root.update() playing = False break else: canvas.create_text(250, 250, text="GAME OVER!!", fill="red", font="Calibri 24 ") root.update_idletasks() root.update() playing = False break else: try: if m == None: pass except: m = canvas.create_text(250, 250, text="PAUSE!!", fill="green", font="Calibri 24 ") root.update_idletasks() root.update()
In the code above, which is for starting a game function use and design of a game. If you hit all the ball it will appear YOU WIN, and if you lose it will appear GAME OVER.(Bouncing Ball Game in Python)
Module for the Class Function Use
class Ball: def __init__(self, cvs, clr, pole, stones, scre): self.stones = stones self.cvs = cvs self.pole = pole self.scre = scre self.bottom_hit = False self.hit = 0 self.id = canvas.create_oval(10, 10, 25, 25, fill=clr, width=1) self.cvs.move(self.id, 230, 461) start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6] random.shuffle(start) self.a = start[0] self.b = -start[0] self.cvs.move(self.id, self.a, self.b) self.cvs_height = canvas.winfo_height() self.cvs_width = canvas.winfo_width()
In the code above, which is for the class function use in the system. (Bouncing Ball Game in Python)
Module for Left and Right Key
def turn_left(self, event): self.a = -3.5 def turn_right(self, event): self.a = 3.5 def pause(self, event): self.pauseSeconds += 1 if self.pauseSeconds == 2: self.pauseSeconds = 0
In the code above, which is for the function of left and right key use in the game window.(Bouncing Ball Game in Python)
Complete Source Code on How to Make Bouncing Ball Game in Python
from tkinter import * import time import random root = Tk() root.title("Bounce Ball Game") root.geometry("500x570") root.resizable(0, 0) root.wm_attributes("-topmost", 1) canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0, highlightbackground="Red", bg="Black") canvas.pack(padx=10, pady=10) score = Label(height=50, width=80, text="Score: 00", font="Calibri 14 italic") score.pack(side="left") root.update() class Ball: def __init__(self, cvs, clr, pole, stones, scre): self.stones = stones self.cvs = cvs self.pole = pole self.scre = scre self.bottom_hit = False self.hit = 0 self.id = canvas.create_oval(10, 10, 25, 25, fill=clr, width=1) self.cvs.move(self.id, 230, 461) start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6] random.shuffle(start) self.a = start[0] self.b = -start[0] self.cvs.move(self.id, self.a, self.b) self.cvs_height = canvas.winfo_height() self.cvs_width = canvas.winfo_width() def stone_strike(self, push): for stone_line in self.stones: for stone in stone_line: stone_push = self.cvs.coords(stone.id) try: if push[2] >= stone_push[0] and push[0] <= stone_push[2]: if push[3] >= stone_push[1] and push[1] <= stone_push[3]: canvas.bell() self.hit += 1 self.scre.configure(text="Score: " + str(self.hit)) self.cvs.delete(stone.id) return True except: continue return False def pole_strike(self, push): pole_push = self.cvs.coords(self.pole.id) if push[2] >= pole_push[0] and push[0] <= pole_push[2]: if push[3] >= pole_push[1] and push[1] <= pole_push[3]: return True return False def draw(self): self.cvs.move(self.id, self.a, self.b) push = self.cvs.coords(self.id) # print(pos) start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6] random.shuffle(start) if self.stone_strike(push): self.b = start[0] if push[1] <= 0: self.b = start[0] if push[3] >= self.cvs_height: self.bottom_hit = True if push[0] <= 0: self.a = start[0] if push[2] >= self.cvs_width: self.a = -start[0] if self.pole_strike(push): self.b = -start[0] class Pole: def __init__(self, cvs, clr): self.cvs = cvs self.id = canvas.create_rectangle(0, 0, 100, 10, fill=clr) self.cvs.move(self.id, 200, 485) self.a = 0 self.pauseSeconds = 0 self.cvs_width = canvas.winfo_width() self.cvs.bind_all("<Left>", self.turn_left) self.cvs.bind_all("<Right>", self.turn_right) self.cvs.bind_all("<space>", self.pauseSeconds) def draw(self): push = self.cvs.coords(self.id) # print(pos) if push[0] + self.a <= 0: self.a = 0 if push[2] + self.a >= self.cvs_width: self.a = 0 self.cvs.move(self.id, self.a, 0) def turn_left(self, event): self.a = -3.5 def turn_right(self, event): self.a = 3.5 def pause(self, event): self.pauseSeconds += 1 if self.pauseSeconds == 2: self.pauseSeconds = 0 class Stone: def __init__(self, cvs, clr): self.cvs = cvs self.id = canvas.create_oval(5, 5, 25, 25, fill=clr, width=2) playing = False def start_game(event): global playing if playing is False: playing = True score.configure(text="Score: 00") canvas.delete("all") BALL_COLOR = ["blue", "green", "violet"] STONE_COLOR = ["green", "dark blue", "red", "pink", "violet", "yellow", "orange", "gray", "brown", "white", "blue", "yellow green", "navajo white", "dark gray", "violet red", "powder blue", "blue violet"] random.shuffle(BALL_COLOR) pole = Pole(canvas, "yellow") stones = [] for i in range(0, 5): b = [] for j in range(0, 19): random.shuffle(STONE_COLOR) tmp = Stone(canvas, STONE_COLOR[0]) b.append(tmp) stones.append(b) for i in range(0, 5): for j in range(0, 19): canvas.move(stones[i][j].id, 25 * j, 25 * i) ball = Ball(canvas, BALL_COLOR[0], pole, stones, score) root.update_idletasks() root.update() time.sleep(1) while 1: if pole.pauseSeconds != 1: try: canvas.delete(m) del m except: pass if not ball.bottom_hit: ball.draw() pole.draw() root.update_idletasks() root.update() time.sleep(0.01) if ball.hit == 95: canvas.create_text(250, 250, text="YOU WON !!", fill="yellow", font="Calibri 24 ") root.update_idletasks() root.update() playing = False break else: canvas.create_text(250, 250, text="GAME OVER!!", fill="red", font="Calibri 24 ") root.update_idletasks() root.update() playing = False break else: try: if m == None: pass except: m = canvas.create_text(250, 250, text="PAUSE!!", fill="green", font="Calibri 24 ") root.update_idletasks() root.update() root.bind_all("<Return>", start_game) canvas.create_text(250, 250, text="Press Enter to start Game!!", fill="yellow", font="Calibri 18") j = canvas.find_all() root.mainloop()
First Output

Second Output

How To Run the How to Make Bouncing Ball Game in Python with Source Code?
To run this project, you must have installed a Pycharm on your PC (for Windows). How to Make Bouncing Ball Game in Python is for educational purposes only!
After downloading the project you must follow the steps below:
Step 1: Unzip the file or Extract the file
Step 2: Double click the Bounce-ball
Step 3: Project is ready to run
Downloadable Source Code
I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.
Summary
This is an easy game created for the beginners. Bouncing Ball Game in Python is free to download the open source code and it is use for educational purposes only.
Related Articles
- Python MySQL UPDATE Query: Step-by-Step Guide in Python
- Bank Management System Project in Python With Source Code
- Python MySQL INSERT Query: Step by Step Guide in Python
- How To Make Game In Python With Source Code
- Complaint Management System Source Code In PHP
Inquiries
If you have any questions or suggestions about How to Make Bouncing Ball Game in Python with Source Code , please feel free to leave a comment below.