首页 > 解决方案 > 尽管与屏幕大小相同,pygame 表面在中间被切断

问题描述

screen = pygame.display.set_mode((WIDTH,HEIGHT))
canvas = pygame.Surface((WIDTH,HEIGHT))
def makefunnytiles(saice):
    global WIDTH
    global HEIGHT
    global screen
    global roomdata
    global tiledefinitions
    print("Baking Tiles")
    print(roomdata)
    index = 0
    index2 = 0
    for symbolic in saice:
        index2 = 0
        symbolic = symbolic.strip()
        print("sanity")
        for symbol in list(symbolic):
            print(symbol)
            if symbol in tiledefinitions:
                canvas.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2),(32 * index)))
            index2 += 1
        index += 1
    screen.blit(canvas, (0,0))
    pygame.display.flip()
    print("drew screen")
    print(str(canvas.get_width))

我遇到了一个问题,由于某种原因,画布在屏幕中间被切断了。

切断的图像

我遇到的另一个问题是pgzrun.go()在文件末尾,这会导致程序因以下错误而崩溃:

Traceback (most recent call last):
  File "C:\Users\RushVisor\Documents\Glamore\main.py", line 100, in <module>
    pgzrun.go()
  File "E:\Python39\lib\site-packages\pgzrun.py", line 31, in go
    run_mod(mod)
  File "E:\Python39\lib\site-packages\pgzero\runner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 217, in run
    self.mainloop()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 225, in mainloop
    self.reinit_screen()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 73, in reinit_screen
    self.mod.screen.surface = self.screen
AttributeError: 'pygame.Surface' object has no attribute 'surface'

我尝试修改画布和屏幕的分辨率值,甚至修改精灵的位置。我将画布粘贴到屏幕上而不是直接绘制到屏幕上,因为如果我是对的,它应该允许我更轻松地添加滚动。

我感谢任何人可以给我的任何帮助。

编辑:代码在这里https://paste.pythondiscord.com/tetogequze.py

标签: pythonpgzero

解决方案


错误

self.mod.screen.surface = self.screen

变量seams 是类pygame.Surfaceself.mod.screen的一个对象。此类的对象没有名为 的变量,这就是错误似乎来自的地方。surfaceAttributeError: 'pygame.Surface' object has no attribute 'surface'

如果您不想实现具有表面属性的额外类,这应该可以解决问题:self.mod.screen = self.screen。但是不要忘记这不会复制表面self.screen,如果你想要你需要这样的东西:self.mod.screen = self.screen.copy()

滚动

您的想法是实现滚动的一种可能方式。另一个是您存储某种来源并基于该来源的瓷砖。如果你现在想滚动,你只需要改变你之前定义的这个原点。

原产地定义

origin = [10,10]  # or use pygame.math.Vector2 or something else

位代码

index = 0
index2 = 0
for symbolic in saice:
    index2 = 0
    symbolic = symbolic.strip()
    print("sanity")
    for symbol in list(symbolic):
        print(symbol)
        if symbol in tiledefinitions:
            screen.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2)+origin[0],(32 * index)+origin[1]))
        index2 += 1
    index += 1

现在您只需要每帧调用一次 blit 代码。如果您现在想要滚动,只需执行以下操作:(origin[0] += 10将所有符号向右移动)

上面的 blit 代码不是最快的,因为它需要在每一帧上加载所有图像,但这只是滚动如何与其他想法一起工作的一个示例

编辑:我对 Pygame-Zero 不是很熟悉,所以这只是基于我对标准 Pygame 的了解。


推荐阅读