Game Development Reference
In-Depth Information
level selection screen. Another reason for hiccups is garbage collection (destroying
objects that are no longer used, and freeing up memory). Unfortunately, there is no
way to control when garbage collection happens. Anything that is an object falls
under garbage collection. This becomes a problem when your game uses many small
objects such as vectors. In that case, try to minimize the number of new objects you
create, or pass along x and y values to methods instead of using vector objects.”
Coherency of Code
Another thing that is very important when you write code is to make sure your code is coherent .
Coherency can be achieved on several levels. First, coherency is important in the design of the code.
For example, in all the games in this topic, I have assumed that the game loop does three things:
Handles player input
Updates the game world
Draws the game world
This is a code design decision I made, but other developers might make a different choice. For
example, some game engines don't make a distinction between handling player input and updating
the game world. Other game engines see drawing as a highly separate process that isn't a part of
the game-object classes. What is important is that such a design decision is applied coherently
throughout the game. If handling input and updating the game world are supposed to be two
separate processes, this should be evident in all the classes. Another example where coherency can
be seen in the code design is the way you deal with objects that need only a single instance, such
as the game-state manager or the object responsible for drawing on the canvas ( Canvas2D ). For all
these objects, this topic's examples religiously use the Singleton design pattern. When you start
programming your own games, think explicitly about the design choices you make, and apply them
coherently while you're programming.
Coherency is also important on the structural level of your code. Each game-object class has a
separate method for each of the game-loop elements. These methods have exactly the same header
in each of the classes, and they therefore expect the same parameters. For example, in all the game-
object classes, the update method has a single parameter delta . If you're coherent in the structure
of the classes, users of those classes know what to expect. Another example is separating generic
classes such as GameObjectList (which can be used in many different games) from game-specific
classes such as WaterDrop by putting the generic classes in a namespace called powerupjs . Again,
this helps other developers understand how to use the classes and where they belong.
Finally, code should be coherent on the lexical level. Make sure all your methods have a similar
naming convention. Some developers like method and property names to always start with
an uppercase character. This topic follows the convention that variable, method, and property
names start with a lowercase character, whereas class names start with an uppercase character.
 
Search WWH ::




Custom Search