2013-12-17 6 views
2
나는 다음과 같은 코드를 파이썬과 파이 게임을 사용하여 등각 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) 

사용되는 타일 크기는 64 × 64이다; 위의 코드를 실행하면 다음과 같은 출력이 생성됩니다. enter image description here 타일은 모두 투명 에지가 있으며이 예제에서는 'wall'타일 만 사용되지만 타일이 너무 멀리 떨어져 있으므로 분명히 잘못된 것이 있습니다.

온라인 튜토리얼을 읽으려고 시도했지만 실제로 파이썬으로 작성된 튜토리얼을 찾을 수 없으므로 어디서 잘못되었는지 조언 해주십시오. 사전에

감사합니다, Ilmiont

답변

2

이 시도 : convert_alpha()로 변환()를 수정, 테스트 후,

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

을 그리고 변환에 최적화 킬 알파()

때문에 또한 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

아마도 모두를 위해 컬러 키를 설정하는 더 좋은 방법이 있지만이 방법을 사용하면 효과가있는 것 같습니다. 알파에 대한 의견을 보았습니다.

위치 지정과 관련하여 화면 가운데에서 렌더링을 시작하면됩니다. 나는 간단한 변경하여 코드와 함께이 작업을 수행 할 수 있었다 :

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

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

에 I는이 스레드에 어떤 초보자 도움이되기를 바랍니다!

관련 문제