简体   繁体   English

如何在 Pygame 中将图像“blit”到屏幕上

[英]How to 'blit' the image to the screen in Pygame

I'm making a game analyser, and I thought it would be nice if I had a user iterface instead of just using text and raw input to communicate.我正在制作一个游戏分析器,我认为如果我有一个用户界面而不是仅仅使用文本和原始输入进行交流会很好。 I am having problems with 'blitting' an image to my screen.我在将图像“闪烁”到屏幕上时遇到问题。

My image is inside the pycharm file called 'CATANYLISER' as is my code.我的图像和我的代码一样位于名为“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()

The expected result is the image on the screen in the top left corner.预期的结果是屏幕左上角的图像。 However when I run the program, I have an empty screen (pygame.QUIT still works).然而,当我运行程序时,我有一个空屏幕(pygame.QUIT 仍然有效)。

When I run this code there is no error message, and I am completely lost about how to fix this.当我运行此代码时,没有错误消息,我完全不知道如何解决这个问题。

first, make sure that the empty_board.png is in your working directory.首先,确保empty_board.png在您的工作目录中。

Second, you have to clear the screen before each frame using window.fill([255, 255, 255])其次,您必须使用window.fill([255, 255, 255])在每一帧之前清除屏幕

Finally, you could try using pygame.display.flip() instead of update()最后,您可以尝试使用pygame.display.flip()而不是update()

My code would look like:我的代码看起来像:

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()


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM