Monday, 31 March 2014

Creating Minecraft Pi Earthquakes

I've been playing around with controlling the Pi edition of Minecraft from Squeak Smalltalk and had a thought: wouldn't it be cool to create earthquakes?

Some people have tried this already from within Minecraft, e.g. in this video:
https://www.youtube.com/watch?v=dqJg0WW691Y

But what about if you could create cracks in the ground more randomly as you wander around your world?

First follow this article to get Squeak controlling Minecraft.

Now try this from a Workspace in Squeak:

m := Minecraft connect. 
p := m playerTile + {0. 0. 2}.    "two blocks in front of me"
100 timesRepeat: [
    m blockAt: p put: 0.
    p := p - {0. 1. 0}.           "move one block down"
]

That should create a deep hole in front of you, right through to the end of the world!

So you could now repeat this for several more blocks in front of you and add in a bit of randomness:

m := Minecraft connect. 
p := m playerTile + {0. 0. 2}.    "two blocks in front of me"
20 timesRepeat: [
    q := p.
    50 + 10 atRandom timesRepeat: [
        m blockAt: q put: 0.
        q := q - {0. 1. 0}.       "move one block down"
    ].
    p := p + {0. 0. 1}.           "move one block away"
]

Now we have a very straight crack, running 20 blocks ahead of us.

What next?

What about making the crack run in a more zig zag fashion? 

Or make branches off of the main crack?

Let me know how you get on...

Saturday, 29 March 2014

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:
MCHttpRepository
    location: 'http://ss3.gemstone.com/ss/minecraft'
    user: ''
    password: ''

Then...
  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: '192.168.1.113'.

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: '192.168.1.113'.

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.
  ]
 ].

 "Legs"
 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.

Thursday, 20 March 2014

Finding Factors with Smalltalk

I've been looking for ways to cover the primary maths curriculum with real world examples or visual representations. Some of the curriculum is easy (for angles and gradients we can cycle up some hills) but some is a bit harder.

What about factors and prime numbers? While I have a think about the real world example of this I thought I'd put together a visual created with a little Smalltalk program to explore further.

You can see the results here... circles represent factors, so 10 has factors of 2 and 5, and each factor has a white stripe up to the number, so the whiter the background the more factors. This makes the primes easy to spot, e.g. 3, 5, 7, 11...


Some interesting questions:


  1. Why do some numbers have more factors than others? 
  2. Are there any repeating patterns in the dots?
  3. Do we see any of the numbers with a high number of factor (e.g. 12, 24, 36) in real life?
--

Here's the code:

First add the new class...

Object subclass: #FactorGrid
instanceVariableNames: 'rect max spacing'
classVariableNames: ''
poolDictionaries: ''
category: 'GeekClub'

And then the methods...

drawXLabels
| t |

1 to: max do: [ :i | 
t := TextMorph new.
t newContents: i asStringWithCommas.
t position: (i * spacing)@25.
rect addMorph: t.
]

highlightNumber: i forFactor: f
| r |

r := RectangleMorph new.
r position: ((i * spacing )- 4)@25.
r extent: (spacing * 0.9)@((f * 25) - 5).
r color: ((Color white) alpha: 0.2).
r borderWidth: 0.
rect addMorph: r.

initialize
max := 40.
spacing := 24.

rect := RectangleMorph new openInWorld.
rect color: (Color r: 0.972 g: 0.878 b: 0.819).
rect extent: 1000@500.
rect position: 0@20.
rect borderWidth: 1.

self drawXLabels.

plot: aNumber
| c t |

t := TextMorph new.
t newContents: aNumber asStringWithCommas.
t position: 5@(aNumber * 25).
rect addMorph: t.

1 to: max do: [ :i | 
(i \\ aNumber) isZero ifTrue: [ 
c := CircleMorph new.
c position: ((i * spacing) + 3)@(aNumber * 25).
c extent: 10@10.
(i = aNumber) ifTrue: [
c color: Color red.
]
ifFalse: [
self highlightNumber: i forFactor: aNumber.
].
rect addMorph: c.
]
]

plot: i to: j 
i to: j do: [ :a | self plot: a ].

Now you can create the drawing... open a new Workspace and run the following (one line at a time):

g := FactorGrid new.
g plot: 2.
g plot: 3.
g plot: 4 to: 20.


Smalltalk on the Raspberry Pi

Smalltalk is a great language for learning. Everything runs in an integrated environment, so it's quick to try things out; it's simple to grasp the basics and there are lots of visual things included in the environment.

Each Raspberry Pi comes with the Squeak version of the Smalltalk programming language. Well almost, most of what you need it there, but there are a few bits to collect from the internet to complete the package. Run these commands from a terminal shell to get them...

mkdir squeak-downloads; cd squeak-downloads

wget http://ftp.squeak.org/4.1/SqueakV41.sources.gz
gunzip SqueakV41.sources.gz

wget http://ftp.squeak.org/4.4/Squeak-4.4-All-in-One.zip
unzip -j Squeak-4.4-All-in-One.zip "**Squeak4.*.image"
unzip -j Squeak-4.4-All-in-One.zip "**Squeak4.*.changes"

sudo mv SqueakV41.sources /usr/share/squeak
sudo mv Squeak4.*.image /usr/share/squeak/
sudo mv Squeak4.*.changes /usr/share/squeak/

Starting Squeak

Now you can start up Squeak, you'll find it in the programming menu, and open a new Workspace from the Tools menu. This is where you can execute commands with immediate effect.

Type in the line below and execute it by bringing up the right mouse menu, or pressing Ctrl-D (for 'do it').

b := BouncingAtomsMorph new openInWorld.

You should see a panel with some bouncing atoms in it. Now try each line below, running each one in turn:

b setAtomCount.
b startInfection.

When you've had enough you can kill the morph by Alt-Clicking it and choosing the X icon, or type `b delete`

Drawing with a Pen

Now type in the following:

p := Pen new.
p goto: 300@300.
p squareNib: 4.
p down.
p spiral: 200 angle:45.

Select all the lines (Ctrl-A), right click and choose 'do it' from the menu. You should see a nice rainbow spiral.

What just happened? You created a new `Pen` and sent it some messages:
  • to go to a point some way into the middle of the screen
  • that you want a square nib of width 4 pixels
  • to put the pen down ready for drawing
  • to draw a spiral with 200 segments turning 45 degrees at each step.
There's lots more you can do with pens, take a look by typing Pen in the search box at the top of the page and browsing all of its methods. E.g. try: `p filberts: 5 side: 4.`

A few things to notice:
  • messages that take a parameter, e.g. `p goto: 300@300` have a colon after the message name, this style is repeated if you need to supply multiple parameters. 
  • each Smalltalk expression ends in a dot '.' -- don't forget this otherwise Smalltalk will join your lines together and you'll get strange error messages.
  • for more info on Syntax see http://en.wikipedia.org/wiki/Smalltalk#Syntax

Sound?

Sound doesn't work in Squeak without a bit of tweaking. Here's what I tried first:

FMSound bass1 play.

...nothing. But there's a solution here: http://raspberrypi.stackexchange.com/a/13221/12203

What next?

You could try creating some Fractal Trees.

If the above has got you interested, a good place to learn more is the free book: http://squeakbyexample.org/

Monday, 10 March 2014

Smalltalk Fractal Trees

Making Fractal Trees in Smalltalk is pretty simple, especially with Squeak, which has lots of drawing classes included out of the box.

All the drawing in the example below is done with the Pen class, I've used recursion to build up the tree branch by branch—there's not much code so you should be able to see what's going on—plus there's some example enhancements at the end of the page.

First download Squeak here: http://ftp.squeak.org/4.4/Squeak-4.4-All-in-One.zip 



Start by opening a new Browser from the Tools menu, right click in the first column and add a new category to contain your new class. I called mine 'GeekClub', but you can pick whatever name you like. Now add this new class...

Object subclass: #FractalTree
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'GeekClub'

Add these two methods to the class...

tree: aPoint length: aLength angle: anAngle
    | p a |
        
    (aLength > 10) ifTrue: [
        p := Pen new.
        p up.
        p goto: aPoint.
        p turn: anAngle.
        p down.
        5 timesRepeat: [
            p go: aLength / 5.
            p turn: 5.
        ].
        a := anAngle - 30.
        3 timesRepeat: [
            self tree: p location length: aLength * 0.7 angle: a.
            a := a + 30.
        ]
    ].

draw
    Display restoreAfter: [
        Display fillWhite.      
        self tree: 700@700 length: 200 angle: 0.
    ]

Now open a Workspace and enter:

FractalTree new draw.

You should see a fractal tree like the one above. Click to return to the code view.

Improvements

There's lots of improvements you can make to the above code, try these:

Make thicker branches at the base, add this to the tree method after p up.
p squareNib: aLength / 25.

Add a bit of randomness to the angle between branches, replace a := a + 30. with
a := a + 30 + (20 atRandom).

Find out more

There's a great, free PDF book here if you want to find out more about Squeak: http://squeakbyexample.org/ and the official Squeak site: http://www.squeak.org/

Friday, 7 February 2014

Learn to read music with Scratch

MIT Scratch is a great visual programming language for kids and people that want to prototype something audio/visual quickly.

You can download version 1.4 of Scratch here: https://scratch.mit.edu/scratch_1.4/ or use version 2 in the browser: https://scratch.mit.edu/

In this blog post we'll get Scratch to teach you to read music. We'll show a random note on the stave and you'll have a few seconds to play it. Scratch then plays the note so that you can see if you got it right.

I (try to) play the guitar, so I've included the open strings on the stave below.


Before you create the script, you need to add a few variables:
  • note
  • note-ypos
  • notenumber
  • prepause
Then click the stage and draw the background stave above (you could just copy that image and import it).

Now change the main sprite (the cat) to a black circle. Add another costume and colour the circle red.

Now add the script to the main sprite...


You can now click the green flag and the program should run, show a note and play it after a few seconds. It'll repeat forever, until you click the stop button.

If the note appears in the wrong place on the stave then edit the background and move the stave up or down until the note appears in the right place.

Here are a few ideas to improve the program:

  • stop the program playing the same note twice
  • only show the stave extenders (what's the correct name for these?) when the note needs them
  • record which notes the player finds easy in a list and play less of the easy notes and more of the harder ones
  • allow the player to mark notes as easy as the program runs.
Have fun!

P.S. There's now a version of this program written in Racket:
http://ericclack.blogspot.co.uk/2015/12/learn-to-read-music-with-racket-scheme.html

Thursday, 23 January 2014

Running a wiki on the Raspberry Pi

Wikis are great, and personal ones enable you to capture and organise content -- your own private website with whatever knowledge you like: recipes, how-tos, your diary. Set up right you can access your wiki from anywhere on your home wifi network, so your whole family can take part.

There are various wikis that come with Raspian, so installing these is as easy as apt-get install... but I like MoinMoin (http://moinmo.in/) and because the Pi comes with Python out of the box I thought I'd give it a go. It turned out to be pretty easy to install...

The install instructions for running a personal wiki are pretty short, and I've included them in this page below. You can read the full details here: http://master.moinmo.in/InstallDocs#quick

Installing MoinMoin

To get the software open a terminal window and type:
wget http://static.moinmo.in/files/moin-1.9.7.tar.gz
tar -xvzf moin-1.9.7.tar.gz
mv moin-1.9.7 moin

To start the wiki you need to run the server:
cd moin
./wikiserver.py

Now test with your browser: http://localhost:8080/

Note: I've found on the first run things can be very slow to respond, but successive runs are very usable. If nothing happens in your browser, close the terminal window, open a new one and rerun the commands above (cd moin onwards).

Once the page loads you should see instructions on what to do next...

Next steps

To get the most out of the wiki you need to follow these steps:

Create your own user account

Register by clicking the link on the login form, or following this link: http://localhost:8080/LanguageSetup?action=newaccount

Make yourself a super user and change the homepage

Stop the server by typing Ctrl-C in the terminal window. Now edit the site config file to add yourself as a superuser
nano wikiconfig.py

Find the second line that reads DON'T TOUCH THIS EXCEPT IF YOU KNOW WHAT YOU DO -- surrounded by ^ symbols rather than v.

And add this line under it -- make sure you add the initial 4 spaces to match the surrounding lines.

    superuser = [u"YourLoginName", ]

While you are in the config file, change the homepage by uncommenting the line (removing the #):

    page_front_page = u'FrontPage'

Restart the server and create your homepage

Restart the wiki server by running ./wikiserver.py again.
You should see a link to Create a new page. Go ahead and create your page. Some initial tips:
  • If you add words like this MyPageAboutGames that will automatically become a link to a new page
  • See the box at the end of the edit page to see how to add styles, headings and so on
  • You can add images by first adding them as an attachment.

Monday, 20 January 2014

Programming the Pi, what next after Scratch?

What next after Scratch programming on the Raspberry Pi? Lots of kids outgrow MIT Scratch, but what could they try next? I'm looking for a tool that:
  • Makes it easy to get visual or audio effects
  • Gives immediate results (like Scratch does), type some commands, press run, see results
  • Runs easily on the Pi (of course)
  • Has depth with plenty to explore.
Two ideas have come up so far...
  1. Trying Squeak Smalltalk on the Raspberry Pi... —I've seen some success with this, while Smalltalk is a big leap, the environment gives instant feedback and you can get great results with a few lines.
  2. Trying python programming Scratch style —This is an attempt to create an library of commands that will be familiar to Scratch programmers, e.g. sprite.pen_down(); sprite.move(100,0).