在 pygame 中创建闪电

Create a lightning in pygame

我正在尝试在我的应用程序中创建闪电。到目前为止我做到了

import pygame
import random as rd
import copy

pygame.init()


color = 250,250,250
starting_point =0,0
ending_point = 400,400

a = [[400,0]]

depth = rd.randrange(50,80)

for i in range(depth):
    rand1 = rd.randrange(-20,20)
    rand2 = rd.randrange(2,15)
    a.append([a[i][0]+rand1,a[i][1]+rand2])

screen = pygame.display.set_mode((800,600))

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0,0,0))

    for j in range(0,depth):
        pygame.draw.aaline(screen, (200-2*j,200-2*j,200-2*j), [ a[j][0] , a[j][1] ], [ a[j+1][0] , a[j+1][1] ],1)

    pygame.draw.line(screen, color, a[0], a[1], 1)
    pygame.display.update()

这段代码创建了一条随机生成的闪电线,如下所示:

我想做这样的事情:

我希望从最初生成的行中提取更多行

我只需要初步的想法,我应该使用 class 还是其他列表?

您可以将代码放入函数中,并在生成上一行时使用递归在随机时刻开始下一行。但有时你会得到一行,有时你会得到太多行。

我将它保存在平面列表中,因此它不知道使用什么颜色绘制下一行 - 代码可以为每个点添加颜色或深度,或者它应该为每一行添加深度作为列表中的第一个元素。

import pygame
import random as rd

def generate(x, y, depth):

    result = [ [(x,y)] ] # list for many lines

    for i in range(depth):
        x = result[0][-1][0] + rd.randrange(-20,20)
        y = result[0][-1][1] + rd.randrange(2,15)
        result[0].append((x, y))

        # should I generate next line from here?
        if rd.randrange(0,100) < 5:
            # add all generated lines to result
            result.extend(generate(x, y, depth-i))

    return result

# ---

pygame.init()

depth = rd.randrange(50, 80)
result = generate(400, 0, depth)
print(result)

screen = pygame.display.set_mode((800, 600))

screen.fill((0,0,0))

for line in result:
    for n, (point1, point2) in enumerate(zip(line, line[1:]), 1):
        #x1, y1 = point1
        #x2, y2 = point2
        c = 200-2*n
        pygame.draw.aaline(screen, (c,c,c), point1, point2, 1)

pygame.display.flip()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()    


编辑: 我将深度添加为列表中的第一个元素,因此现在它具有更好的颜色。如果我使用 line(..., width=) 则顶部较厚,底部较薄

我想给 rd.randrange(-20+R,20+R) 添加随机值,这样一些行会更靠左或更靠右。

import pygame
import random as rd


def generate(x, y, depth, c):

    result = [[c, (x,y)]]
    for i in range(depth):
        x = result[0][-1][0] + rd.randrange(-20,20)
        y = result[0][-1][1] + rd.randrange(2,15)
        result[0].append((x, y))

        if rd.randrange(0,100) < 5:
            result.extend(generate(x, y, depth-i, c+i))

    return result

# ---

pygame.init()

depth = rd.randrange(50, 80)
result = generate(400, 0, depth, 0)
print(result)

screen = pygame.display.set_mode((800, 600))

screen.fill((0,0,0))

for line in result:
    depth = line[0]
    line = line[1:]
    for n, (point1, point2) in enumerate(zip(line, line[1:]), depth):
        c = 200-2*n
        #pygame.draw.aaline(screen, (c,c,c), point1, point2, 1)
        pygame.draw.line(screen, (c,c,c), point1, point2, 12-n//6)

pygame.display.flip()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()