1
0

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.

Raspberry jam mod + Minecraft APIでPythonを使いMinecraft上でデジタル時計を作成する

Last updated at Posted at 2021-02-03

Raspberry jam mod

Minecraft pi Editionはスクリプトから動作させることができるエディションとして知られていますが、このmodを導入すればjava版でもpi EditionのようにPython上からMinecraftを操作することが出来ます。

環境

  • Python 3.8.7
  • Minecraft forge 1.12.2
  • Raspberry jam mod
※追記:記事を書いた当時は1.12.2まで対応していたようですが、2021/9/5現在は1.16.5まで対応しているようです。

導入

windowsの場合

``` pip install mcpi ```

をcmdで打つとmcpiのモジュールがインストールされると思います

forgeやmodのインストールはこちらを参考にしてください

コード

```python:time_block.py # ライブラリのimport from mcpi import minecraft import mcpi.block as block import datetime

mc = minecraft.Minecraft.create("localhost")

<p>ここまでは決め打ちのようなもの</p>
```python:time_block.py
# 続きその1
# ブロックを配置するためのデータ置き場
one=[
    "□□□■□□□□□",
    "□■■■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□□□■□□□□□",
    "□■■■■■□□□",
    ]

# 2から9と0も同じように書く(長くなるので省略)
# ただし行の数と列の数は同じにしてください

colon=[
    "□□□□□□□□□",
    "□□□□□□□□□",
    "□□□■■■□□□",
    "□□□■■■□□□",
    "□□□□□□□□□",
    "□□□□□□□□□",
    "□□□□□□□□□",
    "□□□□□□□□□",
    "□□□□□□□□□",
    "□□□■■■□□□",
    "□□□■■■□□□",
    "□□□□□□□□□",
    "□□□□□□□□□",
    ]

AA文字の生成はこのサイトを使いました

```python:time_block.py # 続きその2 # プレイヤーの位置を取得 pos = mc.player.getPos() basex = int(pos.x) + 0 basey = int(pos.y) + 20 basez = int(pos.z) - 5 w=""

while True:
time=datetime.datetime.now()#現在時刻を取得
hour=time.hour
minute=time.minute
second=time.second

#時間が二桁ではなかった場合の処理
if hour==0 or hour==1 or hour==2 or hour==3 or hour==4 or hour==5 or hour==6 or hour==7 or hour==8 or hour==9:
    hour="0"+str(hour)#先頭に0を追加

if minute==0 or minute==1 or minute==2 or minute==3 or minute==4 or minute==5 or minute==6 or minute==7 or minute==8 or minute==9:
    minute="0"+str(minute)

if second==0 or second==1 or second==2 or second==3 or second==4 or second==5 or second==6 or second==7 or second==8 or second==9:
    second="0"+str(second)

n=0

time2=str(hour)+":"+str(minute)+": "+str(second)#デジタル時計風に記述
if w!=time2:#1秒に一回実行させるための処理(文字列の内容が変わった場合に動かす)
    w=""
    w+=time2#初期化し、比較用の文字列に代入
    mc.setBlocks(basex,basey,basez,basex+80,basey-18,basez,0)#ブロック同士の重なりを防ぐためブロックを全消し(最後の0は空気ブロックの意味)

    for word in w:
        #0から:までのそれぞれの処理
        if word=="0":
            for y, line in enumerate(zero):#listの要素内の取り出し
                for x, c in enumerate(line):#要素内の文字列の取り出し
                    if c=="■":#塗りつぶされてる四角のみ実行
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)#特定の座標にブロックを置くという意
            n+=10#一文字の処理が終わったときに前の文字と塗りつぶされないようにnの値を足す

        elif word=="1":
            for y, line in enumerate(one):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="2":
            for y, line in enumerate(two):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="3":
            for y, line in enumerate(three):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="4":
            for y, line in enumerate(four):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="5":
            for y, line in enumerate(five):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="6":
            for y, line in enumerate(six):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="7":
            for y, line in enumerate(seven):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="8":
            for y, line in enumerate(eight):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word=="9":
            for y, line in enumerate(nine):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10

        elif word==":":
            for y, line in enumerate(colon):
                for x, c in enumerate(line):
                    if c=="■":
                        mc.setBlock(basex + x + n, basey - y, basez, block.STONE.id)
            n+=10
<p>こんな感じになると思います</p>
![デジタル時計_minecraft.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1073363/39ed6cbb-7cdd-a923-64d1-ab40700e79d4.png)


<h1>参考資料</h1>

<ul>
<li>[Raspberry Pi + Minecraft Pi Edition で Hello World](https://qiita.com/niwasawa/items/42cf74e053af6478c497)</li>
<li>[Minecraft API](https://www.stuffaboutcode.com/p/minecraft-api-reference.html)</li>
<li>[MODをインストールしよう](https://prosense.me/textbooks/GettingStartedMinecraftProgramming/3/#mod%E3%81%A8%E3%81%AF)</li>
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?