6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Minecraftをpythonで制御して自動で階段を作った話

Last updated at Posted at 2020-06-20

MinecraftをPythonで制御できるみたいなので、簡単なことからやってみた。
#コード

Stairs.py
import mcpi.minecraft as minecraft
import math
import time

mc = minecraft.Minecraft()


while True:
    x,y,z = mc.player.getPos()
    distance = 1
    rad = math.radians(mc.player.getRotation())
    x1 = 0-math.sin(rad)*distance
    z1 = math.cos(rad)*distance

    if 0.0006462497831514223 > rad and -0.78485008484511 < rad:
        b=2
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)


    if 0.7852897061620548 > rad and 0.0006462497831514223 < rad:
        b=2
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)


    if 1.5711064519038362 > rad and 0.7852897061620548 < rad:
        b=1
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)


    if 2.3567598792917686 > rad and 1.5711064519038362 < rad:
        b=1
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)


    if 3.1414014386235256 > rad and 2.3567598792917686 < rad:
        b=3
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)

    if -3.141350838562814 > rad and -2.3557998097187953 < rad:
        b=3
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)


    if -1.5708293499924135 > rad and -2.3557998097187953 < rad:
        b=8
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)

    if -0.78485008484511 > rad and -1.5708293499924135 < rad:
        b=8
        mc.setBlock(x+x1,y,z+z1,53,b)
        time.sleep(0.001)

#解説

    x,y,z = mc.player.getPos()
    distance = 1
    rad = math.radians(mc.player.getRotation())
    x1 = 0-math.sin(rad)*distance
    z1 = math.cos(rad)*distance

ここで、プレイヤーの向きを調べて計算します。
引用:https://www.omotenashi-mind.com/index.php?title=Minecraft%EF%BC%9A%E4%B8%80%E6%AC%A1%E5%A4%89%E6%8F%9B%E3%82%92%E6%B4%BB%E7%94%A8%E3%81%97%E3%81%A6%E7%9B%AE%E3%81%AE%E5%89%8D%E3%81%AB%E3%83%96%E3%83%AD%E3%83%83%E3%82%AF%E3%82%92%E5%87%BA%E7%8F%BE%E3%81%95%E3%81%9B%E3%82%8B

残りのifのところで階段の向きを指定して置いています。
それをwhile True:で繰り返しています。
#まとめ
実行すると、多少詰まってしまうことはあるけれど、うまく動きました。
環境作りができずに一時は断念したMinecraft制御、うまくいってよかったです。
コメント 2020-06-20 212150.png

6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?