Home > Net >  Lines drawn in pygame but are not displayed
Lines drawn in pygame but are not displayed

Time:03-26

I'm trying to program a simple financing program and still designing the UI. However, I ran into some issues.

def stockdisplay(asset, turn):
    win.fill(dblue)
    stockA = pygame.Rect(0, 160, 250, 170)
    stockB = pygame.Rect(250, 160, 250, 170)
    stockC = pygame.Rect(0, 330, 250, 170)
    leave = pygame.Rect(250, 330, 250, 170)
    loadImage("../Images/stock.png", (245, 150), (200, 300))
    WriteLine("S    T", white, bigFont, (110, 75))
    WriteLine("C    K", white, bigFont, (410, 75))
    pygame.draw.line(win, white, (0, 160), (500, 160))
    pygame.draw.line(win, white, (250, 160), (250, 500))
    pygame.draw.line(win, white, (0, 330), (500, 330))
    while True:
        for event in pygame.event.get():
            if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or (event.type == pygame.QUIT): exit() #Escape Mechanism
            if (event.type == pygame.MOUSEBUTTONUP and event.button == 1):
                if stockA.collidepoint(event.pos): print("Stock A")
                elif stockB.collidepoint(event.pos): print("Stock B")
                elif stockC.collidepoint(event.pos): print("Stock C")
                elif leave.collidepint(event.pos): main(asset, turn)
def WriteLine(text, colour, font, pos):
    text2 = font.render(text, True, colour)
    textRect = text2.get_rect()
    textRect.center = pos
    win.blit(text2, textRect)
    pygame.display.update()
def loadImage(src, pos, scale):
    img = pygame.image.load(src)
    img = pygame.transform.scale(img, scale)
    imgRect = img.get_rect()
    imgRect.center = pos
    win.blit(img, imgRect)
    pygame.display.update()

These are the functions I wrote to display the UI. However, whenever I run the program, the lines that were supposed to be drawn by the pygame.draw.line() is simply not there. The rectangles are still in the programs as they still respond to clicks. Is there any way to fix this?

CodePudding user response:

One call of pygame.display.update() is sufficient, but it needs to be done after all the drawing. Remove pygame.display.update() from WriteLine and loadImage, but do a single pygame.display.update() at the end of the application loop:

def stockdisplay(asset, turn):
    win.fill(dblue)
    stockA = pygame.Rect(0, 160, 250, 170)
    stockB = pygame.Rect(250, 160, 250, 170)
    stockC = pygame.Rect(0, 330, 250, 170)
    leave = pygame.Rect(250, 330, 250, 170)
    loadImage("../Images/stock.png", (245, 150), (200, 300))
    WriteLine("S    T", white, bigFont, (110, 75))
    WriteLine("C    K", white, bigFont, (410, 75))
    pygame.draw.line(win, white, (0, 160), (500, 160))
    pygame.draw.line(win, white, (250, 160), (250, 500))
    pygame.draw.line(win, white, (0, 330), (500, 330))
    
    while True:
        for event in pygame.event.get():
            if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or (event.type == pygame.QUIT): exit() #Escape Mechanism
            if (event.type == pygame.MOUSEBUTTONUP and event.button == 1):
                if stockA.collidepoint(event.pos): print("Stock A")
                elif stockB.collidepoint(event.pos): print("Stock B")
                elif stockC.collidepoint(event.pos): print("Stock C")
                elif leave.collidepint(event.pos): main(asset, turn)

        pygame.display.update() # <-- INSERT

def WriteLine(text, colour, font, pos):
    text2 = font.render(text, True, colour)
    textRect = text2.get_rect()
    textRect.center = pos
    win.blit(text2, textRect)
    # pygame.display.update() <-- DELETE

def loadImage(src, pos, scale):
    img = pygame.image.load(src)
    img = pygame.transform.scale(img, scale)
    imgRect = img.get_rect()
    imgRect.center = pos
    win.blit(img, imgRect)
    # pygame.display.update() <-- DELETE

The typical PyGame application loop has to:

  • Related