Bootstrap

我的世界(Minecraft)计算器python源码

我的世界(Minecraft)计算器python源码

1.介绍

在这里插入图片描述

使用教程

2.源码

文件一

# CreateBigScreen.py    (创建大屏幕并返回大屏幕坐标)

from mcpi.minecraft import Minecraft
from mcpi.minecraft import CmdPositioner

mcDriver=Minecraft.create()  # 与Minecraft进行交互,里面有很多交互的方法,通过mcDriver.方法名来使用
playerId=mcDriver.getPlayerEntityId('Janium')           # 获取玩家的Id,'Janium'为玩家名称
positioner=CmdPositioner(mcDriver.conn,b'entity')       # 用来处理实体的位置信息
playerPosition=positioner.getTilePos(playerId)          # 获取玩家的位置
x=playerPosition.x
y=playerPosition.y                                      # 玩家的坐标
z=playerPosition.z

def createBigScreen():
    mcDriver.setBlocks(x,y+48,z,x+98,y+20,z,123)
    
createBigScreen()

mcDriver.postToChat("创建大屏幕成功!创建时的坐标x,y,z分别是  {},{},{}".format(x,y,z))

文件二

# ProcessAndDisplay.py

# 导入mcpi库
from mcpi.minecraft import Minecraft
import mcpi.block as block

import time


mcDriver=Minecraft.create()                          # 创建Minecraft对象,与Minecraft进行交互


x,y,z=-44,-1,-170                                    # 就是在这里输入CreateBigScreen得到的坐标


# 用相对坐标来划分每个子屏幕的位置,一共七个子屏幕,也就是可以处理七位数以内的计算。
subScreen1=[[x+5,y+44,z-1],[x+15,y+44,z-1],[x+5,y+34,z-1],[x+15,y+34,z-1],[x+5,y+24,z-1],[x+15,y+24,z-1]]
subScreen2=[[x+18,y+44,z-1],[x+28,y+44,z-1],[x+18,y+34,z-1],[x+28,y+34,z-1],[x+18,y+24,z-1],[x+28,y+24,z-1]]
subScreen3=[[x+31,y+44,z-1],[x+41,y+44,z-1],[x+31,y+34,z-1],[x+41,y+34,z-1],[x+31,y+24,z-1],[x+41,y+24,z-1]]
subScreen4=[[x+44,y+44,z-1],[x+54,y+44,z-1],[x+44,y+34,z-1],[x+54,y+34,z-1],[x+44,y+24,z-1],[x+54,y+24,z-1]]
subScreen5=[[x+57,y+44,z-1],[x+67,y+44,z-1],[x+57,y+34,z-1],[x+67,y+34,z-1],[x+57,y+24,z-1],[x+67,y+24,z-1]]
subScreen6=[[x+70,y+44,z-1],[x+80,y+44,z-1],[x+70,y+34,z-1],[x+80,y+34,z-1],[x+70,y+24,z-1],[x+80,y+24,z-1]]
subScreen7=[[x+83,y+44,z-1],[x+93,y+44,z-1],[x+83,y+34,z-1],[x+93,y+34,z-1],[x+83,y+24,z-1],[x+93,y+24,z-1]]
subScreenList=[subScreen1,subScreen2,subScreen3,subScreen4,subScreen5,subScreen6,subScreen7]


# 在子屏幕内清除之前计算结果、创建数字以及符号的方法都在这个类里
class CreateBlock:
    def clearScreen():
        mcDriver.setBlocks(x,y+48,z-1,x+98,y+20,z-1,block.AIR)
    def zero(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
        mcDriver.setBlocks(subScreen[0],subScreen[4],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def one(subScreen):
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def two(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[1],subScreen[3],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[2],subScreen[4],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
    def three(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def four(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[2],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def five(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[0],subScreen[2],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[3],subScreen[5],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
    def six(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[0],subScreen[4],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
        mcDriver.setBlocks(subScreen[3],subScreen[5],152)
    def seven(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def eight(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
        mcDriver.setBlocks(subScreen[0],subScreen[4],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
    def nine(subScreen):
        mcDriver.setBlocks(subScreen[0],subScreen[1],152)
        mcDriver.setBlocks(subScreen[1],subScreen[5],152)
        mcDriver.setBlocks(subScreen[0],subScreen[2],152)
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)
        mcDriver.setBlocks(subScreen[4],subScreen[5],152)
    # 创建小数点
    def point(subScreen):
        mcDriver.setBlock(subScreen[4],152)
    # 创建负号
    def minus(subScreen):
        mcDriver.setBlocks(subScreen[2],subScreen[3],152)


# 算式所包含的字符,用来判断输入格式是否有效
arithmeticSymbolList=['0','1','2','3','4','5','6','7','8','9','+','-','*','/','(',')','.','-']
# 算式是否有效,有效为1,无效为0
validOrNot=1 

mcDriver.postToChat('计算器启动中......')
time.sleep(0.5)
mcDriver.postToChat('计算器已启动!')

while True:
    # 子屏幕位置
    screenNumber=0
    while True:
        time.sleep(0.5)
        # 获取mc那边玩家输入的信息
        mcInfo=mcDriver.events.pollChatPosts()  
        if mcInfo != []:
            break
    # 在信息中提取算式
    equation=str(mcInfo[0])[31:-1]

    # 判断字符是否在算式列表里
    for num in equation:
        if num not in arithmeticSymbolList:
            validOrNot=0
            break
    # 有效,则开始计算结果
    if validOrNot==1:
        # 利用python自带的eval()方法直接算出算式的值
        numResult=eval(equation)
        # 字符串类型的算式计算结果,方便遍历每个数字,在屏幕中显示出来
        strResult=str(eval(equation))
        # 如果超过七位以上的浮点数,则取前七位(不是四舍五入)
        if type(numResult)==type(1.1):
            strResult=strResult[0:7]
        # 如果计算结果小于七位数,则开始显示
        if len(strResult)<=7:
            CreateBlock.clearScreen()
            # 遍历结果,显示数字
            for digitPosition in strResult:
                time.sleep(0.5)
                if   digitPosition=='0': CreateBlock.zero(subScreenList[screenNumber])
                elif digitPosition=='1': CreateBlock.one(subScreenList[screenNumber])
                elif digitPosition=='2': CreateBlock.two(subScreenList[screenNumber])
                elif digitPosition=='3': CreateBlock.three(subScreenList[screenNumber])
                elif digitPosition=='4': CreateBlock.four(subScreenList[screenNumber])
                elif digitPosition=='5': CreateBlock.five(subScreenList[screenNumber])
                elif digitPosition=='6': CreateBlock.six(subScreenList[screenNumber])
                elif digitPosition=='7': CreateBlock.seven(subScreenList[screenNumber])
                elif digitPosition=='8': CreateBlock.eight(subScreenList[screenNumber])
                elif digitPosition=='9': CreateBlock.nine(subScreenList[screenNumber])
                elif digitPosition=='.': CreateBlock.point(subScreenList[screenNumber])
                elif digitPosition=='-': CreateBlock.minus(subScreenList[screenNumber])
                screenNumber+=1
        # 如果超出七位数则显示结果溢出
        else:
            mcDriver.postToChat('结果溢出!')  
    # 无效,则提示输入错误    
    else:
        validOrNot=1
        mcDriver.postToChat('输入错误!')
;