使用Python在Minecraft中建造城堡

11

一旦我尝试了Minecraft-Python API,我就想要一个小项目来尝试一下。因此,我决定创建一个脚本,该脚本将自动创建一个带有护城河和几个外围墙的城堡。

This seemed like a good choice as the walls and the keep are really the same construct just a different width and height. 的keep would just need a bit of extra work to add floors, windows and a door.

这是完成的城堡。

我的世界Python城堡#0

设定

要使用以下脚本,您需要安装Minecraft和Python API。在最新的Raspbian图像中,Minecraft和API已经设置好,因此您可以直接进入有趣的地方。

Download 的Castle Builder Script

这是我的脚本:

#!/usr/bin/python
#--------------------------------------
#
#     我的世界 蟒蛇 API
#        Castle Builder
#
# This script creates a castle complete
# with moat and perimeter walls.
#
# Author : 马特 Hawkins
# Date   : 07/06/2014
#
# //www.ytguangda.com/
#
#--------------------------------------

# Import 我的世界 libraries
import mcpi.minecraft as minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create()

mc.postToChat("Let's build a castle!")

#--------------------------------------
# Define Functions
#--------------------------------------

def 创造墙(size,baseheight,height,material,battlements,walkway):
  # Create 4 walls with a specified width, height and material.
  # Battlements and walkways can also be added to the top edges.

  mc.setBlocks(-size,baseheight+1,-size,size,baseheight+height,-size,material)
  mc.setBlocks(-size,baseheight+1,-size,-size,baseheight+height,size,material)
  mc.setBlocks(size,baseheight+1,size,-size,baseheight+height,size,material)
  mc.setBlocks(size,baseheight+1,size,size,baseheight+height,-size,material) 

  # Add battlements to top edge
  if battlements==True:
    for x in range(0,(2*size)+1,2):
      mc.setBlock(size,baseheight+height+1,(x-size),material)
      mc.setBlock(-size,baseheight+height+1,(x-size),material)
      mc.setBlock((x-size),baseheight+height+1,size,material)
      mc.setBlock((x-size),baseheight+height+1,-size,material)

  # Add wooden walkways
  if walkway==True:
    mc.setBlocks(-size+1,baseheight+height-1,size-1,size-1,baseheight+height-1,size-1,block.WOOD_PLANKS)
    mc.setBlocks(-size+1,baseheight+height-1,-size+1,size-1,baseheight+height-1,-size+1,block.WOOD_PLANKS)
    mc.setBlocks(-size+1,baseheight+height-1,-size+1,-size+1,baseheight+height-1,size-1,block.WOOD_PLANKS)
    mc.setBlocks(size-1,baseheight+height-1,-size+1,size-1,baseheight+height-1,size-1,block.WOOD_PLANKS)  

def CreateLandscape(moatwidth,moatdepth,islandwidth):
  # Set upper half to 空气
  mc.setBlocks(-128,1,-128,128,128,128,block.AIR)
  # Set lower half of world to 污垢 with a layer of grass
  mc.setBlocks(-128,-1,-128,128,-128,128,block.DIRT)
  mc.setBlocks(-128,0,-128,128,0,128,block.GRASS)
  # Create water moat
  mc.setBlocks(-moatwidth,0,-moatwidth,moatwidth,-moatdepth,moatwidth,block.WATER)
  # Create island inside moat
  mc.setBlocks(-islandwidth,0,-islandwidth,islandwidth,1,islandwidth,block.GRASS)  

def 创建保持(size,baseheight,levels):
  # Create a keep with a specified number
  # of floors levels and a roof
  height=(levels*5)+5

  创造墙(size,baseheight,height,block.STONE_BRICK,True,True)

  # Floors & Windows
  for level in range(1,levels+1):
    mc.setBlocks(-size+1,(level*5)+baseheight,-size+1,size-1,(level*5)+baseheight,size-1,block.WOOD_PLANKS)

  # Windows
  for level in range(1,levels+1):
    创建视窗(0,(level*5)+baseheight+2,size,"N")
    创建视窗(0,(level*5)+baseheight+2,-size,"S")
    创建视窗(-size,(level*5)+baseheight+2,0,"W")
    创建视窗(size,(level*5)+baseheight+2,0,"E")

  # Door
  mc.setBlocks(0,baseheight+1,size,0,baseheight+2,size,block.AIR)

def 创建视窗(x,y,z,dir):

  if dir=="N" or dir=="S":
    z1=z
    z2=z
    x1=x-2
    x2=x+2

  if dir=="E" or dir=="W":
    z1=z-2
    z2=z+2
    x1=x
    x2=x

  mc.setBlocks(x1,y,z1,x1,y+1,z1,block.AIR)
  mc.setBlocks(x2,y,z2,x2,y+1,z2,block.AIR) 

  if dir=="N":
    a=3
  if dir=="S":
    a=2
  if dir=="W":
    a=0
  if dir=="E":
    a=1

  mc.setBlock(x1,y-1,z1,109,a)
  mc.setBlock(x2,y-1,z2,109,a)

#--------------------------------------
#
# Main Script
#
#--------------------------------------

print("Create ground and moat")
CreateLandscape(33,10,23)  

print("Create outer walls")
CreateWalls(21,1,5,block.STONE_BRICK,True,True)

print("Create inner walls")
CreateWalls(13,1,6,block.STONE_BRICK,True,True)

print("Create Keep with 4 levels")
CreateKeep(5,1,4)

print("Position player 上 Keep's walkway")
mc.player.setPos(0,30,4)

如果要直接将其下载到您的体育彩票31选7,请使用wget:

wget //bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/minecraft/castle.py

 

需要注意的几点

的“CreateLandscape”函数清除了整个Minecraft世界(256x256x256立方体)。它将上半部分设置为“air”而下半部分“dirt”. It adds a top layer of grass to the 污垢. You may wish to save your world if you have been working 上 something else as running this script will replace it.

的castle will be placed directly in the centre of the world.

运行脚本来建造城堡

在Minecraft运行时,您可以使用以下命令在终端窗口中执行Python脚本:

蟒蛇 castle.py

根据您使用的体育彩票31选7型号,最多可能需要20秒才能完成。

的script consists of some function definitions with the main script at the end just calling these functions. This makes it easier to tweak a function 上ce and it then be used multiple times.

的perimeter walls are created using the 蟒蛇 function “CreateWalls”具有不同的宽度和高度。它们的内边缘和城垛都有木制人行道。

我的世界Python城堡#4

的Keep is created using “CreateKeep”依次调用“CreateWalls”. 的Keep is just a set of walls that are taller than the others. It has battlements and a wooden walkway.

我的世界Python城堡#3

的inside of the keep has floors splitting it up into a number of levels. You can make the Keep taller by telling “CreateKeep” to use more levels.

我的世界Python城堡#2

的“CreateKeep”功能还可以在地面墙壁上打一个孔以形成门口。

我的世界Python城堡#1

的function “CreateWindows”将窗口添加到Keep的所有侧面。小心你别’t fall out!

In general the 蟒蛇 API is really easy to use. 的hard bit is working out the x,y,z co-ordinates so that blocks end up in the correct place.

未来升级

的obvious thing that is missing is a way of getting from 上e Keep floor level to another. This really needs a new function to create a hole in the floor with some steps which can be called for each level as it is created in the “CreateKeep” function.

分享。

11条留言

  1. 嘿,我对您的代码做了一些更改,使城堡更凉爽(仅使用了积木)代替水,它使用熔岩和木材,而是使用发光石,因此可以照亮城堡。新代码将从我的pi(用户名BRYANPI)复制并粘贴

  2. 奇怪的! MC跑得很慢– unplayably slowly –在运行脚本后大约5分钟,然后恢复到正常速度。在此期间,我退出了MC并重新启动。通常说“产生世界。建筑地形”当我开始演奏时,它会显示一条我没有的新消息’t seen before: “Chunking…”
    任何想法是什么原因造成的?无论如何现在看来还是可以的!

    • 听起来您正在使用Python 3,但是我的脚本包含Python 2样式的Print语句。一世’已更新了脚本,因此它可以与Python 2和Python 3一起使用。再次下载它,它应该可以正常工作。

    • 原则上,它可以在使用Python 2的任何体育彩票31选7模型上运行。可能需要对print语句进行一些修改才能在Python 3上运行。’如果有机会,我会更新。

发表评论

该网站使用Akismet减少垃圾邮件。 了解如何处理您的评论数据.