Try and Triumph!
Programming can be a frustrating task and no more so did this fact strike home than when developing the code concerned with game play. Before I delve into the issues I experienced I should first note that I have
been able to create a simple interface system which, using buttons, switch from one page to another.
I was able to achieve this using a enum variable containing all the different states which can be found within the game such as playing, main menu and quitting.
Using boo-leans I could then switch state and then load everything needed within that one state using if statements which works very well and effectively. To those of you who aren't very confident with coding so far it simple means that I can set the game state to say main menu and then write code saying if the state is equivalent to main menu execute the corresponding code snippets.
I was able to switch between each state by writing that if a certain task is performed then the state needs to be changed to the next appropriate state required. This was fairly simple to code and I didn't encounter any issues however this changed when I began developing code for game play.
I should first start by explaining that when producing my code I wanted to create re-usable code which would then increase game performance as the less game assets that's needed to be loaded the less time is needed to run code making it more efficient. The game consists of a list of civilian agents and a number of targets which will walk randomly on the screen.
This was easy to achieve as it was simply an extension of a task I had learned a year previous during my first year at university. There were two ways which I could program the agents into the game which was to create separate lists for each level which would take up considerably more lines of code and decrease performance or create one list and simply reuse them in each level.
I wrote the code and then tested my game and the first level worked well however it was when I progressed to the second level where the first error was encountered. All the game agents were still and the code wasn't executing properly. The source for this error was that using a boolean value I would set all agent and user variables to null/zero meaning they wouldn't move again. I figured I could use a while statement and then reset the boolean value at the beginning of each level however this proved problematic as the game would freeze straight away.
After many tries and errors I came up with the idea of adding a loading page between each level and adding a reset method to each agent class. The reset method would simply set each variable to their default values and using a four second loading page I could call and execute this method for each agent as well as all the other game play variables. Doing this proved successful as the booleans would all be set to false again and the agents would spawn randomly again and walk randomly.