import pygame import sys 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("Galaga") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) # Set up the clock clock = pygame.time.Clock() # Player player_image = pygame.image.load("player_ship.png") player_rect = player_image.get_rect() player_rect.centerx = WIDTH // 2 player_rect.bottom = HEIGHT - 20 player_speed = 5 # Bullets bullet_image = pygame.Surface((5, 15)) bullet_image.fill(YELLOW) bullet_speed = 8 bullets = [] # Enemies enemy_image = pygame.image.load("enemy_ship.png") enemy_speed = 2 enemies = [] # Main game loop running = True while running: screen.fill(BLACK) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullet = bullet_image.get_rect(midtop=(player_rect.centerx, player_rect.top)) bullets.append(bullet) # Move player keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_rect.left > 0: player_rect.x -= player_speed if keys[pygame.K_RIGHT] and player_rect.right < WIDTH: player_rect.x += player_speed # Move bullets for bullet in bullets: bullet.y -= bullet_speed if bullet.bottom < 0: bullets.remove(bullet) # Draw player screen.blit(player_image, player_rect) # Draw bullets for bullet in bullets: pygame.draw.rect(screen, WHITE, bullet) # Draw enemies for enemy in enemies: screen.blit(enemy_image, enemy) # Update the display pygame.display.flip() # Cap the frame rate clock.tick(60) # Quit pygame pygame.quit() sys.exit()