python - 尝试将Tiled映射加载到pygame中,没有引发任何错误,但pygame没有响应

标签 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)

最佳答案

将平铺 map 导出为CSV文件,然后将其读取到游戏中时,可能会更容易

关于python - 尝试将Tiled映射加载到pygame中,没有引发任何错误,但pygame没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402439/

相关文章:

python - 声音库 python

ios - 当值为 nil 更新到 3.10 时,iOS 上的 Google Analytics 崩溃

Android 1.5 模拟器明显随机崩溃 [WIN DEATH]

android - mediaplayer.release()不起作用

python - 使用 pygame 在 python 中通过套接字进行流式传输

python - 如何将pygame转换为exe?

Python 装饰器 : how to use parent class decorators in a child class

python - Blender 和 Tensorflow 之间的通信

python - 如何优化pygame中的图 block 渲染?

python - 如何在 python 中正确关闭 Selenium WebDriver 实例?