Thursday, July 21, 2011

Reticulated programing: playing with snakes part 1

Well for a change of pace some hopefully useful or at least intriguing information if people were ever interested in programing. So with my current weapon of choice I present playing with snakes.

Image via this website

So as I like to be goal orientated to go about things we're going to make a very bare bones terminal based RPG that my friends had dubbed a long time ago the Fighter RPG. Very basic, no real options, you get to level up and chose some stats to bring up and maybe use some potions. Sounds simple, but programing it is a bit of a challenge.

First things first, you need to download the Python interpreter. For the purposes of me not having to read up on version 3 go with version 2.7. Though I doubt it'll make a difference. I don't believe version 3 broke a lot of code.

For this post I'll just stick with basic output right now. Since I'm not doing anything fancy everything is just text. 

After it extracts and you run it you'll be greeted with the shell. Which will be where we will play with today.
snake skin
The IDLE gui will work well enough to program in and it will give us some syntax coloring. Though in my experience that'll be erroneous sometimes though it'll at least show that it's an important keyword. What that means I'll get to later. For now I'll show the neat thing about interpretive languages. Right now you are able to input code and Python will execute it. For the age old starter we'll do the age old "Hello World" 'program'. To do that you'll need to type the following.

Print "Hello World"

After that just hit enter and it'll execute. You should notice Print and "Hello World" were different colors. Print is a command in the language while "Hello World" is a string, for now we'll just denote strings by the pairing of " but it can be done otherwise. Anything between the two "s will be counted as being part of the string. Print will try it's best to print out what's following after it. For instance you can print out the result of a math function. Try the following.

Print 5+4

You get the obvious result of 9. Now for a tricky part, in most languages there is a concept of  Concatination. Where you are able to combine strings together. To show this off try the following.

Print "Hello " + "World"

The result is the more complex version of Hello World above. What happens is effectively string math. It takes 'World' and 'adds' it to 'Hello ' to produce 'Hello World'. There are other math commands that python likes using with strings but I'll save those for later. Though speaking of math, what happens if we wanted to use numbers with strings? Like print out #1) for some innane reason? Well lets try it.

print "#"+1+")"

You got a big wall of red text there. That's an error, the interpreter has met with something it did not like. In this case I'll skip letting you figure out the horrors of that message and tell you what went wrong. It's an annoying part of python to me but you can not straight out concatinate strings and numeral objects. You have to convert the int, I'll explain what that means later, into a string. So to do that.

print "#"+str(1)+")"

No ugly red text. str is an inbuilt function in Python which I will get into later, all you need to know is by placing something between the brackets it'll try it's best to make it a string. Though this is an academic case, if you knew you were going to need one there you could have had it contained as "#1)". I don't know the best way to explain this but when it's in a string, it's in a string meaning when it's surrounded by "s it's seen as a character and not as a potential number by the interpretation. So you should have something like this.

I think this is week 1 of a computer science course in a nutshell

And I'll leave that as it for today. While not immediately useful it's a glimpse into programing. I may take this to completion faster if anyone is interested though it'll serve as something I like doing, showing people how to program.

Edit: Forgot to briefly touch str()


  1. Wow, there's a lot of work that goes into these kinds of things!

  2. I wish this post was actually about snakes :\

  3. I'm actually too tired to do this right now, but I do love stuff like this, so I'm more than likely going to give it a go when I have more energy, you know, during my funeral.