Advertisement
EdmundC

fort

Sep 23rd, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.30 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Screen settings
  9. SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
  10. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  11. pygame.display.set_caption("Advanced Fort Management Game")
  12.  
  13. # Colors
  14. WHITE = (255, 255, 255)
  15. GRAY = (200, 200, 200)
  16. GREEN = (0, 255, 0)
  17. BLUE = (0, 0, 255)
  18. RED = (255, 0, 0)
  19. BLACK = (0, 0, 0)
  20. YELLOW = (255, 255, 0)
  21. BROWN = (139, 69, 19)
  22.  
  23. # Grid settings
  24. GRID_SIZE = 40
  25. MAP_WIDTH, MAP_HEIGHT = 10, 10  # Number of grid cells
  26.  
  27. # Define building types
  28. EMPTY, FARM, WORKSHOP, SMITHY, SOLDIER = 0, 1, 2, 3, 4
  29.  
  30. # Define resources
  31. resources = {
  32.     'food': 50,
  33.     'tools': 20,
  34.     'weapons': 10,
  35.     'soldiers': 0,
  36.     'max_soldiers': 10
  37. }
  38.  
  39. # Production rates for each building type (per second)
  40. production_rates = {
  41.     FARM: {'food': 1},
  42.     WORKSHOP: {'tools': 0.5},
  43.     SMITHY: {'weapons': 0.2}
  44. }
  45.  
  46. # Construction times for each building type (in seconds)
  47. construction_times = {
  48.     FARM: 15,
  49.     WORKSHOP: 20,
  50.     SMITHY: 25
  51. }
  52.  
  53. # Costs for each building type
  54. building_costs = {
  55.     FARM: {'food': 10, 'tools': 0, 'weapons': 0},
  56.     WORKSHOP: {'food': 15, 'tools': 5, 'weapons': 0},
  57.     SMITHY: {'food': 20, 'tools': 10, 'weapons': 5}
  58. }
  59.  
  60. # Create a grid representing the map (all cells start empty)
  61. map_grid = [[EMPTY for _ in range(MAP_HEIGHT)] for _ in range(MAP_WIDTH)]
  62. building_timers = {}  # Track construction timers for buildings
  63.  
  64. # Enemy settings
  65. enemy_spawn_time = 8000  # Spawn every 8 seconds
  66. last_enemy_spawn = pygame.time.get_ticks()
  67. enemies = []
  68. enemy_health = 50
  69. enemy_speed = 0.05  # Speed of enemies (slow)
  70.  
  71. # Soldier settings
  72. soldier_damage = 10
  73. soldiers_positions = []
  74.  
  75. # Helper function to draw the grid
  76. def draw_grid():
  77.     for x in range(MAP_WIDTH):
  78.         for y in range(MAP_HEIGHT):
  79.             rect = pygame.Rect(x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE)
  80.             if map_grid[x][y] == FARM:
  81.                 pygame.draw.rect(screen, GREEN, rect)
  82.             elif map_grid[x][y] == WORKSHOP:
  83.                 pygame.draw.rect(screen, BLUE, rect)
  84.             elif map_grid[x][y] == SMITHY:
  85.                 pygame.draw.rect(screen, RED, rect)
  86.             elif map_grid[x][y] == SOLDIER:
  87.                 pygame.draw.rect(screen, YELLOW, rect)
  88.             else:
  89.                 pygame.draw.rect(screen, GRAY, rect, 1)
  90.  
  91. # Helper function to get mouse position in grid coordinates
  92. def get_grid_position(pos):
  93.     x, y = pos
  94.     grid_x, grid_y = x // GRID_SIZE, y // GRID_SIZE
  95.     return grid_x, grid_y
  96.  
  97. # Update resources based on buildings
  98. def update_resources():
  99.     for building, timer in building_timers.items():
  100.         building_type, grid_x, grid_y = building
  101.         if timer <= 0:  # If the building is done constructing
  102.             if building_type in production_rates:
  103.                 for resource, rate in production_rates[building_type].items():
  104.                     resources[resource] += rate
  105.  
  106. # Draw resources on the screen
  107. def draw_resources():
  108.     font = pygame.font.SysFont(None, 24)
  109.     text_surface = font.render(f"Food: {resources['food']} Tools: {resources['tools']} Weapons: {resources['weapons']} Soldiers: {resources['soldiers']}/{resources['max_soldiers']}", True, BLACK)
  110.     screen.blit(text_surface, (10, SCREEN_HEIGHT - 30))
  111.  
  112. # Handle construction timers for buildings
  113. def update_building_timers():
  114.     finished_buildings = []
  115.     for building, timer in building_timers.items():
  116.         if timer > 0:
  117.             building_timers[building] -= 1 / 60  # Decrease by 1 second per frame
  118.         if building_timers[building] <= 0:
  119.             finished_buildings.append(building)
  120.     for building in finished_buildings:
  121.         del building_timers[building]
  122.  
  123. # Handle enemy spawns
  124. def spawn_enemy():
  125.     enemy_x = random.randint(0, MAP_WIDTH - 1)
  126.     enemies.append({'x': enemy_x, 'y': 0, 'health': enemy_health})
  127.  
  128. # Draw enemies
  129. def draw_enemies():
  130.     for enemy in enemies:
  131.         enemy_rect = pygame.Rect(enemy['x'] * GRID_SIZE, enemy['y'] * GRID_SIZE, GRID_SIZE, GRID_SIZE)
  132.         pygame.draw.rect(screen, BLACK, enemy_rect)
  133.  
  134. # Move enemies down and check for breach
  135. def move_enemies():
  136.     for enemy in enemies:
  137.         enemy['y'] += enemy_speed
  138.         if enemy['y'] >= MAP_HEIGHT:  # If they reach the bottom
  139.             print("Enemies have breached the fort!")
  140.  
  141. # Draw construction progress bars
  142. def draw_construction_progress():
  143.     font = pygame.font.SysFont(None, 18)
  144.     for building, timer in building_timers.items():
  145.         building_type, grid_x, grid_y = building
  146.         if timer > 0:
  147.             rect = pygame.Rect(grid_x * GRID_SIZE, grid_y * GRID_SIZE - 10, GRID_SIZE, 5)
  148.             progress = (construction_times[building_type] - timer) / construction_times[building_type]
  149.             pygame.draw.rect(screen, BROWN, rect)
  150.             pygame.draw.rect(screen, GREEN, rect.inflate(-2, -2), width=0, border_radius=1)
  151.             progress_surface = font.render(f"{int(progress * 100)}%", True, BLACK)
  152.             screen.blit(progress_surface, (grid_x * GRID_SIZE, grid_y * GRID_SIZE - 20))
  153.  
  154. # Game loop
  155. running = True
  156. current_building = FARM  # Start with farm selected
  157. clock = pygame.time.Clock()
  158.  
  159. while running:
  160.     screen.fill(WHITE)
  161.  
  162.     # Draw the grid
  163.     draw_grid()
  164.  
  165.     # Update and display resources
  166.     update_resources()
  167.     draw_resources()
  168.  
  169.     # Draw construction progress
  170.     draw_construction_progress()
  171.  
  172.     # Draw enemies
  173.     draw_enemies()
  174.  
  175.     # Event handling
  176.     for event in pygame.event.get():
  177.         if event.type == pygame.QUIT:
  178.             running = False
  179.         elif event.type == pygame.MOUSEBUTTONDOWN:
  180.             mouse_pos = pygame.mouse.get_pos()
  181.             grid_x, grid_y = get_grid_position(mouse_pos)
  182.             if 0 <= grid_x < MAP_WIDTH and 0 <= grid_y < MAP_HEIGHT:
  183.                 # Start construction timer for the selected building if resources allow
  184.                 if map_grid[grid_x][grid_y] == EMPTY and current_building in building_costs:
  185.                     cost = building_costs[current_building]
  186.                     if (resources['food'] >= cost['food'] and
  187.                         resources['tools'] >= cost['tools'] and
  188.                         resources['weapons'] >= cost['weapons']):
  189.                         building_timers[(current_building, grid_x, grid_y)] = construction_times[current_building]
  190.                         resources['food'] -= cost['food']
  191.                         resources['tools'] -= cost['tools']
  192.                         resources['weapons'] -= cost['weapons']
  193.                         map_grid[grid_x][grid_y] = current_building
  194.         elif event.type == pygame.KEYDOWN:
  195.             # Use number keys to switch between building types
  196.             if event.key == pygame.K_1:
  197.                 current_building = FARM
  198.             elif event.key == pygame.K_2:
  199.                 current_building = WORKSHOP
  200.             elif event.key == pygame.K_3:
  201.                 current_building = SMITHY
  202.             elif event.key == pygame.K_4 and resources['soldiers'] < resources['max_soldiers']:
  203.                 # Recruit soldiers using resources
  204.                 if resources['food'] >= 5 and resources['tools'] >= 3 and resources['weapons'] >= 2:
  205.                     resources['food'] -= 5
  206.                     resources['tools'] -= 3
  207.                     resources['weapons'] -= 2
  208.                     resources['soldiers'] += 1
  209.             elif event.key == pygame.K_p and resources['soldiers'] > 0:
  210.                 # Place a soldier on the grid
  211.                 mouse_pos = pygame.mouse.get_pos()
  212.                 grid_x, grid_y = get_grid_position(mouse_pos)
  213.                 if 0 <= grid_x < MAP_WIDTH and 0 <= grid_y < MAP_HEIGHT:
  214.                     if map_grid[grid_x][grid_y] == EMPTY:
  215.                         map_grid[grid_x][grid_y] = SOLDIER
  216.                         resources['soldiers'] -= 1
  217.  
  218.     # Handle construction timers
  219.     update_building_timers()
  220.  
  221.     # Handle enemy spawns
  222.     current_time = pygame.time.get_ticks()
  223.     if current_time - last_enemy_spawn > enemy_spawn_time:
  224.         spawn_enemy()
  225.         last_enemy_spawn = current_time
  226.  
  227.     # Move enemies
  228.     move_enemies()
  229.  
  230.     # Update display
  231.     pygame.display.flip()
  232.    
  233.     # Frame rate control
  234.     clock.tick(60)
  235.  
  236. # Quit Pygame
  237. pygame.quit()
  238. sys.exit()
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement