Done by
S. RAMAKRISHNAN
XII- VIBRANT
1
CERTIFICATE
This is to certify that S. RAMAKRISHNAN of XII-VIBRANT
has successfully completed the computer science project
Small Game Development In Python 8 Ball Pool under the
guidance of SUMALATHA.S and submitted for the practical
examination conducted by the CBSE BOARD during the
year 2022-2023.
INTERNAL EXAMINER EXTERNAL EXAMINER
2
ACKNOWLEDGEMENT
I gratefully acknowledge my sincere thanks to
computer teacher Mrs. SUMALATHA.S and our principal
Mrs. VIDHYAHARI for giving me an opportunity and
guiding me to complete the project successfully
S. RAMAKRISHNAN
XII-VIBRANT
3
The word Python- isn't it scary? Does it bring the image of
big reptile that we prefer to see either in jungles or zoo? Well, it's
time to change the image. Now on, you'll remember word Python
for its playfulness and pleasant productivity. Confused? Well,
don’t be - because, now on you'll get introduced to a new
programming language namely 'Python, which promises to make
you a big programming fan.
Python programming language was developed by Guido Van
Rossum in February [Link] is based on or influenced with
two programming languages:
ABC language, a teaching language created as a replacement
of BASIC, and
Modula-3
Python is an easy-to-learn yet powerful object oriented
programming language. It is a very high level programming
language yet as powerful as many other middle-level not so high-
level languages like C, C++, Java etc.
Do you know Python, the programming language, was named after
famous BBC comedy show namely Monty Python’s Flying Circus
4
Though Python language came into being in early 1990's, yet
it is competing with ever-popular languages such as C, C++, Java
etc. in popularity index.
Let's see what are these advantages of Python.
Python is compact and very easy to use object oriented language
with very simple syntax rules. It is a very high level language and
thus very-very programmer-friendly.
Python is an expressive language fewer
lines of code and simpler syntax. For example, consider
following two sets of codes :
// In C++ : Swap Values
int a =2,b=3,temp; # In Python : Swap values
temp=a; a, b = 2, 3
a=b; a, b = b, a
b=temp;
which one is compact and easier to understand? Need I say more?
Python is an interpreted language, not a compiled language. It
makes Python an easy-to-debug language and thus suitable for to
advanced users.
5
For most types of required functionality is available through
various modules of Python standard library¹. For example, for
diverse functionality such as emails, web-pages databases GUI
development, network connections and many more, everything is
available in Python standard library. Thus, it is also called Python
follows "Batteries Included" philosophy.
Python can run equally well on variety of platforms Windows,
Linux/UNIX Macintosh, supercomputers, smart phones etc. Isn't
that amazing? And that makes Python a true cross-platform
language. Or in other words, Python is a portable language.
Free and Open Source:
Python language is freely available along with its source-code.
Python has evolved into a powerful, complete and useful
language over these years. These days Python is being used in
many diverse fields / applications, some of which are:
Scripting GUI Programs
Web Applications Database Applications
Game development System Administrations
Rapid Prototyping
6
Eight-ball (also spelled 8-ball or eight ball, and sometimes called solids
and stripes, spots and stripes or rarely highs and lows) is a pool billiards
played on a billiard table with six pockets, cue sticks, and sixteen billiard
balls: a cue ball and fifteen object balls. The object balls include seven solid-
colored balls numbered 1 through 7, seven striped balls numbered 9
through 15, and the black 8 ball. After the balls are scattered with a break
shot, a player is assigned either the group of solid or striped balls once they
have legally pocketed a ball from that group. The object of the game is to
legally pocket the 8-ball in a "called" pocket, which can only be done after all
of the balls from a player's assigned group have been cleared from the table.
The game is the most frequently played discipline of pool, and is often
thought of as synonymous with "pool". The game has numerous variations,
mostly regional. It is the second most played professional pool game, after
nine-ball, and for the last several decades ahead of straight pool.
Helps Improve Focus.
Helps Develop Presence Of Mind.
A great source to develop early learning skills for younger children.
Enhances memory, brain's speed and concentration
Better Hand-Eye Coordination.
An Excellent Way To Relax & Relieve Stress.
Play online with real money, Play With Friends, Quick Deposit and Money
Withdrawal, Advertisement.
7
import pygame
import sys
from math import *
import random
[Link]()
width = 660
height = 360
outerHeight = 400
margin = 30
display = [Link].set_mode((width, outerHeight))
[Link].set_caption("8 Ball Pool")
clock = [Link]()
background = (51, 51, 51)
white = (236, 240, 241)
gray = (123, 125, 125)
black = (23, 32, 42)
yellow = (244, 208, 63)
blue = (52, 152, 219)
red = (203, 67, 53)
purple = (136, 78, 160)
orange = (230, 126, 34)
green = (40, 180, 99)
brown = (100, 30, 22)
stickColor = (249, 231, 159)
8
colors = [yellow, blue, red, purple, orange, green, brown, black, yellow, blue, red, purple,
orange, green, brown]
balls = []
noBalls = 15
radius = 10
friction = 0.005
# Ball Class
class Ball:
def __init__(self, x, y, speed, color, angle, ballNum):
self.x = x + radius
self.y = y + radius
[Link] = color
[Link] = angle
[Link] = speed
[Link] = ballNum
[Link] = [Link]("Agency FB", 10)
# Draws Balls on Display Window
def draw(self, x, y):
[Link](display, [Link], (x - radius, y - radius, radius*2, radius*2))
if [Link] == black or [Link] == "cue":
ballNo = [Link](str([Link]), True, white)
[Link](ballNo, (x - 5, y - 5))
else:
ballNo = [Link](str([Link]), True, black)
if [Link] > 9:
[Link](ballNo, (x - 6, y - 5))
else:
9
[Link](ballNo, (x - 5, y - 5))
# Moves the Ball around the Screen
def move(self):
[Link] -= friction
if [Link] <= 0:
[Link] = 0
self.x = self.x + [Link]*cos(radians([Link]))
self.y = self.y + [Link]*sin(radians([Link]))
if not (self.x < width - radius - margin):
self.x = width - radius - margin
[Link] = 180 - [Link]
if not(radius + margin < self.x):
self.x = radius + margin
[Link] = 180 - [Link]
if not (self.y < height - radius - margin):
self.y = height - radius - margin
[Link] = 360 - [Link]
if not(radius + margin < self.y):
self.y = radius + margin
[Link] = 360 - [Link]
# Pocket Class
class Pockets:
def __init__(self, x, y, color):
self.r = margin/2
self.x = x + self.r + 10
self.y = y + self.r + 10
[Link] = color
10
# Draws the Pockets on Pygame Window
def draw(self):
[Link](display, [Link], (self.x - self.r, self.y - self.r, self.r*2, self.r*2))
# Checks if ball has entered the Hole
def checkPut(self):
global balls
ballsCopy = balls[:]
for i in range(len(balls)):
dist = ((self.x - balls[i].x)**2 + (self.y - balls[i].y)**2)**0.5
if dist < self.r + radius:
if balls[i] in ballsCopy:
if balls[i].ballNum == 8:
gameOver()
else:
[Link](balls[i])
balls = ballsCopy[:]
# Cue Stick Class
class CueStick:
def __init__(self, x, y, length, color):
self.x = x
self.y = y
[Link] = length
[Link] = color
[Link] = 0
# Applies force to Cue Ball
def applyForce(self, cueBall, force):
[Link] = [Link]
11
[Link] = force
# Draws Cue Stick on Pygame Window
def draw(self, cuex, cuey):
self.x, self.y = [Link].get_pos()
[Link] = (degrees(atan2((cuey - self.y), (cuex - self.x))))
[Link](display, white, (cuex + [Link]*cos(radians([Link])), cuey +
.[Link]*sin(radians([Link]))), (cuex, cuey), 1)
[Link](display, [Link], (self.x, self.y), (cuex, cuey), 3)
# Checks Collision
def collision(ball1, ball2):
dist = ((ball1.x - ball2.x)**2 + (ball1.y - ball2.y)**2)**0.5
if dist <= radius*2:
return True
else:
return False
# Checks if Cue Ball hits any Ball
def checkCueCollision(cueBall):
for i in range(len(balls)):
if collision(cueBall, balls[i]):
if balls[i].x == cueBall.x:
angleIncline = 2*90
else:
u1 = balls[i].speed
u2 = [Link]
balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 +u2*sin(radians(cueBall,angle)
))**2**0.5
[Link] = ((u2*cos(radians([Link])))**2 +(u1*sin(radians(balls[i].angle
12
)))**2)**0.5
tangent = degrees((atan((balls[i].y - cueBall.y)/(balls[i].x - cueBall.x)))) + 90
angle = tangent + 90
balls[i].angle = (2*tangent - balls[i].angle)
[Link] = (2*tangent - [Link])
balls[i].x += (balls[i].speed)*sin(radians(angle))
balls[i].y -= (balls[i].speed)*cos(radians(angle))
cueBall.x -= ([Link])*sin(radians(angle))
cueBall.y += ([Link])*cos(radians(angle))
# Checks Collision Between Balls
def checkCollision():
for i in range(len(balls)):
for j in range(len(balls) - 1, i, -1):
if collision(balls[i], balls[j]):
if balls[i].x == balls[j].x:
angleIncline = 2*90
else:
u1 = balls[i].speed
u2 = balls[j].speed
balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 + (u2*sin(radians(balls[j].angl
e)))** 2)**0.5
balls[j].speed = ((u2*cos(radians(balls[j].angle)))**2 + (u1*sin(radians(balls[i].angl
e)))**2)**0.5
tangent = degrees((atan((balls[i].y - balls[j].y)/(balls[i].x - balls[j].x)))) + 90
angle = tangent + 90
balls[i].angle = (2*tangent - balls[i].angle)
balls[j].angle = (2*tangent - balls[j].angle)
13
balls[i].x += (balls[i].speed)*sin(radians(angle))
balls[i].y -= (balls[i].speed)*cos(radians(angle))
balls[j].x -= (balls[j].speed)*sin(radians(angle))
balls[j].y += (balls[j].speed)*cos(radians(angle))
def border():
[Link](display, gray, (0, 0, width, 30))
[Link](display, gray, (0, 0, 30, height))
[Link](display, gray, (width - 30, 0, width, height))
[Link](display, gray, (0, height - 30, width, height))
def score():
font = [Link]("Agency FB", 30)
[Link](display, (51, 51, 51), (0, height, width, outerHeight))
for i in range(len(balls)):
balls[i].draw((i + 1)*2*(radius + 1), height + radius + 10)
text = [Link]("Remaining Balls: " + str(len(balls)), True, stickColor)
[Link](text, (width/2 + 50, height + radius/2))
def reset():
global balls, noBalls
noBalls = 15
balls = []
s = 70
b1 = Ball(s, height/2 - 4*radius, 0, colors[0], 0, 1)
b2 = Ball(s + 2*radius, height/2 - 3*radius, 0, colors[1], 0, 2)
b3 = Ball(s, height/2 - 2*radius, 0, colors[2], 0, 3)
b4 = Ball(s + 4*radius, height/2 - 2*radius, 0, colors[3], 0, 4)
b5 = Ball(s + 2*radius, height/2 - 1*radius, 0, colors[4], 0, 5)
b6 = Ball(s, height/2, 0, colors[5], 0, 6)
14
b7 = Ball(s + 6*radius, height/2 - 1*radius, 0, colors[6], 0, 7)
b8 = Ball(s + 4*radius, height/2, 0, colors[7], 0, 8)
b9 = Ball(s + 8*radius, height/2, 0, colors[8], 0, 9)
b10 = Ball(s + 6*radius, height/2 + 1*radius, 0, colors[9], 0, 10)
b11 = Ball(s + 2*radius, height/2 + 1*radius, 0, colors[10], 0, 11)
b12 = Ball(s, height/2 + 2*radius, 0, colors[11], 0, 12)
b13 = Ball(s + 4*radius, height/2 + 2*radius, 0, colors[12], 0, 13)
b14 = Ball(s + 2*radius, height/2 + 3*radius, 0, colors[13], 0, 14)
b15 = Ball(s, height/2 + 4*radius, 0, colors[14], 0, 15)
[Link](b1)
[Link](b2)
[Link](b3)
[Link](b4)
[Link](b5)
[Link](b6)
[Link](b7)
[Link](b8)
[Link](b9)
[Link](b10)
[Link](b11)
[Link](b12)
[Link](b13)
[Link](b14)
[Link](b15)
def gameOver():
font = [Link]("Agency FB", 75)
15
if len(balls) == 0:
text = [Link]("You Won!", True, (133, 193, 233))
else:
text = [Link]("You Lost! Black in Hole!", True, (241, 148, 138))
while True:
for event in [Link]():
if [Link] == [Link]:
close()
if [Link] == [Link]:
if [Link] == pygame.K_q:
close()
if [Link] == pygame.K_r:
poolTable()
[Link](text, (50, height/2))
[Link]()
[Link]()
def close():
[Link]()
[Link]()
# Main Function
def poolTable():
loop = True
reset()
noPockets = 6
pockets = []
p1 = Pockets(0, 0, black)
p2 = Pockets(width/2 - p1.r*2, 0, black)
16
p3 = Pockets(width - p1.r - margin - 5, 0, black)
p4 = Pockets(0, height - margin - 5 - p1.r, black)
p5 = Pockets(width/2 - p1.r*2, height - margin - 5 - p1.r, black)
p6 = Pockets(width - p1.r - margin - 5, height - margin - 5 - p1.r, black)
[Link](p1)
[Link](p2)
[Link](p3)
[Link](p4)
[Link](p5)
[Link](p6)
cueBall = Ball(width/2, height/2, 0, white, 0, "cue")
cueStick = CueStick(0, 0, 100, stickColor)
start = 0
end = 0
while loop:
for event in [Link]():
if [Link] == [Link]:
close()
if [Link] == [Link]:
if [Link] == pygame.K_q:
close()
if [Link] == pygame.K_r:
poolTable()
if [Link] == [Link]:
start = [cueBall.x, cueBall.y]
x, y = [Link].get_pos()
end = [x ,y]
17
dist = ((start[0] - end[0])**2 + (start[1] - end[1])**2)**0.5
force = dist/10.0
if force > 10:
force = 10
[Link](cueBall, force)
[Link](background)
[Link](cueBall.x, cueBall.y)
[Link]()
if not ([Link] > 0):
[Link](cueBall.x, cueBall.y)
for i in range(len(balls)):
balls[i].draw(balls[i].x, balls[i].y)
for i in range(len(balls)):
balls[i].move()
checkCollision()
checkCueCollision(cueBall)
border()
for i in range(noPockets):
pockets[i].draw()
for i in range(noPockets):
pockets[i].checkPut()
if len(balls) == 1 and balls[0].ballNum == 8:
gameOver()
score()
[Link]()
[Link](60)
poolTable()
18
8 Ball Pool is a really famous game and therefore it will be hard to penetrate the
market. However, the success rate of the 8 Ball Pool game and the revenue model is
quite appealing and therefore one can penetrate the market with the perfect product.
Your app should be able to provide the best gaming experience, plus, it should have
new features and functionalities so that the users are drawn towards your app. Just
make sure that you hire the best gaming app development company so that you can
have the perfect 8 Ball Pool app that can penetrate the existing market.
Python
[Link] scan this
Python/blob/master/8_Ball_Pool/[Link] for site
19