Refactoring our code
In the previous code, we've drawn three textures from our game class. We hardcoded the positions, something we shouldn't do. None of the textures were moving but if we want to add movement now, our game class would get cluttered, especially if we have many sprites. Therefore we will refactor our code and introduce some classes. We will create two classes: a GameObject2D
class that is the base class for all 2D objects, and a GameSprite
class, that will represent a sprite.
We will also create a RenderContext
class. This class will hold our graphics device, sprite batch, and game time objects. We will use all these classes even more extensively when we begin building our own framework in Chapter 6, Building a Basic Framework.
Render context
Create a class called RenderContext
. To create a new class, do the following:
Right-click on your solution.
Click on Add | New Item.
Select the Code template on the left.
Select Class and name it RenderContext.
Click on OK.
This class will contain...