0% found this document useful (0 votes)
19 views

CS Project Tank Stars

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CS Project Tank Stars

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

KENDRIYA VIDYALAYA ONGC PANVEL

COMPUTER SCIENCE PROJECT


SESSION 2022-2023

TANK STARS IN PYTHON

SUBJECT TEACHER:- MRS. SHRUTI


SRIVASTAVA

NAMES:-AINESH INGLE 11203


MURALI.V 11209
RISHI GUPTA 11211
SARTHAK SINGH 11212

CLASS:- XI B
CERTIFICATE
This is to certify that the students have done the project
on a shooting based game under my supervision.
They have completed this project with utmost
dedication.
I certify this project up to my expectations and as per
the guidelines issued by CBSE.

EXAMINER

PRINCIPAL SCHOOL STAMP


ACKNOWLEDGEMENT
With great pleasure we would like to sincerely thank our
teacher Mrs. Shruti Srivastava for guiding us throughout the
project and for teaching us so dedicatedly. It is always a great
experience to learn from her.

We would also like to express our gratitude to our principal


Mr. Vijay Kumar Garg who has always inspired us.
HARDWARE AND SOFTWARE
REQUIREMENTS
Processor: Intel Core i3
Operating system: Linux- Ubuntu 16.04 to 17.10, or
Windows 7 to 10, with 2GB RAM (4GB preferable)
Python Version: 3.9
INDEX

Sr No. Topic
1. Introduction to project
2. Code
3. Output
4. Bibliography
INTRODUCTION

This project file contains a python script. This is a


complex program of a shooting based game. It is
constructed by importing three libraries (pygame,
random and math). It utilizes the concepts of
conditional statements i.e (def , if, elif, else, for & while
loop). For visuals it has 4 elements (i.e background,
opponent, bullet, and player).
We have to defeat the opponent by shooting the
opponent. The score is displayed at the top.
CODE
import math
import random

import pygame
from pygame import mixer

# Initialize the pygame


pygame.init()

# Creating the screen


screen = pygame.display.set_mode((800,600))

# Image for Background


background = pygame.image.load('udo.png')

# background sound
mixer.music.load('background.wav')
mixer.music.play(-1)

# Title and Icon


pygame.display.set_caption("Tank Stars")
icon = pygame.image.load('player.png')
pygame.display.set_icon(icon)

# About player
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 450
playerX_change = 0

# About Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6 # you can change the no. of
enemies in future

for i in range(num_of_enemies):

enemyImg.append(pygame.image.load('opponent.png'))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(2)
enemyY_change.append(40)

# Bullet

# Ready - You cant see the bullet on the screen


# Fire - The bullet is currently moving

bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 9
bullet_state = "ready"

# Score of the game

score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)

textX = 10
testY = 10

# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)

def show_score(x, y):


score = font.render("Score : " + str(score_value),
True, (255, 255, 255))
screen.blit(score, (x, y))

def game_over_text():
over_text = over_font.render("GAME OVER", True,
(255, 255, 255))
screen.blit(over_text, (200, 250))

def player(x, y):


screen.blit(playerImg, (x, y))
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))

def fire_bullet(x , y):


global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))

def isCollision(enemyX, enemyY, bulletX, bulletY):


distance = math.sqrt((math.pow(enemyX-bulletX,2))
+ (math.pow(enemyY-bulletY,2)))
if distance < 27:
return True
else:
return False

# Game Loop
running = True
while running:

# RGB means Red, Green, Blue


screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or
left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
playerX_change = -6
if event.key == pygame.K_d:
playerX_change = 6
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bulletSound = mixer.Sound("laser.wav")
bulletSound.play()
# Get the current x cordinate of the
spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)

if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key ==
pygame.K_d:
playerX_change = 0

# 5 = 5 + -0.1 -> 5 = 5 - 0.1


# 5 = 5 + 0.1

playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Movement of Enemy
for i in range(num_of_enemies):

# Game Over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
break

enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 2
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -2
enemyY[i] += enemyY_change[i]

# Collision
collision = isCollision(enemyX[i], enemyY[i],
bulletX, bulletY)
if collision:
explosionSound =
mixer.Sound("explosion.wav")
explosionSound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 150)

enemy(enemyX[i], enemyY[i], i)

# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"

if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change

player(playerX, playerY)
show_score(textX, testY)
pygame.display.update()
OUTPUT
BIBLIOGRAPHY

1. https://www.flaticon.com/
2. https://knowpythonbytes.blogspot.com/
3. https://python4csip.com/
4. Online tutor:- Sagar
THANK
YOU

You might also like