minecraft-障碍跳跃收集方块-python

# -*- coding: cp936 -*-
import mcpi.minecraft as minecraft
import mcpi.block as block
import mcpi.minecraftstuff as minecraftstuff
import time
import random
import thread
ARENAX = 10
ARENAZ = 20
ARENAY = 3
def createArena(pos):
    mc = minecraft.Minecraft.create()
    mc.setBlocks(
        pos.x-1,pos.y,pos.z-1,
        pos.x + ARENAX + 1,pos.y - 3,
        pos.z + ARENAZ + 1,
        block.GRASS.id)
    mc.setBlocks(
        pos.x - 1,pos.y + 1,pos.z - 1,
        pos.x + ARENAX + 1,pos.y + ARENAY,
        pos.z + ARENAZ + 1,
        block.GLASS.id)
    mc.setBlocks(
        pos.x,pos.y + 1,pos.z,
        pos.x + ARENAX,pos.y + ARENAY,
        pos.z + ARENAZ,
        block.AIR.id)


def theWall(arenaPos,wallZPos):
    mc = minecraft.Minecraft.create()
    wallPos = minecraft.Vec3(arenaPos.x,arenaPos.y,arenaPos.z + wallZPos)
    wallBlocks = []
    for x in range(0,ARENAX + 1):
        for y in range(1,ARENAY):
            wallBlocks.append(minecraftstuff.ShapeBlock(
                x,y,0,block.BRICK_BLOCK.id))
    wallShape = minecraftstuff.MinecraftShape(mc,wallPos,wallBlocks)
    while not gameover:
        for i in range(3):
            wallShape.moveBy(0,1,0)
            time.sleep(1.3)
        for i in range(3):
            wallShape.moveBy(0,-1,0)
            time.sleep(1.3)
def theRiver(arenaPos,riverZPos):
    mc = minecraft.Minecraft.create()

    BRIDGEWIDTH = 2
    mc.setBlocks(
        arenaPos.x,
        arenaPos.y-2,
        arenaPos.z+riverZPos,
        arenaPos.x + ARENAX,
        arenaPos.y,
        arenaPos.z + riverZPos + RIVERWIDTH - 1,
        block.AIR.id)
    mc.setBlocks(
        arenaPos.x,
        arenaPos.y - 2,
        arenaPos.z + riverZPos,
        arenaPos.x + ARENAX,
        arenaPos.y - 2,
        arenaPos.z + riverZPos + RIVERWIDTH - 1,
        block.WATER.id)
    bridgePos = minecraft.Vec3(arenaPos.x,arenaPos.y,arenaPos.z + riverZPos + 1)
    bridgeBlocks = []
    for x in range(0,BRIDGEWIDTH):
        for z in range(0,RIVERWIDTH - 2):
            bridgeBlocks.append(minecraftstuff.ShapeBlock(x,0,z,
                                                          block.STONE_BRICK.id))
    bridgeShape = minecraftstuff.MinecraftShape(mc,bridgePos,bridgeBlocks)
    steps = ARENAX - BRIDGEWIDTH
    while not gameover:
        for left in range(steps):
            bridgeShape.moveBy(1,0,0)
            time.sleep(1)
        for right in range(steps):
            bridgeShape.moveBy(-1,0,0)
            time.sleep(1)

def theHoles(arenaPos,holesZPos,HOLESWIDTH):
    mc = minecraft.Minecraft.create()
    HOLES = 15

    while not gameover:
        holes=[]
        for count in range(HOLES):
            x = random.randint(arenaPos.x,arenaPos.x + ARENAX)
            z = random.randint(arenaPos.z + holesZPos,
                               arenaPos.z + holesZPos + HOLESWIDTH)
            if not (mc.getBlock(x,arenaPos.y,z) == block.DIAMOND_BLOCK.id):
                holes.append(minecraft.Vec3(x,arenaPos.y,z))
        for hole in holes:
            mc.setBlock(hole.x,hole.y,hole.z,
                         block.WOOL.id,15)
        time.sleep(0.5)
        for hole in holes:
            mc.setBlocks(hole.x,hole.y,hole.z,
                         hole.x,hole.y-2,hole.z,
                         block.AIR.id)
        time.sleep(2)

        for hole in holes:
            mc.setBlocks(hole.x,hole.y,hole.z,
                         hole.x,hole.y-2,hole.z,
                         block.GRASS.id)
        time.sleep(2)
def createDiamonds(arenaPos,number):
    mc = minecraft.Minecraft.create()
    x = random.sample((range(arenaPos.x,arenaPos.x + ARENAX)),1)
    z = random.sample((range(arenaPos.z,arenaPos.z + RIVERZ - 1)),1)
    mc.setBlock(x,arenaPos.y,z,block.DIAMOND_BLOCK.id)
    for diamond in range(number-1):
        x = random.sample((range(arenaPos.x,arenaPos.x + ARENAX)),1)
        z = random.sample((range(arenaPos.z + RIVERZ + RIVERWIDTH,
                                 arenaPos.z + ARENAZ)),1)
        mc.setBlock(x,arenaPos.y,z,block.DIAMOND_BLOCK.id)
        time.sleep(0.1)
LEVELS = 3
DIAMONDS = [3,5,8]
TIMEOUTS = [30,25,20]
level = 0
points = 0

gameover = False
mc = minecraft.Minecraft.create()
arenaPos = mc.player.getTilePos()
createArena(arenaPos)
WALLZ = 10
thread.start_new_thread(theWall,(arenaPos,WALLZ))
WALL2Z = 13
thread.start_new_thread(theWall,(arenaPos,WALL2Z))
RIVERZ = 4
RIVERWIDTH = 4
thread.start_new_thread(theRiver,(arenaPos,RIVERZ))
HOLESZ = 15
HOLESWIDTH = 3
thread.start_new_thread(theHoles,(arenaPos,HOLESZ,HOLESWIDTH))

#游戏循环
while not gameover:
    time.sleep(0.01)
    mc.player.setPos(arenaPos.x+4,arenaPos.y+2,arenaPos.z+2)
    createDiamonds(arenaPos,DIAMONDS[level])
    diamondsLeft = DIAMONDS[level]
    start = time.time()
    levelComplete = False
    #关卡循环
    while not gameover and not levelComplete:
        time.sleep(0.1)

        pos = mc.player.getTilePos()
        if pos.y .y:
            mc.player.setPos(arenaPos.x+4,arenaPos.y+2,arenaPos.z+2)
        if pos.z == arenaPos.z + ARENAZ and diamondsLeft == 0:
            levelComplete = True
        secondsLeft = TIMEOUTS[level] - (time.time() - start)
        if secondsLeft <= 5.1 and secondsLeft >= 0:
            time.sleep(0.90)
            mc.postToChat(str(int(round(secondsLeft))))
        if secondsLeft < 0:
            gameover = True
            mc.postToChat("叮咚...时间到了")
        hits = mc.events.pollBlockHits()
        for hit in hits:
            blockHitType = mc.getBlock(hit.pos.x,hit.pos.y,hit.pos.z)
            if blockHitType == block.DIAMOND_BLOCK.id:
                mc.setBlock(hit.pos.x,hit.pos.y,hit.pos.z,block.AIR.id)
                diamondsLeft -= 1
    if levelComplete:
            points = points + (DIAMONDS[level]*int(secondsLeft))
            mc.postToChat("关卡"+str(level+1)+"通过."+"   分数"+str(points))
            level+=1
            if level == LEVELS:
                gameover = True
                mc.postToChat("恭喜-所有关卡已通过")
mc.postToChat("最终分数"+str(points))

你可能感兴趣的:(python)