LoginSignup
9
8

More than 3 years have passed since last update.

Raspberry Pi + Minecraft Pi Edition で Hello World

Posted at

概要

  • Raspberry Pi + Raspbian 環境で動作する Minecraft Pi Edition にて、Python 用ライブラリ mcpi を使って Hello World を表示する

今回の環境

  • Raspberry Pi 3 Model B
  • Raspbian GNU/Linux 10 (buster)
  • Python 2.7
  • Minecraft Pi Edition v0.1.1 alpha
  • mcpi 0.1.1 (Minecraft Pi Edition API Python Library)

Minecraft Pi Edition とは

Minecraft Pi Edition は Raspberry Pi でのプログラミング学習用に開発された Minecraft の無料版。

Minecraft Pi Edition と mcpi のインストール

Raspbian にはおそらく最初からインストールされているが、無い場合は apt または apt-get コマンドで minecraft-pi パッケージをインストールする。

$ sudo apt install minecraft-pi
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
minecraft-pi はすでに最新バージョン (0.1.1-6) です。

Python の mcpi パッケージ (Minecraft Pi Edition API Python Library) も一緒にインストールされていると思うが、無い場合は pip コマンドでインストールする。

$ sudo pip install mcpi
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: mcpi in /usr/lib/python2.7/dist-packages (0.1.1)
$ pip show mcpi
Name: mcpi
Version: 0.1.1
Summary: API for Minecraft Pi
Home-page: http://pi.minecraft.net/
Author: Mojang
Author-email: UNKNOWN
License: UNKNOWN
Location: /usr/lib/python2.7/dist-packages
Requires: 
Required-by: 

Minecraft のワールドに Hello World を表示する

Minecraft Pi Edition を起動し、

minecraft-pi-edition-1.png

ワールドを生成して入っておく。

minecraft-pi-edition-2.png

以下のソースコードを保存して、

hello-minecraft.py
from mcpi import minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create("localhost")
pos = mc.player.getPos()
basex = int(pos.x) + 0
basey = int(pos.y) + 6
basez = int(pos.z) + 0

data = [
  "#  # ### #   #    ##   #   #  ##  ###  #   ### ",
  "#  # #   #   #   #  #  # # # #  # #  # #   #  #",
  "#### ### #   #   #  #  # # # #  # ###  #   #  #",
  "#  # #   #   #   #  #  # # # #  # # #  #   #  #",
  "#  # ### ### ###  ##    # #   ##  #  # ### ### "
]

for y, line in enumerate(data):
  for x, c in enumerate(line):
    if c == "#":
      mc.setBlock(basex + x, basey - y, basez, block.DIAMOND_BLOCK.id)

実行すると、

$ python hello-minecraft.py

ダイヤモンドブロックによる HELLO WORLD が出現する。

minecraft-pi-edition-3.png

参考資料

9
8
0

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
9
8