Computer science project
Computer science project
I
COMPUTER SCIENCE INVESTIGATORY
PROJECT
(2024 – 2025)
2
CERTIFICATE
3
ACKNOWLEDGMENT
4
OBJECTIVE
To design and implement a 2D arcade-style game,
"Space Alien Invader," using Python programming
and the Pygame library. The project aims to enhance
understanding of game development concepts such as
object movement, collision detection, and event
handling.
INTRODUCTION
Game development is an exciting domain that
combines programming skills with creativity. "Space
Alien Invader" is inspired by classic arcade games,
where the player controls a spaceship to eliminate
incoming alien threats. The game integrates concepts
of real-time rendering, user input handling and
dynamic scoring, providing an engaging experience.
SYSTEM REQUIREMENTS
Python 3.9 or later
Pygame library
Computer with Windows/Linux/MacOS
IDLE or Pycharm
5
PROCEDURE
Setup: - Install Python and the Pygame Library
on your Pc or Laptop.
Development: - Write the code to initialize the
game environment, load assets, and implement
game logic.
Testing: Test the game for bugs, and validate
player controls, enemy behaviour, and scoring
mechanisms.
Deployment: Package the game for execution on
compatible systems.
CODE IMPLEMENTATION
import pygame
import random
import os # For file handling
pygame.init()
pygame.mixer.init()
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode((screen_width, screen_height),
pygame.FULLSCREEN)
pygame.display.set_caption("Space Alien Invader")
6
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
# Load images
background_image = pygame.image.load('background.png')
background_image = pygame.transform.scale(background_image,
(screen_width, screen_height))
intro_image = pygame.image.load('intro_image.png')
player_image = pygame.image.load('player_image.png')
alien_image = pygame.image.load('alien_image.png')
big_alien_25_image = pygame.image.load('big_alien_25.png') #
Image for 25-point alien
big_alien_50_image = pygame.image.load('big_alien_50.png') #
Image for 50-point alien
player_image = pygame.transform.scale(player_image, (50, 50))
alien_image = pygame.transform.scale(alien_image, (50, 50))
big_alien_25_image =
pygame.transform.scale(big_alien_25_image, (80, 80))
big_alien_50_image =
pygame.transform.scale(big_alien_50_image, (100, 100))
# Rotate the big alien images by 180 degrees
big_alien_25_image =
7
pygame.transform.rotate(big_alien_25_image, 180)
big_alien_50_image =
pygame.transform.rotate(big_alien_50_image, 180)
player_width = 50
player_height = 50
player_speed = 5
bullet_width = 5
bullet_height = 10
bullet_speed = 7
enemy_bullet_width = 5
enemy_bullet_height = 10
enemy_bullet_speed = 5
alien_width = 50
alien_height = 50
alien_speed = 2
score = 0
high_score = 0 # Variable for the high score
high_score_file = "high_score.txt"
font = pygame.font.SysFont("Arial", 30)
clock = pygame.time.Clock()
shooting_sound = pygame.mixer.Sound('shooting_sound.wav')
# Load high score from file
if os.path.exists(high_score_file):
with open(high_score_file, "r") as file:
8
high_score = int(file.read().strip())
def save_high_score():
"""Save the high score to a file."""
with open(high_score_file, "w") as file:
file.write(str(high_score))
def reset_game():
global player_x, player_y, bullets, aliens, big_aliens,
enemy_bullets, score, game_active, last_spawn_time,
alien_spawn_time, last_player_shot_time, last_enemy_shot_time
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 10
bullets = []
aliens = []
big_aliens = [] # List for big aliens
enemy_bullets = []
score = 0
game_active = True
last_spawn_time = pygame.time.get_ticks()
alien_spawn_time = 2000
last_player_shot_time = 0 # Reset player shot time
last_enemy_shot_time = 0 # Reset enemy shot time
def draw_player(x, y):
screen.blit(player_image, (x, y))
9
def draw_bullet(bullets):
for bullet in bullets:
pygame.draw.rect(screen, white, bullet)
def draw_enemy_bullets(enemy_bullets):
for bullet in enemy_bullets:
pygame.draw.rect(screen, red, bullet)
def draw_aliens(aliens, big_aliens):
for alien in aliens:
screen.blit(alien_image, (alien.x, alien.y))
for big_alien in big_aliens[:]:
if big_alien['type'] == 25:
screen.blit(big_alien_25_image, (big_alien['rect'].x,
big_alien['rect'].y))
elif big_alien['type'] == 50:
screen.blit(big_alien_50_image, (big_alien['rect'].x,
big_alien['rect'].y))
def draw_score(score, high_score):
score_text = font.render(f"Score: {score}", True, white)
high_score_text = font.render(f"High Score: {high_score}",
True, white)
screen.blit(score_text, (10, 10))
screen.blit(high_score_text, (10, 40))
def check_collision(bullets, aliens, big_aliens):
10
global score
for bullet in bullets[:]:
for alien in aliens[:]:
if bullet.colliderect(alien):
if bullet in bullets:
bullets.remove(bullet)
if alien in aliens:
aliens.remove(alien)
score += 10
for big_alien in big_aliens[:]:
if bullet.colliderect(big_alien['rect']):
if bullet in bullets:
bullets.remove(bullet)
# Reduce the health of the big alien
big_alien['health'] -= 1
if big_alien['health'] <= 0:
big_aliens.remove(big_alien)
score += big_alien['type'] # Add the points based on
the type (25 or 50)
def check_enemy_bullet_collision(enemy_bullets):
global game_active
for bullet in enemy_bullets[:]:
if bullet.colliderect(pygame.Rect(player_x, player_y,
player_width, player_height)):
11
enemy_bullets.remove(bullet)
game_active = False
def show_game_over():
global score, high_score
if score > high_score:
high_score = score
save_high_score()
game_over_text = font.render("Game Over! Press Shift to
Restart", True, white)
screen.blit(game_over_text, (250, 250))
quit_text = font.render("Press Tab to Quit", True, white)
high_score_text = font.render(f"High Score: {high_score}",
True, white)
screen.blit(high_score_text, (270, 200))
screen.blit(quit_text, (270, 300))
pygame.display.update()
def show_intro_screen():
screen.fill(black)
# Center the intro image
intro_rect = intro_image.get_rect(center=(screen_width // 2,
screen_height // 2))
screen.blit(intro_image, intro_rect)
# Render and position the "Press Enter to Start" text below the
intro image
12
intro_text = font.render("Press Enter to Start", True, green)
text_rect = intro_text.get_rect(center=(screen_width // 2,
screen_height - 100))
screen.blit(intro_text, text_rect)
pygame.display.update()
def update_enemy_bullets(enemy_bullets):
"""Move the enemy bullets down and remove if they go off-
screen."""
global game_active
for bullet in enemy_bullets[:]:
bullet.y += enemy_bullet_speed
if bullet.y > screen_height:
enemy_bullets.remove(bullet)
if bullet.colliderect(pygame.Rect(player_x, player_y,
player_width, player_height)):
enemy_bullets.remove(bullet)
game_active = False # End the game if the player is hit
def main():
global player_x, player_y, bullets, aliens, big_aliens, score,
enemy_bullets, game_active, last_spawn_time, alien_spawn_time,
last_player_shot_time, last_enemy_shot_time
reset_game()
show_intro_screen()
waiting_for_enter = True
13
while True:
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_TAB:
pygame.quit()
return
if waiting_for_enter and event.key ==
pygame.K_RETURN:
waiting_for_enter = False
reset_game()
if not waiting_for_enter and game_active:
screen.blit(background_image, (0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < screen_width -
player_width:
player_x += player_speed
if keys[pygame.K_UP] and player_y > 0:
14
player_y -= player_speed
if keys[pygame.K_DOWN] and player_y < screen_height
- player_height:
player_y += player_speed
if keys[pygame.K_SPACE] and pygame.time.get_ticks() -
last_player_shot_time > 300:
bullet = pygame.Rect(player_x + player_width // 2 -
bullet_width // 2, player_y, bullet_width, bullet_height)
bullets.append(bullet)
shooting_sound.play()
last_player_shot_time = pygame.time.get_ticks()
for bullet in bullets[:]:
bullet.y -= bullet_speed
if bullet.y < 0:
bullets.remove(bullet)
# Spawn normal aliens
if pygame.time.get_ticks() - last_spawn_time >
alien_spawn_time:
alien_x = random.randint(0, screen_width - alien_width)
alien_y = random.randint(-150, -50)
alien = pygame.Rect(alien_x, alien_y, alien_width,
alien_height)
aliens.append(alien)
last_spawn_time = pygame.time.get_ticks()
15
# Spawn big aliens based on score
if score >= 50 and not any(ba['type'] == 25 for ba in
big_aliens):
big_aliens.append({'rect':
pygame.Rect(random.randint(0, screen_width - 80), -80, 80, 80),
'type': 25, 'health': 2})
if score >= 75 and not any(ba['type'] == 50 for ba in
big_aliens):
big_aliens.append({'rect':
pygame.Rect(random.randint(0, screen_width - 100), -100, 100,
100), 'type': 50, 'health': 3})
# Enemy shooting (including big aliens)
if pygame.time.get_ticks() - last_enemy_shot_time > 1000
and (aliens or big_aliens):
for alien in aliens:
enemy_bullet = pygame.Rect(alien.x + alien_width //
2 - enemy_bullet_width // 2, alien.y + alien_height,
enemy_bullet_width, enemy_bullet_height)
enemy_bullets.append(enemy_bullet)
for big_alien in big_aliens:
enemy_bullet = pygame.Rect(big_alien['rect'].x +
big_alien['rect'].width // 2 - enemy_bullet_width // 2,
big_alien['rect'].y +
16
big_alien['rect'].height, enemy_bullet_width,
enemy_bullet_height)
enemy_bullets.append(enemy_bullet)
last_enemy_shot_time = pygame.time.get_ticks()
for alien in aliens[:]:
alien.y += alien_speed
if alien.y > screen_height:
aliens.remove(alien)
for big_alien in big_aliens[:]:
big_alien['rect'].y += alien_speed
if big_alien['rect'].y > screen_height:
big_aliens.remove(big_alien)
check_collision(bullets, aliens, big_aliens)
check_enemy_bullet_collision(enemy_bullets)
draw_player(player_x, player_y)
draw_bullet(bullets)
draw_enemy_bullets(enemy_bullets)
draw_aliens(aliens, big_aliens)
draw_score(score, high_score)
update_enemy_bullets(enemy_bullets) # Move and update
enemy bullets
else:
if waiting_for_enter:
show_intro_screen()
17
else:
show_game_over()
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]:
reset_game()
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()
18
RESULTS
The game runs successfully, providing an interactive
and engaging experience.
Players found the increasing difficulty levels
challenging and enjoyable.
High scores are saved and displayed accurately.
CONCLUSION
"Space Alien Invader" demonstrates the practical
application of programming concepts in creating an
interactive game. The project displays skills in
coding, debugging, and game design.
APPLICATIONS
Introduces students to game development
concepts.
Encourages creative problem-solving and
programming skills.
Provides a foundation for exploring advanced
topics like AI in games.
19
BIBLIOGRAPHY
Pygame Documentation:
https://www.pygame.org/docs/
Online Game Development Tutorials:
https://realpython.com
Official Python Website: https://www.python.org
ADDITIONAL NOTE
The game file was successfully converted from a Python
script (.py) to a standalone executable (.exe) using
PyInstaller. This step ensures easier deployment and
execution on systems without Python installed.
20