我正在尝试在pygame中显示平铺地图,我想使用png,因为它们是透明的,但是当我将瓷砖blit到窗口上时,4/6不同的瓷砖显示正常,但是2个瓷砖不显示(草在顶部和中心的污垢)。当我尝试用jpegs做同样的事情时,地图显示正常,但由于它们不透明,我不想使用它们。
这就是jpegs显示的内容:https://gyazo.com/f4bcd87763ebdcb718759cdc3e5132ba
这是pngs显示的内容:https://gyazo.com/df286b8d6f23d0f622de097eb423ea7c
此外,我收到垃圾邮件:“libpng警告:iCCP:已知错误的sRGB配置文件” 但我被告知这是不重要的。
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))
pygame.display.update()
def loadMap(self):
self.map = tiledMap()
self.map_img = self.map.make_map()
self.gameWindow.blit(self.map_img, (0,624))
def gameLoop(self):
self.clock.tick(FPS)
self.event()
self.update()
self.loadMap()
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
import pygame
import pytmx
pygame.init()
def load():
loaded = {}
loaded["ninjaIdle01"] = pygame.image.load("gameImages\s_ninjaIdle_01.png")
loaded["ninjaIdle02"] = pygame.image.load("gameImages\s_ninjaIdle_02.png")
loaded["ninjaIdle03"] = pygame.image.load("gameImages\s_ninjaIdle_03.png")
loaded["ninjaIdle04"] = pygame.image.load("gameImages\s_ninjaIdle_04.png")
loaded["ninjaIdle05"] = pygame.image.load("gameImages\s_ninjaIdle_05.png")
loaded["ninjaIdle06"] = pygame.image.load("gameImages\s_ninjaIdle_06.png")
loaded["ninjaIdle07"] = pygame.image.load("gameImages\s_ninjaIdle_07.png")
loaded["ninjaIdle08"] = pygame.image.load("gameImages\s_ninjaIdle_08.png")
loaded["ninjaIdle09"] = pygame.image.load("gameImages\s_ninjaIdle_09.png")
loaded["ninjaIdle10"] = pygame.image.load("gameImages\s_ninjaIdle_10.png")
loaded["ninjaJump01"] = pygame.image.load("gameImages\s_ninjaJump_01.png")
loaded["ninjaJump02"] = pygame.image.load("gameImages\s_ninjaJump_02.png")
loaded["ninjaJump03"] = pygame.image.load("gameImages\s_ninjaJump_03.png")
loaded["ninjaJump04"] = pygame.image.load("gameImages\s_ninjaJump_04.png")
loaded["ninjaJump05"] = pygame.image.load("gameImages\s_ninjaJump_05.png")
loaded["ninjaJump06"] = pygame.image.load("gameImages\s_ninjaJump_06.png")
loaded["ninjaJump07"] = pygame.image.load("gameImages\s_ninjaJump_07.png")
loaded["ninjaJump08"] = pygame.image.load("gameImages\s_ninjaJump_08.png")
loaded["ninjaJump09"] = pygame.image.load("gameImages\s_ninjaJump_09.png")
loaded["ninjaJump010"] = pygame.image.load("gameImages\s_ninjaJump_10.png")
loaded["ninjaRun01"] = pygame.image.load("gameImages\s_ninjaRun_01.png")
loaded["ninjaRun02"] = pygame.image.load("gameImages\s_ninjaRun_02.png")
loaded["ninjaRun03"] = pygame.image.load("gameImages\s_ninjaRun_03.png")
loaded["ninjaRun04"] = pygame.image.load("gameImages\s_ninjaRun_04.png")
loaded["ninjaRun05"] = pygame.image.load("gameImages\s_ninjaRun_05.png")
loaded["ninjaRun06"] = pygame.image.load("gameImages\s_ninjaRun_06.png")
loaded["ninjaRun07"] = pygame.image.load("gameImages\s_ninjaRun_07.png")
loaded["ninjaRun08"] = pygame.image.load("gameImages\s_ninjaRun_08.png")
loaded["ninjaRun09"] = pygame.image.load("gameImages\s_ninjaRun_09.png")
loaded["ninjaRun10"] = pygame.image.load("gameImages\s_ninjaRun_10.png")
loaded["distantCity"] = pygame.image.load("gameImages\s_distantCity.png").convert()
return loaded
class tiledMap():
def __init__(self):
self.gameMap = pytmx.load_pygame("s_gameMapJPG.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:
if isinstance(layer, pytmx.TiledTileLayer):
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((640, 96))
self.render(mapSurface)
return mapSurface
s_gameMap.tmx
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="20" height="3" tilewidth="32" tileheight="32" nextobjectid="1">
<tileset firstgid="1" name="isTiles" tilewidth="32" tileheight="32" tilecount="6" columns="0">
<tile id="0">
<image width="32" height="32" source="gameImages/s_grassLeft.png"/>
</tile>
<tile id="1">
<image width="32" height="32" source="gameImages/s_grassMiddle.png"/>
</tile>
<tile id="2">
<image width="32" height="32" source="gameImages/s_grassRight.png"/>
</tile>
<tile id="3">
<image width="32" height="32" source="gameImages/s_dirtLeft.png"/>
</tile>
<tile id="4">
<image width="32" height="32" source="gameImages/s_dirtMiddle.png"/>
</tile>
<tile id="5">
<image width="32" height="32" source="gameImages/s_dirtRight.png"/>
</tile>
</tileset>
<layer name="Tile Layer 1" width="20" height="3">
<data encoding="csv">
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,
4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6
</data>
</layer>
</map>
s_gameMapJPG.tmx
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="20" height="3" tilewidth="32" tileheight="32" nextobjectid="1">
<tileset firstgid="1" name="small jpeg tiles" tilewidth="32" tileheight="32" tilecount="6" columns="0">
<tile id="0">
<image width="32" height="32" source="gameImages/s_grassRightJPG.jpg"/>
</tile>
<tile id="1">
<image width="32" height="32" source="gameImages/s_grassMiddleJPG.jpg"/>
</tile>
<tile id="2">
<image width="32" height="32" source="gameImages/s_grassLeftJPG.jpg"/>
</tile>
<tile id="3">
<image width="32" height="32" source="gameImages/s_dirtRightJPG.jpg"/>
</tile>
<tile id="4">
<image width="32" height="32" source="gameImages/s_dirtMiddleJPG.jpg"/>
</tile>
<tile id="5">
<image width="32" height="32" source="gameImages/s_dirtLeftJPG.jpg"/>
</tile>
</tileset>
<layer name="Tile Layer 1" width="20" height="3">
<data encoding="csv">
3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,
6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4,
6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4
</data>
</layer>
</map>
答案 0 :(得分:0)
在每个地形上放置trans="ffffff"
<image width="32" height="32" trans="ffffff" source="gameImages/s_grassLeft.png"/>