programming

I wrote my first computer game

battleshipsIn a long ago post I wrote about learning computer programming, and how I think it’s a good thing for everyone to explore at least a little. I finished my first real program the other day. It’s a very simple game. I call it Battleship (no relation to the board game).

Here’s how it works:

  • You select a famous battleship from a list (stats are different for each ship)
  • The program randomly selects an opponent
  • Your ship “fights” the opponent in a series of text-based combat rounds (I haven’t learned anything to do with graphics yet)
  • The combat module uses a random number generator to determine the probability of a “hit” during each combat round.
  • If the random number generator registers a “hit,” the code divides the “attack” rating of the shooter by the “defense” rating of the defender to determine the amount of damage.
  • Once a ship reaches 100% damage the event triggers a change in a Boolean value, which in turn triggers the end of the battle and announcement of the winner.
  • Finally, just before exit a pithy bit of text appears describing the outcome

Clearly this isn’t exactly going to set the world on fire. Still, it was a valuable learning experience. I really enjoyed coming up with creative solutions for the problems that cropped up along the way. More importantly, as I worked on Battleship I began to see some of the same principles at work in (much) more complex mainstream games. When you strip away all the fanciest features every videogame in existence boils down to a bit of code running on a loop.

I don’t have nearly as much time or energy right now as I’d like to code. I’ve started sketching out a plan for a new text-based game – one that people might actually enjoy playing. I will work on it as time permits. When I finish this next game I hope to put it online for all to enjoy.

Everyone should learn computer programming (to some extent)

I think everyone should learn a bit of computer programming. Computers and computer-like devices (tablets, smartphones) are just too important to our everyday lives for us to bumble around treating them like black boxes with incomprehensible innards. I recently started to learn a bit of basic programming myself. While Python is a pretty basic language and I am clearly a beginner, it’s already given me some insight into what the hell it is my laptop/phone is doing when I load up a program or app. It’s also helped me get a batter handle on the technology-related aspects of my fiction writing.

Here are the two beginner resources I would recommend if you want to learn to code:

Another great thing about learning coding and programming is there are plenty of helpful folks out there willing to share their expertise. This means there are a whole slew of free resources available on the web in addition to the two listed above. Give it a try. You won’t be disappointed.

Who knows, maybe it’ll help you start a tech company that you can sell for billions of dollars despite running an operating loss (oops! digression…).