Luyện thi hackathon PYTHON_MINECRAFT

huyhuy17

Link tài liệu luyện tập

Ôn nhanh Python

Hackthon code mẫu


import time

from mc import mc as mine_connect
import MinecraftStuff
import os
import json
mc = mine_connect
dirname = os.path.dirname(__file__)
data_folder = "structure-data"
data_root = os.path.join(dirname, data_folder)


class Structure(MinecraftStuff.MinecraftShape):
    def __init__(self, mc, position, shapeBlocks=None, visible=True):
        if mine_connect:
            MinecraftStuff.MinecraftShape.__init__(self, mine_connect, position, shapeBlocks=None, visible=True)
        else:
            MinecraftStuff.MinecraftShape.__init__(self, mc, position, shapeBlocks=None, visible=True)
        self.mc = mc
        pass

    def save(self):
        data = []
        for dataRow in self.shapeBlocks:
            vec = dataRow.originalPos
            data.append([vec.x, vec.y, vec.z, dataRow.blockType, dataRow.blockData])
        filename = input("Nhập tên file")
        save_data = {
            "shapeBlocks": data
        }

        f = open(os.path.join(data_root, "{}.json".format(filename)), 'w+', )
        json.dump(save_data, f, ensure_ascii=False)
        f.close()
        pass
    def save_full(self):
        data = []
        filename = input("Nhập tên file")
        total =len(self.shapeBlocks)
        count = 0
        for dataRow in self.shapeBlocks:
            vec = dataRow.originalPos
            vec_actual= dataRow.actualPos
            block = mine_connect.getBlockWithData(vec_actual.x,vec_actual.y,vec_actual.z)
            data.append([vec.x, vec.y, vec.z, block.id, block.data])
            count= count + 1
            print("copy {}/{}".format(count, total))
        save_data = {
            "shapeBlocks": data
        }
        f = open(os.path.join(data_root, "{}.json".format(filename)), 'w+', )
        json.dump(save_data, f, ensure_ascii=False)
        f.close()
        pass
        pass
    def load(self, filename=False):
        if not(filename):
            filename = input("Nhập tên file bạn muốn load")
        # try:
        with open(os.path.join(data_root, "{}.json".format(filename))) as f:
            data = json.load(f)
            if type(data) is dict and type(data['shapeBlocks']) is list:
                self.shapeBlocks =[]
                self.clear()
                self.visible = True
                for block in data['shapeBlocks']:
                    x = block[0]
                    y = block[1]
                    z = block[2]
                    blockType = block[3]
                    blockData = block[4]
                    self.setBlock(x, y, z, blockType, blockData)
            else:
                print("file data không hợp lệ")
        # except:
        #     print('Không tồn tại file này')
        #     input('')
        # pass

    def introduce(self):
        mine_connect.postToChat(self.name)
        pass
    def is_near_house (self):
        if self.position:
            self.introduce()
    def handle_answer(self):
        pass



Trả lời