Import pygame import random import math import time pygame.init() width, height = 1920, 1080 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("KosmoDesant") BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) SUN_COLOR = (255, 215, 0) ROCKET_COLOR = (200, 200, 200) PLANET_COLORS = [ (255, 100, 100), (100, 255, 100), (100, 100, 255), (255, 165, 0), (128, 0, 128), (255, 192, 203), (200, 200, 200), (0, 255, 255) ] def init_planets(): planets = [ {"radius": 12, "distance": 120, "speed": 0.03, "angle": 0}, {"radius": 16, "distance": 180, "speed": 0.025, "angle": 1.5}, {"radius": 18, "distance": 250, "speed": 0.02, "angle": 3.0}, {"radius": 22, "distance": 330, "speed": 0.015, "angle": 4.5}, {"radius": 20, "distance": 410, "speed": 0.012, "angle": 6.0}, {"radius": 15, "distance": 490, "speed": 0.01, "angle": 7.5}, {"radius": 14, "distance": 570, "speed": 0.008, "angle": 9.0}, {"radius": 10, "distance": 650, "speed": 0.006, "angle": 10.5} ] for i, planet in enumerate(planets): planet["color"] = PLANET_COLORS[i % len(PLANET_COLORS)] return planets def init_stars(): stars = [] for i in range(1000): stars.append(( random.randint(0, width), random.randint(0, height), random.uniform(0.3, 1.0) )) return stars def init_asteroids(): asteroids = [] for i in range(7): asteroid = { "x": random.randint(0, width), "y": random.randint(0, height), "radius": random.randint(30, 30), "speed": random.uniform(1, 4), "angle": random.uniform(0, 2 * math.pi) } asteroids.append(asteroid) return asteroids def init_rocket(): return { "x": width // 2, "y": height // 2, "speed": 5, "angle": 0, "width": 40, "height": 20, "flame_active": False, "rotation_speed": 0.1, "acceleration": 0.2, "max_speed": 6, } def draw_star(x, y, brightness): color_value = int(255 * brightness) pygame.draw.circle(screen, (color_value, color_value, color_value), (int(x), int(y)), 2) def update_rocket(rocket, keys): if keys[pygame.K_LEFT]: rocket["angle"] -= rocket["rotation_speed"] if keys[pygame.K_RIGHT]: rocket["angle"] += rocket["rotation_speed"] if keys[pygame.K_UP]: rocket["flame_active"] = True rocket["speed"] = min(rocket["speed"] + rocket["acceleration"], rocket["max_speed"]) else: rocket["flame_active"] = False rocket["speed"] = max(0, rocket["speed"] - 0.10) if keys[pygame.K_DOWN]: rocket["speed"] = max(0, rocket["speed"] - rocket["acceleration"] * 2) rocket["x"] += rocket["speed"] * math.cos(rocket["angle"]) rocket["y"] += rocket["speed"] * math.sin(rocket["angle"]) if rocket["x"] < 0: rocket["x"] = width elif rocket["x"] > width: rocket["x"] = 0 if rocket["y"] < 0: rocket["y"] = height elif rocket["y"] > height: rocket["y"] = 0 scale_width = 55 scale_height = 30 scale_width_1 = 90 scale_height_1 = 90 asteroid_image = pygame.image.load("asteroid.png") asteroid_image = pygame.transform.scale(asteroid_image, (scale_width_1, scale_height_1)) asteroid_image.set_colorkey((255, 255, 255)) font = pygame.font.SysFont("Arial", 36) big_font = pygame.font.SysFont("Arial", 72) rocket_image = pygame.image.load("rocket.png") rocket_image = pygame.transform.scale(rocket_image, (scale_width, scale_height)) image_rect = rocket_image.get_rect(center=(rocket["x"], rocket["y"])) rocket_image.set_colorkey((0, 0, 0)) def draw_rocket(rocket): rotated_image = pygame.transform.rotate(rocket_image, -math.degrees(rocket["angle"])) image_rect = rotated_image.get_rect(center=(rocket["x"], rocket["y"])) screen.blit(rotated_image, image_rect.topleft) if rocket["flame_active"]: flame_points = [ (rocket["x"] - 10, rocket["y"] - rocket["height"] // 4), (rocket["x"] - 30, rocket["y"]), (rocket["x"] - 10, rocket["y"] + rocket["height"] // 4) ] rotated_flame = [] for px, py in flame_points: px -= rocket["x"] py -= rocket["y"] new_x = px * math.cos(rocket["angle"]) - py *math.sin(rocket["angle"]) new_y = px * math.sin(rocket["angle"]) + py * math.cos(rocket["angle"]) rotated_flame.append((new_x + rocket["x"], new_y + rocket["y"])) pygame.draw.polygon(screen, (255, 0, 0), rotated_flame) def d_asteroids(asteroids): for asteroid in asteroids: asteroid_rect = asteroid_image.get_rect(center=(asteroid["x"], asteroid["y"])) screen.blit(asteroid_image, asteroid_rect.topleft) def u_asteroids(asteroids): for asteroid in asteroids: asteroid["x"] += asteroid["speed"] * math.cos(asteroid["angle"]) asteroid["y"] += asteroid["speed"] * math.sin(asteroid["angle"]) if asteroid["x"] < 0 or asteroid["x"] > width or asteroid["y"] < 0 or asteroid["y"] > height: asteroid["x"] = random.randint(0, width) asteroid["y"] = random.randint(0, height) def check_collision(rocket, asteroids): rocket_rect = pygame.Rect(rocket["x"] - rocket["width"] // 2, rocket["y"] - rocket["height"] // 2, rocket["width"], rocket["height"]) for asteroid in asteroids: asteroid_rect = pygame.Rect(asteroid["x"] - asteroid["radius"], asteroid["y"] - asteroid["radius"], asteroid["radius"] * 2, asteroid["radius"] * 2) if rocket_rect.colliderect(asteroid_rect): return True return False def draw_hud(elapsed_time): time_text = font.render(f"Время: {elapsed_time:.1f} сек", True, WHITE) screen.blit(time_text, (width - 250, 20)) def show_game_over(elapsed_time): screen.fill(BLACK) game_over_text = big_font.render("GAME OVER", True, RED) time_text = big_font.render(f"Время выживания: {elapsed_time:.1f} сек", True, WHITE) restart_text = font.render("Нажмите ПРОБЕЛ для перезапуска", True, WHITE) screen.blit(game_over_text, (width//2 - game_over_text.get_width()//2, height//2 - 100)) screen.blit(time_text, (width//2 - time_text.get_width()//2, height//2)) screen.blit(restart_text, (width//2 - restart_text.get_width()//2, height//2 + 100)) pygame.display.flip() waiting = True while waiting: for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: return True clock.tick(60) def game_loop(): planets = init_planets() stars = init_stars() asteroids = init_asteroids() rocket = init_rocket() center_x, center_y = width // 2, height // 2 sun_radius = 40 start_time = time.time() running = True while running: screen.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: return False elapsed_time = time.time() - start_time keys = pygame.key.get_pressed() update_rocket(rocket, keys) for star in stars: draw_star(star[0], star[1], star[2]) for i in range(3, 0, -1): glow_radius = sun_radius * i glow_surface = pygame.Surface((glow_radius * 2, glow_radius * 2), pygame.SRCALPHA) alpha = 100 // i pygame.draw.circle(glow_surface, (*SUN_COLOR, alpha), (glow_radius, glow_radius), glow_radius) screen.blit(glow_surface, (center_x - glow_radius, center_y - glow_radius)) pygame.draw.circle(screen, SUN_COLOR, (center_x, center_y), sun_radius) for planet in planets: planet["angle"] += planet["speed"] x = center_x + planet["distance"] * math.cos(planet["angle"]) y = center_y + planet["distance"] * math.sin(planet["angle"]) pygame.draw.circle(screen, (50, 50, 50), (center_x, center_y), planet["distance"], 1) pygame.draw.circle(screen, planet["color"], (int(x), int(y)), planet["radius"]) shadow_offset = planet["radius"] // 4 shadow_color = tuple(c // 2 for c in planet["color"]) pygame.draw.circle(screen, shadow_color, (int(x + shadow_offset), int(y + shadow_offset)), planet["radius"]) draw_rocket(rocket) u_asteroids(asteroids) d_asteroids(asteroids) draw_hud(elapsed_time) if check_collision(rocket, asteroids): if show_game_over(elapsed_time): return True # Перезапустить игру else: return False # Выйти из игры pygame.display.flip() clock.tick(60) clock = pygame.time.Clock() restart = True while restart: restart = game_loop() pygame.quit() после столкновения с астероидами можно будет выйти на кнопку R вставь изменения в код и на пробел можно перезапустить игру и вставь изменения в код мой
Ответ нейросети
10 апреля 2025 13:42
Понравился ответ?
Задай свой вопрос
Напиши любую задачу или вопрос, а нейросеть её решит



