Minecraft Building Miscellanea
We're doing some more computer science workshops demonstrating building stuff in minecraft on the Raspberry Pi. Here are few of the examples.
Trampoline
Stand on some grass and bounce!
import mcpi.minecraft as minecraft
import mcpi.block as block
world = minecraft.Minecraft.create()
while True:
position = world.player.getTilePos()
block = world.getBlock(position.x, position.y - 1, position.z)
# standing on grass?
if block == block.GRASS:
world.player.setPos(position.x, position.y + 20, position.z)
Treasure Hunt
import mcpi.minecraft as minecraft
import mcpi.block as block
import random
import time
world = minecraft.Minecraft.create()
world.postToChat("I have hidden a diamond...")
x = 0
y = 0
z = 0
# random position?
# x = random.randrange(-50, 50)
# y = random.randrange(0, 20)
# z = random.randrange(-50, 50)
world.setBlock(x, y, z, block.DIAMOND)
world.camera.setFixed()
world.camera.setPos(x, y + 5, z)
time.sleep(10)
world.postToChat("Treasure hunt!")
world.camera.setNormal()
# or
# world.camera.setFollow()
Frozen
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
world = minecraft.Minecraft.create()
while True:
time.sleep(0.2)
x, y, z = world.player.getPos()
# change the block type ?
world.setBlock(x, y - 1, z, block.ICE)
# block_below = world.getBlock(x, y - 1, z)
# if block_below == block.WATER:
# world.setBlock(x, y - 1, z, block.ICE)
Pyramid
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
import math
def build_pyramid(x, y, z, width, material):
if width % 2 == 0:
width = width + 1
height = int((width + 1) / 2)
layer_width = int(math.floor(width/2))
world.setBlocks(x - layer_width - 2, y - 1, z - layer_width - 2, x + layer_width + 2, y - 1, z + layer_width + 2, block.DIRT)
for level in range(y, y + height):
world.setBlocks(x - layer_width, level, z - layer_width, x + layer_width, level, z + layer_width, material)
layer_width = layer_width - 1
world = minecraft.Minecraft.create()
# Customise with different block types
x = 0
y = 1
z = 0
width = 20
build_pyramid(x, y, z, width, block.ICE)
Castle
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
import math
def build_tower(x, y, z, width, height, material):
# tower
world.setBlocks(x, y, z, x + width, y + height, z + width, material)
# create crennelations
for dx in range(width + 1):
for dz in range(width + 1):
if dx % 2 == 0 and dz % 2 == 0:
world.setBlock(x + dx, y + height + 1, z + dz, material)
# carve out top of tower
world.setBlocks(x + 1, y + height, z + 1, x + width - 1, y + height + 1, z + width - 1, block.AIR)
world.setBlocks(x + 2, y + height - 1, z + 2, x + width - 2, y + height + 1, z + width - 2, block.AIR)
def build_wall(x, y, z, length, height, material, plane):
if plane == "x":
# wall
world.setBlocks(x, y, z, x + length, y + height, z, material)
# create crennelations
for i in range(length + 2):
if i % 2 == 0:
world.setBlock(x + i, y + height, z, block.AIR)
if plane == "z":
# wall
world.setBlocks(x, y, z, x, y + height, z + length, material)
# crennelations
for i in range(length + 2):
if i % 2 == 0:
world.setBlock(x, y + height, z + i, block.AIR)
def build_castle(x, y, z, length, height, material):
build_wall(x, y, z, length, height, material, "x")
time.sleep(1)
build_wall(x, y, z, length, height, material, "z")
time.sleep(1)
build_wall(x, y, z + length, length, height, material, "x")
time.sleep(1)
build_wall(x + length, y, z, length, height, material, "z")
time.sleep(1)
tower_width = int(length /5)
if tower_width % 2 > 0:
tower_width = tower_width + 1
tower_height = int(height * 1.75)
build_tower(x, y, z, tower_width, tower_height, material)
time.sleep(1)
build_tower(x, y, z + length - tower_width, tower_width, tower_height, material)
time.sleep(1)
build_tower(x + length - tower_width, y, z, tower_width, tower_height, material)
time.sleep(1)
build_tower(x + length - tower_width, y, z + length - tower_width, tower_width, tower_height, material)
def create_moat(x, y, z, width, depth, island):
world.setBlocks(x - width, y - depth, z - width, x + island + width, y, z + island + width, block.WATER)
world.setBlock(x, y - depth, z, x + island, y, z + island, block.GRASS)
world = minecraft.Minecraft.create()
x = z = 10
y = 0
moat_width = 3
moat_depth = 5
island_size = 60
castle_size = island_size - 2
wall_height = 10
create_moat(x, y, z, moat_width, moat_depth, island_size)
build_castle(x + 1, y, z + 1, castle_size, wall_height, block.STONE)
Clearing Changes
import mcpi.minecraft as minecraft
import mcpi.block as block
world = minecraft.Minecraft.create()
# clear all buildings
world.postToChat("blanking...")
world.setBlocks(-128, 0, -128, 128, 60, 128, block.AIR)
world.setBlocks(-128, -10, -128, 128, -1, 128, block.DIRT)
world.setBlocks(-128, 0, -128, 128, 0, 128, block.GRASS)
# world.player.setPos(0, 1, 0)
world.postToChat("blanked.")