Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import math
- # Initialize PyGame
- pygame.init()
- # Screen dimensions
- WIDTH, HEIGHT = 800, 600
- WIN = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Mutual Assured Destruction")
- # Colors
- WHITE = (255, 255, 255)
- BLACK = (0,0,0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
- GREEN = (0, 255, 0)
- YELLOW = (255, 255, 0)
- # Game variables
- FPS = 60
- MISSILE_EVENT = pygame.USEREVENT + 1
- pygame.time.set_timer(MISSILE_EVENT, 1000) # Spawn a missile every second
- CITY_COUNT = 12
- # Font
- FONT = pygame.font.SysFont('arial', 20)
- class Missile:
- def __init__(self):
- self.x = random.randint(0, WIDTH)
- self.y = 0
- self.target_x = random.randint(50, WIDTH - 50)
- self.target_y = HEIGHT - 50
- self.speed = random.uniform(3, 7)
- self.angle = math.atan2(self.target_y - self.y, self.target_x - self.x)
- self.dx = math.cos(self.angle) * self.speed
- self.dy = math.sin(self.angle) * self.speed
- def move(self):
- self.x += self.dx
- self.y += self.dy
- def draw(self, win):
- pygame.draw.circle(win, RED, (int(self.x), int(self.y)), 3)
- class Interceptor:
- def __init__(self, x, y):
- self.x = WIDTH / 2
- self.y = HEIGHT - 50
- self.target_x = x
- self.target_y = y
- self.speed = 5
- self.angle = math.atan2(self.target_y - self.y, self.target_x - self.x)
- self.dx = math.cos(self.angle) * self.speed
- self.dy = math.sin(self.angle) * self.speed
- self.exploded = False
- self.explosion_max_radius = 50
- self.explosion_duration = 30 # frames
- self.explosion_timer = 0
- def move(self):
- if not self.exploded:
- # Move towards target
- self.x += self.dx
- self.y += self.dy
- # Check if reached target
- distance_to_target = math.hypot(self.x - self.target_x, self.y - self.target_y)
- if distance_to_target <= self.speed:
- self.exploded = True
- self.explosion_timer = 0
- else:
- # Explosion timer
- self.explosion_timer += 1
- def draw(self, win):
- if not self.exploded:
- pygame.draw.circle(win, BLUE, (int(self.x), int(self.y)), 3)
- else:
- # Explosion radius increases over time
- current_radius = (self.explosion_timer / self.explosion_duration) * self.explosion_max_radius
- pygame.draw.circle(win, YELLOW, (int(self.x), int(self.y)), int(current_radius))
- class City:
- def __init__(self, x):
- self.x = x
- self.y = HEIGHT - 20
- self.alive = True
- def draw(self, win):
- if self.alive:
- pygame.draw.rect(win, GREEN, (self.x - 20, self.y, 40, 20))
- def main():
- run = True
- clock = pygame.time.Clock()
- missiles = []
- interceptors = []
- cities = [City(WIDTH * i / CITY_COUNT + WIDTH / (2 * CITY_COUNT)) for i in range(CITY_COUNT)]
- score = 0
- while run:
- clock.tick(FPS)
- WIN.fill(BLACK)
- # Event handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- elif event.type == MISSILE_EVENT:
- missiles.append(Missile())
- elif event.type == pygame.MOUSEBUTTONDOWN:
- x, y = pygame.mouse.get_pos()
- interceptors.append(Interceptor(x, y))
- # Move and draw missiles
- for missile in missiles[:]:
- missile.move()
- missile.draw(WIN)
- # Check for collision with cities
- for city in cities:
- if city.alive and math.hypot(missile.x - city.x, missile.y - city.y) < 10:
- city.alive = False
- missiles.remove(missile)
- break
- else:
- # Check if missile reached the ground
- if missile.y >= HEIGHT:
- missiles.remove(missile)
- # Move and draw interceptors
- for interceptor in interceptors[:]:
- interceptor.move()
- interceptor.draw(WIN)
- if interceptor.exploded:
- # Current explosion radius
- current_radius = (interceptor.explosion_timer / interceptor.explosion_duration) * interceptor.explosion_max_radius
- # Check for missiles within current explosion radius
- for missile in missiles[:]:
- distance = math.hypot(interceptor.x - missile.x, interceptor.y - missile.y)
- if distance <= current_radius:
- try:
- missiles.remove(missile)
- score += 100
- except ValueError:
- pass
- # Remove interceptor if explosion is over
- if interceptor.explosion_timer >= interceptor.explosion_duration:
- interceptors.remove(interceptor)
- else:
- # Remove interceptor if it goes off screen
- if interceptor.y <= 0 or interceptor.x < 0 or interceptor.x > WIDTH:
- interceptors.remove(interceptor)
- # Draw cities
- for city in cities:
- city.draw(WIN)
- # Draw score
- score_text = FONT.render(f"Score: {score}", True, WHITE)
- WIN.blit(score_text, (10, 10))
- # Check for game over
- if not any(city.alive for city in cities):
- game_over_text = FONT.render("Game Over", True, RED)
- WIN.blit(game_over_text, (WIDTH / 2 - 50, HEIGHT / 2))
- pygame.display.update()
- pygame.time.delay(3000)
- run = False
- pygame.display.update()
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement