试图将Tiled地图加载到pygame中,没有抛出错误但pygame没有响应

时间:2017-09-25 09:59:32

标签: python crash pygame tiled

我已经把这个乱了一下,我正在尝试运行一些显示我在Tiled中制作的tilemap的代码,但是我似乎已经搞砸了,因为pygame窗口不断崩溃而没有响应。不知道我是否格式正确,但我不做这么多,而且很多都是评论。这是代码:

import pygame as pg
import pytmx
from settings import *
import os
import time

def collide_hit_rect(one, two):
    return one.hit_rect.colliderect(two.rect)


class TiledMap:
    def __init__(self, filename):
        #loads .tmx tilemap, pixelalpha make sure transparency
        tm = pytmx.load_pygame(filename, pixelalpha=True)
        #multiplies how many tiles across the map by how many pixels each tile uses
        self.width = tm.width * tm.tilewidth
        self.height = tm.height * tm.tileheight
        #stores all data above in variable tmxdata
        self.tmxdata = tm
    #draws Tiled map onto pg surface
    def render(self, surface):
        #stores command that finds image that goes with specific tiles by searching tmx data into ti
        ti = self.tmxdata.get_tile_image_by_gid
        #for all visible layers in map
        for layer in self.tmxdata.visible_layers:
            #if statement dependant on layer being Tile layer
            if isinstance(layer, pytmx.TiledTileLayer):
                #gets coordinates and gid(from .tmx) for each tile in layer
                for x, y, gid, in layer:
                    #ti command gets images for the gid from last line
                    tile = ti(gid)
                    if tile:
                        #draws tile on surface
                        surface.blit(tile, (x * self.tmxdata.tilewidth,
                                            y * self.tmxdata.tileheight))

    def make_map(self):
        #creates surface(as big as tilemap) to draw map onto
        temp_surface = pg.Surface((self.width, self.height))
        #render function will draw tilemap onto temp_surface
        self.render(temp_surface)
        return temp_surface

class Camera:
    def __init__(self, width, height):
        self.camera = pg.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        return entity.rect.move(self.camera.topleft)

    #takes rectangle
    def apply_rect(self, rect):
        # returns rectangle moved by offset position from top left
        return rect.move(self.camera.topleft)

    def update(self, target):
        x = -target.rect.centerx + int(WIDTH / 2)
        y = -target.rect.centery + int(HEIGHT / 2)

        # limit scrolling to map size
        x = min(0, x)  # left
        y = min(0, y)  # top
        x = max(-(self.width - WIDTH), x)  # right
        y = max(-(self.height - HEIGHT), y)  # bottom
        self.camera = pg.rect(x, y, self.width, self.height)

class Display():
#This is the class that makes the changes that you want to display. You would add most of your changes here. """

    def __init__(self):

        self.displayRunning = True
        self.displayWindow = pg.display.set_mode((500, 200))
        self.clock = pg.time.Clock()

    def update(self):

        pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
        pg.display.update()

    def loadMap(self):

        self.map = TiledMap('tilemap skeleton.tmx')
        self.map_img = self.map.make_map()
        self.map_rect = self.map_img.get_rect()

    def displayLoop(self):

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

# Here is the start of the main driver
runDisplay = Display()

runDisplay.update()
runDisplay.loadMap()
time.sleep(60)

1 个答案:

答案 0 :(得分:0)

将Tiled地图导出为CSV文件然后将其读入游戏

可能会更容易