简体   繁体   English

在pyglet中绘制没有空格的PNG

[英]Draw PNG without whitespace in pyglet

I wrote a program to render maps I make in a program named tiled.我编写了一个程序来渲染我在一个名为 tiled 的程序中制作的地图。 The maps have multiple layers and the program can output to.json so I wrote a program to interpret the.json file and display the map.地图有多个图层,程序可以 output 到.json 所以我写了一个程序来解释.json 文件并显示 Z90178DC81ED51244E Z90178DC8ED51244. However after I got the program to work I realized that when I draw bushes on second layer it deletes everything in the layer behind it because of the white space on the png.然而,在我让程序运行后,我意识到当我在第二层上绘制灌木丛时,由于 png 上的空白区域,它会删除后面层中的所有内容。 Is there any way to draw PNGs in pyglet without the whitespace?有没有办法在没有空格的pyglet中绘制PNG?

With whitespaces: https://imgur.com/VlnXUP2带空格: https://imgur.com/VlnXUP2

In tiled (intended look without dotted lines): https://imgur.com/xbw1K4K平铺(没有虚线的预期外观): 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()

Make sure that your.PNGs are actually transparent.确保您的.PNG 实际上是透明的。 You haven't provided the files you are loading so I cannot verify that.您尚未提供要加载的文件,因此我无法验证。

If they aren't, the easiest is to remove the background in a program like Gimp and load the files with transparent background.如果不是,最简单的方法是在 Gimp 之类的程序中删除背景并加载具有透明背景的文件。

BTW: from my testing pyglet.resource.image() is faster than pyglet.image.load() and make sure you close your json file with f.close() or use顺便说一句:根据我的测试pyglet.resource.image()pyglet.image.load()快,并确保使用f.close()关闭 json 文件或使用

with open("TILED-FILES/16x16 map2.json", "r") as file:
    map = json.load(file)

that closes it for you once you exit with一旦你退出它就会with你关闭它

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

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