Fireworks (Processing)
A little something I knocked together in Processing language (source code here). I used a bit of calculus to determine the trajectories of the sparks — nothing too tricky, just a bit of integration.
I have an idea for an animation (or maybe a game?) which would involve finding the roots of a 4th degree polynomial to determine where a trajectory (two quadratics) will intersect with a circle. I’ll be using NumPy to do this, as once you’ve defined the polynomial NumPy can find the roots automatically.
I’ll also investigate NumPy alternatives for Java so that I can use Numerical programming with Processing.
Philosophy
According to XKCD, you can start at any Wikipedia article, and by continually clicking on the first non-italic, un-parenthesised link in the article text, eventually end up at “Philosophy.”
Let’s see how that works:
http://en.wikipedia.org/wiki/Wiki http://en.wikipedia.org/wiki/Website http://en.wikipedia.org/wiki/Web_page http://en.wikipedia.org/wiki/Document http://en.wikipedia.org/wiki/Non-fiction http://en.wikipedia.org/wiki/Narrative http://en.wikipedia.org/wiki/Latin http://en.wikipedia.org/wiki/Italic_language http://en.wikipedia.org/wiki/Indo-European_languages http://en.wikipedia.org/wiki/Language_family http://en.wikipedia.org/wiki/Language http://en.wikipedia.org/wiki/Communication http://en.wikipedia.org/wiki/Meaning_(philosophy_of_language) http://en.wikipedia.org/wiki/Aristotle <----- Oh, so close! http://en.wikipedia.org/wiki/Greeks http://en.wikipedia.org/wiki/Nation http://en.wikipedia.org/wiki/Sovereign_state http://en.wikipedia.org/wiki/State_(polity) http://en.wikipedia.org/wiki/Social_sciences http://en.wikipedia.org/wiki/Umbrella_term http://en.wikipedia.org/wiki/Superset http://en.wikipedia.org/wiki/Mathematics http://en.wikipedia.org/wiki/Quantity http://en.wikipedia.org/wiki/Property_(philosophy) http://en.wikipedia.org/wiki/Modern_philosophy http://en.wikipedia.org/wiki/Philosophy <----- Achievement unlocked.
25 Steps from Wiki to Philosophy. Next step is to write a script to test the generalised case.
Or go out for a nice walk.
Faster turtles
Using the Python Imaging Library I’ve created a faster alternative to the turtle.py module. It takes only a few seconds to draw images that were taking several minutes with turtle.py.
There are a few downsides to this approach I suppose:
- There is no on-screen animation of the image being created which is useful tool when teaching turtle graphics — only a finished image is created.
- Output is a raster image, not a nice svg or eps file.
On the up side:
- Super fast
- Antialiased
- All Python Imaging Library tools and methods also available.
- At the moment is largely compatible with the turtle.py module
Who knows… once I’ve got the code into a more finished state I may share my fast-turtle module with the world.
Here’s a nice animation that I created with my aforementioned fast turtle module:
Turtle.py
Python comes with a turtle graphics module right there in the standard library. A new and improved version was introduced with Python 2.6, and it is quite good. I think its real strength could be in the initial stages of teaching kids to program in Python.
For my use I found two major drawbacks. First, there is no built-in way to export your drawing to an image file (taking a screenshot is the solution I’ve been using). More importantly, the turtle drawing is intolerable slow for complex images.
I did manage to create this rather nice recursive tree:
(The leaves are dead turtles)
Nevertheless I think I will concentrate on some other turtle graphics implementation for now. After all, I already program in Python so I won’t be learning anything new there. Also did I mention slow? So slow.
I would be interested in learning UCBLogo as the language itself would be interesting for me to learn (A far more turtle-y Lisp dialect). Also from my initial tests it seems to be so much faster than turtle.py. However it also has limited capacity to save drawings as images (only eps output with no filled shapes 😦 ). The output window also appears buggy on my Ubuntu Linux system… The image in the window does not refresh after being obscured or minimised, which pretty much makes it useless to me until this issue has been addressed. I figured out that turning on window compositing fixes this somewhat. The output is no longer deleted after the output window is obscured by another window. If you minimise or resize the window the image disappears, but seems to come back most of the time if you type “REFRESH” into the Logo session.
That’s just a giant logo
Lately I’ve been feeling quite nostalgic about the turtle graphic environment, Logo. Then I saw this and my new mini-obsession was confirmed:
[marriedtothesea.com]
I certainly have some fond memories of my early programming efforts in Logo. First on a Commodore 64, circa 9 years old, with some Logo disks borrowed from school. This version supported music and animation (sprites even!) and was great fun. I even had to learn trigonometry prematurely in order to complete the base of my leaning tower of Pisa (so that the bungee jumper had something to jump from.)
Later at high school we used Logo on the Amiga 500 as part of the scandalously brief computer studies curriculum. Who would of thought in 1992 that computers would be an important part of our future? Anyway I don’t remember this version having any particularly special capabilities. I do remember being set tasks such as “A working set of traffic lights” and “A cake with flickering candles” while the rest of the class was tasked with “a circle” or “a stack of squares”.
More recently, when I first discovered the Python programming language, the “turtle” module in the standard library was the first method of creating graphics that I discovered. Thankfully I’ve discovered better methods since.
So what am I feeling so nostalgic for? The Lisp-like Logo programming language, or just turtle graphics in general? Probably a little of both. That is, I feel like I’d like to revisit the original Logo language, or even dive into learning Common Lisp. On the other hand, something like Cheloniidae looks like a lot of fun and that’s a Java library.
Either way, expect to see lots of circles and stacks of squares on this blog soon.
Talking Python
The following was a small amusement that I put together for my 5 year old daughter. It must have made quite the impression on her as she has informed me that she wants to be a “horse programmer” when she grows up.
This program relies on another program called “espeak” which should be present on most Linux systems. This is a command line tool which takes text and converts it to speech. If you are using a Mac you could adapt the program to use the command “say” which does the same thing (the command line arguments are bound to be different though). Windows doesn’t include such a tool, because Microsoft just doesn’t love you, however Google turned up this result when I searched for “Windows command line speech”.
Apart from that you will see a few Python constructs which may be interesting to Python newbies, such as calling another program from within your Python script, and generating random silly sentences.
Note that it also uses the new style of string formatting so you will need at least Python version 2.6.
#! /usr/bin/python import subprocess import random def say(sentence): speed = "-s " + str(random.randrange(120, 200)) pitch = "-p " + str(random.randrange(100)) subprocess.call(["espeak", speed, pitch, sentence]) sentences = [ "Hello {name}.", "{name}? What a {adjective} name!", "Congratulations, {name}! You have won a {noun}!", "Excuse me {name}. Have you seen my {adjective} {noun}?", "Hello {name}. Are you going to Wellington Zoo soon {name}?", "What is your name {name}? Oh, that's right, silly me. Your name is {name}!", "{name}! Your {adjective} {noun} is {verb}ing! {adverb} grab a {noun2}, {name}!", "What is your favourite {noun}, {name}? Is it the {adjective} one?", "Quickly quickly {name}! No and Yes!", "Where's your mummy {name}? Is she {verb}ing today?", "That's what I said {name}", "What are you doing {name}? Are you {adverb} {verb}ing? Do you think that's a good idea?", "{noun}s are fun. Don't you think so {name}?", "Red alert. We go beserk. No {name} moves, no {name} gets hurt.", "Please {name}. May we go to the {noun} shop?", "Happy birthday to you. Happy Birthday to you." " Happy birthday dear {name}. Happy birthday to you!", "This is a shout out to {name}. Keep it real homey.", "Last night I had the strangest dream. {name} was {adverb} riding on " "a {adjective} {noun}, being chased by a thousand {adjective2} {noun2}s. " "Luckily, the {noun} was just too {adjective} for the {noun2}", "{name} is a good reader!", "Time for bed {name}. Go on... off you go!", "Make me a {noun} {name}, as fast as you can! I would like a {adjective} one please!", "Who ate all of my cake? {name}, was it you?", "I'm lost {name}! Can you tell me where I am?", "Do you like the circus {name}?", "This is the {adjective}est {noun} that I have ever seen. It is even more {adjective} than " "that {noun2} that I saw yesterday. Although, to be honest, that {noun2} " "was probably more {adjective2} than {adjective}", "I just saw a {adjective} {noun} {adverb} {verb}ing a {adjective2} {noun2}.", "Never let a {noun} {verb} more than once a day, otherwise it may {adverb} turn " "into a {noun2}", ] nouns = [ "pony", "horse", "cheesecake", "muffin", "dragon", "car", "DVD", "Computer", "Rollercoaster", "Hamburger", "Chestnut Park", "Cat", "dog", "TV", "soccer ball", "sandwich", "X box", "house", "bus", "van", ] adjectives = [ "silly", "fast", "brown", "pink", "yellow", "blue", "purple", "golden", "funky", "red", "bouncy", "pretend", "angry", "madd", "sadd", "badd", "grumpy", "stupid", "awesome", "ridiculous", "dubious", "dodgy", "flightless", "sleepy", "antogonistic", ] verbs = [ "talk", "ignore", "runn", "grump", "riyd", "gallop", "program", "slide", "gasp", "shout", "jump", "read", "play" "dodge", "crawl", "whisper", "hiyd", "smell", "sink", ] adverbs = [ "quickly", "suddenly", "slowly", "agrily", "Greedily", "happily", "mostly", "rapidly", "forcefully", "woodenly", "passionately", ] n = "" while True: n = raw_input("What is your name? ") if not n: break #[say(word) for word in random.choice(sentences).format(name=n).split()] for i in range(3): say(random.choice(sentences).format(name=n, noun = random.choice(nouns), noun2 = random.choice(nouns), verb = random.choice(verbs), adverb = random.choice(adverbs), adjective = random.choice(adjectives), adjective2 = random.choice(adjectives), ))
Making a rogue-like game with libtcod
Well I haven’t yet made a roguelike game, but I have completed a short tutorial on using the libtcod library.
Libtcod is a library with wrappers available for numerous programming languages which aids in the creation of roguelike games. I happen to be using the Python wrapper.
It provides a special true-colour console with plenty of neat tricks like customizable fonts, blitting of images and off screen drawing areas. These provide the potential for some stunning visuals that still have a blocky, old-school, feel.
While I’m sure these facts alone would make it useful for a great many styles of game, libtcod also provides numerous helpful toolkits which, for the most part, are aimed at roguelike developers.
Basically all of the standard routines that you would otherwise need to figure out for yourself before you can start designing your unique game. These include:
- Field of view toolkit. What can you see in the dungeon… where does your torchlight fall?
- Path finding toolkit. Compute the path from A to Z in your dungeon.
- Keyboard and mouse support
- BSP dungeon generation toolkit. Tweak the parameters for dungeons, towns, forests or whatever.
Plus a few more neat features you may not have ever imagined you would need for a roguelike:
- BMP and PNG support
- Heightmap toolkit 😯
- Perlin noise support. Hmmm… ocean anyone?
- Random number and compression toolkits
It’s probably important to note that you can use any or all of the toolkits that you choose… If you would prefer to implement your own dungeon generator for example, that’s fine too. In fact there is still plenty of work still to be done as a game developer. For instance, libtcod won’t implement the monster AI or combat for you (yet).
Lately I’ve been focusing on learning C++, and if I’m to learn one new language a year (which seems a reasonable goal) then this is certainly the year of C++. However, I would also like to have fun with a language that I already know well (ie. Python), and this is where I see libtcod fitting in. My verdict so far is that I like this library and I think I could have a lot of fun tinkering with it.
I’ll post my game here when it’s awesome 😀
Crikey, I nearly forgot! The tutorial that I was blathering on about at the top of this post is here. At the time of writing this only the first part has been written, but that is enough to create the explorable dungeon level seen in the screenshot above. Here’s hoping that the authors finish the remainder of the tutorial sometime soon.