A tilemap in PyQt for a bigger game project



I wanted to get back into game development, as a hobby, for some time now. I have some idea of what I want: a simple 2d rpg game, like Pokemon in Game Boy. So for starts, I needed a way to draw tilemaps, a good editor, something I could customize for my needs.

But I really couldn't find anything as easy to use as I wanted so I decided to write my own. I opted to use Python and PyQt - and I don't know much about any of them, so I had a slow start. I'm reading about PyQt for two days in all my free time.

Until now I just have a window showing a hardcoded tilemap on screen. I'm sharing it, even unfinished, since I had a really hard time figuring out the steps to do this.

Since python cares about identation, I'm sharing a link: http://pastebin.com/d1FGDCJf

 #!/usr/bin/env python  
 # display a tiled image from tileset with PyQt  
 import sys  
 from PIL import Image  
 from PIL.ImageQt import ImageQt  
 from PyQt4 import QtGui, QtCore  
 from PyQt4.QtGui import QImage  
 from numpy import ndarray  
 # Simple background, will use open from a file in future  
 background =   [[12, 2,12, 2, 1, 2, 3, 3, 1, 1],  
      [ 1, 1, 7, 8, 5, 6, 7, 8,12, 3],  
      [ 1, 3, 1, 3,12,10,10, 1,12,12],  
      [ 2,12, 0, 4,10, 3,12, 2,12,12],  
      [12,12, 1, 1,10, 3,12, 2,12, 1],  
      [12,12,12, 0,10, 2, 1,12, 1,12],  
      [ 3,12, 3,12, 0, 2, 2,12,12, 3],  
      [ 1,12, 1,12, 1, 1,12,12, 3,12],  
      [ 3,12, 0,12,12,12,12,12, 3, 3],  
      [12, 3, 1, 2, 3,12,12,12, 1,12]]  
 # This will have the tileset  
 tileset = []   
 # last time I was writing this save function!  
 def save():  
   f = open( "map.txt" , "wb" )  
   f.write( "background :   [" )  
   for i in range(len(background) ):  
         f.write( "[" )  
     for j in range(len(background[0])):  
       f.write( str(background[j][i]) )  
       f.write( "," ) if j != len(background[0])-1 else (f.write( "]," ) if i != len(background)-1 else f.write( "]" ))  
     f.write( "\n" ) if i != len(background)-1 else f.write( "]" )  
   f.close()  
 class MyImage(QtGui.QWidget):  
   def __init__(self, parent, width, height):  
     QtGui.QWidget.__init__(self, parent)  
     BOX_SIZE = 32  
     image_file = Image.open("simpletile.png")  
     self.setWindowTitle("View tiled background")  
     # get tileset file and split it in images that can be pointed through array  
     if image_file.size[0] % BOX_SIZE == 0 and image_file.size[1] % BOX_SIZE ==0 :  
       currentx = 0  
       currenty = 0  
       tilei = 0  
       while currenty < image_file.size[1]:  
         while currentx < image_file.size[0]:  
           print currentx,",",currenty  
           tileset.append( image_file.crop((currentx,currenty,currentx + BOX_SIZE, currenty + BOX_SIZE)) )  
           tilei += 1  
           currentx += BOX_SIZE  
         currenty += BOX_SIZE  
         currentx = 0  
     # get the background numbers and use to get the tiles    
     for i in range(len(background) ):  
       for j in range(len(background[0])):  
         image = ImageQt( tileset[ background[j][i] ] )  
         pixmap = QtGui.QPixmap.fromImage(image)  
         image = QtGui.QPixmap(pixmap)  
         label = QtGui.QLabel(self)  
         label.setGeometry(i*BOX_SIZE+10, j*BOX_SIZE+10, BOX_SIZE, BOX_SIZE)  
         label.setPixmap(image)  
 save()  
 app = QtGui.QApplication(sys.argv)  
 width = 320  
 height = 320  
 w = MyImage(None, width, height)  
 w.setGeometry(100, 100, width+20, height+20)  
 w.show()  
 app.exec_()  

Powered by Blogger.