Cs Project New
Cs Project New
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:
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).
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)
8250U
pygame.init()
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")
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
else:
pygame.draw.rect(screen, (0, 0, 0, 0), (x, y, w, h))
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))
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
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
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()
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))
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))
bullet_group.add(bullet)
score += (len(current_word) * 100)
bottom_spaceship.animate()
current_word = ""
asteroid_group.update()
spaceship_group.update()
bullet_group.update()
asteroid_group.draw(screen)
spaceship_group.draw(screen)
bullet_group.draw(screen)
pygame.display.flip()
clock.tick(FPS)
show_start_screen()
OUTPUT
BIBILIOGRAPHY