Python - 重新打开 pygame 应用程序后黑屏

Python - Black screen afther re-opening pygame application

我用 python pygame 和 android 子集创建了一个简单的基于图块的游戏。 当我在 android 设备上退出应用程序并重新打开它时,我看到黑屏,如果我从 'tabs' 关闭程序,那么游戏将再次运行,有人知道这是什么问题吗?我必须在程序中添加一些代码吗?

from pygame.locals import *
from settings import *
from sprites import *
import pygame
import time
import sys
import os

try:
    import pygame_sdl2
    pygame_sdl2.import_as_pygame()
except ImportError:
    pass

pygame.init()
pygame.display.set_caption(TITLE)

class Game:

    def __init__(self):
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))

        self.up_button = pygame.Rect(1000, 400, 100, 100)
        self.down_button = pygame.Rect(1000, 600, 100, 100)
        self.right_button = pygame.Rect(1100, 500, 100, 100)
        self.left_button = pygame.Rect(900, 500, 100, 100)

        self.all_sprites = pygame.sprite.Group()
        self.walls = pygame.sprite.Group()
        self.player = Player(self, 2, 2)

        for x in range(5, 10):
            Wall(self, x, 3)
        for x in range(5, 10):
            Wall(self, x, 7)
        for y in range(3, 8):
            Wall(self, 10, y)

        self.buttons = pygame.sprite.Group()
        self.game_over = False

    def update(self):
        self.all_sprites.update()

    def draw(self):
        self.screen.fill(BLACK)
        self.draw_grid()
        self.all_sprites.draw(self.screen)
        pygame.draw.rect(self.screen, GREEN, self.up_button)
        pygame.draw.rect(self.screen, RED, self.down_button)
        pygame.draw.rect(self.screen, BLUE, self.right_button)
        pygame.draw.rect(self.screen, YELLOW, self.left_button)
        pygame.display.update()

    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                if self.up_button.collidepoint(mouse_pos):
                    self.player.move(dy = -1)
                elif self.down_button.collidepoint(mouse_pos):
                    self.player.move(dy = 1)
                elif self.right_button.collidepoint(mouse_pos):
                    self.player.move(dx = 1)
                elif self.left_button.collidepoint(mouse_pos):
                    self.player.move(dx = -1)

    def run(self):
        self.title_menu()
        while not self.game_over:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pygame.draw.line(self.screen, LIGHTGRAY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pygame.draw.line(self.screen, LIGHTGRAY, (0, y), (WIDTH, y))

    def title_menu(self):
        pass

if __name__ == '__main__':
    game = Game()
    game.run()

由于某些我不太明白的原因,看起来对屏幕的引用(您存储在 Game 对象的 screen 属性 中)是在 Android main Activity 失去焦点后丢失。一个可能的原因可能与 EGL Context Lost 有关,尽管我没有重要的元素来证实这个假设。

尽管造成这个问题的原因很多,但我想我已经找到了解决办法。首先,您需要在应用程序返回前台时捕获事件(有关如何通过特定 SDL2 事件执行此操作的更多详细信息,请查看 this answer),然后恢复屏幕引用。

以下是对我有用的方法,适用于您的 events 方法:

def events(self):
    for event in pygame.event.get():
        if event.type == 261:
            # 261 is SDL_APP_WILLENTERFOREGROUND event code
            self.screen = pygame.display.set_mode((WIDTH, HEIGHT))

        # ...