import pygame
import random
import math
# Initialize Pygame
pygame.init()
# Screen dimensions (retro feel: small resolution, but we can scale if needed)
WIDTH = 640
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Retro Bug Blaster")
# Colors (limited 80's palette)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)
# Game variables
player_pos = [WIDTH // 2, HEIGHT - 50]
player_speed = 5
bullets = []
bullet_speed = -10 # Negative to move up
bugs = []
bug_speed = 2
bug_spawn_rate = 30 # Frames between spawns
frame_count = 0
score = 0
lives = 3
game_over = False
# Font for text (using default system Hawkins for retro feel)
font = pygame.font.Font(None, 36) # Default font
# Function to draw the player (simple triangle ship like in 80's games)
def draw_player(x, y):
pygame.draw.polygon(screen, WHITE, [(x, y), (x - 20, y + 30), (x + 20, y + 30)])
# Function to draw a bug (simple retro bug: circle body with legs)
def draw_bug(x, y):
# Body
pygame.draw.circle(screen, GREEN, (x, y), 15)
# Eyes
pygame.draw.circle(screen, RED, (x - 5, y - 5), 3)
pygame.draw.circle(screen, RED, (x + 5, y - 5), 3)
# Legs (simple lines)
pygame.draw.line(screen, GREEN, (x - 10, y), (x - 20, y + 10), 2)
pygame.draw.line(screen, GREEN, (x + 10, y), (x + 20, y + 10), 2)
pygame.draw.line(screen, GREEN, (x - 10, y), (x - 20, y - 10), 2)
pygame.draw.line(screen, GREEN, (x + 10, y), (x + 20, y - 10), 2)
# Function to draw bullet (small rectangle)
def draw_bullet(x, y):
pygame.draw.rect(screen, YELLOW, (x - 2, y - 5, 4, 10))
# Function to draw score and lives
def draw_hud():
score_text = font.render(f"Score: {score}", True, WHITE)
lives_text = font.render(f"Lives: {lives}", True, WHITE)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (WIDTH - 100, 10))
# Function to draw game over screen
def draw_game_over():
game_over_text = font.render("GAME OVER", True, RED)
score_text = font.render(f"Final Score: {score}", True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2 - 50))
screen.blit(score_text, (WIDTH // 2 - 100, HEIGHT // 2))
# Simple collision detection
def check_collision(rect1, rect2):
return (rect1[0] < rect2[0] + rect2[2] and
rect1[0] + rect1[2] > rect2[0] and
rect1[1] < rect2[1] + rect2[3] and
rect1[1] + rect1[3] > rect2[1])
# Main game loop
clock = pygame.time.Clock()
running = True
while running:
screen.fill(BLACK) # Clear screen
# 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 and not game_over:
# Shoot bullet
bullets.append([player_pos[0], player_pos[1] - 10])
if not game_over:
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_pos[0] > 20:
player_pos[0] -= player_speed
if keys[pygame.K_RIGHT] and player_pos[0] < WIDTH - 20:
player_pos[0] += player_speed
# Spawn bugs periodically
frame_count += 1
if frame_count % bug_spawn_rate == 0:
bug_x = random.randint(20, WIDTH - 20)
bugs.append([bug_x, 0]) # Start at top
# Update bullets
for bullet in bullets[:]:
bullet[1] += bullet_speed
if bullet[1] < 0:
bullets.remove(bullet)
# Update bugs (move down, some side to side for variety)
for bug in bugs[:]:
bug[1] += bug_speed
# Add some horizontal movement for challenge (sine wave)
bug[0] += math.sin(bug[1] / 20) * 2
if bug[1] > HEIGHT:
bugs.remove(bug)
lives -= 1
if lives <= 0:
game_over = True
# Check collisions: bullets and bugs
for bullet in bullets[:]:
bullet_rect = [bullet[0] - 2, bullet[1] - 5, 4, 10]
for bug in bugs[:]:
bug_rect = [bug[0] - 15, bug[1] - 15, 30, 30] # Approximate bug size
if check_collision(bullet_rect, bug_rect):
bugs.remove(bug)
bullets.remove(bullet)
score += 10
break
# Check collisions: bugs and player
player_rect = [player_pos[0] - 20, player_pos[1], 40, 30]
for bug in bugs[:]:
bug_rect = [bug[0] - 15, bug[1] - 15, 30, 30]
if check_collision(player_rect, bug_rect):
bugs.remove(bug)
lives -= 1
if lives <= 0:
game_over = True
# Draw everything
draw_player(player_pos[0], player_pos[1])
for bullet in bullets:
draw_bullet(bullet[0], bullet[1])
for bug in bugs:
draw_bug(bug[0], bug[1])
draw_hud()
else:
# Game over screen
draw_game_over()
# Press any key to quit (simplified)
keys = pygame.key.get_pressed()
if any(keys):
running = False
# Retro effect: add some scan lines for 80's CRT feel
for y in range(0, HEIGHT, 4):
pygame.draw.line(screen, (0, 0, 0, 50), (0, y), (WIDTH, y), 1)
pygame.display.flip()
clock.tick(60) # 60 FPS for smooth retro action
pygame.quit()