I love bashing my head against a problem until I break through the barrier. The actual bashing part doesn’t always make for an exciting evening, but I would be lying if I said the intellectual challenge wasn’t enjoyable from time to time., The best part is when everything clicks into place, and you realize you’re learning something!
Amidst all of the little challenges life in general has to offer, I’ve been working in my free time on how to make punches work better. As I wrote in an earlier post, I want to build in actual punches and boxing mechanics to make the game both more realistic and challenging. In order to do that I decided to go back to the drawing board and create one set of punches that would apply equally to both the player and the opponent. The hard part was: how could I write the code elegantly and simply enough that it won’t be a mess when I go back later to refactor it. How could I make, for example, code apply to either the player or the opponent without specifically checking whose throwing the punch, defending the punch, remembering the punch, and so on? As it turns out, the basis for the solution is incredibly simple, and I owe Aaron Cannon a huge debt of gratitude for bringing it to my attention.
Basically, it goes like this: when you pass a complex object around in Python, you don’t actually pass around copies of the object; instead, you pass along a reference to that object. It’s like telling your co-workers, “Hey, I brought doughnuts for everyone. If you want them, they’re in the break room.” In this way, anyone who wants to modify your object—or steal all of the jelly or cream-filled doughnuts like that jerk Bob from accounting—can do so easily. This turns out to be an awesome thing, because it means I don’t have to keep track of lots of copies of my opponent and player. Instead, I create one player and one opponent, and then I just let all of the punches, stats, and everything else know where to find them.
Also I want doughnuts now.
Figuring out how to make all of this work has been a thorn in my side for the past few days, and I’ve been pondering it in spare moments. When the solution finally came to me tonight while I was running on the treadmill, I couldn’t wait to write it down and make it work. And wouldn’t you know, it works like a charm!
I’ve missed this kind of zeal—this excitement—in programming for a long time, and I’m glad it finally came home to roost. It’s like turning the corner and seeing a whole new vista spread out in front of you, and the view is breathtaking. I almost wish I could call in sick to work tomorrow so I could keep working. Almost.
Oh hey! Did you guys know Christmas is coming up? You might have heard something or other about it. Well in addition to all of the awesome family and togetherness and good cheer and celebration that the Christmas season brings, it also gives me two weeks off of work while the students travel home to see their own families. And you know what that means …
Two straight weeks of Swamp!
No wait. That’s not it. Is it?