无法将图像blit到屏幕

时间:2016-04-18 22:25:25

标签: python pygame blit

我正在尝试使用pygame将地图绘制到屏幕上,但无法理解为什么它会赢。我没有得到追溯。屏幕正在初始化,然后没有绘制图像。我尝试过使用相同结果的其他.bmp图像,因此我的代码中必定存在未正确排序/写入的内容。

以下是游戏的主要模块:

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图片不会被绘制?

1 个答案:

答案 0 :(得分:1)

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

要实现此目的,只需将“电路板”循环修改为以下内容:

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