简体   繁体   English

有没有更好的方法在 python 中生成等距 map 网格?

[英]Is there a better way to generate an isometric map grid in python?

def map_grid(obj, startx, starty, map_size):
    grid = []
    for n in range(map_size[1]):
        startx -= 32
        starty += 16
        grid.append([[startx, starty]])
    for n in range(map_size[0]):
        startx = grid[n][0][0]
        starty = grid[n][0][1]
        for n in range(map_size[0]):
            startx += 32
            starty += 16
            grid[n].append([startx, starty])
    for n in range(map_size[1]):
        for m in range(map_size[0]):
            main_window.blit(obj.src, (grid[m][n][0], grid[m][n][1]))

Obj is the image to be displayed(In this case, green tiles), startx & starty are the starting cartesian coordinates, map_size is the grid size in '(row, column)' format. Obj 是要显示的图像(在本例中为绿色图块),startx 和 starty 是起始笛卡尔坐标,map_size 是 '(row, column)' 格式的网格大小。

When I input mapsize as (10,10), I expected a 10x10 isometric map but instead i am getting this, adbnormal map which is 11x9 with an extra 10th coordinate in the first of the 11 rows.(Please refer to image for better understanding.)当我将 mapsize 输入为 (10,10) 时,我期望一个 10x10 等距 map 但我得到了这个, adbnormal map它是 11x9,在 11 行的第一行中有一个额外的第 10 个坐标。(请参阅图片以更好地理解.)

I apologize this isn't in python, but the way I do this in Swift is as below (should be easy to map to python, I've made it a little pseudo-codey) I apologize this isn't in python, but the way I do this in Swift is as below (should be easy to map to python, I've made it a little pseudo-codey)

for y in 0..<mapHeight {
    for x in 0..<mapWidth {
        drawTile(x: (y * tileWidth) - (x * tileWidth), y: (y * tileHeight) + (x * tileHeight))
    }
}

Thanks for you question though, it helped me simplify my own algorithm!不过,感谢您的提问,它帮助我简化了自己的算法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM