gpt4 book ai didi

python - Pygame 在 blitting 期间返回锁定错误

转载 作者:行者123 更新时间:2023-12-01 15:00:51 25 4
gpt4 key购买 nike

我正在尝试在 pygame 中制作一个 2D 游戏,并且有一个具有表面属性的相机类,每次相机更新时,它们的表面都会更新。所有图形都被 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 - 所以最后它尝试从 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

subsurface(Rect) -> Surface

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

关于python - Pygame 在 blitting 期间返回锁定错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com