Quote for the day ! Mistakes are a fact of life.It is the response to the error that counts.
Select Page

Python and Data Science Sample Projects

by | Oct 12, 2021 | Uncategorized | 0 comments

1. Bricks Breakout Game

import pygame
pygame.init()

WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
RED = (255,0,0)

bricks1=[pygame.Rect(10 + i* 100,60,80,30) for i in range(6)]
bricks2=[pygame.Rect(10 + i* 100,100,80,30) for i in range(6)]
bricks3=[pygame.Rect(10 + i* 100,140,80,30) for i in range(6)]

score = 0

velocity=[1,1]
size = (600, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption(“Breakout Game”)
ball=pygame.Rect(200,250,10,10)
carryOn = True
while carryOn:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
font = pygame.font.Font(None, 34)
text = font.render(“Score: ” + str(score), 1, WHITE)
screen.blit(text, (20,10))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if event.key == pygame.K_LEFT:
# brick wall
for i in bricks1:
pygame.draw.rect(screen,RED,i)
for i in bricks2:
pygame.draw.rect(screen,RED,i)
for i in bricks3:
pygame.draw.rect(screen,RED,i)

#ball movement
ball.x+=velocity
ball.y+=velocity

if ball.x>=590 or ball.x<=0:
velocity = -velocity
if ball.y<=38 :
velocity = -velocity
velocity=-velocity
if ball.y>=590:
font = pygame.font.Font(None, 74)
text = font.render(“GAME OVER”, 1, RED)
screen.blit(text, (150,350))
pygame.display.flip()
pygame.time.wait(2000)
break
pygame.draw.rect(screen,WHITE ,ball)
#score
for i in bricks1:
if i.collidepoint(ball.x,ball.y):
bricks1.remove(i)
velocity = -velocity
velocity=-velocity
score+=1
for i in bricks2:
if i.collidepoint(ball.x,ball.y):
bricks2.remove(i)
velocity = -velocity
velocity=-velocity
score+=1
for i in bricks3:
if i.collidepoint(ball.x,ball.y):
velocity = -velocity
velocity=-velocity
bricks3.remove(i)
score+=1

if score==18:
font = pygame.font.Font(None, 74)
text = font.render(“YOU WON!!”, 1, RED)
screen.blit(text, (150,350))
pygame.display.flip()
pygame.time.wait(2000)
break
pygame.time.wait(1)
pygame.display.flip()
pygame.quit( ) Explanation

Python is very useful for creating games. Bricks breakout game is such a game in which pygame python package plays the major role. Large number of libraries are available for different functionality.

This program can be done in spyder and install the package pygame with the command

pip install pygame

3 bricks are created using list and function and breaks that bricks with the help of paddle and mouse.

2. RG Sharma Cricket Score Game

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#%matplotlib inline

df_Rohit = df[df[“player_dismissed”] == “RG Sharma”]

df_Rohit_dismissed = df_Rohit.groupby(“ball”).count()

plt.bar(df_Rohit_dismissed.index, df_Rohit_dismissed[“player_dismissed”])

df_runs_per_ball = df.groupby(“ball”).sum()

plt.bar( df_runs_per_ball.index, df_runs_per_ball[“batsman_runs”]) We have plotted bar chart for df_Rohit_dismissed dataframe’s index value which are, infact, each ball of the over as
categories or x-axis of the chart and the dismissals of Rohit Sharma as y-axis.
plt.show() function combines all the elements of charts and shows them in harmony.
In : df_Rohit = df[df[“player_dismissed”] == “RG Sharma”]
df_Rohit_dismissed =  f_Rohit.groupby(“ball”).count()
df_Rohit_dismissed
Out:
In : plt.title(“Number of times the player was dismissed on each ball of over”)
plt.bar(df_Rohit_dismissed.index, df_Rohit_dismissed[“player_dismissed”])
plt.xlabel(“Ball of the over”)
We have plotted the graph with 0-8 being the categories representing each ball of the over where 7 & 8 are the balls that occurred when the over had wide or no balls. The height of the categories is based upon the count of the player_dismissed feature. The graph is an output of
the code.
Conclusion: RG Sharma is most vulnerable to 2nd ball of the over.
2. Understanding the most productive ball for Rohit Sharma
Grouping of data
we have grouped data using .grouby() function using various values of ball feature/column. The groupby() function is then followed by .sum() to summarize values for other numerical columns in the dataframe. The resulting dataframe is then assigned to dataframe df_runs_per_ball .
batsman_runs ball
1 804
2 819
3 890
4 920
5 795
6 855
7 136
8 11
Plotting of information
plt.ylabel(“Dismissals”)
plt.show() Blog Technical Support Developing Resources