clothtrail / app.py
kamranmu890's picture
Create app.py
1c5e5ef verified
import pygame
import sys
import random
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Cloth Trail")
clock = pygame.time.Clock()
trail = []
max_length = 50
color = (100, 149, 237) # Cornflower Blue
fade_speed = 5 # Decrease in alpha per frame
def draw_trail():
for i, point in enumerate(trail):
x, y, alpha = point
current_alpha = max(0, alpha - i * fade_speed // max_length)
current_color = (color[0], color[1], color[2], current_alpha) # RGBA
size = max(1, 10 * (1 - i / max_length))
pygame.draw.circle(screen, current_color, (int(x), int(y)), int(size // 2))
alpha_surface = pygame.Surface((width, height), pygame.SRCALPHA)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
mouse_x, mouse_y = pygame.mouse.get_pos()
trail.append([mouse_x, mouse_y, 255]) # Initial alpha is fully opaque
if len(trail) > max_length:
trail.pop(0)
screen.fill((0, 0, 0)) # Black background
# Draw the trail onto the alpha surface
alpha_surface.fill((0, 0, 0, 0)) # Clear the alpha surface
for i, point in enumerate(trail):
x, y, alpha = point
current_alpha = max(0, 255 - i * fade_speed)
current_color = (color[0], color[1], color[2], current_alpha)
size = max(1, 10 * (1 - i / max_length))
pygame.draw.circle(alpha_surface, current_color, (int(x), int(y)), int(size // 2))
# Blit the alpha surface onto the main screen
screen.blit(alpha_surface, (0, 0))
pygame.display.flip()
clock.tick(60)