Is time to do some changes to our app to start to create a proper game. First, we’re going to change our window size where the player is going to play.

Changing the screen size

Changing the screen size is very easy, just we need to use our GraphicsDeviceManager in our Game1 class. In the constructor of the class, we need to add the following lines

If you run the game, you should see a window with the size that we pass it.

Adding background and positioning the player in the right place

Now we need to put our background to start to see our game as we want. After this point, we going to start with the real gameplay, so this is an exciting step.

First, you need to add the desert background (you need to change the name of this, because has a typo, change it to “background” for an easy reference)

With the asset in our Content directory, is time to adding to our game. Because we’re going to use the width and height of our screen a lot, I want to add a variable to not have any magic number in our code, so, in the Game1 class, we’re going to add two new variables.

Now, we need to initialize our Rectangle and then we’re going to use the screenRectangle variable to replace the magic numbers.

Now, we need to load our background, add this code to the LoadContent method

And finally, we need to Draw our background

It’s very important the order that we draw our objects because the first one is drawing before that the next.

If you run the project, you should see this

Changed the player to the right place

With the background in place, is time to change the Player2D class to position the player at the bottom of the screen and stop the auto movement.

First, we need to change our Player2D class, we have the variable playerSprite as private, we need to change to public property because we’re going to need the position of our sprite. So do that

Now change all playerSprite references in the document to change it to PlayerSprite

Next, change the default direction to 0

You need to remove the default position, this position we’re going to put in the Game1 class. You Initialize method should be like this

Then, in the Update method, we need to remove all these lines because we only use the keyboard to move the player

Now, in the Game1 class, we need to tell to our sprite where is going to take place when the game  is loaded.

In the Initialize method, we need to add the following code after playerSprite initialize

First, because we do not have access to Heigh and Width of the spritesheet because we haven’t loaded, we need to put the dimensions of the sprite manually, using the Vector2.

Then, we put the position of the sprite using the screenRectangle value and the new variable. With a little mathematical operation, we put the sprite in the center of screen and bottom

Run the project to see

Moving the player with the keyboard

With all stuff in place, we are going to start to move the sprite with the keyboard. This is done in the Update method of our Player2D class.

For now, we only move the sprite to left and right.

First, we need a way to get the state of the keyboard, and MonoGame provides us with a class to accomplish that we want. We need to add the Input framework.

Then we need to add a private variable to get the state of the Keyboard

Now we need to change the Update method as follow

  1. First, we need to get the current keyboard state, we need this because MonoGame checks what keys are pressed down
  2. MonoGame provides us with utilities to get the a key from the keyboard we check if the Left or Right arrow is pressed down, if yes, then we put the direction as we have done before when the player was moving automatically.
  3. This not changed, just we put the position of the player using the direction and speed
  4. We need to check that the player does not go outside of the screen, if the player position is lesses than 0, the position is 0, if is more than the graphics device and viewport, the position is that.
  5. After all checkings of position, we put the PlayerSprite position to the current playerPos
  6. We put the direction to 0, to stop moving the player

If you run the project, you will notice that the player is moving slowly, that is we have the speed to 60, change the value to one that is comfortable for you, I put it to 250, but you are free to change i


We have done our first steps to a real gameplay with the player. For now, we’re going to start to adding more fun to the game and create our Space shooter.

You can see the actual code in my Github