我正在嘗試撰寫一個簡單的融資程式,并且仍在設計 UI。但是,我遇到了一些問題。
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()
這些是我為顯示 UI 而撰寫的函式。但是,每當我運行程式時,應該由 繪制的線條pygame.draw.line()根本不存在。矩形仍然在程式中,因為它們仍然回應點擊。有沒有什么辦法解決這一問題?
uj5u.com熱心網友回復:
一次呼叫pygame.display.update()就足夠了,但需要在所有繪圖之后完成。pygame.display.update()從WriteLineand中洗掉loadImage,但pygame.display.update()在應用程式回圈結束時執行一次:
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
典型的 PyGame 應用程式回圈必須:
- 限制每秒幀數以限制 CPU 使用率
pygame.time.Clock.tick - 通過呼叫
pygame.event.pump()或來處理事件pygame.event.get()。 - 根據輸入事件和時間(分別為幀)更新物件的游戲狀態和位置
- 清除整個顯示或繪制背景
- 繪制整個場景(
blit所有物件) - 通過呼叫
pygame.display.update()或更新顯示pygame.display.flip()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449646.html
