[英]Draw PNG without whitespace in pyglet
我编写了一个程序来渲染我在一个名为 tiled 的程序中制作的地图。 地图有多个图层,程序可以 output 到.json 所以我写了一个程序来解释.json 文件并显示 Z90178DC81ED51244E Z90178DC8ED51244. 然而,在我让程序运行后,我意识到当我在第二层上绘制灌木丛时,由于 png 上的空白区域,它会删除后面层中的所有内容。 有没有办法在没有空格的pyglet中绘制PNG?
带空格: https://imgur.com/VlnXUP2
平铺(没有虚线的预期外观): https://imgur.com/xbw1K4K
import pyglet
import pyglet.gl as gl
import json
# read json file and specifically get the "data","height","width"
f = open("TILED-FILES/16x16 map2.json","r")
map = json.load(f)
layers = map['layers']
screen = pyglet.window.Window(resizable=True)
background = pyglet.image.load('/Users/naghs/srcHome/game/images/grass.png')
background.anchor_x = background.width // 2
background.anchor_y = background.height // 2
def center_image(img):
img.anchor_x = img.width // 2
img.anchor_y = img.height // 2
def formatMap(alist,width,height):
matrix = []
j1 = []
for j in range(height):
j1 = []
for i in range(width):
j1.append(alist[i*j])
pass
matrix.append(j1)
return matrix
def drawTile(x,y,i=int,scale = 2):
if i == 0:
return
tile = pyglet.image.load(f'tiny 16 basic tilesets/basictiles/basictiles{i}.png')
width = 16 * scale
height = 16 * scale
texture = tile.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
texture.width = width # resize from 8x8 to 16x16
texture.height = height
texture.blit(x,y)
center_image(tile)
texture.blit(x,y)
pass
def draw_layer(layer,scale=2):
map = layer["data"]
height = layer['height']
width = layer['width']
tile = 16 * scale
map = formatMap(map,width,height)
print(map)
y = 0
for y1 in range(0,height*tile,tile):
x = 0
for x1 in range(0,width*tile,tile):
print(map[y][x])
drawTile(x1,y1,i=map[y][x],scale=scale)
x += 1
y += 1
# Old
def draw_background():
tile = background.width
for x in range(0,screen.width+tile,tile):
for y in range(0,screen.height+tile,tile):
background.blit(x,y)
@screen.event
def on_draw():
screen.clear()
draw_layer(layers[0])
draw_layer(layers[1])
pyglet.app.run()
确保您的.PNG 实际上是透明的。 您尚未提供要加载的文件,因此我无法验证。
如果不是,最简单的方法是在 Gimp 之类的程序中删除背景并加载具有透明背景的文件。
顺便说一句:根据我的测试pyglet.resource.image()
比pyglet.image.load()
快,并确保使用f.close()
关闭 json 文件或使用
with open("TILED-FILES/16x16 map2.json", "r") as file:
map = json.load(file)
一旦你退出它就会with
你关闭它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.