【问题标题】:Pygame returning locking error during blittingPygame在blitting期间返回锁定错误
【发布时间】:2020-06-17 23:21:19
【问题描述】:

我正在尝试在 pygame 中制作 2D 游戏,并且有一个具有表面属性的相机类,并且每次更新相机时,它们的表面都会更新。所有的图形都被传送到game.worldSurface,然后主相机拍摄一张照片并将其传送到显示表面。但是,当使用其他相机时,我无法进入世界表面并出现锁定错误。我试过.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()

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。

标签: python pygame pygame-surface


【解决方案1】:

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

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

但是如果在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

次表面(矩形)-> 表面

返回一个与其新父级共享其像素的新 Surface。

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 2022-01-02
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2023-03-19
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多