07月24, 2019

02. Python 玩转 Minecraft——我是荒野大表哥

今天介绍一个新库 minecraftstuff,利用它我们可以轻松的在 minecraft 中绘制各种形状,抓紧时间绘制一块表,成为 minecraft 世界里最牛的“大表哥”。 快开始学习吧!

minecraftsuff 库的文档

https://minecraft-stuff.readthedocs.io/en/latest/index.html

示意图

clipboard.png

视频中的代码

# encoding=utf-8

from minecraftstuff import MinecraftDrawing
from mcpi.minecraft import Minecraft
from mcpi import block
import datetime
from time import sleep
import math

# 指定一些常量
# 表盘半径
CLOCK_RADIUS = 20
#各个针的长度
HOUR_HAND_LENGTH = 10
MIN_HAND_LENGTH = 17
SEC_HAND_LENGTH = 18

# 计算原点上指针末端坐标
def findPointOnCircle(cx, cy, radius, angle):
    x = cx + math.sin(math.radians(angle)) * radius
    y = cy + math.cos(math.radians(angle)) * radius
    x = int(round(x, 0))
    y = int(round(y, 0))
    return(x,y)

# 创建 minecraft 对象
mc = Minecraft.create("192.168.199.159")
# 创建 minecraft drawing 对象
mcdrawing = MinecraftDrawing(mc)

# 获取当前玩家坐标
pos = mc.player.getTilePos()

# 钟表圆心为玩家头顶 25 个像素块
clockMiddle = pos
clockMiddle.y = clockMiddle.y + 25

# 先绘制表盘
mcdrawing.drawCircle(clockMiddle.x, clockMiddle.y, clockMiddle.z,CLOCK_RADIUS, block.DIAMOND_BLOCK.id)

while True:
    timeNow = datetime.datetime.now()

    # 分别获得 时分秒
    hours = timeNow.hour % 12
    minutes = timeNow.minute
    seconds = timeNow.second

    # 计算各自角度和指针末端坐标
    hourHandAngle = (360 / 12) * hours
    hourHandEndX, hourHandEndY = findPointOnCircle(clockMiddle.x, clockMiddle.y, HOUR_HAND_LENGTH, hourHandAngle)

    minHandAngle = (360 / 60) * minutes
    minHandEndX, minHandEndY = findPointOnCircle(clockMiddle.x, clockMiddle.y, MIN_HAND_LENGTH, minHandAngle)

    secHandAngle = (360 / 60) * seconds
    secHandEndX, secHandEndY = findPointOnCircle(clockMiddle.x, clockMiddle.y, SEC_HAND_LENGTH, secHandAngle)

    # 绘制指针
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, hourHandEndX, hourHandEndY,clockMiddle.z,block.DIRT.id)
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, minHandEndX, minHandEndY,clockMiddle.z,block.DIRT.id)
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, secHandEndX, secHandEndY,clockMiddle.z,block.DIRT.id)
    sleep(1)
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, secHandEndX, secHandEndY,clockMiddle.z,block.AIR.id)
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, hourHandEndX, hourHandEndY,clockMiddle.z,block.AIR.id)
    mcdrawing.drawLine(clockMiddle.x, clockMiddle.y, clockMiddle.z, minHandEndX, minHandEndY,clockMiddle.z,block.AIR.id)

本文链接:http://www.thinkinpython.com/post/mc_02.html

-- EOF --