[Repost] Shape Shifter (Processing)

[This was originally posted last year, on an old blog to promote computational thinking and programming in schools. I’ll be reposting these over the next month as I think they’re still useful to schools embarking on the new Computer Science course]

proc1

So far we’ve covered only 2 programming ‘languages’, of which, only one is an actual language (Scratch is more of a simulator i suppose?). The other language I think really (REALLY) benefits students is Processing. It’s very much a sequel to Scratch (and free), but requires real coding rather than drag-and-drop boxes. It’s a kind of stripped down Java, but with a much bigger emphasis on graphics manipulation. Think LOGO x 1000. It’s incredibly straight forward to use and has a huge amount of support and tutorials readily available.

The reason why I think it’s so perfect for schools is it’s huge focus on graphics. It allows the user to make moving images, and  urges them to. The end product therefore is more satisfying in a lot of ways for children and young adults. The best part is that there are a wealth of export options so that a student can take home their work as a windows stand alone file, or mac, or linux, or as a .gif, or even as an android app for their phone (!). Convinced yet?

The tutorial that follows is largely taken from the book (recommended) written by the creators of Processing, which can be found here.

This program prints a shape at the point of the cursor as you move it around a screen. When you click the mouse, the shape ‘fills in’ (black), otherwise it is empty (white).

The result looks something like this:

proc2

The code is blissfully simple and short:

proc3

I think you’ll be able to figure out the code so I won’t spend paragraphs decoding it above and beyond the comments (note comments are with “//” rather than # (from Python). Syntax is slightly different from Python, but syntax is slightly different in all programming languages. “void” is basically a declaration of a function that doesn’t return any values to the rest of the program.

That reminds me, if you don’t know what something means in Processing, you can right click on it and there’s a Processing Dictionary that will tell you what it means and how to use it, with examples! Great stuff.

As a lesson, I’d get students to write the code above themselves, just to get used to the interface, how to run it, check for errors etc.

Once they have it working, get them to try replacing the circles with other shapes.

Basic ones are below:

triangle(x1, y1, x2, y2, x3, y3)

(where x1, y1 are the coordinates of the first corner of the triangle, etc)

quad(x1, y1, x2, y2, x3, y3, x4, y4)

(for quadrilateral)

rect(x, y, width, height)

That last one should be straight forward enough!

As an extension you could get them to replace the original shape with a different shape when the mouse is clicked, rather than colouring it in.

Below is an example of where you can go with Processing in not a lot of time:

proc4

[Repost] The Name Game (Python)

[This was originally posted last year, on an old blog to promote computational thinking and programming in schools. I’ll be reposting these over the next month as I think they’re still useful to schools embarking on the new Computer Science course]

namegame

This project involves very little code but some great ‘figuring out’ beforehand.

If you’re unfamiliar with the song, watch below:

The lyrics of the song actually describe the rules you need to apply in Python, which would make for a great exercise for students to work through – figuring out pseudocode from a song!

The lyrics in full are at the bottom of this post.

We’re going to write Python code that generates lyrics to the song based on a user’s first name.

The pseudocode should essentially go something like this:

input word
for word:
if 1st letter = vowel, then word = word,
else word = word – 1st letter
print lyrics

(at its most basic)

From this students should be able to deduce that they’re going to need 2 versions of the input name, one unaltered, and one altered. The altered version will be the original name minus any consonants before the first vowel.

To check whether a letter is a vowel, you’ll need a list of vowels to check against.

The only part of the code that hasn’t been discussed or used in previous examples is the checking of characters against other characters.

Specifically, we have the line “if name2[0] in vowels : “

the [0] part is returning the first character in the string ‘name2′. You’d think the first character would be [1] but you’d think wrong. Python starts counting from 0.  We’re also using “in” which we haven’t used before. It pretty much does what it says on the tin.

The complete code, with comments is below. Copy and Paste version below that (remember to indent once you’ve pasted it!)

Finally, I got the idea from this post on a forum.

code1


name = input (“what is your first name?”)

name2 = name

vowels = “aeiouAEIOU”

if name2[0] in vowels:
name2 = name

else:
name2 = name2.replace(name2[0],””)

if name[0]==”B”or name[0]==”b”:
print (name + “!\n” + name + ” ” + name + ” Bo ” + name2 +
“\nBanana Fanna Fo F” + name2 + “\nFe Fi Mo M” + name2 + “\n” + name)

elif name[0]==”F” or name[0]==”f”:
print (name + “!\n” + name + ” ” + name + ” Bo ” + “B” + name2 +
“\nBanana Fanna Fo ” + name2 + “\nFe Fi Mo M” + name2 + “\n” + name)

elif name[0]==”M” or name[0]==”m”:
print (name + “!\n” + name + ” ” + name + ” Bo ” + “B” + name2 +
“\nBanana Fanna Fo F” + name2 + “\nFe Fi Mo ” + name2 + “\n” + name)

else:
print (name + “!\n” + name + ” ” + name + ” Bo ” + “B” + name2 +
“\nBanana Fanna Fo F” + name2 + “\nFe Fi Mo M” + name2 + “\n” + name)

____________

Shirley Ellis Lyrics:

Shirley!
Shirley, Shirley bo Birley Bonana fanna fo Firley
Fee fy mo Mirley, Shirley!

Lincoln!
Lincoln, Lincoln bo Bincoln Bonana fanna fo Fincoln
Fee fy mo Mincoln, Lincoln!

Come on everybody!
I say now let’s play a game
I betcha I can make a rhyme out of anybody’s name
The first letter of the name, I treat it like it wasn’t there
But a B or an F or an M will appear
And then I say bo add a B then I say the name and Bonana fanna and a
fo
And then I say the name again with an F very plain
and a fee fy and a mo
And then I say the name again with an M this time
and there isn’t any name that I can’t rhyme

Arnold!
Arnold, Arnold bo Barnold Bonana fanna fo Farnold
Fee fy mo Marnold Arnold!

But if the first two letters are ever the same,
I drop them both and say the name like
Bob, Bob drop the B’s Bo ob
For Fred, Fred drop the F’s Fo red
For Mary, Mary drop the M’s Mo ary
That’s the only rule that is contrary.

Okay? Now say Bo: Bo
Now Tony with a B: Bony
Then Bonana fanna fo: bonana fanna fo
Then you say the name again with an F very plain: Fony
Then a fee fy and a mo: fee fy mo
Then you say the name again with an M this time: Mony
And there isn’t any name that you can’t rhyme

Every body do Tony!
Pretty good, let’s do Billy!
Very good, let’s do Marsha!
A little trick with Nick!
The name game