0

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()
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • 1
    I'm not sure it's clear what you're asking, can you clarify what you think is going wrong? Why all the globals? – Russia Must Remove Putin Oct 01 '14 at 23:17
  • @user3683573 - take a screen capture, and then add it to the question. – jww Oct 02 '14 at 00:53
  • @AaronHall - I think that the problem is due to a logic error when the spark is drawn, so that the random locations of the line start and end points do not join up. However, i do not have an idea as to what sections of code are causing this problem. – user3683573 Oct 02 '14 at 21:34
  • I am not able to upload the screenshot as i do not have enough reputation. – user3683573 Oct 02 '14 at 21:52

0 Answers0