Blocks into bombs using events

Here is a very cool script again by the amazing author Martin O’Hanlon, you can right click block and have it explode like TNT in Minecraft.

Here is the code:

#Raspberry Pi, Minecraft Bombs - Turn any block into a bomb!

#import the module from the minecraft directory
import minecraft.minecraft as minecraft
#import minecraft block module
import minecraft.block as block
#import time, so delays can be used
import time
#import threading, so threads can be used
import threading

class ExplodingBlock(threading.Thread):

def __init__(self, pos, fuseInSecs, blastRadius):
 #Setup object
 self.pos = pos
 self.fuseInSecs = fuseInSecs
 self.blastRadius = blastRadius

def run(self):
 #Open connect to minecraft
 mc = minecraft.Minecraft.create()

#Get values
 pos = self.pos
 blastRadius = self.blastRadius

#Explode the block!
 # get block type
 blockType = mc.getBlock(pos.x, pos.y, pos.z)
 # flash the block
 for fuse in range(0, self.fuseInSecs):
 mc.setBlock(pos.x, pos.y, pos.z, block.AIR)
 mc.setBlock(pos.x, pos.y, pos.z, blockType)
 # create sphere of air
 for x in range(blastRadius*-1,blastRadius):
 for y in range(blastRadius*-1, blastRadius):
 for z in range(blastRadius*-1,blastRadius):
 if x**2 + y**2 + z**2 < blastRadius**2:
 mc.setBlock(pos.x + x, pos.y + y, pos.z + z, block.AIR)

if __name__ == "__main__":

 #Connect to minecraft by creating the minecraft object
 # - minecraft needs to be running and in a game
 mc = minecraft.Minecraft.create()

#Post a message to the minecraft chat window
 mc.postToChat("Minecraft Bombs, Hit (Right Click) a Block,")

#loop until Ctrl C
 while True:
 #Get the block hit events
 blockHits =
 # if a block has been hit
 if blockHits:
 # for each block that has been hit
 for blockHit in blockHits:
 #Create and run the exploding block class in its own thread
 # pass the position of the block, fuse time in seconds and blast radius
 # threads are used so multiple exploding blocks can be created
 explodingBlock = ExplodingBlock(blockHit.pos, 3, 3)
 except KeyboardInterrupt:

It’s also on the github

Source: Here


2 thoughts on “Blocks into bombs using events

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s