Archive for March, 2012

My modifications to Liam Fraser’s PiShooter


I am a big fan of Liam Fraser’s tutorials on YouTube. I wanted to change PiShooter so that there was an explosion when you hit the Pi. I made a few changes to Liam’s code. Becuase I am only just getting to grips with pygame, I didn’t make a class for my explosion – I just found a “quick and dirty” was to achieve my goal.

First,  I loaded up an explosion image into “boom”.

Then I added a bit to the collision detection part.

I stored the location of the shot in x1 and y1. Then, if there was a hit, I killed the Pi sprite and made a new one (so the one we hit disappears and a new one begins again at x=0. I’ve made a variable “count” and then in the main loop I blit the explosion to the screen for 20 frames. On my computer this is enough to make the explosion visible (but my laptop is pretty slow!)




Scratch program that does averages.

Computer Guesses Your Number!


Well, after “Guess the Number”, it seemed logical to write a program that let’s the computer guess your number!

The program starts with some instructions, then we import random and time. Time allows us to make the program pause for 2 seconds before making its first guess. This gives the user a bit of time to actually think of a number!

We set some initial variables and then enter the main loop of the game. I had to make a string including the guess converted to a string, because the input() function only works with strings. If the user doesn’t type y, l or h, we use the “continue” command to go back to the top of the while loop.

I’ve used random numbers to make the computer’s guesses a bit more realistic. We set “high” and “low” to whatever the upper and lower limits are, based on the user’s responses to the guesses made so far.

If you can think of any ways to improve this game, please let me know in a comment!

happy coding


Guess the Number


Back in the early days of this blog, I posted a screenshot of a program that plays “guess the number”, but I thought I’d like to do a proper post on it,

This program is very simple, so I won’t write too much about it. The only thing here that might be new is the “try:” section. This is a bit of error handling. If your user types in a number above 100 or below 0, the program can cope very well. But if the user types in something that is not a (whole) number, there would be an error if we didn’t use this approach. Basically, the program tries to convert “guess” to an integer. If this isn’t possible, the “ValueError” is trapped and the code tells the user that he/she didn’t enter a number.

Anyway, have a go. “Guess the Number” is a classic beginner’s program!


Pygame animation


Here’s a simple python program that uses pygame to create a moving image.

You’ll need these images to run the code: python.png, pi.png.

Here’s the code, you can also download that here, to save you typing it all!

Hopefully the comments make it pretty clear what is happening. You’ll recognise a lot of the code from the simple pygame gui we did in the last post. I am going to come back and add some more explanation a little later, but I wanted to get this online while I thought about it.


New – Facebook page


I’ve set up a facebook page for RacyPy2. Please like it!

Also, I am really delighted that Raspberry Pi shared a link to this site on their facebook page – that explains the high number of visits I’ve had recently!

Use pygame to make a gui.


Remember that I said the main loop in our tkinter gui program was like a pygame graphics window? Well in this post I am going to use pygame to make a gui.

Here’s the code.

[Note – I will add to this post later!]

Getting started with GUIs


So far all the programs I’ve done on here have been decidedly old-school and run in the command line. As I’ve mentioned before, I am learning as I go and I haven’t got very far yet with using GUIs, but I thought I’d do a post that uses the tkinter features of Python to make GUIs.

Here’s what our first GUI will look like. You can move it, resize it, minimise it and click the button (although it doesn’t do anything!).

Here’s the code.

The first line imports the tkinter modules we need. The next lines create a window and give it a size and a title. You can play around with changing these aspects, if you like. We are using a “class” within tkinter here. We’re getting into Object Oriented Programming with this little script. The line: “root = Tk()” makes a new object: an instance of the class “Tk”. We can then use some of the “methods” which that class contains. The method “title” sets the title and “geometry” defines the size.

The dot notation shows us that we are using methods. We have used this type of thing before, for instance, to raise a number to a power we said something like:

i = math.pow(2, 3)

Here the dot notation shows that we are using one of the methods defined in the math module. This one is called “pow” and it raises the first number to the power of the second. An even better example would be when we sorted a list:


Here we have created a list (“mylist”) and we use its name followed by “.sort()” to get the list-method “sort” to order our list.

Next we need a “frame” within the window, and a button. The “.grid()” method causes the layout manager to make the items we have created visible.

At the end of the code we start the main loop. This makes the window keep displaying itself on the screen and checking for any action the user might make. You may have seen similar things if you’ve had a go at pygame in which our graphics windows are controlled by a loop which runs continually until we quit the game.

I hope this makes sense! Anyway, the code itself is pretty simple. Look out for more GUI posts!




The version of the Java Development Kit you have installed in RacyPy2 is 1.7.0_02.

Firstly, I’d like to recommend Herbert Schildt’s books. I am using “Java: The Complete Reference”.

You can download the (free) first chapter of his “Java: A Beginner’s Guide” from various places online.

I am not going to write a lot about Java here – it’s better if you read the Schildt chapter. But I will mention that in Java, all programs use the Object Orientated programming model. We will get around to using this approach in Python (it’s very useful and amazingly powerful). But for simple programs, we haven’t really needed to use it.

What this means in practice is that every Java program has at least one “class”. Here’s an example:

The first four lines are a multi-line comment – we use /* to start these and */ to close them.

Next we have the command that declares the main “class”  of the program. It’s essential that this has the same name as the name of the file. It’s traditional to give this class an initial capital letter. Java is case-sensitive. So if i have called my file “”, my class has to be called “Hello” (not “hello”). The curly braces “{” are used to group a block of code – here the contents of this class.

The next line is a single-line comment which we do by starting the line with “//”.

The next line is one you will see in almost all Java programs (I think – I am only a learner!). I’m not entirely sure what each of the commands means yet! But the important thing is that this line include the call to “main()” which all Java programs need. The next curly brace opens a new code-block – this time for what our program will actually do. What this line actually does is to invoke a class called “System” which gives us control of things like output to the screen. Within this class there is a method called “out” which does just that – outputs to the screen. The “println()” bit is just like print() in Python3 – it will print what we put inside the brackets – in this case a string enclosed in quotation marks – and add a newline. The line ends with a semi-colon because all “statements” in Java must end with a semi-colon. The lines that don’t end in semi-colons are not actually “statements” (yes – I found this a bit confusing at first, but you’ll get the hang of it!).

Then we close the curly braces that enclose the “main()” part of our code, and finally close the curly braces that end the description of our class “Hello”.

Anyway, the fun bit comes next! We have to compile our program!

Open a terminal in the directory where you saved “”. Then type:


This compiles your program into Java bytecode. You then issue the command:

java Hello

And your program should run.

Just to make this really clear, I’ve done a few extra commands here. Typing “pwd” displays the directory we are in. Then “ls” lists the files in it. As you can see, there’s just the file “Hello,java”. Once we have run “javac” a new file is created: “Hello.class”. When we type: “java Hello”, Java looks for a class called “Hello” and executes it, printing: “Hello World!”.

Wow, are you having fun yet!


C++ (and other things) in RacyPy2


If you want to compile in RacyPy2, you need to load the devx file.

It is on the cd (it’s called “devx_racy_5.2.2.sfs”).

If you want to compile you’ll need to do a frugal install (this is quite simple and if you need some help, go to the Puppy Linux forum, or leave me a comment).

Then you need to put the devx file in your /mnt/home folder.

Then choose Menu > System > BootManager.

Click on the button at the top that mentions sfs files. Your devx file should appear in the list on the left. Click it and add it to the list on the right. Then reboot the machine and you will be able to compile.

There is one little problem with this – when you load the devx it will break the script that launches the Python2 IDLE when you click on this snake on the desktop.

I will sort this out for the next release. In the meantime all you need to do is download this .pet (choose open with PetGet) and it will fix the problem!

Happy compiling!