Advertisement
Guest User

Sample Code Tile Map Editor

a guest
Aug 5th, 2014
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # display a tiled image from tileset with PyQt
  3. import sys
  4. from PIL import Image
  5. from PIL.ImageQt import ImageQt
  6. from PyQt4 import QtGui, QtCore
  7. from PyQt4.QtGui import QImage
  8. from numpy import ndarray
  9.  
  10. # Simple background, will use open from a file in future
  11. background =     [[12, 2,12, 2, 1, 2, 3, 3, 1, 1],
  12.           [ 1, 1, 7, 8, 5, 6, 7, 8,12, 3],
  13.           [ 1, 3, 1, 3,12,10,10, 1,12,12],
  14.           [ 2,12, 0, 4,10, 3,12, 2,12,12],
  15.           [12,12, 1, 1,10, 3,12, 2,12, 1],
  16.           [12,12,12, 0,10, 2, 1,12, 1,12],
  17.           [ 3,12, 3,12, 0, 2, 2,12,12, 3],
  18.           [ 1,12, 1,12, 1, 1,12,12, 3,12],
  19.           [ 3,12, 0,12,12,12,12,12, 3, 3],
  20.           [12, 3, 1, 2, 3,12,12,12, 1,12]]
  21.  
  22. # This will have the tileset
  23. tileset = []
  24.  
  25. # last time I was writing this save function!
  26. def save():
  27.     f = open( "map.txt" , "wb" )
  28.     f.write( "background :   [" )
  29.     for i in range(len(background) ):
  30.                 f.write( "[" )
  31.         for j in range(len(background[0])):
  32.             f.write( str(background[j][i]) )
  33.             f.write( "," ) if j != len(background[0])-1 else (f.write( "]," ) if i != len(background)-1 else f.write( "]" ))
  34.         f.write( "\n" ) if i != len(background)-1 else f.write( "]" )
  35.     f.close()
  36.  
  37. class MyImage(QtGui.QWidget):
  38.     def __init__(self, parent, width, height):
  39.         QtGui.QWidget.__init__(self, parent)
  40.  
  41.         BOX_SIZE = 32
  42.         image_file = Image.open("simpletile.png")
  43.  
  44.         self.setWindowTitle("View tiled background")
  45.  
  46.         # get tileset file and split it in images that can be pointed through array
  47.  
  48.         if image_file.size[0] % BOX_SIZE == 0 and image_file.size[1] % BOX_SIZE ==0 :
  49.             currentx = 0
  50.             currenty = 0
  51.             tilei = 0
  52.             while currenty < image_file.size[1]:
  53.                 while currentx < image_file.size[0]:
  54.                     print currentx,",",currenty
  55.                     tileset.append( image_file.crop((currentx,currenty,currentx + BOX_SIZE, currenty + BOX_SIZE)) )
  56.                     tilei += 1
  57.                     currentx += BOX_SIZE
  58.                 currenty += BOX_SIZE
  59.                 currentx = 0
  60.  
  61.         # get the background numbers and use to get the tiles  
  62.    
  63.         for i in range(len(background) ):
  64.             for j in range(len(background[0])):
  65.  
  66.                 image = ImageQt( tileset[ background[j][i] ] )
  67.                 pixmap = QtGui.QPixmap.fromImage(image)
  68.                 image = QtGui.QPixmap(pixmap)
  69.  
  70.                 label = QtGui.QLabel(self)
  71.                 label.setGeometry(i*BOX_SIZE+10, j*BOX_SIZE+10, BOX_SIZE, BOX_SIZE)
  72.                 label.setPixmap(image)
  73. save()
  74.  
  75. app = QtGui.QApplication(sys.argv)
  76.  
  77. width = 320
  78. height = 320
  79. w = MyImage(None, width, height)
  80.  
  81. w.setGeometry(100, 100, width+20, height+20)
  82. w.show()
  83. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement