Pygame 在 blitting 期间返回锁定错误

Pygame returning locking error during blitting

我正在尝试在 pygame 中制作一个 2D 游戏,并且有一个具有表面属性的相机 class,每次更新相机时,它们的表面都会更新。所有图形都被 blits 到 game.worldSurface,然后主摄像头拍摄一张照片并将其 blits 到显示表面。但是,当使用其他相机时,我无法 blit 到世界表面并出现锁定错误。我试过.unlock()。是什么原因造成的?

import pygame
import pickle
class Tileset:
    def __init__(self, location):
        pass

class Tilemap:
    def __init__(self):
        pass

class Collisionmap(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

class Player(pygame.sprite.Sprite):
    def __init__(self, spritesheet):
        super().__init__()
        self.spritesheet = pygame.image.load(spritesheet)
        self.x = 0
        self.y = 0
    def draw(self, surface):
        surface.blit(self.spritesheet, (self.x, self.y))

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

class Camera:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.width = 100
        self.height = 100
        self.surface = pygame.Surface((self.width, self.height))
    def moveToSprite(self, sprite):
        self.x = sprite.rect.centerx - WIDTH // 2
        self.y = sprite.rect.centery - HEIGHT // 2
    def update(self, world):
        self.surface = world.subsurface((self.x, self.y, self.width, self.height))

class Level:
    def __init__(self, terrarin, collision, mobs):
        self.terrain = terrain
        self.collision = collision
        self.mobs = mobs

class Game:
    def __init__(self):
        pygame.init()
        self.DISPLAYSURF = pygame.display.set_mode((0, 0))
        self.mainCamera = Camera()
        self.mainCamera.width = self.DISPLAYSURF.get_width()
        self.mainCamera.height = self.DISPLAYSURF.get_height()
        self.otherCameras = []
        self.worldSurface = pygame.Surface((10000, 10000))
        self.player = Player("marioSS.jpg")
        self.otherCameras.append(Camera())
        self.run()
    def run(self):
        while True:
            for event in pygame.event.get():
                pass
            self.earlyUpdate()
            self.update()
            self.lateUpdate()
            self.graphicsUpdate()
    def update(self):
        pass
    def earlyUpdate(self):
        pass
    def lateUpdate(self):
        pass
    def graphicsUpdate(self):
        for each in self.otherCameras:
            each.update(self.worldSurface)
        self.player.draw(self.worldSurface)
        self.otherCameras[0].surface.unlock()
        self.worldSurface.unlock()
        self.worldSurface.blit(self.otherCameras[0].surface, (100, 100)) ##Error here
        self.mainCamera.update(self.worldSurface)
        self.DISPLAYSURF.blit(self.mainCamera.surface, (0, 0))
        pygame.display.update()

x = Game()

问题使得 world.subsurface()Camera.update()

它不会将数据从 world 复制到 surface,但会分配对原始 world 的访问权限。稍后您有:camera.surface 保持对 world 的访问权限,并且 blit 尝试从 camera.surface 复制到 world - 所以最后它尝试从 worldworld。也许它会锁定它。

但是如果在 Camera.update() 你使用 .copy()

self.surface = world.subsurface((self.x, self.y, self.width, self.height)).copy()

或blit它

self.surface.blit(world.subsurface((self.x, self.y, self.width, self.height)), (0,0))

然后就可以了。


文档:subsurface

subsurface(Rect) -> Surface

Returns a new Surface that shares its pixels with its new parent.