我正在制作一个游戏分析器,我认为如果我有一个用户界面而不是仅使用文本和原始输入进行通信,那就太好了。我在将图像“传输”到屏幕时遇到问题。
我的图像位于名为“CATANYLISER”的 pycharm 文件内,就像我的代码一样。
import pygame
pygame.init()
# py-game variables
(width, height) = (1000, 600)
window = pygame.display.set_mode((width, height))
window_title = pygame.display.set_caption('the CATANALYSER')
does_quit = False
# py-game images
empty_board = pygame.image.load('empty_board.png')
# py-game window loop
while not does_quit:
# receives input from window
for event in pygame.event.get():
# stops program when red x clicked
if event.type == pygame.QUIT:
does_quit = True
window.blit(empty_board, (0, 0))
pygame.display.update()
# activates when the loop finishes, just makes sure everything shuts down properly
pygame.quit()
预期结果是屏幕左上角的图像。但是,当我运行该程序时,我有一个空屏幕(pygame.QUIT 仍然有效)。
当我运行此代码时,没有错误消息,并且我完全不知道如何解决此问题。
最佳答案
首先,确保 empty_board.png
位于您的工作目录中。
其次,您必须在每帧之前使用 window.fill([255, 255, 255]) 清除屏幕
最后,您可以尝试使用pygame.display.flip()
而不是update()
我的代码如下所示:
import pygame
pygame.init()
window = pygame.display.set_mode([640, 480])
doQuit = False
board = pygame.image.load("empty_board.png")
while not doQuit:
window.fill([255, 255, 255])
window.blit(board, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
doQuit = True
pygame.quit()
关于python - 如何在 Pygame 中将 'blit' 图像显示到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550647/