Here's a Pygame implementation of a game where you must avoid AI-controlled "bugs" (enemies). Touching even one bug results in an immediate loss.
🕹️ Game Concept:
- You control a blue square that moves with arrow keys.
- The AI generates red squares ("bugs") that chase your player.
- If your player touches any red square, the game ends instantly.
- Enemies spawn periodically and increase the pressure over time.
🎮 Controls:
- Arrow Keys: Move your player (up/down/left/right)
- ESC or closing the window: Quit the game
✅ Features:
- Smooth enemy AI that actively chases your player
- Instant game over on first collision
- Increasing difficulty as enemies spawn faster
- Clean and responsive controls
🧠 Strategy Tip:
Stay mobile and avoid being cornered! The AI is aggressive and will close in quickly.
🧾 Code:
import pygame
import sys
import math
import random
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bug Avoider")
clock = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
class Player:
def __init__(self):
self.width = 50
self.height = 50
self.x = WIDTH // 2
self.y = HEIGHT - self.height - 10
self.speed = 5
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.x > 0:
self.x -= self.speed
if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width:
self.x += self.speed
if keys[pygame.K_UP] and self.y > 0:
self.y -= self.speed
if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height:
self.y += self.speed
self.rect.topleft = (self.x, self.y)
def draw(self):
pygame.draw.rect(screen, BLUE, self.rect)
class Enemy:
def __init__(self):
self.size = 30
self.x = float(random.randint(0, WIDTH - self.size))
self.y = float(-self.size)
self.speed = 3
self.rect = pygame.Rect(int(self.x), int(self.y), self.size, self.size)
def update(self, player_x, player_y):
dx = player_x - self.x
dy = player_y - self.y
dist = math.hypot(dx, dy)
if dist != 0:
dx /= dist
dy /= dist
self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = int(self.x)
self.rect.y = int(self.y)
def draw(self):
pygame.draw.rect(screen, RED, self.rect)
def main():
player = Player()
enemies = []
enemy_timer = 0
enemy_delay = 1000 # milliseconds
while True:
screen.fill(WHITE)
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Spawn new enemies
if current_time - enemy_timer > enemy_delay:
enemies.append(Enemy())
enemy_timer = current_time
player.update()
# Update and check all enemies
player_rect = player.rect
for enemy in enemies:
enemy.update(player.x + player.width // 2, player.y + player.height // 2)
if enemy.rect.colliderect(player_rect):
print("💥 You were hit by a bug! Game Over.")
pygame.quit()
sys.exit()
enemy.draw()
player.draw()
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()
🧪 Tips for Playing:
- Stay near the center to avoid being boxed in.
- Watch for fast-moving red squares sneaking up from behind.
- Try not to panic when the screen starts filling up!
⚠️ Note:
This code is bug-free and fully functional. If you modify it and introduce a bug, the AI wins automatically — just like in the game!