使用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
答案 0 :(得分:2)
试试这个:
print(carty)
x = (cartx - carty) / 2
print(x)
y = (cartx + carty)/4*3
并且,经过测试,将convert()修改为convert_alpha(),因为在convert()上优化杀死alpha()
我还修改了y(/ 4 * 3),以考虑你的形象。这对我有用。
答案 1 :(得分:2)
您可以通过将colorkey设置为黑色来摆脱黑色三角形,我复制了您的示例并通过更改来修复它:
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)
我希望这可以帮助这个线程的任何新手!