Advertisement
CatNamedWill

Missile Defence

Mar 25th, 2025
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | None | 0 0
  1. import pygame
  2. import random
  3. import math
  4.  
  5. # Initialize PyGame
  6. pygame.init()
  7.  
  8. # Screen dimensions
  9. WIDTH, HEIGHT = 800, 600
  10. WIN = pygame.display.set_mode((WIDTH, HEIGHT))
  11. pygame.display.set_caption("Mutual Assured Destruction")
  12.  
  13. # Colors
  14. WHITE = (255, 255, 255)
  15. BLACK = (0,0,0)
  16. RED = (255, 0, 0)
  17. BLUE = (0, 0, 255)
  18. GREEN = (0, 255, 0)
  19. YELLOW = (255, 255, 0)
  20.  
  21. # Game variables
  22. FPS = 60
  23. MISSILE_EVENT = pygame.USEREVENT + 1
  24. pygame.time.set_timer(MISSILE_EVENT, 1000)  # Spawn a missile every second
  25. CITY_COUNT = 12
  26.  
  27. # Font
  28. FONT = pygame.font.SysFont('arial', 20)
  29.  
  30. class Missile:
  31.     def __init__(self):
  32.         self.x = random.randint(0, WIDTH)
  33.         self.y = 0
  34.         self.target_x = random.randint(50, WIDTH - 50)
  35.         self.target_y = HEIGHT - 50
  36.         self.speed = random.uniform(3, 7)
  37.         self.angle = math.atan2(self.target_y - self.y, self.target_x - self.x)
  38.         self.dx = math.cos(self.angle) * self.speed
  39.         self.dy = math.sin(self.angle) * self.speed
  40.  
  41.     def move(self):
  42.         self.x += self.dx
  43.         self.y += self.dy
  44.  
  45.     def draw(self, win):
  46.         pygame.draw.circle(win, RED, (int(self.x), int(self.y)), 3)
  47.  
  48. class Interceptor:
  49.     def __init__(self, x, y):
  50.         self.x = WIDTH / 2
  51.         self.y = HEIGHT - 50
  52.         self.target_x = x
  53.         self.target_y = y
  54.         self.speed = 5
  55.         self.angle = math.atan2(self.target_y - self.y, self.target_x - self.x)
  56.         self.dx = math.cos(self.angle) * self.speed
  57.         self.dy = math.sin(self.angle) * self.speed
  58.         self.exploded = False
  59.         self.explosion_max_radius = 50
  60.         self.explosion_duration = 30  # frames
  61.         self.explosion_timer = 0
  62.  
  63.     def move(self):
  64.         if not self.exploded:
  65.             # Move towards target
  66.             self.x += self.dx
  67.             self.y += self.dy
  68.             # Check if reached target
  69.             distance_to_target = math.hypot(self.x - self.target_x, self.y - self.target_y)
  70.             if distance_to_target <= self.speed:
  71.                 self.exploded = True
  72.                 self.explosion_timer = 0
  73.         else:
  74.             # Explosion timer
  75.             self.explosion_timer += 1
  76.  
  77.     def draw(self, win):
  78.         if not self.exploded:
  79.             pygame.draw.circle(win, BLUE, (int(self.x), int(self.y)), 3)
  80.         else:
  81.             # Explosion radius increases over time
  82.             current_radius = (self.explosion_timer / self.explosion_duration) * self.explosion_max_radius
  83.             pygame.draw.circle(win, YELLOW, (int(self.x), int(self.y)), int(current_radius))
  84.  
  85. class City:
  86.     def __init__(self, x):
  87.         self.x = x
  88.         self.y = HEIGHT - 20
  89.         self.alive = True
  90.  
  91.     def draw(self, win):
  92.         if self.alive:
  93.             pygame.draw.rect(win, GREEN, (self.x - 20, self.y, 40, 20))
  94.  
  95. def main():
  96.     run = True
  97.     clock = pygame.time.Clock()
  98.     missiles = []
  99.     interceptors = []
  100.     cities = [City(WIDTH * i / CITY_COUNT + WIDTH / (2 * CITY_COUNT)) for i in range(CITY_COUNT)]
  101.     score = 0
  102.  
  103.     while run:
  104.         clock.tick(FPS)
  105.         WIN.fill(BLACK)
  106.  
  107.         # Event handling
  108.         for event in pygame.event.get():
  109.             if event.type == pygame.QUIT:
  110.                 run = False
  111.             elif event.type == MISSILE_EVENT:
  112.                 missiles.append(Missile())
  113.             elif event.type == pygame.MOUSEBUTTONDOWN:
  114.                 x, y = pygame.mouse.get_pos()
  115.                 interceptors.append(Interceptor(x, y))
  116.  
  117.         # Move and draw missiles
  118.         for missile in missiles[:]:
  119.             missile.move()
  120.             missile.draw(WIN)
  121.             # Check for collision with cities
  122.             for city in cities:
  123.                 if city.alive and math.hypot(missile.x - city.x, missile.y - city.y) < 10:
  124.                     city.alive = False
  125.                     missiles.remove(missile)
  126.                     break
  127.             else:
  128.                 # Check if missile reached the ground
  129.                 if missile.y >= HEIGHT:
  130.                     missiles.remove(missile)
  131.  
  132.         # Move and draw interceptors
  133.         for interceptor in interceptors[:]:
  134.             interceptor.move()
  135.             interceptor.draw(WIN)
  136.             if interceptor.exploded:
  137.                 # Current explosion radius
  138.                 current_radius = (interceptor.explosion_timer / interceptor.explosion_duration) * interceptor.explosion_max_radius
  139.                 # Check for missiles within current explosion radius
  140.                 for missile in missiles[:]:
  141.                     distance = math.hypot(interceptor.x - missile.x, interceptor.y - missile.y)
  142.                     if distance <= current_radius:
  143.                         try:
  144.                             missiles.remove(missile)
  145.                             score += 100
  146.                         except ValueError:
  147.                             pass
  148.                 # Remove interceptor if explosion is over
  149.                 if interceptor.explosion_timer >= interceptor.explosion_duration:
  150.                     interceptors.remove(interceptor)
  151.             else:
  152.                 # Remove interceptor if it goes off screen
  153.                 if interceptor.y <= 0 or interceptor.x < 0 or interceptor.x > WIDTH:
  154.                     interceptors.remove(interceptor)
  155.  
  156.         # Draw cities
  157.         for city in cities:
  158.             city.draw(WIN)
  159.  
  160.         # Draw score
  161.         score_text = FONT.render(f"Score: {score}", True, WHITE)
  162.         WIN.blit(score_text, (10, 10))
  163.  
  164.         # Check for game over
  165.         if not any(city.alive for city in cities):
  166.             game_over_text = FONT.render("Game Over", True, RED)
  167.             WIN.blit(game_over_text, (WIDTH / 2 - 50, HEIGHT / 2))
  168.             pygame.display.update()
  169.             pygame.time.delay(3000)
  170.             run = False
  171.  
  172.         pygame.display.update()
  173.  
  174.     pygame.quit()
  175.  
  176. if __name__ == "__main__":
  177.     main()
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement