Sunday 21 December 2014

Minecraft - QR Codes

This isn't a particularly new idea and it has certainly been done before... but I like it.

Point your phone at this...

I saw a tweet from @ImmersiveMind (aka Stephen Reid) linking to a new blog post about creating qr codes in Minecraft as a way of showing how to link the real world with the digital.

That afternoon I had 30 minutes to wait before I was picked up to be taken out for festive activities - could I code a Minecraft QR generator in time? It turns out I could:


I used the fantastic python module qrcode to create the QR, which has a useful function, get_matrix(), to allow you to get the QR as a 2 dimensional list of True and False's.  I then used this matrix to create white and black wool - dead easy!

The qrcode module can be installed using pip and requires PIL (python imaging library) to work - the first step is to install pip and PIL and then use pip to install qrcode:
sudo apt-get install python-pip python-imaging
sudo pip install qrcode
You can get the code from github:
git clone https://github.com/martinohanlon/minecraft-qrcode
Run it:
python minecraft-qrcode/minecraft-qrcode.py
The code:
from qrcode import *
from mcpi.minecraft import *
from mcpi.block import *

def getQR(inputString):
    qr = QRCode(version=1,
                error_correction=ERROR_CORRECT_L,
                border=1)
    qr.add_data(inputString)
    qr.make()
    return qr.get_matrix()

mc = Minecraft.create()
pos = mc.player.getTilePos()

qrMatrix = getQR("http://www.stuffaboutcode.com")

rowNo = 0
for row in qrMatrix:
    rowNo -= 1
    columnNo = 0
    for column in row:
        columnNo -= 1
        if column:
            #black
            blockColour = 15
        else:
            #white
            blockColour = 0

        mc.setBlock(pos.x + rowNo, pos.y + columnNo, pos.z, WOOL.id, blockColour)

6 comments:

  1. You appear to have infringed my copyright without giving proper attribution.

    Please see http://www.themagpi.com/issue/issue-27/ based on code I wrote in August 2014.

    ReplyDelete
    Replies
    1. Like I say not new or original. A very different implementation from the one in the magpi tho.

      Delete
    2. Hi Martin, I love what you've done with this...permission to use this in schools?

      Also, I too have received the 'copyright infringement' message from Dougie Lawson. I have since sent him proof of my work making QR Codes in Minecraft from over two years ago as well as a video showcasing that work and published in mid 2013. What disturbs and disappoints me most is the method in which he has chosen to challenge us. Commenting directly onto a blog post can be damaging and shake the trust of our readers. I asked him kindly to send a mail or make a phone call should he require any more contact.

      Should you yourself require the evidence of my work in this area, going back two years, please just ask. Since your work is inspired by my work and while my permission is not necessary, please be assured you have my full permission and support.

      Stephen Reid - ImmersiveMind

      Delete
    3. Thanks a lot Stephen. Of course you have my permission, its very nice of you to ask.

      I originally thought Dougie Lawson was being sarcastic, such is my generally positive view of people and their attitudes, clearly wrong this time though!

      Perhaps Dougie should be asking for your attribution?

      Delete
  2. Hi Martin,

    I've written the code exactly as above, but keep getting the error:

    Traceback (most recent call last):
    File "/home/pi/qrcode.py", line 1, in
    from qrcode import *
    File "/home/pi/qrcode.py", line 14, in
    qrMatrix = getQR("Minecraft is Awesome!")
    File "/home/pi/qrcode.py", line 6, in getQR
    qr = QRCode(version=1, error_correction=ERROR_CORRECT_L, border=1)
    NameError: global name 'QRCode' is not defined

    Any idea why?

    ReplyDelete
    Replies
    1. I think its because you have saved your program as qrcode.py, python is getting confused because you are trying to import a module which is also called qrcode...

      You should save you program as something else and delete the qrcode.py and qrcode.pyc files.

      Delete

Note: only a member of this blog may post a comment.