Difference between revisions of "Bricks"
(→Game1.cs Declarations) |
|||
Line 45: | Line 45: | ||
=Game1.cs Constructor= | =Game1.cs Constructor= | ||
To set the size of your screen, enter the following commands in the constructor method. A constructor method is uses the name of the class, so look for 'public Game1()' This is run once when your game is created: | To set the size of your screen, enter the following commands in the constructor method. A constructor method is uses the name of the class, so look for 'public Game1()' This is run once when your game is created: | ||
+ | |||
<syntaxhighlight lang=csharp> | <syntaxhighlight lang=csharp> | ||
graphics.PreferredBackBufferHeight = 600; | graphics.PreferredBackBufferHeight = 600; | ||
graphics.PreferredBackBufferWidth = 400; | graphics.PreferredBackBufferWidth = 400; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
=Game1.cs LoadContent= | =Game1.cs LoadContent= | ||
Line 69: | Line 69: | ||
wallBounceSound = Content.Load<SoundEffect>("WallBounceSound"); | wallBounceSound = Content.Load<SoundEffect>("WallBounceSound"); | ||
missSound = Content.Load<SoundEffect>("MissSound"); | missSound = Content.Load<SoundEffect>("MissSound"); | ||
+ | |||
+ | paddlePosition = new Vector2((graphics.PreferredBackBufferWidth / 2) - imgPaddle.Width, 550); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =Game1.cs Update= | ||
+ | The update method is essentially the game loop and is continuously run for your game, it will be run before the draw method is called. | ||
+ | |||
+ | This method will be used to read the user input and move any of the sprite characters. So to move the paddle we can add the following code: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | if (Keyboard.GetState().IsKeyDown(Keys.Z) | ||
+ | { | ||
+ | if (paddlePostion.X < 0) | ||
+ | paddlePosition.X = 0; | ||
+ | else | ||
+ | paddlePosition.X -= 2; | ||
+ | } | ||
+ | |||
+ | if (Keyboard.GetState().IsKeyDown(Keys.X) | ||
+ | { | ||
+ | if (paddlePostion.X > (graphics.PreferredBackBufferWidth - imgPaddle.Width)) | ||
+ | paddlePosition.X = graphics.PreferredBackBufferWidth - imgPaddle.Width; | ||
+ | else | ||
+ | paddlePosition.X += 2; | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | =Game1.cs Draw Method= | ||
+ | This will always be run after the update method. So add the following code into the Draw method: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | spriteBatch.Begin(); | ||
+ | spriteBatch.Draw(imgPaddle, paddlePostion); | ||
+ | spriteBatch.End(); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | If you test the game you should be able to control the paddle on the screen, test if it can go off the screen. |
Revision as of 10:05, 25 October 2017
Contents
Original Tutorial
the original tutorial can be found on the link below:
Create a new project
Using the MonoGame templates, create a MonoGame Windows Project.
Resources Required
All of the resources for this tutorial can be downloaded here. Extract these files into the Content folder for your project.
In Visual Studio, double click the Content.mgcb file in the solution explorer panel. This will run the content pipeline tool. In the content pipeline tool, click the add existing item icon. Now browse for the Content folder of your project, and select all of the png & wav files downloaded above.
Now build the pipeline, you should see zero errors and each of the 9 items should be successful.
Game1.cs Using References
At the very start of the Game1.cs file, you will find some using declarations. make sure the following are included:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
Game1.cs Declarations
Find the line of code which states 'SpriteBatch spriteBatch;', now press enter to go onto the next line.
This is where you can declare any variables within your game. We are going to declare some Texture2D for the sprites/graphics within this game and also some sound effects. Add the following declarations:
public Texture2D imgBrick;
public Texture2D imgPaddle;
public Texture2D imgBall;
public Texture2D imgPixel;
public SoundEffect startSound;
public SoundEffect brickSound;
public SoundEffect paddleBounceSound;
public SoundEffect wallBounceSound;
public SoundEffect missSound;
public Vector2 paddlePosition;
Game1.cs Constructor
To set the size of your screen, enter the following commands in the constructor method. A constructor method is uses the name of the class, so look for 'public Game1()' This is run once when your game is created:
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 400;
Game1.cs LoadContent
Now find the LoadContent method of Game1.cs. This is the method to load all of your game assets into your game. We will use this to load your built resources and to assign them to the variables we have declared above.
So add the following code to the LoadContent method:
//load images
imgBall = Content.Load<Texture2D>("Ball");
imgPixel = Content.Load<Texture2D>("Pixel");
imgPaddle = Content.Load<Texture2D>("Paddle");
imgBrick = Content.Load<Texture2D>("Brick");
//load sounds
startSound = Content.Load<SoundEffect>("StartSound");
brickSound = Content.Load<SoundEffect>("BrickSound");
paddleBounceSound = Content.Load<SoundEffect>("PaddleBounceSound");
wallBounceSound = Content.Load<SoundEffect>("WallBounceSound");
missSound = Content.Load<SoundEffect>("MissSound");
paddlePosition = new Vector2((graphics.PreferredBackBufferWidth / 2) - imgPaddle.Width, 550);
Game1.cs Update
The update method is essentially the game loop and is continuously run for your game, it will be run before the draw method is called.
This method will be used to read the user input and move any of the sprite characters. So to move the paddle we can add the following code:
if (Keyboard.GetState().IsKeyDown(Keys.Z)
{
if (paddlePostion.X < 0)
paddlePosition.X = 0;
else
paddlePosition.X -= 2;
}
if (Keyboard.GetState().IsKeyDown(Keys.X)
{
if (paddlePostion.X > (graphics.PreferredBackBufferWidth - imgPaddle.Width))
paddlePosition.X = graphics.PreferredBackBufferWidth - imgPaddle.Width;
else
paddlePosition.X += 2;
}
Game1.cs Draw Method
This will always be run after the update method. So add the following code into the Draw method:
spriteBatch.Begin();
spriteBatch.Draw(imgPaddle, paddlePostion);
spriteBatch.End();
If you test the game you should be able to control the paddle on the screen, test if it can go off the screen.