如何在Python中渲染基于等距平铺的世界?

如何在Python中渲染基于等距平铺的世界?,python,pygame,render,tile,isometric,Python,Pygame,Render,Tile,Isometric,我在使用Python和Pygame以及以下代码渲染基于等轴测2D平铺的世界时遇到了一些问题: ''' Map Rendering Demo rendermap.py By James Walker (trading as Ilmiont Software). Copyright (C)Ilmiont Software 2013. All rights reserved. This is a simple program demonstrating rendering a 2D map in P

我在使用Python和Pygame以及以下代码渲染基于等轴测2D平铺的世界时遇到了一些问题:

'''
Map Rendering Demo
rendermap.py
By James Walker (trading as Ilmiont Software).
Copyright (C)Ilmiont Software 2013. All rights reserved.

This is a simple program demonstrating rendering a 2D map in Python with Pygame from a list of map data.
Support for isometric or flat view is included.
'''

import pygame
from pygame.locals import *

pygame.init()

DISPLAYSURF = pygame.display.set_mode((640, 480), DOUBLEBUF)    #set the display mode, window title and FPS clock
pygame.display.set_caption('Map Rendering Demo')
FPSCLOCK = pygame.time.Clock()

map_data = [
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]
]               #the data for the map expressed as [row[tile]].

wall = pygame.image.load('wall.png').convert()  #load images
grass = pygame.image.load('grass.png').convert()

tileWidth = 64  #holds the tile width and height
tileHeight = 64

currentRow = 0  #holds the current map row we are working on (y)
currentTile = 0 #holds the current tile we are working on (x)

for row in map_data:    #for every row of the map...
    for tile in row:
        tileImage = wall

        cartx = currentTile * 64    #x is the index of the currentTile * the tile width
        print(cartx)
        carty = currentRow * 64     #y is the index of the currentRow * the tile height
        print(carty)
        x = cartx - carty
        print(x)
        y = (cartx + carty) / 2
        print(y)
        print('\n\n')
        currentTile += 1    #increase the currentTile holder so we know that we are starting rendering a new tile in a moment

        DISPLAYSURF.blit(tileImage, (x, y)) #display the actual tile
    currentTile = 0 #reset the current working tile to 0 (we're starting a new row remember so we need to render the first tile of that row at index 0)
    currentRow += 1 #increment the current working row so we know we're starting a new row (used for calculating the y coord for the tile)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

    pygame.display.flip()
    FPSCLOCK.tick(30)
使用的瓷砖尺寸为64x64;运行时,上述代码生成以下输出: 瓷砖都有透明的边缘,在本例中只有“墙”瓷砖,但显然有些地方出了问题,因为瓷砖相距太远

我试着在网上阅读一些教程,但我似乎找不到一个真正用Python编写的教程,所以请告诉我哪里出了问题

提前感谢,, Ilmiont

试试这个:

  print(carty)
  x = (cartx - carty) / 2
  print(x)
  y = (cartx + carty)/4*3
测试后,将convert()修改为convert_alpha(),因为优化会在convert()上杀死alpha


我还修改了y(/4*3),以考虑您的图像。这对我来说很有用。

您可以通过将colorkey设置为黑色来消除黑色三角形,我复制了您的示例,并通过更改以下内容来修复它:

for row in map_data:    #for every row of the map...
for tile in row:
    tileImage = wall
x = cartx - carty
print(x)
y = (cartx + carty) / 2

可从以下地址找到:

可能有更好的方法来设置所有颜色的关键点,但我尝试了这个,它似乎工作。我看到了关于alpha的评论,我相信这也会更好

关于定位,您只需要从屏幕中间开始渲染。我可以通过简单的更改对您的代码执行此操作:

for row in map_data:    #for every row of the map...
for tile in row:
    tileImage = wall
x = cartx - carty
print(x)
y = (cartx + carty) / 2


我希望这有助于任何新来此线程

好吧,这样效果更好,但我现在得到了这个结果:黑色三角形(部分)保留。你确定那些黑色部分是透明的吗?你能在附件中发布你的map.png吗?您也可以尝试:wall=pygame.image.load('wall.png')。convert_alpha()这是我正在演示的“wall.png”互动程序:哦,对不起。。。A.H是对的;我没有转换成阿尔法。因此,现在贴图渲染正确,但它仍然不是真正正确的形状。。。如何在屏幕中央渲染它???