2013-12-17 94 views
2
我有一點麻煩渲染使用Python和pygame的用下面的代碼的等距-基於區塊2D世界

如何在Python中渲染基於瓦片的等距世界?

''' 
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) 

使用的瓦片大小爲64×64;上面的代碼在運行時會生成以下輸出: enter image description here 這些瓷磚都具有透明的邊緣,只有「牆」瓷磚在此示例中顯示,但顯然由於瓷磚距離太遠而出現問題。

我曾嘗試在線閱讀一些教程,但我似乎無法找到一個實際用Python編寫的東西,所以請告訴我我要出錯的地方。

由於提前, Ilmiont

回答

2

試試這個:

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

而且,測試,修改()轉換到convert_alpha(),因爲轉換優化殺α()

我也修改y(/ 4 * 3),以考慮你的形象。這對我有用。

+0

那好,但我現在得到這個結果: http://i.stack.imgur.com/XJgBC.png所以黑色三角形(部分)仍然存在。 – Ilmiont

+0

你確定這些黑色部分是透明的嗎? – Matthias

+0

你可以在附件中發佈你的map.png嗎?你也可以嘗試:wall = pygame.image.load('wall.png')。convert_alpha() –

2

您可以通過色鍵設置爲黑色擺脫黑色三角形的,我複製你的榜樣,並能夠通過改變來解決它:

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

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

從實測: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey

可能有更好的方法來設置所有的colorkey,但我試過這個,它似乎工作。我看到關於alpha的評論,我相信它也可以更好地發揮作用。

關於定位,您只需要從屏幕中間開始渲染。我能夠通過簡單的改變你的代碼要做到這一點:

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

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

我希望這可以幫助任何初來乍到這個線程!