Commits

Cory Li committed 00efd8a

Basic tile splicing added

  • Participants
  • Parent commits e3238a2

Comments (0)

Files changed (2)

     WALL = 2
 
 
+class TileSet:
+
+    def __init__(self,filename,size):
+        self.size = size
+        self.filename = filename
+
+    def load(self):
+        size = self.size
+        image = pygame.image.load(self.filename).convert()
+        image_width, image_height = image.get_size()
+        tile_table = []
+        for tile_y in range(0, image_height/size):
+            for tile_x in range(0, image_width/size):
+                rect = (tile_x*size, tile_y*size, size, size)
+                tile_table.append(image.subsurface(rect))
+
+        return tile_table
+
+
 class TiledMap:
 
     # Resonable Defaults
             }
 
 class MapViewer:
-    tile_size = 8 #pixels
+    tile_size = 16 #pixels
 
-    camWidth = 100
-    camHeight = 100
+    camWidth = 20
+    camHeight = 20
     camX = 0
     camY = 0
 
-    def __init__(self,model):
+
+    def __init__(self,model,tileset):
         pygame.init()
         self.model = model
         self.screen = pygame.display.set_mode((
                         self.camWidth * self.tile_size,
                         self.camHeight * self.tile_size))
+        self.tileset = tileset.load()
         pygame.display.set_caption('Spells Map Editor')
         self.screen.fill((255,255,255))
 
         for x in range(sizeX):
             for y in range(sizeY):
                 if mapdata[x][y] == 0:
-                    self.draw_tile((x,y),(255,0,0))
+                    self.draw_color((x,y),(255,0,0))
                 if mapdata[x][y] == 1:
-                    self.draw_tile((x,y),(0,255,0))
+                    self.draw_tile((x,y),1)
 
-    def draw_tile(self,pos,color):
+    def draw_color(self,pos,color):
         pygame.draw.rect(self.screen, color,
                 (pos[0]*self.tile_size, pos[1]*self.tile_size, self.tile_size, self.tile_size))
 
+    def draw_tile(self,pos,tile_num):
+        scaled_tile = pygame.transform.scale(self.tileset[tile_num],(self.tile_size,self.tile_size))
+        self.screen.blit(scaled_tile, (pos[0]*self.tile_size, pos[1]*self.tile_size))
 
     def update(self):
         self.draw_layer(self.model.map_collision)
         return (x/self.tile_size, y/self.tile_size)
 
 
-
-
 if __name__=='__main__':
-    model = TiledMap((100,100))
-    view = MapViewer(model)
+    model = TiledMap((20,20))
+    tiles = TileSet('tileset_one.png',16)
+    view = MapViewer(model,tiles)
 
     mainloop = True
     clock = pygame.time.Clock()

tileset_one.png

Added
New image