我是一个17岁的程序员,试图用pygame编写python中的等距游戏。在完成一个平铺引擎后,使用不好看的gimp-drawn PNG工作,我想知道,是否可以通过纹理渲染一些Tiles。我希望我提供了所有需要理解的内容,我的问题是什么,请原谅我的不完美的英语。Python,Pygame,图像处理:Restretch加载PNG,成为等轴测图的纹理
只要是我想做的事情,就是要产生一个等距瓷砖的128×128像素宽度图像,使用下面的图片作为纹理块的所有三个方面:
(这里的环节,因为我还没有允许把图片中,由于这是我的第一篇)
更好地说明,我想要做的,我得出这个小图片:
我已经在互联网上搜索了大约2个小时,但没有找到解决方案,除了平铺的顶部,这里是我已经得到的代码:
这是图像操作模块,在transformToRightPart()是在我需要帮助的方法:
import pygame
class Image(object):
'''
Use this Module to create Tiles by Texture to use them later in the Tileengine.
It is important to run pygame.init() before creating objects of this class!
Contains unfinished Elements!
'''
def __init__(self, path):
self.loadFromPath(path)
def getIMG(self):
assert self.originalIMG is not None, "No picture to return"
if not self.IMG == None:
return self.IMG
else:
return self.originalIMG
def loadFromPath(self, path):
'''
Don't do convert() or convert_alpha() here,
as Objects of this class are created during the loading process,
with no pygame.display() created.
'''
self.originalIMG = pygame.image.load(path)
self.IMG = None
def transformToTopPart(self):
'''
Transforms the loaded Image to the Top Part of an Isometric Tile, with the Dimensions 2:1,
said in Pixels: 128 px Width by 64 px Height.
'''
self.IMG = pygame.transform.rotate(self.originalIMG, 45)
self.IMG = pygame.transform.scale(self.IMG, (128, 64))
def transformToRightPart(self):
'''
TODO!! Don't ask how (X.X)
Transforms the loaded Image to the right Part of an Isometric Tile.
'''
assert False, "This method isn't finished, try something different ;)"
def transformToLeftPart(self):
'''
Transforms the loaded Image to the left Part of an Isometric Tile.
Due to the nice geometric fact, that the shape of the left part,
is just the flipped right part shape and we don't lose quality by flipping,
we do this little trick, to enshorten the code.
'''
self.originalIMG = pygame.transform.flip(self.originalIMG, True, False)
self.transformToRightPart()
self.IMG = pygame.transform.flip(self.IMG, True, False)
self.originalIMG = pygame.transform.flip(self.originalIMG, True, False)
这是模块,它与瓷砖创建一个窗口来呈现:
import pygame, sys
from ImageManipulation import Image
from pygame.locals import *
if __name__ == '__main__':
pygame.init()
FPS=20
fpsClock = pygame.time.Clock()
picture = Image("Stone_Floor_texture.png")
picture.transformToTopPart()
DISPLAY = pygame.display.set_mode((400,400),0,32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAY.blit(picture.getIMG(),(0,0))
pygame.display.update()
fpsClock.tick(FPS)
代码的输出是这样的:
我想要实现的是,它看起来,像这样:
请参阅[2D钻石(等距)地图编辑器 - 纹理无限延伸?](https://stackoverflow.com/a/36454198/2521214)的一些想法。是的,它可以用无缝纹理完成。通过将像素复制到等距平面视图中,使用3D渲染或2D。在后者中,您需要定义精灵的3个区域并将纹理像素映射到它。我不使用Python,所以我不能告诉你的代码有什么问题,但使用旋转看起来像矫枉过正,你只需交换基本向量... – Spektre
非常感谢Spektre,用于改变我的问题链接到图片,并感谢您提到的文章,对我的proplem没有任何帮助,但是虽然很有趣! :D - 其实我是绑定到python的,所以不幸的是我不能使用3D渲染引擎。 – Blarify
你有直接的像素访问?否则你将需要构建你的精灵与旋转翻译和裁剪/掩码,你需要知道坐标系... – Spektre