Programming Raspberry Pi Minecraft with Squeak Smalltalk

You can control Minecraft from Smalltalk, create blocks, make shapes and more, all from Squeak.

In this example we'll draw a rainbow, make tables and build trees, all without having to place blocks with the mouse.

First you need to install the Smalltalk Minecraft Bindings (thanks to Dr. Bert Freudenberg for writing them :). Open up Squeak, then open a Monticello browser from the Tools menu. Now:
  1. Click +Repository
  2. Choose HTTP
  3. Paste in this code:
    location: ''
    user: ''
    password: ''

  1. You'll see the minecraft repository on the right hand side of the window
  2. Make sure its selected and then click Open
  3. Now click the underlined item on the right and click Load to load the latest version, then repeat for the Demo collection
  4. You can now open a Browser from the Tools menu and find two new Minecraft collections at the end of the list: Minecraft-Pi-Base and Minecraft-Pi-Demo

Connecting to Minecraft

Before you can control Minecraft you need to connect Squeak to it. If you are running Squeak and Minecraft on the same computer then it's easy to connect -- however you might find that your Pi struggles to run both, with everything slowing down considerably.

To connect to Minecraft on the same computer:

m := Minecraft connect.

To connect to another computer

Find your Minecraft computer's IP address by running ifconfig in a Terminal -- look for the line under eth0 that starts with inet addr:192.168.1.... That bit starting192.168.1. is your IP address. Make a note of it.

In Squeak run the following from a Workspace window (making sure you use your own IP address):

m := Minecraft connectTo: ''.

Drawing a block

Once you have created a Minecraft object and stored it in the variable m using one of the lines above you can send Minecraft messages.

Try the following (you don't need to type the bit in quotes):

p := m playerTile.     "store the player's location"
m blockAt: p put: 1.   "place a block of stone"

Now look down in Minecraft, you should see a stone block. Try this next:

1 to: 100 do: [ :i | 
    m blockAt: (p + {i. i. i}) put: 1.

You now have a stone staircase! It's actually pretty difficult to climb this staircase as each block is diagonal to the last one, see if you can fix this. Hint: {i. i. i} represents {x. y. z} for each value of i from 1 to 100, where x is left/right, y up/down and z forwards/backwards.

Try the demos

The class MinecraftDemo contains a few demo methods including one to draw a rainbow, a table and a tree.

If Minecraft is running on a different computer you'll need to connect first.

MinecraftDemo host: ''.

Now try the demos, run each line at a time. Look around in Minecraft to see the results (they may be behind you).

MinecraftDemo tree: 15.
MinecraftDemo table: 5.
MinecraftDemo rainbow: 10.

What next?

A good next step is to take a look at the code in the demo class. Type MinecraftDemo into the search box, or select that class name and hit Ctrl-B to browse the code.

This is how the method `table:` is defined:

table: size
 "E.g. Draw a 5x5 table with legs 5 high:
  MinecraftDemo table: 5"
 | mc pos |

 mc := Minecraft connectTo: self host.
 pos := mc playerTile.

 "Table top"
 1 to: size do: [ :x | 
  1 to: size do: [ :z | 
   mc blockAt: (pos + {x. size. z})
    put: Stone.

 1 to: size do: [ :i |
  mc blockAt: (pos + {1. i. 1}) put: Stone.
  mc blockAt: (pos + {size. i. 1}) put: Stone.
  mc blockAt: (pos + {1. i. size}) put: Stone.
  mc blockAt: (pos + {size. i. size}) put: Stone.

 mc close.

No comments:

Post a Comment