我是一個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