import pygame import sys import random import math # Initialize Pygame pygame.init() # Set up the display width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Asteroids") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Ship ship_size = 20 ship_pos = [width // 2, height // 2] ship_angle = 0 ship_speed = 0 # Bullets bullets = [] bullet_speed = 10 # Asteroids asteroids = [] asteroid_count = 10 # Score score = 0 font = pygame.font.Font(None, 36) # Game over flag game_over = False def create_asteroid(): size = random.randint(20, 50) x = random.choice([random.randint(0, width // 4), random.randint(3 * width // 4, width)]) y = random.choice([random.randint(0, height // 4), random.randint(3 * height // 4, height)]) speed = random.uniform(0.5, 2) angle = random.uniform(0, 2 * math.pi) sides = random.choice([5, 6]) return {"pos": [x, y], "size": size, "speed": speed, "angle": angle, "sides": sides} def draw_polygon(surface, color, pos, size, sides, angle=0): points = [] for i in range(sides): angle_point = angle + (2 * math.pi * i / sides) point = (pos[0] + size * math.cos(angle_point), pos[1] + size * math.sin(angle_point)) points.append(point) pygame.draw.polygon(surface, color, points, 2) # Create initial asteroids for _ in range(asteroid_count): asteroids.append(create_asteroid()) # Game loop clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q or event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() elif event.key == pygame.K_SPACE and not game_over: bullets.append([ship_pos[0], ship_pos[1], ship_angle]) if not game_over: # Ship movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: ship_angle += 0.1 if keys[pygame.K_RIGHT]: ship_angle -= 0.1 if keys[pygame.K_UP]: ship_speed = min(ship_speed + 0.1, 5) else: ship_speed = max(ship_speed - 0.05, 0) ship_pos[0] += math.cos(ship_angle) * ship_speed ship_pos[1] -= math.sin(ship_angle) * ship_speed # Wrap ship around screen ship_pos[0] %= width ship_pos[1] %= height # Update bullets for bullet in bullets[:]: bullet[0] += math.cos(bullet[2]) * bullet_speed bullet[1] -= math.sin(bullet[2]) * bullet_speed if bullet[0] < 0 or bullet[0] > width or bullet[1] < 0 or bullet[1] > height: bullets.remove(bullet) # Update asteroids for asteroid in asteroids[:]: asteroid["pos"][0] += math.cos(asteroid["angle"]) * asteroid["speed"] asteroid["pos"][1] += math.sin(asteroid["angle"]) * asteroid["speed"] # Wrap asteroids around screen asteroid["pos"][0] %= width asteroid["pos"][1] %= height # Check collision with ship if (abs(asteroid["pos"][0] - ship_pos[0]) < asteroid["size"] and abs(asteroid["pos"][1] - ship_pos[1]) < asteroid["size"]): game_over = True # Check collision with bullets for bullet in bullets[:]: if (abs(asteroid["pos"][0] - bullet[0]) < asteroid["size"] and abs(asteroid["pos"][1] - bullet[1]) < asteroid["size"]): asteroids.remove(asteroid) bullets.remove(bullet) score += 1 asteroids.append(create_asteroid()) break # Draw everything screen.fill(BLACK) if not game_over: # Draw ship points = [ (ship_pos[0] + math.cos(ship_angle) * ship_size, ship_pos[1] - math.sin(ship_angle) * ship_size), (ship_pos[0] + math.cos(ship_angle + 2.6) * ship_size, ship_pos[1] - math.sin(ship_angle + 2.6) * ship_size), (ship_pos[0] + math.cos(ship_angle - 2.6) * ship_size, ship_pos[1] - math.sin(ship_angle - 2.6) * ship_size), ] pygame.draw.polygon(screen, WHITE, points, 2) # Draw bullets for bullet in bullets: pygame.draw.circle(screen, WHITE, (int(bullet[0]), int(bullet[1])), 2) # Draw asteroids for asteroid in asteroids: draw_polygon(screen, WHITE, asteroid["pos"], asteroid["size"], asteroid["sides"]) # Draw score score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) if game_over: game_over_text = font.render("GAME OVER", True, WHITE) screen.blit(game_over_text, (width // 2 - 70, height // 2 - 18)) pygame.display.flip() clock.tick(60)