TeachCraft Lesson 8

Code-Magic Combos

We can also use chat messages to change the action on our bow/arrow or sword!

We can type messages into chat to activate different spells on our arrow. Check out the example on the right, which supports toggling between spells "gold" and "teleport". Run the script, and type either "gold" or "teleport" into chat and then shoot an arrow!

Sample Code

import time
from mcpi import minecraft
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

active_spell = "gold"

while True:

    for chatpost in mc.player.pollChatPosts():
        active_spell = chatpost.message

    for blockhit in mc.player.pollProjectileHits():

        if active_spell == "gold":
            pos = blockhit.pos
            gold_block_id = 41
            mc.setBlock(pos.x, pos.y, pos.z, gold_block_id)

        elif active_spell == "teleport":
            pos = blockhit.pos
            mc.player.setPos(pos.x, pos.y, pos.z)

        else:
            mc.postToChat("Your spell '"+active_spell+"' was not found. Currently we only support the spells 'gold' and 'teleport'")


    time.sleep(1)




Your Challenge: Create your own superpowers

Your Challenge: Using the concepts from this lesson, craft your own superhero! Add your own powers set of magic powered by python code, and do battle in our PVP worlds with friends!

Ideas:
  • Booby Traps
    • Triggered via sword: Spawn TNT, and under the TNT a redstone block. Three second delayed explosion!
    • Triggered via sword: Spawn a pressure pad, and under the pressure pad a sand block, and under the sand a TNT block, and under the TNT 50 blocks of air. When something walks on the pressure pad, they will fall to their death!
    • Triggered via sword: Build a wall of bedrock, 3 blocks high and 20 blocks wide to slow down anyone chasing you
    • Triggered via bow/arrow: Spawn a lava pool under wherever the arrow lands
    • Triggered via bow/arrow: Spawn a 10x10x10 cuboid of sand, 10 blocks above wherever your arrow lands. The sand will fall and crush anything underneath it.
  • Defensive Spells
    • Make it so if you type "f" into chat, your code will assume you are falling and will spawn water 10 blocks under you to catch you unharmed
    • If you type "shield" into chat, make a bedrock shield appear around you which disappears after 5 seconds (being turned back into air)
  • Just for fun
    • Make it so right clicking with your sword turns on/off a trail of blocks that constantly spawns under you, following you. Make it so the block ID used for the trail can be changed dynamically by you typing it into chat! Great for flowers, lava, gold, etc.


Want to focus on building cool things instead? Check out our Community Projects / Examples page for a ton of awesome ideas!