This is a basic implementation of a Space Invaders game using the Pygame library in Python. The game features a player spaceship, alien ships, and projectiles. The objective is to destroy all the alien ships before they reach the player's spaceship.
Requirements
Python 3.x
Pygame library (pip install pygame)
Game Code
python
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Define constants
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Set up display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
class Player:
def __init__(self):
self.rect = pygame.Rect(375, 550, 50, 50)
def draw(self, screen):
pygame.draw.rect(screen, GREEN, self.rect)
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
# Boundary checking
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > WIDTH:
self.rect.right = WIDTH
class Alien:
def __init__(self):
self.rect = pygame.Rect(random.randint(0, WIDTH - 50), random.randint(-50, 0), 50, 50)
def draw(self, screen):
pygame.draw.rect(screen, RED, self.rect)
def move(self):
self.rect.y += 2
def reset(self):
self.rect.x = random.randint(0, WIDTH - 50)
self.rect.y = random.randint(-50, 0)
class Projectile:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 10, 10)
self.speed = 5
def draw(self, screen):
pygame.draw.rect(screen, WHITE, self.rect)
def move(self):
self.rect.y -= self.speed
def main():
clock = pygame.time.Clock()
player = Player()
aliens = [Alien() for _ in range(10)]
projectiles = []
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
projectiles.append(Projectile(player.rect.centerx, player.rect.top))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-5, 0)
if keys[pygame.K_RIGHT]:
player.move(5, 0)
screen.fill((0, 0, 0))
player.draw(screen)
for alien in aliens:
alien.draw(screen)
alien.move()
if alien.rect.bottom > HEIGHT:
alien.reset()
for projectile in projectiles:
projectile.draw(screen)
projectile.move()
if projectile.rect.bottom < 0:
projectiles.remove(projectile)
for projectile in projectiles:
for alien in aliens:
if projectile.rect.colliderect(alien.rect):
score += 1
aliens.remove(alien)
projectiles.remove(projectile)
alien.reset()
for alien in aliens:
if alien.rect.colliderect(player.rect):
print("Game Over")
pygame.quit()
sys.exit()
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()
Explanation
This code creates a game window of size 800x600 and initializes the Pygame library. It defines three classes: Player, Alien, and Projectile, each representing the player spaceship, alien ships, and projectiles, respectively.
The game loop continually checks for events (e.g., closing the window, pressing keys), updates the game state (e.g., moving the player, updating alien positions, checking collisions), and redraws the screen.
The game ends when an alien collides with the player or when all aliens have been destroyed.
Notes
This is a basic implementation and can be improved upon (e.g., adding sound effects, smoother animations, more levels).
The game uses a simple collision detection system (rectangular intersection).
The alien ships move down the screen and reset to the top when they reach the bottom.
The player can move left and right using the arrow keys and shoot projectiles using the space bar.
The score is displayed at the top left of the screen.
“make a space invaders type game in python.” Conversation with llama-3.1-8b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m85066pm21byb203szwcxk5c