首页 > 解决方案 > Pygame 输出:黑色背景

问题描述

我想查看我的blit()但是一旦我添加了self.screen_dim.fill([255, 255, 255]) pygame.display.update()它重叠的线

def __init__(self):       
self.width = 500
self.height = 500    
self.screen_dim = pygame.display.set_mode((self.width, self.height))
self.mole = pygame.image.load("mole.png")
self.mole_hit = pygame.image.load("mole-hit.png")



    

我对如何让我的pygame屏幕感到困惑,该功能中的图像正在工作,但是如果我添加了新的背景颜色,它会重叠

def start(self):

    stat = True
    num_display = 1
    x_array = []
    y_array = []

    for i in self.mole_pos:
        x_array.append(i[0])
        y_array.append(i[1])
    

     while stat:
        Rand_Pos = random.randint(0, 8)
        if num_display == 1:
            self.screen_dim.blit(self.mole, (x_array[Rand_Pos], y_array[Rand_Pos]))

        for Event in pygame.event.get():
            if Event.type == pygame.QUIT:
                stat = False
            if self.mouse_clicked(mouse.get_pos(), self.mole_pos[Rand_Pos]):
                num_display = 0
                self.screen_dim.blit(self.mole_hit, (x_array[Rand_Pos], y_array[Rand_Pos]))
                continue

    pygame.display.flip()

标签: pythonpygame

解决方案


这没什么关系class。这只是一个缩进的问题。您必须绘制self.mole_hit应用程序循环而不是事件循环:

def start(self):
    # [...]

    while stat:
        # [...]

        for Event in pygame.event.get():
            if Event.type == pygame.QUIT:
                stat = False
            
        #<--| INDENTATION
    
        if self.mouse_clicked(mouse.get_pos(), self.mole_pos[Rand_Pos]):
            num_display = 0
            self.screen_dim.blit(self.mole_hit, (x_array[Rand_Pos], y_array[Rand_Pos]))

        pygame.display.flip()

推荐阅读