Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import random
- # Initialize Pygame
- pygame.init()
- # Screen settings
- SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Advanced Fort Management Game")
- # Colors
- WHITE = (255, 255, 255)
- GRAY = (200, 200, 200)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- RED = (255, 0, 0)
- BLACK = (0, 0, 0)
- YELLOW = (255, 255, 0)
- BROWN = (139, 69, 19)
- # Grid settings
- GRID_SIZE = 40
- MAP_WIDTH, MAP_HEIGHT = 10, 10 # Number of grid cells
- # Define building types
- EMPTY, FARM, WORKSHOP, SMITHY, SOLDIER = 0, 1, 2, 3, 4
- # Define resources
- resources = {
- 'food': 50,
- 'tools': 20,
- 'weapons': 10,
- 'soldiers': 0,
- 'max_soldiers': 10
- }
- # Production rates for each building type (per second)
- production_rates = {
- FARM: {'food': 1},
- WORKSHOP: {'tools': 0.5},
- SMITHY: {'weapons': 0.2}
- }
- # Construction times for each building type (in seconds)
- construction_times = {
- FARM: 15,
- WORKSHOP: 20,
- SMITHY: 25
- }
- # Costs for each building type
- building_costs = {
- FARM: {'food': 10, 'tools': 0, 'weapons': 0},
- WORKSHOP: {'food': 15, 'tools': 5, 'weapons': 0},
- SMITHY: {'food': 20, 'tools': 10, 'weapons': 5}
- }
- # Create a grid representing the map (all cells start empty)
- map_grid = [[EMPTY for _ in range(MAP_HEIGHT)] for _ in range(MAP_WIDTH)]
- building_timers = {} # Track construction timers for buildings
- # Enemy settings
- enemy_spawn_time = 8000 # Spawn every 8 seconds
- last_enemy_spawn = pygame.time.get_ticks()
- enemies = []
- enemy_health = 50
- enemy_speed = 0.05 # Speed of enemies (slow)
- # Soldier settings
- soldier_damage = 10
- soldiers_positions = []
- # Helper function to draw the grid
- def draw_grid():
- for x in range(MAP_WIDTH):
- for y in range(MAP_HEIGHT):
- rect = pygame.Rect(x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE)
- if map_grid[x][y] == FARM:
- pygame.draw.rect(screen, GREEN, rect)
- elif map_grid[x][y] == WORKSHOP:
- pygame.draw.rect(screen, BLUE, rect)
- elif map_grid[x][y] == SMITHY:
- pygame.draw.rect(screen, RED, rect)
- elif map_grid[x][y] == SOLDIER:
- pygame.draw.rect(screen, YELLOW, rect)
- else:
- pygame.draw.rect(screen, GRAY, rect, 1)
- # Helper function to get mouse position in grid coordinates
- def get_grid_position(pos):
- x, y = pos
- grid_x, grid_y = x // GRID_SIZE, y // GRID_SIZE
- return grid_x, grid_y
- # Update resources based on buildings
- def update_resources():
- for building, timer in building_timers.items():
- building_type, grid_x, grid_y = building
- if timer <= 0: # If the building is done constructing
- if building_type in production_rates:
- for resource, rate in production_rates[building_type].items():
- resources[resource] += rate
- # Draw resources on the screen
- def draw_resources():
- font = pygame.font.SysFont(None, 24)
- text_surface = font.render(f"Food: {resources['food']} Tools: {resources['tools']} Weapons: {resources['weapons']} Soldiers: {resources['soldiers']}/{resources['max_soldiers']}", True, BLACK)
- screen.blit(text_surface, (10, SCREEN_HEIGHT - 30))
- # Handle construction timers for buildings
- def update_building_timers():
- finished_buildings = []
- for building, timer in building_timers.items():
- if timer > 0:
- building_timers[building] -= 1 / 60 # Decrease by 1 second per frame
- if building_timers[building] <= 0:
- finished_buildings.append(building)
- for building in finished_buildings:
- del building_timers[building]
- # Handle enemy spawns
- def spawn_enemy():
- enemy_x = random.randint(0, MAP_WIDTH - 1)
- enemies.append({'x': enemy_x, 'y': 0, 'health': enemy_health})
- # Draw enemies
- def draw_enemies():
- for enemy in enemies:
- enemy_rect = pygame.Rect(enemy['x'] * GRID_SIZE, enemy['y'] * GRID_SIZE, GRID_SIZE, GRID_SIZE)
- pygame.draw.rect(screen, BLACK, enemy_rect)
- # Move enemies down and check for breach
- def move_enemies():
- for enemy in enemies:
- enemy['y'] += enemy_speed
- if enemy['y'] >= MAP_HEIGHT: # If they reach the bottom
- print("Enemies have breached the fort!")
- # Draw construction progress bars
- def draw_construction_progress():
- font = pygame.font.SysFont(None, 18)
- for building, timer in building_timers.items():
- building_type, grid_x, grid_y = building
- if timer > 0:
- rect = pygame.Rect(grid_x * GRID_SIZE, grid_y * GRID_SIZE - 10, GRID_SIZE, 5)
- progress = (construction_times[building_type] - timer) / construction_times[building_type]
- pygame.draw.rect(screen, BROWN, rect)
- pygame.draw.rect(screen, GREEN, rect.inflate(-2, -2), width=0, border_radius=1)
- progress_surface = font.render(f"{int(progress * 100)}%", True, BLACK)
- screen.blit(progress_surface, (grid_x * GRID_SIZE, grid_y * GRID_SIZE - 20))
- # Game loop
- running = True
- current_building = FARM # Start with farm selected
- clock = pygame.time.Clock()
- while running:
- screen.fill(WHITE)
- # Draw the grid
- draw_grid()
- # Update and display resources
- update_resources()
- draw_resources()
- # Draw construction progress
- draw_construction_progress()
- # Draw enemies
- draw_enemies()
- # Event handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- mouse_pos = pygame.mouse.get_pos()
- grid_x, grid_y = get_grid_position(mouse_pos)
- if 0 <= grid_x < MAP_WIDTH and 0 <= grid_y < MAP_HEIGHT:
- # Start construction timer for the selected building if resources allow
- if map_grid[grid_x][grid_y] == EMPTY and current_building in building_costs:
- cost = building_costs[current_building]
- if (resources['food'] >= cost['food'] and
- resources['tools'] >= cost['tools'] and
- resources['weapons'] >= cost['weapons']):
- building_timers[(current_building, grid_x, grid_y)] = construction_times[current_building]
- resources['food'] -= cost['food']
- resources['tools'] -= cost['tools']
- resources['weapons'] -= cost['weapons']
- map_grid[grid_x][grid_y] = current_building
- elif event.type == pygame.KEYDOWN:
- # Use number keys to switch between building types
- if event.key == pygame.K_1:
- current_building = FARM
- elif event.key == pygame.K_2:
- current_building = WORKSHOP
- elif event.key == pygame.K_3:
- current_building = SMITHY
- elif event.key == pygame.K_4 and resources['soldiers'] < resources['max_soldiers']:
- # Recruit soldiers using resources
- if resources['food'] >= 5 and resources['tools'] >= 3 and resources['weapons'] >= 2:
- resources['food'] -= 5
- resources['tools'] -= 3
- resources['weapons'] -= 2
- resources['soldiers'] += 1
- elif event.key == pygame.K_p and resources['soldiers'] > 0:
- # Place a soldier on the grid
- mouse_pos = pygame.mouse.get_pos()
- grid_x, grid_y = get_grid_position(mouse_pos)
- if 0 <= grid_x < MAP_WIDTH and 0 <= grid_y < MAP_HEIGHT:
- if map_grid[grid_x][grid_y] == EMPTY:
- map_grid[grid_x][grid_y] = SOLDIER
- resources['soldiers'] -= 1
- # Handle construction timers
- update_building_timers()
- # Handle enemy spawns
- current_time = pygame.time.get_ticks()
- if current_time - last_enemy_spawn > enemy_spawn_time:
- spawn_enemy()
- last_enemy_spawn = current_time
- # Move enemies
- move_enemies()
- # Update display
- pygame.display.flip()
- # Frame rate control
- clock.tick(60)
- # Quit Pygame
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement