import pygame import time import random import math # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 GRID_SIZE = 40 WHITE = (255, 255, 255) GREEN = (0, 255, 0) DARK_GREEN = (0, 100, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) PURPLE = (128, 0, 128) BROWN = (139, 69, 19) # Wall color BLACK = (0, 0, 0) LIGHT_GRAY = (211, 211, 211) # Resource system gold = 100 elixir = 100 last_resource_update = time.time() game_start_time = time.time() last_enemy_spawn = time.time() enemy_wave_start_time = game_start_time + 30 # Enemies start after 30 seconds # Buildings storage buildings = [] walls = [] projectiles = [] # Enemy storage enemies = [] # Pygame setup screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Clash of Clans - Pygame") # Font font = pygame.font.Font(None, 24) # Building Types BUILDINGS = { "town_hall": {"color": RED, "cost": {"gold": 50, "elixir": 50}, "health": 100}, "gold_generator": {"color": YELLOW, "cost": {"gold": 30, "elixir": 10}, "health": 50}, "elixir_generator": {"color": PURPLE, "cost": {"gold": 10, "elixir": 30}, "health": 50}, "defense": {"color": BLUE, "cost": {"gold": 40, "elixir": 20}, "range": 100, "damage": 5, "health": 50}, "wall": {"color": BROWN, "cost": {"gold": 20, "elixir": 5}, "health": 5} # Wall takes 5 hits } # Enemy properties ENEMY_SPEED = 0.5 # Slower speed ENEMY_HEALTH = 40 # More health # Current mode mode = "gold_generator" # Place starting town hall in the center center_x = (WIDTH // 2) // GRID_SIZE * GRID_SIZE center_y = (HEIGHT // 2) // GRID_SIZE * GRID_SIZE buildings.append({"x": center_x, "y": center_y, "type": "town_hall", "health": 100}) # Game loop running = True while running: screen.fill(DARK_GREEN) # 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_g: mode = "gold_generator" elif event.key == pygame.K_e: mode = "elixir_generator" elif event.key == pygame.K_d: mode = "defense" elif event.key == pygame.K_w: mode = "wall" elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: x, y = event.pos x = (x // GRID_SIZE) * GRID_SIZE y = (y // GRID_SIZE) * GRID_SIZE # Prevent multiple Town Halls if mode == "town_hall": continue # Check if enough resources cost = BUILDINGS[mode]["cost"] if gold >= cost["gold"] and elixir >= cost["elixir"]: gold -= cost["gold"] elixir -= cost["elixir"] if mode == "wall": walls.append({"x": x, "y": y, "health": BUILDINGS["wall"]["health"]}) else: buildings.append({"x": x, "y": y, "type": mode, "health": BUILDINGS[mode]["health"]}) # Draw grid for x in range(0, WIDTH, GRID_SIZE): pygame.draw.line(screen, BLACK, (x, 0), (x, HEIGHT)) for y in range(0, HEIGHT, GRID_SIZE): pygame.draw.line(screen, BLACK, (0, y), (WIDTH, y)) # Update resources every second current_time = time.time() if current_time - last_resource_update >= 1: for building in buildings: if building["type"] == "gold_generator": gold += 10 elif building["type"] == "elixir_generator": elixir += 10 last_resource_update = current_time # Spawn enemies only after 30 seconds if current_time >= enemy_wave_start_time: if current_time - last_enemy_spawn >= 5: spawn_x = random.choice([0, WIDTH - GRID_SIZE]) spawn_y = random.choice([0, HEIGHT - GRID_SIZE]) enemies.append({"x": spawn_x, "y": spawn_y, "health": ENEMY_HEALTH}) last_enemy_spawn = current_time # Move enemies towards the Town Hall for enemy in enemies: if enemy["health"] <= 0: continue target = None for building in buildings: if building["type"] == "defense": target = building break if target is None: for building in buildings: if building["type"] == "town_hall": target = building break if target: dx = target["x"] - enemy["x"] dy = target["y"] - enemy["y"] distance = math.sqrt(dx ** 2 + dy ** 2) if distance > 0: enemy["x"] += ENEMY_SPEED * (dx / distance) enemy["y"] += ENEMY_SPEED * (dy / distance) # Defenses attack enemies in range for building in buildings: if building["type"] == "defense": for enemy in enemies: dx = building["x"] - enemy["x"] dy = building["y"] - enemy["y"] distance = math.sqrt(dx ** 2 + dy ** 2) if distance < BUILDINGS["defense"]["range"]: projectiles.append({"x": building["x"], "y": building["y"], "target": enemy}) # Move and draw projectiles for projectile in projectiles[:]: enemy = projectile["target"] dx = enemy["x"] - projectile["x"] dy = enemy["y"] - projectile["y"] distance = math.sqrt(dx ** 2 + dy ** 2) if distance > 2: projectile["x"] += 3 * (dx / distance) projectile["y"] += 3 * (dy / distance) else: enemy["health"] -= BUILDINGS["defense"]["damage"] projectiles.remove(projectile) # Draw buildings for building in buildings: pygame.draw.rect(screen, BUILDINGS[building["type"]]["color"], (building["x"], building["y"], GRID_SIZE, GRID_SIZE)) # Draw walls for wall in walls: pygame.draw.rect(screen, BROWN, (wall["x"], wall["y"], GRID_SIZE, GRID_SIZE)) # Draw enemies for enemy in enemies: if enemy["health"] > 0: pygame.draw.circle(screen, LIGHT_GRAY, (int(enemy["x"]), int(enemy["y"])), 10) # Display UI elements screen.blit(font.render(f"Gold: {gold}", True, BLACK), (10, 10)) screen.blit(font.render(f"Elixir: {elixir}", True, BLACK), (10, 30)) # Enemy wave timer countdown = max(0, int(enemy_wave_start_time - current_time)) wave_text = font.render(f"Enemies coming in: {countdown}s" if countdown > 0 else "Enemies have started attacking!", True, RED) screen.blit(wave_text, (WIDTH - 250, 10)) pygame.display.flip() pygame.quit()