第二课 让程序聪明一点

第二课 让程序聪明一点

我在哪里?

上一课,我们学习了获得玩家位置和获取某个位置的方块ID的办法。
我们先来写一个取得我们脚下方块ID的程序。

from mcpi.minecraft import Minecraft
mc = Minecraft.create("robin-pc")

mc.postToChat("Where am I?")

pos = mc.player.getTilePos()
x = pos.x
y = pos.y
z = pos.z

block = mc.getBlock(x, y, z)
print(block)
0

上面的getBlock方法返回值是0,表示玩家站在空气里。或者返回9,表示玩家在水里

在编程中,返回值就是函数返回的结果

if block == 0: 
    mc.postToChat("I am in the air.")
else:
    mc.postToChat("I am not in the air.")

blockonfoot = mc.getBlock(x, y-1, z)
if blockonfoot == 0:
    mc.postToChat("I am really in the air.")
else:
    mc.postToChat(f"I am standing on a {blockonfoot} block.")

去一个可以去的地方

if 语句称为条件判断语句,几乎存在于所有编程语言中。
注意 python 的 if 的后面要跟一个冒号 : 。
if 语句的意思是当 if 后边到 : 之间的表达式成立(为真)时,执行 if 语句下属代码块代码。
python 用缩进(离行首的空格或TAB数量)表示代码块的层级,同一层级的代码。

上面的程序里,我们还用到 mc 的 postToChat 方法

好的,那么接下来,我们要用代码进行一次随机旅行。但我们不希望我们钻进土里,对不对?

import random

distance = 10
x += random.randint(-distance, distance)
y += random.randint(-distance, distance)
z += random.randint(-distance, distance)

block = mc.getBlock(x, y, z)
if block == 0 :
    mc.postToChat("Now, we travel")
    mc.player.setTilePos(x, y, z)
else :
    mc.postToChat("Not a good idea.")

上边这个程序仍然有个问题,我可能会飞到空中,然后落下来。
为了避免这种情况,我们要判断一下脚下是不是悬空的,悬空的地方也不能去。
所以我们要用一个并且的关系,因为我们这里有两个条件,且两个条件同时成立时才能飞过去。

x += random.randint(-distance, distance)
y += random.randint(-distance, distance)
z += random.randint(-distance, distance)

block = mc.getBlock(x, y, z)
blockonfoot = mc.getBlock(x, y-1, z)
if block == 0 and blockonfoot != 0 :
    mc.postToChat("Now, we travel")
    mc.player.setTilePos(x, y, z)
else :
    mc.postToChat("Not a good idea.")

上面这个程序是没问题的,但成功率太低,100次不一定能跳出去一次。
能不能让程序先试探,等获取到合适的位置才跳过去?
答案是肯定的,重复是计算机相对人来讲最大的本事。
计算机术语中同一程序的重复执行叫循环(loop)

一般编程语言会提供条件循环和计数循环两种循环方式。
Python也有 while 循环和 for 循环两种作为对应。
这里我们用 while 循环就可以了,它的意思是当满足某种条件时就一直执行某段代码,直到条件不满足为止。
我们用 while 循环改造一下上面的程序。

x += random.randint(-distance, distance)
y += random.randint(-distance, distance)
z += random.randint(-distance, distance)

block = mc.getBlock(x, y, z)
blockonfoot = mc.getBlock(x, y-1, z)

count = 1

while block != 0 or blockonfoot == 0:
    count += 1
    x += random.randint(-distance, distance)
    y += random.randint(-distance, distance)
    z += random.randint(-distance, distance)

    block = mc.getBlock(x, y, z)
    blockonfoot = mc.getBlock(x, y-1, z)

    mc.postToChat(f"Not a good idea. we are not going to ({x}, {y}, {z}) where is {block} on a {blockonfoot} block")


if block == 0 and blockonfoot != 0 :
    mc.postToChat(f"Now, we travel to ({x}, {y}, {z}) where is {block} on a {blockonfoot} block after {count} times of temptation")
    mc.player.setTilePos(x, y, z)
    

上面的修改可以让我们减少了点鼠标的工作量,但不能避免我们掉水里。
我们还要继续修改。
另外一点,上面的程序稍显麻烦,因为同样的代码,敲了两次。
我们可以适当改造一下,让程序循环进来就计算,达到条件就跳出循环。

count = 0

pos = mc.player.getTilePos()
x = pos.x
y = pos.y
z = pos.z
print(pos)
distance = 10
while True:
    count += 1
    x1 = x + random.randint(-distance, distance)
    y1 = y + random.randint(-distance, distance)
    z1 = z + random.randint(-distance, distance)

    block = mc.getBlock(x1, y1, z1)
    blockonfoot = mc.getBlock(x1, y1-1, z1)

    if block == 0 and blockonfoot != 0 and blockonfoot != 9 :
        break;

    mc.postToChat(f"Not a good idea. we are not going to ({x1}, {y1}, {z1}) where is {block} on a {blockonfoot} block")


mc.postToChat(f"Now, we travel to ({x1}, {y1}, {z1}) where is {block} on a {blockonfoot} block after {count} times of temptation")
mc.player.setTilePos(x1, y1, z1)
    
Vec3(-119,0,18)

while True 意味着循环永远进行。
在计算机中循环的使用有一个隐忧,就是死循环(dead loop)。意思是永远循环执行同样的代码,跳不出去。
为避免这种情况,循环一定会有一个结束条件。并且循环过程中,程序执行的结果应会影响结束条件表达式的结果。
但循环中止条件不一定会得到满足,如果循环中止条件永远得不到满足,程序仍然会死循环。
这个在初学编程时一定要注意。

上面的程序是不是有可能死循环呢?
有一些极端条件,可能会的,比如充满水的世界。

让玩家飞一会儿

上面的程序只跳一次,不过瘾怎么办。
能让他自动跳 10 个地方吗?
我们可以使用计数循环。在 Python 中可以用 for 循环语句。
但 Pythod 的 for 循环语句并不是简单的计数循环。
它的实质是对一个序列从头到位的遍历,比如我们有一组数字,1到10,我们可以让程序把这组数字挨个取出,于是程序就跑了10遍。

计算机里数字往往是从 0 开始数的。
range 是python中常用来生成数字序列的函数,默认情况下它生成的是 0 开始的序列。
比如 range(10) 表示生成 0 ~ 9 的序列

from mcpi.minecraft import Minecraft
import random
import time

mc = Minecraft.create("robin-pc")

pos = mc.player.getTilePos()
x = pos.x
y = pos.y
z = pos.z
print(pos)
distance = 50
for i in range(10) :
    print(f"第{i}次")
    count = 0
    while True:
        count += 1
        x1 = x + random.randint(-distance, distance)
        y1 = y + random.randint(-distance, distance)
        z1 = z + random.randint(-distance, distance)

        block = mc.getBlock(x1, y1, z1)
        blockonfoot = mc.getBlock(x1, y1-1, z1)

        if block == 0 and blockonfoot != 0 and blockonfoot != 9 :
            break;

        print(f"Not a good idea. we are not going to ({x1}, {y1}, {z1}) where is {block} on a {blockonfoot} block")

    x = x1
    y = y1
    z = z1
    msg = f"Now, we travel to ({x}, {y}, {z}) where is {block} on a {blockonfoot} block after {count} times of temptation"
    print(msg)
    mc.postToChat(msg)
    mc.player.setTilePos(x, y, z)
    # 到了停5秒
    time.sleep(5)
1, 51, 96) where is 0 on a 0 block
Not a good idea. we are not going to (-82, 18, 52) where is 0 on a 0 block
Not a good idea. we are not going to (-81, -26, 31) where is 16 on a 16 block
Not a good idea. we are not going to (-50, 47, 64) where is 0 on a 0 block
Not a good idea. we are not going to (-65, 26, 63) where is 0 on a 0 block
Not a good idea. we are not going to (-108, -40, 52) where is 1 on a 1 block
Not a good idea. we are not going to (-101, -23, 34) where is 1 on a 1 block
Not a good idea. we are not going to (-51, 36, 27) where is 0 on a 0 block
Not a good idea. we are not going to (-113, -15, 102) where is 1 on a 1 block
Not a good idea. we are not going to (-89, -40, 64) where is 1 on a 1 block
Not a good idea. we are not going to (-108, 48, 94) where is 0 on a 0 block
Not a good idea. we are not going to (-129, -5, 109) where is 3 on a 3 block
Not a good idea. we are not going to (-96, 52, 75) where is 0 on a 0 block
Not a good idea. we are not going to (-111, -39, 52) where is 1 on a 1 block
Not a good idea. we are not going to (-59, -38, 48) where is 1 on a 1 block
Not a good idea. we are not going to (-63, -32, 103) where is 1 on a 1 block
Not a good idea. we are not going to (-112, -12, 43) where is 1 on a 1 block
Not a good idea. we are not going to (-94, 30, 35) where is 0 on a 0 block
Not a good idea. we are not going to (-120, 52, 27) where is 0 on a 0 block
Not a good idea. we are not going to (-101, 24, 106) where is 0 on a 0 block
Not a good idea. we are not going to (-113, -22, 22) where is 1 on a 1 block
Not a good idea. we are not going to (-39, 11, 77) where is 0 on a 0 block
Not a good idea. we are not going to (-76, -29, 53) where is 1 on a 1 block
Not a good idea. we are not going to (-40, 6, 55) where is 2 on a 3 block
Not a good idea. we are not going to (-99, 24, 13) where is 0 on a 0 block
Not a good idea. we are not going to (-119, -20, 77) where is 0 on a 0 block
Not a good idea. we are not going to (-121, -26, 52) where is 1 on a 1 block
Not a good idea. we are not going to (-103, -13, 20) where is 1 on a 1 block
Not a good idea. we are not going to (-133, 22, 11) where is 0 on a 0 block
Not a good idea. we are not going to (-62, -29, 50) where is 1 on a 1 block
Not a good idea. we are not going to (-100, 39, 47) where is 0 on a 0 block
Not a good idea. we are not going to (-137, -21, 87) where is 0 on a 0 block
Not a good idea. we are not going to (-93, 6, 73) where is 24 on a 24 block
Not a good idea. we are not going to (-74, -28, 43) where is 1 on a 1 block
Not a good idea. we are not going to (-92, 10, 55) where is 0 on a 0 block
Not a good idea. we are not going to (-114, -19, 51) where is 1 on a 1 block
Not a good idea. we are not going to (-74, -37, 65) where is 1 on a 1 block
Not a good idea. we are not going to (-76, 42, 12) where is 0 on a 0 block
Not a good idea. we are not going to (-107, -28, 53) where is 1 on a 1 block
Not a good idea. we are not going to (-47, 2, 85) where is 1 on a 1 block
Not a good idea. we are not going to (-79, 21, 18) where is 0 on a 0 block
Not a good idea. we are not going to (-94, 42, 63) where is 0 on a 0 block
Not a good idea. we are not going to (-87, 13, 68) where is 0 on a 0 block
Not a good idea. we are not going to (-137, -1, 97) where is 0 on a 9 block
Not a good idea. we are not going to (-55, -33, 92) where is 1 on a 1 block
Not a good idea. we are not going to (-94, 33, 97) where is 0 on a 0 block
Not a good idea. we are not going to (-119, 18, 43) where is 0 on a 0 block
Not a good idea. we are not going to (-47, -17, 65) where is 1 on a 1 block
Not a good idea. we are not going to (-128, -25, 97) where is 1 on a 1 block
Not a good idea. we are not going to (-69, 27, 62) where is 0 on a 0 block
Not a good idea. we are not going to (-119, -23, 48) where is 1 on a 1 block
Not a good idea. we are not going to (-57, 29, 103) where is 0 on a 0 block
Not a good idea. we are not going to (-122, -17, 108) where is 1 on a 1 block
Not a good idea. we are not going to (-84, -40, 84) where is 1 on a 1 block
Not a good idea. we are not going to (-52, -29, 72) where is 1 on a 1 block
Now, we travel to (-86, -23, 16) where is 0 on a 1 block after 125 times of temptation
第5次
Not a good idea. we are not going to (-114, -55, 43) where is 1 on a 1 block
Not a good idea. we are not going to (-117, 1, 33) where is 3 on a 3 block
Not a good idea. we are not going to (-87, -1, -20) where is 24 on a 1 block
Not a good idea. we are not going to (-104, -40, 39) where is 1 on a 1 block
Not a good idea. we are not going to (-76, -1, 57) where is 12 on a 12 block
Not a good idea. we are not going to (-115, -42, -1) where is 1 on a 1 block
Not a good idea. we are not going to (-75, -66, -10) where is 0 on a 0 block
Not a good idea. we are not going to (-51, -30, 59) where is 3 on a 1 block
Not a good idea. we are not going to (-91, -63, 3) where is 7 on a 7 block
Not a good idea. we are not going to (-89, -71, -24) where is 0 on a 0 block
Not a good idea. we are not going to (-123, -24, -30) where is 1 on a 1 block
Not a good idea. we are not going to (-130, -21, 32) where is 1 on a 1 block
Now, we travel to (-38, -27, 5) where is 0 on a 1 block after 13 times of temptation
第6次
Not a good idea. we are not going to (-14, -33, 9) where is 0 on a 0 block
Not a good idea. we are not going to (-9, -22, -13) where is 1 on a 1 block
Not a good idea. we are not going to (-53, 23, -6) where is 0 on a 0 block
Not a good idea. we are not going to (-34, 3, -32) where is 1 on a 1 block
Not a good idea. we are not going to (-76, -67, 41) where is 0 on a 0 block
Not a good idea. we are not going to (-19, -38, 12) where is 16 on a 16 block
Not a good idea. we are not going to (-40, -58, 6) where is 1 on a 1 block
Not a good idea. we are not going to (-29, -4, 33) where is 1 on a 1 block
Not a good idea. we are not going to (-8, -41, 2) where is 1 on a 1 block
Not a good idea. we are not going to (-6, -15, 43) where is 1 on a 1 block
Not a good idea. we are not going to (-41, -47, -19) where is 1 on a 1 block
Not a good idea. we are not going to (-17, -65, -40) where is 0 on a 0 block
Not a good idea. we are not going to (6, -20, -42) where is 1 on a 1 block
Not a good idea. we are not going to (4, -72, 39) where is 0 on a 0 block
Not a good idea. we are not going to (9, -29, 21) where is 1 on a 1 block
Not a good idea. we are not going to (-21, 19, -11) where is 0 on a 0 block
Not a good idea. we are not going to (-26, -64, 30) where is 7 on a 0 block
Not a good idea. we are not going to (-85, -49, -36) where is 1 on a 1 block
Not a good idea. we are not going to (-72, -27, -44) where is 3 on a 3 block
Not a good idea. we are not going to (-82, -46, 44) where is 1 on a 1 block
Not a good idea. we are not going to (-39, -45, -21) where is 1 on a 1 block
Not a good idea. we are not going to (-26, -23, -13) where is 1 on a 1 block
Not a good idea. we are not going to (6, 2, -6) where is 1 on a 1 block
Not a good idea. we are not going to (-11, -65, 38) where is 0 on a 0 block
Not a good idea. we are not going to (-35, -28, 31) where is 0 on a 0 block
Not a good idea. we are not going to (-44, -57, -16) where is 1 on a 1 block
Not a good idea. we are not going to (-28, -52, 54) where is 1 on a 1 block
Not a good idea. we are not going to (-11, -45, 30) where is 1 on a 1 block
Not a good idea. we are not going to (-73, -52, 46) where is 1 on a 1 block
Not a good idea. we are not going to (-32, -45, -38) where is 1 on a 1 block
Not a good idea. we are not going to (-31, -6, -5) where is 1 on a 1 block
Not a good idea. we are not going to (-28, -22, -36) where is 0 on a 0 block
Not a good idea. we are not going to (-72, -42, 30) where is 1 on a 1 block
Not a good idea. we are not going to (-7, -64, -30) where is 7 on a 0 block
Now, we travel to (-6, 14, 14) where is 0 on a 161 block after 35 times of temptation
第7次
Not a good idea. we are not going to (-51, -31, 16) where is 1 on a 1 block
Not a good idea. we are not going to (17, 25, 44) where is 0 on a 0 block
Not a good idea. we are not going to (-55, 38, 30) where is 0 on a 0 block
Not a good idea. we are not going to (30, 9, 5) where is 0 on a 0 block
Not a good idea. we are not going to (41, 5, -12) where is 12 on a 12 block
Not a good idea. we are not going to (35, 33, 58) where is 0 on a 0 block
Not a good idea. we are not going to (-45, 37, 56) where is 0 on a 0 block
Not a good idea. we are not going to (34, -8, 25) where is 1 on a 1 block
Not a good idea. we are not going to (26, 32, 25) where is 0 on a 0 block
Not a good idea. we are not going to (-24, 4, 55) where is 3 on a 1 block
Not a good idea. we are not going to (-39, 35, 24) where is 0 on a 0 block
Not a good idea. we are not going to (34, -33, -32) where is 1 on a 1 block
Not a good idea. we are not going to (11, -2, -9) where is 24 on a 24 block
Not a good idea. we are not going to (33, 30, 45) where is 0 on a 0 block
Not a good idea. we are not going to (4, 61, -24) where is 0 on a 0 block
Not a good idea. we are not going to (31, -5, -10) where is 1 on a 1 block
Not a good idea. we are not going to (22, -36, 12) where is 1 on a 1 block
Not a good idea. we are not going to (-43, -36, 40) where is 1 on a 1 block
Not a good idea. we are not going to (-9, 53, 1) where is 0 on a 0 block
Not a good idea. we are not going to (28, 64, 52) where is 0 on a 0 block
Not a good idea. we are not going to (-51, 63, 5) where is 0 on a 0 block
Not a good idea. we are not going to (36, -4, -7) where is 24 on a 1 block
Not a good idea. we are not going to (-18, -11, 57) where is 1 on a 1 block
Not a good idea. we are not going to (-17, 31, -11) where is 0 on a 0 block
Not a good idea. we are not going to (-43, 57, 5) where is 0 on a 0 block
Not a good idea. we are not going to (25, 0, -28) where is 24 on a 24 block
Not a good idea. we are not going to (-25, -17, 64) where is 1 on a 1 block
Now, we travel to (-40, -27, -13) where is 0 on a 1 block after 28 times of temptation
第8次
Not a good idea. we are not going to (-26, -60, -43) where is 1 on a 1 block
Not a good idea. we are not going to (-46, -75, -2) where is 0 on a 0 block
Not a good idea. we are not going to (-43, -28, -62) where is 1 on a 1 block
Not a good idea. we are not going to (-56, -26, 21) where is 1 on a 1 block
Not a good idea. we are not going to (-9, -10, -11) where is 1 on a 1 block
Not a good idea. we are not going to (-72, 0, -3) where is 12 on a 12 block
Not a good idea. we are not going to (-87, -16, -31) where is 16 on a 1 block
Not a good idea. we are not going to (-23, 12, 36) where is 0 on a 0 block
Not a good idea. we are not going to (-83, 1, -54) where is 12 on a 12 block
Not a good idea. we are not going to (-17, -4, 23) where is 1 on a 1 block
Not a good idea. we are not going to (2, -77, 18) where is 0 on a 0 block
Not a good idea. we are not going to (-16, 22, -34) where is 12 on a 24 block
Not a good idea. we are not going to (3, -17, -51) where is 1 on a 1 block
Not a good idea. we are not going to (9, -8, -25) where is 1 on a 1 block
Not a good idea. we are not going to (-79, 4, 25) where is 0 on a 0 block
Not a good idea. we are not going to (-32, 12, -56) where is 24 on a 24 block
Not a good idea. we are not going to (10, -5, -50) where is 1 on a 1 block
Not a good idea. we are not going to (-89, -37, 29) where is 1 on a 1 block
Not a good idea. we are not going to (-86, -29, 30) where is 1 on a 1 block
Not a good idea. we are not going to (-1, -44, -52) where is 1 on a 1 block
Not a good idea. we are not going to (-41, -59, 5) where is 1 on a 1 block
Not a good idea. we are not going to (-24, -21, -57) where is 1 on a 1 block
Not a good idea. we are not going to (-16, -67, 27) where is 0 on a 0 block
Not a good idea. we are not going to (-60, 20, -56) where is 0 on a 0 block
Not a good idea. we are not going to (-25, -13, -23) where is 1 on a 1 block
Not a good idea. we are not going to (-59, -12, -28) where is 0 on a 0 block
Not a good idea. we are not going to (-7, -46, 11) where is 1 on a 1 block
Not a good idea. we are not going to (-60, -2, -23) where is 1 on a 1 block
Not a good idea. we are not going to (-67, -57, -59) where is 1 on a 1 block
Not a good idea. we are not going to (-8, -26, 8) where is 1 on a 1 block
Not a good idea. we are not going to (-59, -27, -28) where is 1 on a 1 block
Not a good idea. we are not going to (-38, -51, -55) where is 1 on a 1 block
Not a good idea. we are not going to (-12, -12, -3) where is 1 on a 1 block
Not a good idea. we are not going to (-4, -28, -10) where is 1 on a 1 block
Not a good idea. we are not going to (-25, -54, -12) where is 1 on a 1 block
Not a good idea. we are not going to (-65, -22, -32) where is 1 on a 1 block
Not a good idea. we are not going to (-90, 12, 28) where is 0 on a 0 block
Not a good idea. we are not going to (-65, -59, -55) where is 1 on a 1 block
Not a good idea. we are not going to (-68, -42, -15) where is 1 on a 1 block
Not a good idea. we are not going to (-70, -43, -42) where is 1 on a 1 block
Not a good idea. we are not going to (-49, -10, 28) where is 1 on a 1 block
Not a good idea. we are not going to (-5, -42, -17) where is 1 on a 1 block
Not a good idea. we are not going to (-40, 9, -53) where is 12 on a 12 block
Not a good idea. we are not going to (-27, -67, -21) where is 0 on a 0 block
Not a good idea. we are not going to (-75, -26, -44) where is 1 on a 1 block
Not a good idea. we are not going to (-37, 19, 7) where is 0 on a 0 block
Not a good idea. we are not going to (-42, -56, 25) where is 1 on a 1 block
Not a good idea. we are not going to (-23, -18, 20) where is 1 on a 1 block
Not a good idea. we are not going to (-64, -40, -50) where is 1 on a 1 block
Not a good idea. we are not going to (-57, -31, -62) where is 1 on a 1 block
Not a good idea. we are not going to (-29, 21, -16) where is 0 on a 0 block
Not a good idea. we are not going to (-22, -68, -6) where is 0 on a 0 block
Not a good idea. we are not going to (-79, -21, 26) where is 1 on a 1 block
Not a good idea. we are not going to (-62, 3, -56) where is 0 on a 0 block
Not a good idea. we are not going to (-77, 10, -25) where is 0 on a 0 block
Not a good idea. we are not going to (-60, -35, -14) where is 1 on a 1 block
Not a good idea. we are not going to (-48, -60, -24) where is 1 on a 7 block
Not a good idea. we are not going to (-61, -9, -46) where is 1 on a 1 block
Not a good idea. we are not going to (-55, -56, -11) where is 1 on a 1 block
Not a good idea. we are not going to (-65, -24, -10) where is 1 on a 1 block
Not a good idea. we are not going to (-52, -18, 4) where is 1 on a 1 block
Not a good idea. we are not going to (-41, 0, 10) where is 24 on a 24 block
Not a good idea. we are not going to (-62, 0, -57) where is 12 on a 1 block
Not a good idea. we are not going to (-77, -30, -1) where is 1 on a 1 block
Not a good idea. we are not going to (-43, 23, 37) where is 0 on a 0 block
Not a good idea. we are not going to (-54, -13, -3) where is 1 on a 1 block
Not a good idea. we are not going to (-38, -63, -32) where is 7 on a 7 block
Not a good idea. we are not going to (-70, -22, 21) where is 1 on a 1 block
Not a good idea. we are not going to (-51, -50, 26) where is 1 on a 1 block
Not a good idea. we are not going to (-6, 5, 11) where is 1 on a 1 block
Not a good idea. we are not going to (-84, -38, -30) where is 1 on a 1 block
Not a good idea. we are not going to (-90, -62, 33) where is 7 on a 7 block
Not a good idea. we are not going to (-51, 20, -26) where is 0 on a 0 block
Not a good idea. we are not going to (-64, -69, -50) where is 0 on a 0 block
Not a good idea. we are not going to (-70, -20, -31) where is 1 on a 1 block
Not a good idea. we are not going to (-61, -26, -18) where is 1 on a 1 block
Not a good idea. we are not going to (-60, -35, -49) where is 1 on a 1 block
Not a good idea. we are not going to (5, -77, -15) where is 0 on a 0 block
Not a good idea. we are not going to (5, -30, -41) where is 1 on a 1 block
Not a good idea. we are not going to (-84, -33, -9) where is 1 on a 1 block
Not a good idea. we are not going to (-11, -77, -40) where is 0 on a 0 block
Not a good idea. we are not going to (-2, 10, -16) where is 24 on a 24 block
Not a good idea. we are not going to (-35, -25, -23) where is 1 on a 1 block
Not a good idea. we are not going to (-84, -73, 36) where is 0 on a 0 block
Not a good idea. we are not going to (8, -63, -40) where is 1 on a 7 block
Not a good idea. we are not going to (6, -64, -16) where is 7 on a 0 block
Not a good idea. we are not going to (-4, -26, 0) where is 1 on a 1 block
Not a good idea. we are not going to (-36, -70, -10) where is 0 on a 0 block
Not a good idea. we are not going to (-30, -32, -55) where is 1 on a 1 block
Not a good idea. we are not going to (-46, -24, 32) where is 16 on a 16 block
Not a good idea. we are not going to (-8, -19, -45) where is 0 on a 0 block
Not a good idea. we are not going to (-52, -4, 36) where is 24 on a 1 block
Not a good idea. we are not going to (-69, -69, -9) where is 0 on a 0 block
Not a good idea. we are not going to (-47, 12, -24) where is 0 on a 0 block
Not a good idea. we are not going to (-23, -57, -54) where is 1 on a 1 block
Not a good idea. we are not going to (-70, -20, -41) where is 1 on a 1 block
Now, we travel to (-72, 0, 29) where is 0 on a 12 block after 97 times of temptation
第9次
Not a good idea. we are not going to (-88, -38, 39) where is 1 on a 1 block
Not a good idea. we are not going to (-119, 48, 42) where is 0 on a 0 block
Not a good idea. we are not going to (-58, 46, 27) where is 0 on a 0 block
Not a good idea. we are not going to (-96, -35, 1) where is 1 on a 1 block
Not a good idea. we are not going to (-42, -46, 6) where is 1 on a 1 block
Not a good idea. we are not going to (-95, 46, -4) where is 0 on a 0 block
Not a good idea. we are not going to (-36, -18, 79) where is 1 on a 1 block
Not a good idea. we are not going to (-98, 33, -5) where is 0 on a 0 block
Not a good idea. we are not going to (-72, -41, 22) where is 1 on a 1 block
Not a good idea. we are not going to (-110, -20, -21) where is 1 on a 1 block
Not a good idea. we are not going to (-81, 44, 47) where is 0 on a 0 block
Not a good idea. we are not going to (-45, 37, 68) where is 0 on a 0 block
Not a good idea. we are not going to (-58, 45, 42) where is 0 on a 0 block
Not a good idea. we are not going to (-37, 16, 3) where is 0 on a 0 block
Not a good idea. we are not going to (-120, 9, -1) where is 1 on a 1 block
Not a good idea. we are not going to (-30, -37, 62) where is 1 on a 1 block
Not a good idea. we are not going to (-105, -1, 15) where is 2 on a 3 block
Not a good idea. we are not going to (-70, 12, 9) where is 0 on a 0 block
Not a good idea. we are not going to (-120, -29, 62) where is 1 on a 1 block
Not a good idea. we are not going to (-29, -30, -20) where is 1 on a 1 block
Not a good idea. we are not going to (-33, -35, 55) where is 1 on a 1 block
Not a good idea. we are not going to (-62, 33, 29) where is 0 on a 0 block
Not a good idea. we are not going to (-102, -42, -15) where is 1 on a 1 block
Not a good idea. we are not going to (-77, 7, 49) where is 0 on a 0 block
Not a good idea. we are not going to (-120, -48, 25) where is 1 on a 1 block
Not a good idea. we are not going to (-90, -17, 39) where is 1 on a 1 block
Not a good idea. we are not going to (-61, -50, 68) where is 1 on a 1 block
Not a good idea. we are not going to (-83, 38, 68) where is 0 on a 0 block
Not a good idea. we are not going to (-77, 42, 36) where is 0 on a 0 block
Not a good idea. we are not going to (-117, 31, 53) where is 0 on a 0 block
Not a good idea. we are not going to (-38, 30, 78) where is 0 on a 0 block
Not a good idea. we are not going to (-94, -35, 69) where is 1 on a 1 block
Not a good idea. we are not going to (-71, 45, -16) where is 0 on a 0 block
Now, we travel to (-119, 0, 18) where is 0 on a 2 block after 34 times of temptation

建一座金字塔

有了循环,很多事情就好办了,比如建造大型建筑物。
我们现在就尝试一下,在我们脚下建一座金字塔。
金字塔的俯视图是一个nxn的正方形,然后每一层的上面碟一个(n-2)x(n-2)的正方形,直到n-2=1为止。
我们选定自己站的位置作为中心点,于是就可以往东西南北各延申出去一个距离 m,那么整个基座就是 (2xm+1)x (2xm+1)的正方形。
然后再往上一层层碟,一共应该是 m+1 层

pos = mc.player.getTilePos()
x = pos.x
y = pos.y
z = pos.z
print(f"我们自己在的位置{pos}")
floors = 10  # 金字塔层数
blockType = 24 # 金字塔的材料
for m in range(floors) :
    print(f"修建第{m+1}层")
    e = floors - (m + 1) # 中心的到边缘距离
    a = 2 * e + 1
    print(f"面积是{a*a}")
    # 垒一层方块
    mc.setBlocks(x - e, y, z - e, x + e, y, z + e, blockType)
    y += 1
mc.player.setTilePos(x, y, z)
我们自己在的位置Vec3(-83,0,48)
修建第1层
面积是361
修建第2层
面积是289
修建第3层
面积是225
修建第4层
面积是169
修建第5层
面积是121
修建第6层
面积是81
修建第7层
面积是49
修建第8层
面积是25
修建第9层
面积是9
修建第10层
面积是1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值