Tower Defense (Python)

Tower Defense games were my favorite type of games when I was young. The format is quite simple: spend resources to build towers to defeat enemies before they reach the end stage. They were popular on many early Flash games due to their simplicity; plus the fact that the static backgrounds minimize the art assets and level design required. 

The framework I've developed for PartyQuest has all the tools needed for a Tower Defense game. The major functions were to import character sprites, unit class, level loading, buttons, and pathfinding. I just needed a few days to create a separate user interface and tune gameplay balance.




With how hectic Tower Defenses can get, I know I needed to stress-test the performance by creating a high number of units (player / enemies). I noticed considerable slowdown due to the inefficient O(n^2) functions throughout my framework. I had combined player and enemy objects within the same list, I had to cycle through the entire list for every interaction. So with 18 player units and 18 monsters, there were 36² (1296) calculations. Many of these checks were redundant based on the game design (player units couldn't damage the other player units), so the easiest solution was to split the objects into separate lists. Thus, with the same number of units the run time becomes 18*18 (324 calculations), which is four times more efficient.


15 frame vs 39 frames

With memory management being especially important within the slower-Python, this was a good exercise in applying algorithmic fundamentals.




Comments