在 pygame 中绘制但未显示的线条
Lines drawn in pygame but are not displayed
我正在尝试编写一个简单的融资程序,并且仍在设计 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()
绘制的线条根本不存在。矩形仍在程序中,因为它们仍然响应点击。
有什么办法可以解决这个问题吗?
调用一次pygame.display.update()
即可,但需要在所有绘图完成后调用。从 WriteLine
和 loadImage
中删除 pygame.display.update()
,但在应用程序循环结束时执行单个 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()
or pygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过调用
pygame.display.update()
or pygame.display.flip()
更新显示
我正在尝试编写一个简单的融资程序,并且仍在设计 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()
绘制的线条根本不存在。矩形仍在程序中,因为它们仍然响应点击。
有什么办法可以解决这个问题吗?
调用一次pygame.display.update()
即可,但需要在所有绘图完成后调用。从 WriteLine
和 loadImage
中删除 pygame.display.update()
,但在应用程序循环结束时执行单个 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()
orpygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过调用
pygame.display.update()
orpygame.display.flip()
更新显示