python - 尽管与屏幕大小相同,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'
我尝试修改画布和屏幕的分辨率值,甚至修改精灵的位置。我将画布粘贴到屏幕上而不是直接绘制到屏幕上,因为如果我是对的,它应该允许我更轻松地添加滚动。
我感谢任何人可以给我的任何帮助。
解决方案
错误
self.mod.screen.surface = self.screen
变量seams 是类pygame.Surfaceself.mod.screen
的一个对象。此类的对象没有名为 的变量,这就是错误似乎来自的地方。surface
AttributeError: '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 的了解。
推荐阅读
- reactjs - 反应和表格
- c - 如何使用 GDB 访问特定的内存位置
- java - 在 Java TensorFlow 1.15 中使用 Python 构建的 TensorFlow 2.1.0 模型 | 图中没有名为 [input] 的操作
- java - 如何保护 Android 应用程序
- python - 如何在python中分配逗号分隔的元素以形成列表
- python - 如何在 Python 中打印出对应于相同值的多个键?
- python - 我有错误****KeyError: 'Weighted average' using spyder
- itext7 - 使用 itext 阅读 pdf 段落文本以及 css(颜色等)
- opengl - OpenGL 光照
- uipath - Uipath studio-修改选择器后出现数据抓取错误