learning python

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.