pygam中的二维火花产生

2024-09-28 05:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在pygame中制作2D随机火花/闪电发生器。但是,当我运行它时,spark的行元素并没有连接成一个完整的螺栓。我在DrawSpark函数中尝试了多次修复,但都没有成功。这是我的密码:

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()

Tags: eventifdisplayexitrandomcurrentglobalpygame

热门问题