python - 如何在 Pygame 中将 'blit' 图像显示到屏幕上

标签 python pygame

我正在制作一个游戏分析器,我认为如果我有一个用户界面而不是仅使用文本和原始输入进行通信,那就太好了。我在将图像“传输”到屏幕时遇到问题。

我的图像位于名为“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/

相关文章:

python - 如何获取嵌套字典中特定键值的级别

python - Numpy:在没有循环的情况下获取索引内的最小值?

python - 如何识别所有库 Rpy2 R

Python 2.7 多重处理

python - 声音文件不在pygame中播放

python - Pygame 能在图形方面做哪些 wxPython 做不到的事情?

python - 类型错误 : must be pygame. 表面,而不是元组。 Python/Pygame 菜鸟

python - 为什么我在 python 3.6 的 jupyter Notebook 中收到警告以及如何修复它们?

python - Pygame 和类

python - 我如何让这个文本动画在pygame中显示