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:
- limit the frames per second to limit CPU usage with
pygame.time.Clock.tick
- handle the events by calling either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by calling either
pygame.display.update()
orpygame.display.flip()