saving and loading map parser prototype #4
@ -27,10 +27,14 @@ class save_Parser:
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return str(self.map_shape)
 | 
			
		||||
 | 
			
		||||
    def shape_to_bytes(self, shape):
 | 
			
		||||
        bytes_ammount = len(shape) // 8
 | 
			
		||||
        bytes_trash = len(shape) % 8
 | 
			
		||||
    def shape_to_bytes(self, shape_matrix):
 | 
			
		||||
        shape_list = [b for r in shape_matrix for b in r]
 | 
			
		||||
        byte_ammount = len(shape_list) // 8 + 1
 | 
			
		||||
        tray = 0
 | 
			
		||||
        for i in shape_list:
 | 
			
		||||
            tray = (tray << 1) | i
 | 
			
		||||
        return tray.to_bytes(byte_ammount, 'big')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def load(self):
 | 
			
		||||
        """
 | 
			
		||||
@ -43,14 +47,19 @@ class save_Parser:
 | 
			
		||||
        """
 | 
			
		||||
        save_data = b''
 | 
			
		||||
        save_data += b'SMS'
 | 
			
		||||
        save_data += bytes(len(map_shape))
 | 
			
		||||
        save_data += shape_to_bytes(self.map_shape)
 | 
			
		||||
        save_data += bytes()
 | 
			
		||||
        save_data += len(self.map_shape).to_bytes(1, 'big')
 | 
			
		||||
        save_data += self.shape_to_bytes(self.map_shape)
 | 
			
		||||
        save_data += len(self.pieces).to_bytes(1, 'big')
 | 
			
		||||
        for piece in self.pieces:
 | 
			
		||||
            save_data +=len(piece).to_bytes(1, 'big')
 | 
			
		||||
            save_data += self.shape_to_bytes(piece)
 | 
			
		||||
        save_data += b'SME'
 | 
			
		||||
        with open(self.filename, mode='bw') as file:
 | 
			
		||||
            file.write(save_data)
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    p = save_Parser('test.map')
 | 
			
		||||
    p.define_map([[0,1],[0,1]])
 | 
			
		||||
    p.define_map([[1,0,1],[1,1,1], [1,0,0]])
 | 
			
		||||
    p.add_piece([[0, 1],[0, 1]])
 | 
			
		||||
    p.save()
 | 
			
		||||
    # print(p.shape_to_bytes([[1,0,1],[1,1,1], [1,0,0]]))
 | 
			
		||||
 | 
			
		||||
@ -1 +1 @@
 | 
			
		||||
SMSSME
 | 
			
		||||
SMS|SME
 | 
			
		||||
		Reference in New Issue
	
	Block a user