简体   繁体   English

如何在 Python 中渲染基于等距图块的世界?

[英]How to render an isometric tile-based world in Python?

I'm having a bit of trouble rendering an isometric 2D tile-based world using Python and Pygame with the following code:我在使用 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)

The tile size used is 64x64;使用的图块大小为 64x64; the above code when run generates the following output:上面的代码在运行时生成以下输出:在此处输入图片说明 The tiles all have transparent edges and only the 'wall' tile is featured in this example but obviously something is going wrong as the tiles are all too far apart.瓷砖都有透明的边缘,在这个例子中只有“墙”瓷砖是特色,但显然有些地方出了问题,因为瓷砖相距太远。

I have tried reading some tutorials online but I can't seem to find one actually written in Python so please advise me as to where I am going wrong.我试过在线阅读一些教程,但我似乎找不到真正用 Python 编写的教程,所以请告诉我我哪里出错了。

Thanks in advance, Ilmiont提前致谢, Ilmiont

Try this:试试这个:

  print(carty)
  x = (cartx - carty) / 2
  print(x)
  y = (cartx + carty)/4*3

And, after test, modify convert() to convert_alpha(), because optimisation kill alpha on convert()并且,经过测试,将convert()修改为convert_alpha(),因为优化杀死了convert()上的alpha

I also modify y ( /4*3 ), to take in account your image.我还修改了 y ( /4*3 ),以考虑您的形象。 It's work for me.这对我有用。

You can get rid of the black triangles by setting the colorkey to black, I copied your example and was able to fix it by changing:您可以通过将颜色键设置为黑色来摆脱黑色三角形,我复制了您的示例并能够通过更改来修复它:

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

to

for row in map:
  for tile in row:
    tileImage = wall
    tileImage.set_colorkey((0,0,0))

Found from: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey来自: https : //www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey

There is probably a better way to set the colorkey for all, but I tried this and it seems to work.可能有更好的方法来为所有人设置色键,但我试过了,它似乎有效。 I see the comment about about alpha, I'm sure that works better too.我看到了关于 alpha 的评论,我相信它也能更好地工作。

In regards to positioning, You would just need to start rendering from the middle of the screen.关于定位,您只需要从屏幕中间开始渲染。 I was able to do this with your code by simple changing:我可以通过简单的更改来使用您的代码执行此操作:

x = cartx - carty
print(x)
y = (cartx + carty) / 2

to

x = 320 + ((cartx - carty) / 2)
y = ((cartx+carty) / 4 * 3)

I hope this helps any newcomers to this thread!我希望这对这个线程的任何新人都有帮助!

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

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