class ItemGrid(object): def __init__(self, width, height): self.grid = {} self.gridWidth = width self.gridHeight = height self.gridSize = width * height self.Clear() def __del__(self): self.grid = {} self.gridWidth = 0 self.gridHeight = 0 self.gridSize = 0 def Clear(self): for pos in range(self.gridSize): self.grid[pos] = False def IsEmpty(self, pos, width, height): row = pos / self.gridWidth if row + height > self.gridHeight: return False if pos + width > row * self.gridWidth + self.gridWidth: return False for y in range(height): start = pos + (y * self.gridWidth) if self.grid[start] == True: return False x = 1 while x < width: x =+ 1 if self.grid[start + x] == True: return False return True def FindBlank(self, width, height): if width > self.gridWidth or height > self.gridHeight: return -1 for row in range(self.gridHeight): for col in range(self.gridWidth): index = row * self.gridWidth + col if self.IsEmpty(index, width, height): return index return -1 def Put(self, pos, width, height): if not self.IsEmpty(pos, width, height): return False for y in range(height): start = pos + (y * self.gridWidth) self.grid[start] = True x = 1 while x < width: x += 1 self.grid[start + x] = True return True # C++'da (grid.h) item slotun python versiyonu. # Ilk Yukleme self.itemGrid = self.ItemGrid(4, 4) #4x4= 16Slot # Memory Destroy self.itemGrid = {} # Slot Temizleme if self.itemGrid: self.itemGrid.Clear() # Itemin boyutunu alalim. itemVnum = 189 itemCount = 1 item.SelectItem(itemVnum) itemIcon = item.GetIconImage() (width, height) = item.GetItemSize() #Genislik, Yukseklik pos = self.itemGrid.FindBlank(width, height) #Bos Slot Bul! if pos == -1: break self.itemGrid.Put(pos, width, height) #Slota Ekle!