import pygame import random import sys # Setup pygame.init() WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Island World Zoom + People") clock = pygame.time.Clock() # Colors WATER = (50, 150, 255) ISLAND = (34, 139, 34) TREE = (0, 100, 0) PLANT = (144, 238, 144) STONE = (120, 120, 120) # Assets person_img = pygame.image.load("person.png").convert_alpha() # World NUM_ISLANDS = 15 islands = [] trees = [] plants = [] stones = [] people = [] # Zoom + Camera zoom = 1.0 camera_x, camera_y = 0, 0 mouse_drag = False last_mouse_pos = (0, 0) def world_to_screen(pos): x, y = pos sx = (x - camera_x) * zoom + WIDTH / 2 sy = (y - camera_y) * zoom + HEIGHT / 2 return int(sx), int(sy) def screen_to_world(pos): sx, sy = pos x = (sx - WIDTH / 2) / zoom + camera_x y = (sy - HEIGHT / 2) / zoom + camera_y return int(x), int(y) def generate_islands(): for _ in range(NUM_ISLANDS): x = random.randint(-1000, 1000) y = random.randint(-1000, 1000) radius = random.randint(40, 90) islands.append((x, y, radius)) for _ in range(random.randint(3, 8)): angle = random.uniform(0, 2 * 3.14) dist = random.uniform(0, radius * 0.8) tx = int(x + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).x) ty = int(y + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).y) trees.append((tx, ty)) for _ in range(random.randint(5, 15)): angle = random.uniform(0, 2 * 3.14) dist = random.uniform(0, radius * 0.9) px = int(x + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).x) py = int(y + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).y) plants.append((px, py)) for _ in range(random.randint(3, 10)): angle = random.uniform(0, 2 * 3.14) dist = random.uniform(0, radius * 0.9) sx = int(x + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).x) sy = int(y + dist * pygame.math.Vector2(1, 0).rotate_rad(angle).y) stones.append((sx, sy)) generate_islands() # Game loop running = True while running: screen.fill(WATER) # Draw islands for x, y, r in islands: pygame.draw.circle(screen, ISLAND, world_to_screen((x, y)), int(r * zoom)) for x, y in trees: pygame.draw.circle(screen, TREE, world_to_screen((x, y)), int(6 * zoom)) for x, y in plants: pygame.draw.circle(screen, PLANT, world_to_screen((x, y)), int(3 * zoom)) for x, y in stones: pygame.draw.circle(screen, STONE, world_to_screen((x, y)), int(4 * zoom)) # Draw people for px, py in people: sprite = pygame.transform.smoothscale(person_img, (int(32 * zoom), int(32 * zoom))) pos = world_to_screen((px, py)) rect = sprite.get_rect(center=pos) screen.blit(sprite, rect) pygame.display.flip() clock.tick(60) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Mouse wheel zoom if event.type == pygame.MOUSEWHEEL: mouse_world_before = screen_to_world(pygame.mouse.get_pos()) zoom *= 1.1 if event.y > 0 else 0.9 mouse_world_after = screen_to_world(pygame.mouse.get_pos()) camera_x += mouse_world_before[0] - mouse_world_after[0] camera_y += mouse_world_before[1] - mouse_world_after[1] # Spawn person if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: world_pos = screen_to_world(pygame.mouse.get_pos()) people.append(world_pos) if event.button == 3: mouse_drag = True last_mouse_pos = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONUP: if event.button == 3: mouse_drag = False if event.type == pygame.MOUSEMOTION: if mouse_drag: mx, my = pygame.mouse.get_pos() dx = (last_mouse_pos[0] - mx) / zoom dy = (last_mouse_pos[1] - my) / zoom camera_x += dx camera_y += dy last_mouse_pos = (mx, my) pygame.quit() sys.exit()