# 練習問題答え

正解のプログラム例は下のとおり

import mcpi.minecraft as minecraft

mc = minecraft.Minecraft.create()

x, y, z = mc.player.getPos()

rows = [
    [0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 15, 15, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 15, 15, 15, 15, 0, 0, 0, 0],
    [0, 0, 15, 15, 14, 15, 15, 14, 15, 15, 0, 0],
    [0, 15, 14, 14, 14, 14, 15, 14, 14, 14, 15, 0],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [0, 0, 15, 14, 14, 15, 15, 14, 14, 15, 0, 0],
    [0, 0, 0, 15, 15, 0, 0, 15, 15, 0, 0, 0]
]

for i in range(15):
    row = rows[i]
    for j in range(12):
        mc.setBlock(x + j, y + 15 - i, z, 35, row[j])

また変数をうまく使えば、下のようにも書くことができます。

import mcpi.minecraft as minecraft

mc = minecraft.Minecraft.create()

x, y, z = mc.player.getPos()

rows = [
    [0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 15, 15, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 15, 15, 15, 15, 0, 0, 0, 0],
    [0, 0, 15, 15, 14, 15, 15, 14, 15, 15, 0, 0],
    [0, 15, 14, 14, 14, 14, 15, 14, 14, 14, 15, 0],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [0, 15, 14, 14, 14, 14, 14, 14, 14, 14, 15, 0],
    [0, 0, 15, 14, 14, 15, 15, 14, 14, 15, 0, 0],
    [0, 0, 0, 15, 15, 0, 0, 15, 15, 0, 0, 0]
]

height = len(rows)

for i in range(height):
    row = rows[i]
    width = len(row)
    for j in range(width):
        mc.setBlock(x + j, y + height - i, z, 35, row[j])

問題へ戻る

現在の章へ戻る(ドット絵作成プログラムを作ろう)