我遵循了本教程:https://www.youtube.com/watch?v=QIXyj3WeyZM当我尝试运行该程序时,我收到了垃圾邮件的libpng警告(虽然我认为这不重要)。地图没有显示在屏幕上,但我的fps确实显着下降,表明发生了一些事情并且没有错误消息(除了libpng警告)。
Loading.py
class tiledMap():
def __init__(self):
self.gameMap = pytmx.load_pygame("s_gameMap.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)
if tile:
surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))
def make_map(self):
mapSurface = pygame.Surface((self.mapwidth, self.mapheight))
self.render(mapSurface)
return mapSurface
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.map_rect = self.map_img.get_rect()
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()
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>
如果需要,文件内部还有更多,但我只包括我认为相关的内容。
编辑: os = windows 10
这是我收到的警告:libpng警告:iCCP:已知错误的sRGB配置文件
我认为它不重要因为我也尝试将我正在使用的图像转换为从png到jpg的图块,警告消失但是图块仍然没有出现在屏幕上。
在mainGame.py中添加了所有内容