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

Cs Project New

The document describes 'The Space Bar', an action-packed typing game where players control spaceships to destroy asteroids by typing words. It includes various difficulty levels, a leaderboard for high scores, and utilizes object-oriented programming concepts along with database management for score storage. Future enhancements may include more complex game mechanics, online score submissions, and adaptations for educational purposes or mobile platforms.

Uploaded by

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

Cs Project New

The document describes 'The Space Bar', an action-packed typing game where players control spaceships to destroy asteroids by typing words. It includes various difficulty levels, a leaderboard for high scores, and utilizes object-oriented programming concepts along with database management for score storage. Future enhancements may include more complex game mechanics, online score submissions, and adaptations for educational purposes or mobile platforms.

Uploaded by

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

THE SPACE BAR

By
Vikesh Mugunth A
INDEX

1. PROBLEM DEFINITION
2. PROBLEM ANALYSIS
3. HARDWARE AND
SOFTWARE REQUIREMENT
4. FUTURE ENHANCEMENT
5. SOURCE CODE
6. OUTPUT
7. BIBILIOGRAPHY
PROBLEM DEFINITION
INTRODUCTION:
"The Space Bar" is an action-packed typing
game where players control spaceships to
destroy asteroids by typing words. The game
features various difficulty levels, exciting
gameplay with bullet shooting, and a
leaderboard to track the best players. Players
must type quickly and accurately to score high.

GENERAL EXPLANATION:
The game involves two spaceships that fire
bullets by typing words displayed on
approaching asteroids. Players must type the
correct words to destroy asteroids while
avoiding collisions. The game has multiple
difficulty levels, each affecting the speed of
asteroid spawn, and stores high scores in an
SQLite database.
WHY I HAVE THE CHOOSEN THE
PROJECT:

I chose this project because it blends


entertainment with skill-building, offering a
fun way to improve typing speed while
playing an engaging space-themed game. It
allowed me to work with key programming
concepts, including object-oriented design,
game mechanics, and database management
for storing high scores. The project also
offers room for expansion, like adding new
features or difficulty levels. Overall, it’s a
great opportunity to create a meaningful and
enjoyable experience.
PROBLEM ANALYSIS
1. load_image(file_path):
This function loads an image from the specified
file path and returns it as a pygame surface
object with transparency(using convert_alpha()).

2. Spaceship Class (Spaceship):


Manages the spaceship's actions, including
movement, animation, death and shooting.
Key Methods:
update(): Updates the spaceship's animation
or death state.
animate(): Starts the spaceship’s shooting.
animation.die(): Marks the spaceship as dead
and triggers the death animation.

3. Bullet Class (Bullet):


Represents a bullet fired by the spaceship.
Key Methods:
update(): Moves the bullet horizontally and
checks if it’s off-screen to remove it.

4. Asteroid Class (Asteroid):


Represents an asteroid that contains a word
the player needs to type.

Key Methods:
update(): Moves the asteroid across the
screen and removes it if it goes off-screen.

5.spawn_asteroid (asteroid_group,
position):
Spawns a new asteroid with a random
word at a given position.

6.draw_button(text,x,y,w,h,
action=None):
Draws a clickable button on the screen and
calls the specified action when clicked.
7. show_start_screen():
Displays the main menu with the game title
and options to start or view the leaderboard.

8. show_leaderboard():
Displays the leaderboard with player
names and scores from the database.

9.choose_difficulty_screen():
Displays the screen where the player can
choose the difficulty level of the game (Easy,
Medium, Hard).

10. save_score(player_name, score):


Saves the player’s score to a database (future
functionality).

11.start_game(speed):
Starts the game with the given difficulty speed
(affecting how quickly asteroids spawn).
12. show_game_over_screen(score):
Displays the game-over screen with the player's
score and allows them to enter their name to
save the score.

Tables:
1. Players Table:
id: Unique player ID
name: Player's name
difficulty: Selected difficulty (easy, medium,
hard)
score: Player's final score
date_played: Date and time when the game was
played

2. Leaderboard Table:
id: Unique leaderboard entry ID
player_name: Player's name
score: Player's score
difficulty: Selected difficulty (easy, medium,
hard)
rank: Player's rank based on score
date_recorded: Date when the score was
recorded.

3. Asteroids Table:
id: Unique asteroid ID
word: Word associated with the asteroid
difficulty: Difficulty level (easy,
medium, hard)
status: Current status of the asteroid
(used/unused)

4. Game Sessions Table:


session_id: Unique session
ID
player_id: Foreign key linking to the
Players table
score: Final score for the session
duration: Duration of the game session in
seconds
end_time: Timestamp when the session
ended
HARDWARE AND
SOFTWARE
REQUIREMENT
OS - Microsoft Windows 10

System type - X86-64bit based processor

Processor - Intel(R) core(TM) i5-

8250U

CPU - 2.4 GHz

Memory (RAM) - 8.00 GB

Front End -Python v3.8.2:7b3ab59, MSC v.1916 64 bit


(AMD64)]

Back End -MySQL Version 8.0


FUTURE ENHANCEMENT

Future scope of the project :

The future scope of this project includes integrating more


complex game mechanics, such as power-ups, levels, and
advanced enemy types. Additionally, enhancing the
leaderboard system with online score submission,
implementing multiplayer modes, and expanding visual
effects and animations can improve gameplay. The
database can also be optimized to store more player data
and improve performance.

Future application area of the project:

The project can be expanded for educational


purposes, helping users improve typing speed and
accuracy through interactive gameplay. It could also
be used for cognitive training in areas like word
recognition and reflexes. Additionally, the game
could be adapted for mobile platforms, integrated
into typing software, or deployed as part of fitness
applications for brain exercise.
SOURCE CODE
from turtle import back
import pygame
import random
import time

pygame.init()

WIDTH, HEIGHT = 800, 600


screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("The Space Bar")

def load_image(file_path):
image = pygame.image.load(file_path).convert_alpha()
return image

spaceship_sheet = load_image("Attack_1.png")
death_sheet = load_image("Destroyed.png")
bullet_image = load_image("Charge_1.png")
asteroid_image = load_image("asteroid.png")
background = load_image("space.png")

WHITE = (255, 255, 255)


BLACK = (0, 0, 0)
RED = (255, 0, 0)

FPS = 60
font = pygame.font.Font(None, 36)
input_font = pygame.font.Font(None, 48)
button_font = pygame.font.Font(None, 50)
title_font = pygame.font.Font(None, 120)
playgame = True
words = ["asteroid", "comet", "spaceship", "meteor", "galaxy", "planet", "star"]

data = {'a':500}

class Spaceship(pygame.sprite.Sprite):
def init (self, position):
super(). init ()
self.frames = [spaceship_sheet.subsurface(pygame.Rect(i * 192, 0, 192, 192)) for i in
range(4)]
self.death_frames = [death_sheet.subsurface(pygame.Rect(i * 192, 0, 192, 192)) for i in
range(21)]
self.current_frame = 0
self.image = self.frames[self.current_frame]
self.rect = self.image.get_rect(midleft=(50, position))
self.is_alive = True
self.death_animating = False
self.death_frame = 0
self.death_delay = 0
self.shooting = False
self.shoot_delay = 0

def update(self):
if self.is_alive:
if self.shooting:
self.shoot_delay += 1
if self.shoot_delay % 5 == 0:
self.current_frame = (self.current_frame + 1) % len(self.frames)
self.image = self.frames[self.current_frame]
if self.current_frame == 0:
self.shooting = False
else:
self.image = self.frames[0]
else:
if self.death_animating:
self.death_delay += 1
if self.death_delay % 5 == 0:
if self.death_frame < len(self.death_frames):
self.image = self.death_frames[self.death_frame]
self.death_frame += 1
else:
self.kill()

def animate(self):
self.shooting = True
self.current_frame = 1

def die(self):
self.is_alive = False
self.death_animating = True

class Bullet(pygame.sprite.Sprite):
def init (self, x, y):
super(). init ()
self.image = bullet_image
self.rect = self.image.get_rect(center=(x, y))

def update(self):
self.rect.x += 5
if self.rect.x > WIDTH:
self.kill()

class Asteroid(pygame.sprite.Sprite):
def init (self, word, position):
super(). init ()
self.word = word
self.image = asteroid_image.copy()
self.rect = self.image.get_rect(midright=(WIDTH, position))
self.text_surf = font.render(word, True, WHITE)
text_rect = self.text_surf.get_rect(center=(self.rect.width // 2, self.rect.height // 2))
self.image.blit(self.text_surf, text_rect.topleft)
self.spawn_time = time.time()

def update(self):
self.rect.x -= 1.5
if self.rect.x < 0:
self.kill()
#have the word variable choose a random words from the table in sql and the delete the word list
at the start of the program

def spawn_asteroid(asteroid_group, position):


word = random.choice(words)
asteroid = Asteroid(word,
position)
asteroid_group.add(asteroid)
return asteroid

def draw_button(text, x, y, w, h, action=None):


mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, (0, 0, 0, 0), (x, y, w,
h)) if click[0] == 1 and action is not None:
action()

else:
pygame.draw.rect(screen, (0, 0, 0, 0), (x, y, w, h))

text_surf = button_font.render(text, True, WHITE)


screen.blit(text_surf, (x + 20, y + 10))

def show_start_screen():
start_screen = True
while start_screen:
screen.fill(BLACK)
screen.blit(background, (0, 0))
title = title_font.render("THE SPACE BAR", True, WHITE)
screen.blit(title, (WIDTH // 2 - title.get_width() // 2, 150))
draw_button("Leaderboard", WIDTH // 3, HEIGHT // 2 , 300, 60, leaderboard_diff)
draw_button("Start Game", WIDTH // 3, HEIGHT // 2 + 80, 300, 60,
choose_difficulty_screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type ==
pygame.QUIT:
pygame.quit()
quit()
def show_leaderboard():
# ive added 3 difficulty options so there should be different leaderboards based on the diffuclty,
so just create 3 tables with names easy medium and hard and based on what the variable diff is,
read the entries fromt that table
leaderboard_screen = True
while leaderboard_screen:
screen.fill(BLACK)
screen.blit(background, (0, 0))
heading_text = font.render("Leaderboard", True, WHITE)
screen.blit(heading_text, (WIDTH // 2 - heading_text.get_width() // 2, 50))

player_heading = font.render("Player", True, WHITE)


score_heading = font.render("Score", True, WHITE)
screen.blit(player_heading, (WIDTH // 3, 100))
screen.blit(score_heading, (WIDTH // 2 + 50, 100))

y_offset = 140
for player, score in data.items(): #here, instead of having it iterate through the dictionary,
make it go through our score database, but only like top 7 eneteries
player_text = font.render(player, True, WHITE)
score_text = font.render(str(score), True, WHITE)
screen.blit(player_text, (WIDTH // 3, y_offset))
screen.blit(score_text, (WIDTH // 2 + 50, y_offset))
y_offset += 40

draw_button("Back to Start", WIDTH // 3, HEIGHT // 2 + 150, 300, 60,


show_start_screen)
pygame.display.flip()

for event in pygame.event.get():


if event.type == pygame.QUIT:
pygame.quit()
quit()

def easy():
diff =
'easy'
if playgame:
start_game(6)
else:
show_leaderboard()
def medium():
diff = 'medium'
if playgame:
start_game(4)
else:
show_leaderboard()

def hard():
diff = 'hard'
if playgame:
start_game(3)
else:
show_leaderboard()

def choose_difficulty_screen():
choose_difficulty_screen = True
while choose_difficulty_screen:
global playgame
screen.fill(BLACK)
screen.blit(background, (0,0))
playgame = True

draw_button("EASY", 50, HEIGHT // 2 - 60, WIDTH // 4, 60, easy)


draw_button("MEDIUM", 300, HEIGHT // 2 - 60, WIDTH // 4, 60, medium)
draw_button("HARD", 550, HEIGHT // 2 - 60, WIDTH // 4, 60, hard)
pygame.display.flip()

for event in pygame.event.get():


if event.type ==
pygame.QUIT:
pygame.quit()
quit()

def leaderboard_diff():
leaderboard_diff = True
while leaderboard_diff:
global playgame
screen.fill(BLACK)
screen.blit(background, (0,0))
playgame = False
draw_button("EASY", 50, HEIGHT // 2 - 60, WIDTH // 4, 60, easy)
draw_button("MEDIUM", 300, HEIGHT // 2 - 60, WIDTH // 4, 60, medium)
draw_button("HARD", 550, HEIGHT // 2 - 60, WIDTH // 4, 60, hard)
pygame.display.flip()

for event in pygame.event.get():


if event.type ==
pygame.QUIT:
pygame.quit()
quit()

def show_game_over_screen(score):
game_over_screen = True
player_name = ""
while game_over_screen:
screen.fill(BLACK)
screen.blit(background, (0, 0))
game_over_text = font.render(f"Game Over! Your score: {score}", True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 -
20))

name_text = input_font.render("Enter your name: " + player_name, True, WHITE)


screen.blit(name_text, (WIDTH // 2 - name_text.get_width() // 2, HEIGHT // 2 + 20))

draw_button("Leaderboard", WIDTH // 3, HEIGHT // 2 + 80, 300, 60, lambda:


save_score(player_name, score))
pygame.display.flip()

for event in pygame.event.get():


if event.type ==
pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.unicode.isalpha():
player_name += event.unicode
elif event.key == pygame.K_BACKSPACE:
player_name = player_name[:-1]
elif event.key == pygame.K_RETURN:
save_score(player_name, score)
#ok over here, instead of updating the score into the data dictionary, have it update into the sql
table and then delted the data dictionary at the start

def save_score(player_name, score):


if player_name:
data[player_name] = score
show_leaderboard()

def start_game(speed):
game_active = True
top_spaceship = Spaceship(HEIGHT // 4)
bottom_spaceship = Spaceship(HEIGHT - HEIGHT // 4)
spaceship_group = pygame.sprite.Group(top_spaceship, bottom_spaceship)

asteroid_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()

current_word = ""
last_spawn_time = speed + 1
score = 0
clock = pygame.time.Clock()
game_over = False

while game_active:
screen.blit(background, (0, 0))

if game_over and time.time() - oof > 1.5:


show_game_over_screen(score)

for event in pygame.event.get():


if event.type ==
pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if not game_over:
if event.unicode.isalpha():
current_word += event.unicode
elif event.key == pygame.K_BACKSPACE:
current_word = current_word[:-1]
elif event.key == pygame.K_RETURN:
for asteroid in asteroid_group:
if current_word == asteroid.word:
if asteroid.rect.centery < HEIGHT // 2:
bullet = Bullet(top_spaceship.rect.centerx + 64,
top_spaceship.rect.centery)
bullet_group.add(bullet)
score += (len(current_word) * 100)
top_spaceship.animate()
current_word = ""
elif asteroid.rect.centery >= HEIGHT // 2:
bullet = Bullet(bottom_spaceship.rect.centerx + 64,
bottom_spaceship.rect.centery)

bullet_group.add(bullet)
score += (len(current_word) * 100)
bottom_spaceship.animate()
current_word = ""

if not game_over and time.time() - last_spawn_time > speed:


spawn_asteroid(asteroid_group, random.randint(100, HEIGHT // 2 - 100))
spawn_asteroid(asteroid_group, random.randint(HEIGHT // 2 + 100, HEIGHT - 100))
last_spawn_time = time.time()

for spaceship in spaceship_group:


if pygame.sprite.spritecollide(spaceship, asteroid_group, False):
for asteroid in asteroid_group:
asteroid.kill()
for spaceship in spaceship_group:
spaceship.die()
game_over = True
oof = time.time()
for bullet in bullet_group:
asteroid_collisions = pygame.sprite.spritecollide(bullet, asteroid_group, True)
if asteroid_collisions:
bullet.kill()

asteroid_group.update()
spaceship_group.update()
bullet_group.update()

asteroid_group.draw(screen)
spaceship_group.draw(screen)
bullet_group.draw(screen)

input_text = input_font.render(current_word, True, WHITE)


screen.blit(input_text, (10, HEIGHT - 50))

score_text = font.render(f"Score: {score}", True, WHITE)


screen.blit(score_text, (10, 10))

pygame.display.flip()
clock.tick(FPS)

show_start_screen()
OUTPUT
BIBILIOGRAPHY

• The Complete Reference Python by Martin C.


Brown from Mc. GrawHill publication.
• Computer Science with Python by Sumita
Arora from Dhanpatrai &Co. publication
• www.geeksforgeeks.org
• www.python.org

You might also like