(ded4a3e0a) v0.9.0.7
This commit is contained in:
+3
@@ -0,0 +1,3 @@
|
||||
This tutorial assumes that you have configured MonoGame correctly on your system, if you haven't, please read the [Setting Up MonoGame](setting_up_monogame.md).
|
||||
|
||||
Depending on the software you plan on using please follow either [Visual Studio](getting_started/1_creating_a_new_project_vs.md), or [VS for Mac / MonoDevelop](getting_started/1_creating_a_new_project_md.md) guide.
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
Start up MonoDevelop / Xamarin Studio and select **New...** in the upper left corner.
|
||||
|
||||

|
||||
|
||||
Now you should see a "New Project" dialog pop up. From here select **MonoGame > App** category, then select **MonoGame Cross Platform Desktop Project** and click **Next**.
|
||||
|
||||

|
||||
|
||||
On the following dialog, type in the name that you wish to give your project. Do note that you should not use space character for it. For this tutorial, it will be named **ExampleGame**. After you've entered the name, click on the **Browse** button next to location text field, and select where you wish to save your project. Finally click **Create** to create a new project.
|
||||
|
||||

|
||||
|
||||
If everything went correctly, you should see an **ExampleGame** project open up like in the picture bellow. To run your game simply press the big **Play Button** in the upper left corner or press **F5**.
|
||||
|
||||

|
||||
|
||||
You should now see your game window running.
|
||||
|
||||

|
||||
|
||||
Currently it's just clearing the surface with blue color. For further information on creating your game, please look at the [Understanding the Code](getting_started/2_understanding_the_code.md).
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
Start up Visual Studio and select **New Project...** in the upper left corner.
|
||||
|
||||

|
||||
|
||||
Now you should see a "New Project" dialog pop up, from here select **Templates > Visual C# > MonoGame** category, and then select **MonoGame Cross Platform Desktop Project**. Next type in the name that you wish to give your project, for this tutorial let's just use **ExampleGame** (do note that you should not use space character for it). After you've entered the name, click on the **Browse** button next to the location text field, and select where you wish to save your project. Finally click **OK** to create a new project.
|
||||
|
||||

|
||||
|
||||
If everything went correctly, you should see an **ExampleGame** project open up like in the picture bellow. To run your game simply press the big **Play Button** in the toolbar or press **F5**.
|
||||
|
||||

|
||||
|
||||
You should now see your game window running.
|
||||
|
||||

|
||||
|
||||
Currently it's just clearing the surface with blue color. For further information on creating your game, please look at the [Understanding the Code](getting_started/2_understanding_the_code.md).
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
|
||||
This file will go over the code that is getting created when you start a blank project. For help on creating a project please look at [Creating a New Project](getting_started/1_creating_a_new_project.md)
|
||||
|
||||
**Using Statements**
|
||||
|
||||
```csharp
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Storage;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
```
|
||||
|
||||
These using statements are required in order to use the code that MonoGame has to offer.
|
||||
|
||||
The reason why they start with Microsoft.Xna.Framework is because MonoGame is an open source implementation of Microsoft Xna framework, and in order to maintain compatibility with the XNA code, it is using the same namespaces.
|
||||
|
||||
**The Game1 Class**
|
||||
|
||||
```csharp
|
||||
public class Game1 : Game
|
||||
```
|
||||
|
||||
The main Game1 class is inheriting from the Game class, which provides all the core methods for your game (ie. Load/Unload Content, Update, Draw etc.). You usually have only one Game class per game so its name isn't that important.
|
||||
|
||||
**Instanced Variables**
|
||||
|
||||
```csharp
|
||||
GraphicsDeviceManager graphics;
|
||||
SpriteBatch spriteBatch;
|
||||
```
|
||||
|
||||
The two default variables that the blank template starts with are GraphicsDeviceManager and SpriteBatch. Both of these variables are used for drawing stuff as you will see in a later tutorial.
|
||||
|
||||
**Constructor**
|
||||
|
||||
```csharp
|
||||
public Game1()
|
||||
{
|
||||
graphics = new GraphicsDeviceManager(this);
|
||||
Content.RootDirectory = "Content";
|
||||
}
|
||||
```
|
||||
The main game constructor is used to initialize the starting variables. In this case we are creating a new GraphicsDeviceManager from our game, and are setting the folder which the game will search for content.
|
||||
|
||||
**Initialize Method**
|
||||
|
||||
```csharp
|
||||
protected override void Initialize()
|
||||
{
|
||||
// TODO: Add your initialization logic here
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
```
|
||||
|
||||
This method is called after the constructor, but before the main game loop(Update/Draw). This is where you can query any required services and load any non-graphic related content.
|
||||
|
||||
**LoadContent Method**
|
||||
|
||||
```csharp
|
||||
protected override void LoadContent()
|
||||
{
|
||||
// Create a new SpriteBatch, which can be used to draw textures.
|
||||
spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
// TODO: use this.Content to load your game content here
|
||||
}
|
||||
```
|
||||
|
||||
This method is used to load your game content. It is called only once per game, after Initialize method, but before the main game loop methods.
|
||||
|
||||
**Update Method**
|
||||
|
||||
```csharp
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||
Exit();
|
||||
|
||||
// TODO: Add your update logic here
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
```
|
||||
|
||||
This method is called multiple times per second, and is used to update your game state (checking for collisions, gathering input, playing audio, etc.).
|
||||
|
||||
**Draw Method**
|
||||
|
||||
```csharp
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||
|
||||
// TODO: Add your drawing code here
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
```
|
||||
|
||||
Similar to the Update method, it is also called multiple times per second.
|
||||
|
||||
For the next part, look at [Adding Content](getting_started/3_adding_content.md) page.
|
||||
@@ -0,0 +1,78 @@
|
||||
This file will go over adding content to your game. For help on creating a project please look at [Creating a New Project](getting_started/1_creating_a_new_project.md)
|
||||
|
||||
First of all you are gonna need some content for your game. For this tutorial use the following image of a ball:
|
||||
|
||||

|
||||
|
||||
Do **right-click > Save Image As** and save it somewhere with the name "ball.png".
|
||||
|
||||
Now open up your game project and look at the left. You should see a solution explorer window. Expand the **Content** folder and open up **Content.mgcb** file by double clicking on it.
|
||||
|
||||

|
||||
|
||||
You should now see a MonoGame Pipeline Tool window open up. In case it didn't get opened, you can right-click on **Content.mgcb**, select **open with** and then select **MonoGame Pipeline**.
|
||||
|
||||

|
||||
|
||||
Your game content is managed from this external tool. You can add content to your game in one of the following ways:
|
||||
|
||||
- **Add Existing Item** toolbar button
|
||||
- **Edit > Add > Existing Item...** menu button
|
||||
- **right-click > Add > Existing Item...** context menu
|
||||
|
||||
In our case let's use the **Add Existing Item** toolbar button.
|
||||
|
||||

|
||||
|
||||
You should now be prompted to select a file. Select the "ball.png" that you have downloaded a moment ago. After that you will be asked on what action you want to do for adding the file. Just leave the it to default and click **OK**.
|
||||
|
||||

|
||||
|
||||
Now simply click **Save** toolbar button and close the tool.
|
||||
|
||||

|
||||
|
||||
Now that we have added the content, it's time to load it. First declare a new variable so we can load the ball image into memory.
|
||||
|
||||
```csharp
|
||||
public class Game1 : Game
|
||||
{
|
||||
Texture2D textureBall;
|
||||
|
||||
GraphicsDeviceManager graphics;
|
||||
```
|
||||
|
||||
Next find the Load Content method and use it to initialize the ball private variable:
|
||||
|
||||
```csharp
|
||||
protected override void LoadContent()
|
||||
{
|
||||
// Create a new SpriteBatch, which can be used to draw textures.
|
||||
spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
// TODO: use this.Content to load your game content here
|
||||
textureBall = Content.Load<Texture2D>("ball");
|
||||
}
|
||||
```
|
||||
|
||||
And finally, find the Draw method, and let's draw the ball onto the screen:
|
||||
|
||||
```csharp
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||
|
||||
// TODO: Add your drawing code here
|
||||
spriteBatch.Begin();
|
||||
spriteBatch.Draw(textureBall, new Vector2(0, 0), Color.White);
|
||||
spriteBatch.End();
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
```
|
||||
|
||||
Now run the game and you should get the following:
|
||||
|
||||

|
||||
|
||||
For the next part, look at [Adding Basic Code](getting_started/4_adding_basic_code.md) page.
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
This file will go over adding basic logic to your game. Do note that this file continues where [Adding Content](getting_started/3_adding_content.md) tutorial left off.
|
||||
|
||||
First of all we need to add few new variables, one for position, and one for speed.
|
||||
|
||||
```csharp
|
||||
public class Game1 : Game
|
||||
{
|
||||
Texture2D ballTexture;
|
||||
Vector2 ballPosition;
|
||||
float ballSpeed;
|
||||
```
|
||||
|
||||
Next let's initialize them. Find the **Initialize** method and add the following lines.
|
||||
|
||||
```csharp
|
||||
// TODO: Add your initialization logic here
|
||||
ballPosition = new Vector2(graphics.PreferredBackBufferWidth / 2,
|
||||
graphics.PreferredBackBufferHeight / 2);
|
||||
ballSpeed = 100f;
|
||||
|
||||
base.Initialize();
|
||||
```
|
||||
|
||||
With this we are putting our ball starting position to the center of the screen. Last thing we need to do is modify the position that the ball is getting drawn to. Find **Draw** method and modify the Draw call to:
|
||||
|
||||
```csharp
|
||||
spriteBatch.Draw(ballTexture, ballPosition, Color.White);
|
||||
```
|
||||
|
||||
Now run the game.
|
||||
|
||||

|
||||
|
||||
As you can see the ball doesn't seem quite centered yet. This is happening because MonoGame uses (0, 0) as the origin point for drawing by default. We can modify this by doing the following:
|
||||
|
||||
```csharp
|
||||
spriteBatch.Draw(
|
||||
ballTexture,
|
||||
ballPosition,
|
||||
null,
|
||||
Color.White,
|
||||
0f,
|
||||
new Vector2(ballTexture.Width / 2, ballTexture.Height / 2),
|
||||
Vector2.One,
|
||||
SpriteEffects.None,
|
||||
0f
|
||||
);
|
||||
```
|
||||
|
||||
With this we are setting the origin to the center of the image. Now the image will get drawn to the center of the screen.
|
||||
|
||||

|
||||
|
||||
Next let's setup some movement. Find the **Update** method and add:
|
||||
|
||||
```csharp
|
||||
// TODO: Add your update logic here
|
||||
var kstate = Keyboard.GetState();
|
||||
|
||||
if (kstate.IsKeyDown(Keys.Up))
|
||||
ballPosition.Y -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
if(kstate.IsKeyDown(Keys.Down))
|
||||
ballPosition.Y += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
if (kstate.IsKeyDown(Keys.Left))
|
||||
ballPosition.X -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
if(kstate.IsKeyDown(Keys.Right))
|
||||
ballPosition.X += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
base.Update(gameTime);
|
||||
```
|
||||
|
||||
Let's discuss the code a bit.
|
||||
|
||||
With this we are getting the current keyboard state and just putting it into a variable.
|
||||
|
||||
```csharp
|
||||
var kstate = Keyboard.GetState();
|
||||
```
|
||||
|
||||
Next is just a simple check to see if the Up arrow key is pressed.
|
||||
|
||||
```csharp
|
||||
if (kstate.IsKeyDown(Keys.Up))
|
||||
```
|
||||
|
||||
And last is a simple code for moving the ball by **ballSpeed**. The reason why **ballSpeed** is getting multiplied by **gameTime.ElapsedGameTime.TotalSeconds** is because Update is not usually fixed, that is the time between update calls is not the same, so in order to get smooth movement we multiple speed by the time since the last update method was called.
|
||||
|
||||
```csharp
|
||||
ballPosition.Y -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
```
|
||||
|
||||
The last 2 code parts repeat for Down, Left and Right arrow keys.
|
||||
|
||||
Run the game and you should be able to move the ball with the arrow keys. You will probably notice that you can get out of the window, so let's make it so that the ball can't escape the window. We will do this by setting bounds onto the ballPosition after it has already been moved.
|
||||
|
||||
```csharp
|
||||
if(kstate.IsKeyDown(Keys.Right))
|
||||
ballPosition.X += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
ballPosition.X = Math.Min(Math.Max(ballTexture.Width / 2, ballPosition.X), graphics.PreferredBackBufferWidth - ballTexture.Width / 2);
|
||||
ballPosition.Y = Math.Min(Math.Max(ballTexture.Height / 2, ballPosition.Y), graphics.PreferredBackBufferHeight - ballTexture.Height / 2);
|
||||
|
||||
base.Update(gameTime);
|
||||
```
|
||||
|
||||
Now run the game and the ball won't be able to escape window bounds anymore.
|
||||
|
||||
Happy Coding ^^
|
||||
@@ -0,0 +1,8 @@
|
||||
This section walks you through the basics of MonoGame and help you create your first game.
|
||||
|
||||
- [Creating a New Project](getting_started/1_creating_a_new_project.md)
|
||||
- [Visual Studio](getting_started/1_creating_a_new_project_vs.md)
|
||||
- [MonoDevelop / Xamarin Studio](getting_started/1_creating_a_new_project_md.md)
|
||||
- [Understanding the Code](getting_started/2_understanding_the_code.md)
|
||||
- [Adding Content](getting_started/3_adding_content.md)
|
||||
- [Adding Basic Code](getting_started/4_adding_basic_code.md)
|
||||
Reference in New Issue
Block a user