|
| 1 | +import pygame |
| 2 | +import math |
| 3 | +import time |
| 4 | +import pmma |
| 5 | + |
| 6 | +def create_gradient_surface(length, width, start_color, end_color): |
| 7 | + """Creates a horizontal gradient surface.""" |
| 8 | + surface = pygame.Surface((length, width), pygame.SRCALPHA) |
| 9 | + for x in range(length): |
| 10 | + t = x / length |
| 11 | + color = ( |
| 12 | + int(start_color[0] * (1 - t) + end_color[0] * t), |
| 13 | + int(start_color[1] * (1 - t) + end_color[1] * t), |
| 14 | + int(start_color[2] * (1 - t) + end_color[2] * t), |
| 15 | + 255 |
| 16 | + ) |
| 17 | + pygame.draw.line(surface, color, (x, 0), (x, width)) |
| 18 | + return surface |
| 19 | + |
| 20 | +def blit_rotated_gradient(surface, start_pos, end_pos, start_color, end_color, width): |
| 21 | + """Blits a correctly positioned and rotated gradient line.""" |
| 22 | + # Compute length of the line |
| 23 | + dx = end_pos[0] - start_pos[0] |
| 24 | + dy = end_pos[1] - start_pos[1] |
| 25 | + length = int(math.sqrt(dx ** 2 + dy ** 2)) |
| 26 | + |
| 27 | + # Create the horizontal gradient |
| 28 | + gradient = create_gradient_surface(length, width, start_color, end_color) |
| 29 | + |
| 30 | + # Find the angle in degrees |
| 31 | + angle = math.degrees(math.atan2(dy, dx)) |
| 32 | + |
| 33 | + # Rotate the gradient surface |
| 34 | + rotated_gradient = pygame.transform.rotate(gradient, -angle) |
| 35 | + |
| 36 | + # Get the new bounding box and recenter it |
| 37 | + rotated_rect = rotated_gradient.get_rect(center=((start_pos[0] + end_pos[0]) // 2, |
| 38 | + (start_pos[1] + end_pos[1]) // 2)) |
| 39 | + |
| 40 | + # Blit the rotated gradient at the corrected position |
| 41 | + surface.blit(rotated_gradient, rotated_rect.topleft) |
| 42 | + |
| 43 | +# Pygame setup |
| 44 | +pygame.init() |
| 45 | +screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN) |
| 46 | +clock = pygame.time.Clock() |
| 47 | + |
| 48 | +inner_color = pmma.ColorConverter() |
| 49 | +outer_color = pmma.ColorConverter() |
| 50 | +radius = pmma.Perlin() |
| 51 | + |
| 52 | +now_time = 0 |
| 53 | +start = time.perf_counter() |
| 54 | +running = True |
| 55 | +while running: |
| 56 | + rx = 1920 // 2 + int(math.sin(now_time) * radius.generate_1D_perlin_noise(now_time/5, new_range=[0, 1080 / 2])) |
| 57 | + ry = 1080 // 2 + int(math.cos(now_time) * radius.generate_1D_perlin_noise(now_time/5, new_range=[0, 1080 / 2])) |
| 58 | + |
| 59 | + blit_rotated_gradient( |
| 60 | + screen, |
| 61 | + (1920 // 2, 1080 // 2), |
| 62 | + (rx, ry), |
| 63 | + inner_color.generate_color_from_perlin_noise(now_time/7, format=pmma.Constants.RGB), |
| 64 | + outer_color.generate_color_from_perlin_noise(now_time/7, format=pmma.Constants.RGB), |
| 65 | + 10) |
| 66 | + |
| 67 | + rx = 1920 // 2 + int(math.sin(now_time + math.pi) * radius.generate_1D_perlin_noise(now_time/5, new_range=[0, 1080 / 2])) |
| 68 | + ry = 1080 // 2 + int(math.cos(now_time + math.pi) * radius.generate_1D_perlin_noise(now_time/5, new_range=[0, 1080 / 2])) |
| 69 | + |
| 70 | + blit_rotated_gradient( |
| 71 | + screen, |
| 72 | + (1920 // 2, 1080 // 2), |
| 73 | + (rx, ry), |
| 74 | + inner_color.generate_color_from_perlin_noise(now_time/7, format=pmma.Constants.RGB), |
| 75 | + outer_color.generate_color_from_perlin_noise(now_time/7, format=pmma.Constants.RGB), |
| 76 | + 10) |
| 77 | + |
| 78 | + pygame.display.flip() |
| 79 | + |
| 80 | + for event in pygame.event.get(): |
| 81 | + if event.type == pygame.QUIT: |
| 82 | + running = False |
| 83 | + |
| 84 | + now_time = time.perf_counter() - start |
| 85 | + clock.tick(60) |
| 86 | + |
| 87 | +pygame.quit() |
0 commit comments