平铺但不在游戏中的透明瓷砖

时间:2017-01-27 06:26:45

标签: python python-3.x pygame tiled pytmx

在平铺地图编辑器中,外部图块显示为透明。但是,当我显示地图时,应该是透明的瓷砖部分显示为黑色。

平铺:https://gyazo.com/81ad813f35f9f0f2907b4c26f7ab4619

在Python中:https://gyazo.com/4030d918b7c727cfb48ee70343226352

mainGame.py

import pygame
from settings import *
from Loading import *
from characters import *
import pytmx

pygame.init()

class game():

    def __init__(self):

        self.gameRunning = True
        self.gameWindow = pygame.display.set_mode((gameWindowWidth, gameWindowHeight))
        self.clock = pygame.time.Clock()
        self.playerSpeed = speed
        self.preloadedData = load()

    def update(self):

        pygame.display.set_caption("{:.2f}".format(self.clock.get_fps()))
        #self.gameWindow.blit(self.preloadedData["distantCity"], (0,0))
        #self.gameWindow.blit(self.preloadedData["ninjaIdle01"], (player1.playerX, player1.playerY))
        self.gameWindow.fill(white)
        self.gameWindow.blit(self.map_img, (0, 400))
        pygame.display.update()

    def loadMap(self):

        self.map = tiledMap()
        self.map_img = self.map.make_map()

    def gameLoop(self):

        self.clock.tick(FPS)
        self.event()
        self.loadMap()
        self.update()

    def event(self):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.gameRunning = False

            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                self.playerSpeed = 5
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                self.playerSpeed = -5
            if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
                self.playerSpeed = 0
            if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
                self.playerSpeed = 0

        player1.playerX = player1.playerX + self.playerSpeed

playGame = game()
player1 = player()
while playGame.gameRunning == True:
    playGame.gameLoop()


pygame.quit()
quit()

Loading.py

class tiledMap():
    def __init__(self):
        self.gameMap = pytmx.load_pygame("maps\m_gameMapPNGNA.tmx", pixelAlpha = True)
        self.mapwidth = self.gameMap.tilewidth * self.gameMap.width
        self.mapheight = self.gameMap.tileheight * self.gameMap.height

    def render(self, surface):
        for layer in self.gameMap.visible_layers:
            for x, y, gid in layer:
                tile = self.gameMap.get_tile_image_by_gid(gid)
                surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))

    def make_map(self):
        mapSurface = pygame.Surface((1280, 320))
        self.render(mapSurface)
        return mapSurface

m_gameMapPNGNA.tmx

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="left-up" width="20" height="5" tilewidth="64" tileheight="64" nextobjectid="1">
 <tileset firstgid="1" name="m_pngTilesNA" tilewidth="64" tileheight="64" tilecount="6" columns="0">
  <tile id="0">
   <image width="64" height="64" source="../gameImages/m_PNG/m_dirtLeftFIX.png"/>
  </tile>
  <tile id="1">
   <image width="64" height="64" source="../gameImages/m_PNG/m_dirtMiddleFIXNA.png"/>
  </tile>
  <tile id="2">
   <image width="64" height="64" source="../gameImages/m_PNG/m_dirtRightFIX.png"/>
  </tile>
  <tile id="3">
   <image width="64" height="64" source="../gameImages/m_PNG/m_grassLeftFIX.png"/>
  </tile>
  <tile id="4">
   <image width="64" height="64" source="../gameImages/m_PNG/m_grassMiddleFIXNA.png"/>
  </tile>
  <tile id="5">
   <image width="64" height="64" source="../gameImages/m_PNG/m_grassRightFIX.png"/>
  </tile>
 </tileset>
 <layer name="Tile Layer 1" width="20" height="5">
  <data encoding="csv">
4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3
</data>
 </layer>
</map>

0 个答案:

没有答案