I am currently making a 2D random spark/lightning bolt generator in pygame. However, when i run it, the line elements of the spark do not join to make a complete bolt. I have tried multiple fixes within the DrawSpark function but with no success. Here is my code:
import pygame,random,os
from pygame.locals import *
DotSize = 5
BGColour = (0,0,0)
MaxVariation = 50
SparkColour = (255,255,255)
def DrawSpark():
global y
global StartX
global current
global DirectionAmount
global x
global Element
global DotSize
global MaxVariation
global StartPos
global endPos
global DISPLAYSURF
DISPLAYSURF.fill(BGColour)
y=0
print("Work in Progress")
StartX = random.randint(480,1440)
print(StartX)
current = StartX
for i in range(0,1080):
DirectionAmount = random.randint(0-MaxVariation,MaxVariation)
while DirectionAmount == 0:
DirectionAmount = random.randint(0-MaxVariation,MaxVariation)
if DirectionAmount != 0:
break
StartPos = (current, y)
endPos = (current+DirectionAmount, y + DotSize)
if DirectionAmount != 0:
pygame.draw.line(DISPLAYSURF, SparkColour, StartPos, endPos, DotSize)
current = StartX+DirectionAmount
x = current
Element = pygame.Rect(x,y,DotSize,DotSize)
# pygame.draw.rect(DISPLAYSURF,SparkColour,Element)
y+=DotSize*2
pygame.display.update()
def Main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
os._exit(1)
elif event.type == KEYDOWN:
if event.key == K_r:
DrawSpark()
elif event.key == K_ESCAPE: #if the escape key is pressed, exit the game
os._exit(1)
pygame.display.update()
def Init():
global DISPLAYSURF
global BGColour
pygame.init()
WINDOW_X_MAX = 1920
WINDOW_Y_MAX = 1080
DISPLAYSURF = pygame.display.set_mode((WINDOW_X_MAX, WINDOW_Y_MAX), FULLSCREEN,32)
pygame.display.set_caption("Spark")
print("Press r to redraw spark\nPress esc to exit")
DISPLAYSURF.fill(BGColour)
DrawSpark()
Main()
Init()