试图加载平铺地图到pygame中,没有错误抛出,但pygame没有响应
问题描述:
我一直在扼杀这一点,我试图运行一些代码,显示我在瓷砖做的瓷砖地图,但我似乎已经messedit了,因为pygame窗口不断崩溃而没有响应。不知道我的格式是否正确,但我没有做太多的工作,而且还有很多评论。下面是代码:试图加载平铺地图到pygame中,没有错误抛出,但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)
答
出口瓷砖的地图为CSV文件时,然后阅读到这些游戏
,请复制粘贴例外你得到可能会更容易。 – JJAACCEeEKK
我没有得到任何异常python游戏窗口只是停止响应,而保持黑色。 –