Friday, April 25, 2008

What is Computer Programming?

I know this question is totally obvious to those who actually program, but to others it is a mystery. Personally I don’t know how to change the oil on my car, but I can make a computer play Magic, lol.

Computer programming is in many ways very tedious. Every dot, character, and bracket has to be perfect otherwise the program won’t compile. But the “fun” doesn’t stop there. Even when the program compiles, it might not do what you want it to do. You get to have even more “fun” when you discover a bug and try to fix it.

Fixing a computer bug or error takes three steps. One, to fix a bug you have to be able to constantly reproduce it, “Does Giant Growth let you target a land?” Two, then you change the code hopefully fixing the problem. Three, you test you code to see if it is fixed. If the bug isn’t fixed, and that is often the case, you change more code and then try again. Occasionally a programmer will skip the last step for some crazy reason that makes sense now but not later, “I’m sure it is fixed so I don’t need to test it.” Sometimes bug fixing can be very rewarding and other times it is totally frustrating.

The best way to know what computer programming really is, is to look at some code. The code below was written in about an hour using Pythin (a programming language like Java) and the graphic library PyGame and PGU. (A graphic library lets you draw on the screen using sprites like in a videogame.) The code just shows a dialog, sometimes called a popup box, that displays a short message and lets the user select yes or no. The reason that the code is so long is that there are several details to take care of, like what if the user closes the dialog? This is interpreted as selecting the no button.

The code below would look different if it were written in another computer language like Java. This code is slightly different since it is drawing on the screen like a videogame. A typical dialog box would use the Windows or Macintosh library to draw itself. This dialog has to draw itself on top of other components that are being drawn. A dialog box in a videogame is very different than a dialog in Word or Internet Explorer.

I view computer programming like doing a crossword puzzle. Some people love doing them, but personally I hate them. The same goes for programming, I love it, but others hate it. I enjoy solving a million tiny problems when I am programming. It probably helps that I have a hint of obsessive compulsion with a dash of perfectionism.

Many computer programmers are also shy and socially awkward, watch the TV show “Beauty and the Geek”. Some programmers play music instruments. Trying to mind-meld with the computer is difficult, so most programmers have highly developed concentration skills. Sometimes when I am reading a magazine article, I forget what day it is. Many, and probably all, programmers love Star Trek and in particular Spock. Did you ever think that Data was just a different form of Spock? In closing, programming is hard, lol :)
def showYesNoDialog(stringTitle, stringMessage, yes=None, no=None):
doc = gui.Document(width=300, align=-1)
doc.block(align=0)
doc.add(Label(stringMessage))

yesButton = gui.button.Button("Yes")
noButton = gui.button.Button("No")

doc.block(align=0)
doc.add(yesButton)
doc.add(Spacer(50,10))
doc.add(noButton)

d = Dialog(Label(stringTitle), doc)

#this looks funny
#clickYes and clickNo have to have an argument,
#otherwise there will be an error
#also, have to reset CLOSE function otherwise
#clickNo will always be called
def clickYes(nothing):
d.connect(gui.CLOSE, lambda(x) : None, None)
d.close()
if yes is not None:
yes()

def clickNo(nothing):
d.connect(gui.CLOSE, lambda(x) : None, None)
d.close()
if no is not None:
no()

yesButton.connect(gui.CLICK, clickYes, None)
noButton .connect(gui.CLICK, clickNo, None)

#closing the dialog executes the "no" fuction
d.connect(gui.CLOSE, clickNo, None)

#show dialog
d.open()

2 comments:

Silly Freak said...

"A dialog box in a videogame is very different than a dialog in Word or Internet Explorer"

does that mean the python version will be fullscreen or is it just hard to make dialogs with that graphic libraries?

Forge said...

It just means that it is hard to make dialogs with graphic libraries. But the Python version I am working on will be fullscreen. Everything will be drawn on the screen so the cards will be scaled down pictures.

The current version, which uses Java, basically uses "regular" visual components which explains why it doesn't look much like a videogame. I hope that helps.