简体   繁体   中英

Draw PNG without whitespace in pyglet

I wrote a program to render maps I make in a program named 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. 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. Is there any way to draw PNGs in pyglet without the whitespace?

With whitespaces: https://imgur.com/VlnXUP2

In tiled (intended look without dotted lines): 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. 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.

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

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

that closes it for you once you exit with

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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