无法将图像传输到屏幕

Failing to blit image to screen

我正在尝试使用 pygame 将地图绘制到屏幕上,但不明白为什么不能。我没有得到追溯。屏幕正在初始化,然后没有绘制图像。我尝试过使用其他 .bmp 图像得到相同的结果,所以我的代码中肯定有某些地方 ordered/written 不正确。

这是游戏的主要模块:

import pygame
import sys
from board import Board

def run_game():

    #Launch the screen.

    screen_size = (1200, 700)
    screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption('Horde')


    #Draw the board.
    game_board = Board(screen)
    game_board.blit_board()

    #Body of the game.
    flag = True
    while flag == True:
        game_board.update_board()

run_game()

这是您看到正在使用的电路板模块。具体来说,blit_board() 函数,它默默地无法绘制我要求的 map.bmp 文件(文件在同一目录中)。

import pygame
import sys

class Board():

    def __init__(self, screen):
        """Initialize the board and set its starting position"""
        self.screen = screen

        #Load the board image and get its rect.
        self.image = pygame.image.load('coll.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #Start the board image at the center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

    def blit_board(self):
        """Draw the board on the screen."""
        self.screen.blit(self.image, self.rect)



    def update_board(self):
        """Updates the map, however and whenever needed."""

        #Listens for the user to click the 'x' to exit.
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        #Re-draws the map.
        self.blit_board()

我得到的只是黑屏。为什么 map.bmp 图片无法绘制?

正如 Dan Mašek 所说,您需要告诉 PyGame 在绘制图像后更新显示。

要实现此目的,只需将 'board' 循环修改为以下内容:

def update_board(self):
    """Updates the map, however and whenever needed."""

    #Listens for the user to click the 'x' to exit.
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    #Re-draws the map.
    self.blit_board()

    pygame.display.update()