The Basic Space Invader Game implemented in Python is an engaging arcade-style experience. The player takes control of a spaceship, moving left and right to dodge enemy attacks while attempting to shoot down the invading alien forces. The objective is to eliminate as many enemies as possible while avoiding their projectiles. The game features intuitive controls, allowing players to navigate the space battlefield with ease. With each successful shot, the player earns points, and as the game progresses, the alien invasion becomes more challenging, testing the player’s reflexes and strategic thinking. The immersive visuals and lively sound effects enhance the gaming experience, creating an adrenaline-pumping adventure that keeps players hooked for hours of intergalactic fun.
Code
import pygame
import random
# Initialize pygame
pygame.init()
# Set up the screen
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Space Invaders")
# Set up colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set up player variables
player_size = 50
player_x = width // 2 - player_size // 2
player_y = height - 2 * player_size
player_speed = 5
# Set up enemy variables
enemy_size = 50
enemy_x = random.randint(0, width - enemy_size)
enemy_y = 0
enemy_speed = 3
clock = pygame.time.Clock()
# Game loop
running = True
while running:
screen.fill(BLACK)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle player movement
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 < width - player_size:
player_x += player_speed
# Update enemy position
enemy_y += enemy_speed
if enemy_y > height:
enemy_x = random.randint(0, width - enemy_size)
enemy_y = 0
# Detect collision between player and enemy
if (player_x + player_size > enemy_x and player_x < enemy_x + enemy_size) and \
(player_y + player_size > enemy_y and player_y < enemy_y + enemy_size):
# Collision occurred, game over
running = False
# Draw player
pygame.draw.rect(screen, WHITE, (player_x, player_y, player_size, player_size))
# Draw enemy
pygame.draw.rect(screen, WHITE, (enemy_x, enemy_y, enemy_size, enemy_size))
# Update the screen
pygame.display.flip()
clock.tick(60)
# Quit the game
pygame.quit()
Output Working Video
In this example, the player is controlled using the left and right arrow keys. The enemy starts from a random position at the top of the screen and moves down. If the player collides with the enemy, the game ends.
Advance Space Invader Game Using Python
Code
import pygame
import random
import math
# Initialize pygame
pygame.init()
# Set up the screen
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Space Invaders")
# Set up colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set up player variables
player_image = pygame.image.load("player.png")
player_size = 64
player_x = width // 2 - player_size // 2
player_y = height - player_size
player_speed = 5
# Set up enemy variables
enemy_image = pygame.image.load("enemy.png")
enemy_size = 64
enemy_x = random.randint(0, width - enemy_size)
enemy_y = random.randint(50, 150)
enemy_speed = 2
# Set up bullet variables
bullet_image = pygame.image.load("bullet.png")
bullet_size = 32
bullet_x = 0
bullet_y = height - bullet_size
bullet_speed = 5
bullet_state = "ready" # "ready" or "fire"
score = 0
font = pygame.font.Font("freesansbold.ttf", 32)
text_x = 10
text_y = 10
clock = pygame.time.Clock()
# Draw score on the screen
def show_score(x, y):
score_text = font.render("Score: " + str(score), True, WHITE)
screen.blit(score_text, (x, y))
# Draw the player on the screen
def draw_player(x, y):
screen.blit(player_image, (x, y))
# Draw the enemy on the screen
def draw_enemy(x, y):
screen.blit(enemy_image, (x, y))
# Fire the bullet
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_image, (x + 16, y + 10))
# Detect collision between the bullet and enemy
def is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
distance = math.sqrt(math.pow(enemy_x - bullet_x, 2) + math.pow(enemy_y - bullet_y, 2))
if distance < 27:
return True
return False
# Game loop
running = True
while running:
screen.fill(BLACK)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle player movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= player_speed
if event.key == pygame.K_RIGHT:
player_x += player_speed
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_x = player_x
fire_bullet(bullet_x, bullet_y)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player_x_change = 0
# Update player position
if player_x < 0:
player_x = 0
elif player_x > width - player_size:
player_x = width - player_size
# Update enemy position
enemy_x += enemy_speed
if enemy_x < 0 or enemy_x > width - enemy_size:
enemy_speed *= -1
enemy_y += 50
# Update bullet position
if bullet_y <= 0:
bullet_y = height - bullet_size
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
bullet_y -= bullet_speed
# Check for collision between bullet and enemy
collision = is_collision(enemy_x, enemy_y, bullet_x, bullet_y)
if collision:
bullet_y = height - bullet_size
bullet_state = "ready"
score += 1
enemy_x = random.randint(0, width - enemy_size)
enemy_y = random.randint(50, 150)
# Draw game elements
draw_player(player_x, player_y)
draw_enemy(enemy_x, enemy_y)
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
show_score(text_x, text_y)
# Update the screen
pygame.display.flip()
clock.tick(60)
# Quit the game
pygame.quit()
Steps to Run the Program
In this code, you’ll need to have the image files “player.png”, “enemy.png”, and “bullet.png” in the same directory as the Python script. You can replace these images with your own custom images or use appropriate image files.
This implementation includes player movement using the left and right arrow keys, firing bullets using the spacebar, collision detection between the bullet and enemy, and a score counter displayed on the screen.
Note:- Feel free to modify and expand upon this code to add more features to your Space Invaders game, such as multiple enemies, enemy movement patterns, sound effects, and more sophisticated gameplay mechanics.