我正在开发一个 Pygame 项目,其中的线条应该在创建时淡入并随着时间的推移淡出。但是,我遇到了一个问题,线条似乎没有按预期淡入或淡出。
我希望线条在第一次创建时逐渐淡入,并随着时间的推移淡出,但淡入淡出部分似乎没有按预期工作。有人可以帮我找出问题并提出解决方案吗?
编辑:
我的新代码:
import pygame
import random
import math
pygame.init()
WIDTH, HEIGHT = 800, 600
FPS = 60
LINE_COLOR = (255, 255, 255)
BACKGROUND_COLOR = (0, 0, 0)
LINE_LENGTH = 100
CONNECT_DISTANCE = 50
MAGNET_RADIUS = 100
MAX_LINES = 50
FADE_OUT_TIME = 3 * FPS
FADE_IN_TIME = 1 * FPS
speed_of_rotation = 0.02
def create_line():
return {'x': random.randint(0, WIDTH),'y': random.randint(0, HEIGHT),'angle': random.uniform(0, 2 * math.pi),'target_angle': random.uniform(0, 2 * math.pi),'length': LINE_LENGTH,'style': 1,'fade_timer': 0}
def update_angle(line):
angle_difference = line['target_angle'] - line['angle']
if angle_difference != 0:
line['angle'] += angle_difference / abs(angle_difference) * speed_of_rotation
def draw_line(line):
if line['style'] == 1:
end_x = line['x'] + line['length'] * math.cos(line['angle'])
end_y = line['y'] + line['length'] * math.sin(line['angle'])
pygame.draw.line(screen, LINE_COLOR, (line['x'], line['y']), (end_x, end_y), 2)
elif line['style'] == 2:
alpha = int((line['fade_timer'] / FADE_OUT_TIME) * 255)
pygame.draw.line(screen, (LINE_COLOR[0], LINE_COLOR[1], LINE_COLOR[2], alpha),
(line['x'], line['y']),
(line['x'] + line['length'] * math.cos(line['angle']),
line['y'] + line['length'] * math.sin(line['angle'])), 2)
def connect_lines(line, other_line):
distance = math.dist((line['x'], line['y']), (other_line['x'], other_line['y']))
if distance < CONNECT_DISTANCE:
pygame.draw.line(screen, LINE_COLOR, (line['x'], line['y']), (other_line['x'], other_line['y']), 2)
pygame.draw.circle(screen, LINE_COLOR, (line['x'], line['y']), 5)
pygame.draw.circle(screen, LINE_COLOR, (other_line['x'], other_line['y']), 5)
def dispose_line(line):
line['x'] += math.cos(line['angle'])
line['y'] += math.sin(line['angle'])
if line['x'] < 0 or line['x'] > WIDTH or line['y'] < 0 or line['y'] > HEIGHT:
return True
return False
def fade_out_line(line):
line['fade_timer'] += 1
if line['fade_timer'] > FADE_OUT_TIME:
return True
return False
def connect_to_mouse(line, mouse_x, mouse_y):
distance_to_mouse = math.dist((line['x'], line['y']), (mouse_x, mouse_y))
if distance_to_mouse < MAGNET_RADIUS:
angle_to_mouse = math.atan2(mouse_y - line['y'], mouse_x - line['x'])
line['x'] += math.cos(angle_to_mouse) * 5
line['y'] += math.sin(angle_to_mouse) * 5
line['angle'] = angle_to_mouse
line['target_angle'] = angle_to_mouse
lines = [create_line() for _ in range(MAX_LINES)]
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Magnet Lines")
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BACKGROUND_COLOR)
if len(lines) < MAX_LINES and random.random() < 0.02:
lines.append(create_line())
for line in lines:
update_angle(line)
draw_line(line)
for other_line in lines:
if line != other_line:
connect_lines(line, other_line)
if line['style'] == 1:
if dispose_line(line):
lines.remove(line)
elif line['style'] == 2:
if fade_out_line(line):
lines.remove(line)
connect_to_mouse(line, *pygame.mouse.get_pos())
pygame.display.flip()
clock.tick(FPS)
我找到了我想要的所有其他东西的解决方案,但我没有在问题中提到:
from pygame import init, QUIT
from pygame.draw import line as l, circle
from pygame.time import Clock
from pygame.display import set_mode, flip
from pygame.event import get
from pygame.mouse import get_pos
from random import randint, random, uniform
from math import sin, cos, dist, atan2
init()
CONNECT_DISTANCE = 50
MAGNET_RADIUS = 100
def update_angle(line):
line['angle'] += line['rotation_speed']
if line['angle'] > 2 * 3.141592653589793:
line['angle'] -= 2 * 3.141592653589793
def draw_line(line):
if line['timer'] == 0:
lines.remove(line)
return
alpha = min([255, line['timer']])
end_x = line['x'] + line['timer'] // 5 * cos(line['angle'])
end_y = line['y'] + line['timer'] // 5 * sin(line['angle'])
l(screen, (alpha, alpha, alpha), (line['x'], line['y']), (end_x, end_y), 1)
def connect_lines(line, other_line):
x1 = line['x'] + line['timer'] // 5 * cos(line['angle'])
y1 = line['y'] + line['timer'] // 5 * sin(line['angle'])
x2 = other_line['x'] + other_line['timer'] // 5 * cos(other_line['angle'])
y2 = other_line['y'] + other_line['timer'] // 5 * sin(other_line['angle'])
distance = dist((line['x'], line['y']), (other_line['x'], other_line['y']))
if distance < CONNECT_DISTANCE:
alpha = min([255, line['timer']])
l(screen, (alpha, alpha, alpha), (line['x'], line['y']), (other_line['x'], other_line['y']), 1)
circle(screen, (alpha, alpha, alpha), (line['x'], line['y']), 2)
circle(screen, (alpha, alpha, alpha), (other_line['x'], other_line['y']), 2)
return
distance = dist((line['x'], line['y']), (x2, y2))
if distance < CONNECT_DISTANCE:
alpha = min([255, line['timer']])
l(screen, (alpha, alpha, alpha), (line['x'], line['y']), (x2, y2), 1)
circle(screen, (alpha, alpha, alpha), (line['x'], line['y']), 2)
circle(screen, (alpha, alpha, alpha), (x2, y2), 2)
return
distance = dist((x1, y1), (x2, y2))
if distance < CONNECT_DISTANCE:
alpha = min([255, line['timer']])
l(screen, (alpha, alpha, alpha), (x1, y1), (x2, y2), 1)
circle(screen, (alpha, alpha, alpha), (x1, y1), 2)
circle(screen, (alpha, alpha, alpha), (x2, y2), 2)
return
distance = dist((x1, y1), (other_line['x'], other_line['y']))
if distance < CONNECT_DISTANCE:
alpha = min([255, line['timer']])
l(screen, (alpha, alpha, alpha), (x1, y1), (other_line['x'], other_line['y']), 1)
circle(screen, (alpha, alpha, alpha), (x1, y1), 2)
circle(screen, (alpha, alpha, alpha), (other_line['x'], other_line['y']), 2)
def dispose_line(line):
line['x'] += cos(line['angle'])
line['y'] += sin(line['angle'])
if line['x'] < 0 or line['x'] > 1500 or line['y'] < 0 or line['y'] > 700:
return True
return False
def fade_out_line(line):
line['fade_timer'] += 1
if line['fade_timer'] > FADE_OUT_TIME:
return True
return False
def connect_to_mouse(line, mouse_x, mouse_y):
distance_to_mouse = dist((line['x'], line['y']), (mouse_x, mouse_y))
if distance_to_mouse < MAGNET_RADIUS:
line['fade-out'] = False
angle_to_mouse = atan2(mouse_y - line['y'], mouse_x - line['x'])
line['x'] += cos(angle_to_mouse) * 5
line['y'] += sin(angle_to_mouse) * 5
lines = []
screen = set_mode((1500, 700))
clock = Clock()
running = True
while running:
for event in get():
if event.type == QUIT:
running = False
screen.fill((0, 0, 0))
if random() < 0.07:
lines.append({'x': randint(0, 1500),'y': randint(0, 700), 'fade-out': False, 'angle': uniform(0, 2 * 3.141592653589793), 'timer': 1, 'rotation_speed': uniform(-0.01, 0.01)})
for line in lines:
update_angle(line)
draw_line(line)
for other_line in lines:
if line != other_line:
connect_lines(line, other_line)
if dispose_line(line):
lines.remove(line)
continue
if line['timer'] > 500:
line['fade-out'] = True
if line['fade-out']:
line['timer'] -= 1
else:
line['timer'] += 1
connect_to_mouse(line, *get_pos())
flip()
clock.tick(100)