2017-09-25 7 views
0

私はタイルで作成したタイルマップを表示するコードをいくつか実行しようとしていますが、 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) 
+0

にこれらを読んだとき、あなたが取得している例外を貼り付けてください容易になるだろう。 – JJAACCEeEKK

+0

私はPythonのゲームウィンドウが黒を残している間単に応答を停止する例外はありません。 –

答えて

0

はおそらく、CSVファイルとしてタイルマップをエクスポートして、ゲーム

関連する問題