Generated by DocFX

Snake Tutorial Part 1: The Main Scene

In this chapter we will be creating the Main Scene for our Snake game. This Scene will handle the main game loop which will update and draw our Snake and the apples he'll be trying to reach.

At the end of this chapter, your game project will load and run a custom Scene instance, with customized initialization, update, and draw behaviors.

Prerequisites

To complete this part, you will need to be familiar with the process and requirements of setting up a new Ladybug project.

For more information on Ladybug's usage requirements, check out the Installation article

For more information on setting up a new Ladybug project, check out the Setting Up the Game article

Step 0: Review - Setting up the Ladybug Project

To get started we'll need to set up a new game project. We'll run through the steps here quickly, but if you need more information on these steps, check out the articles in the Getting Started section.

If you're comfortable with setting up a Ladybug project, feel free to skip ahead to the next step.

Creating the Project Structure

First we'll create the project folder and the subfolders we'll be using to organize our source code and assets.

# Create the root project folder and enter it
mkdir snake && cd ./snake
# Create the Shared Project folder
dotnet new mgshared -n core
# Create the Desktop using OpenGL Platform Folder
dotnet new mgdesktopgl -n xplatform

Review - Shared Project and Platform Target folders

The core folder contains the Shared Project that will be housing the bulk of our game source code. This allows us to store all platform-independent code in one place.

The xplatform folder contains the Platform Target project, which is what we will actually be building our game executable from. It will reference the Shared Project in core and pulling in the game code from there.

If you need a refresher on referencing the Shared Project from the Platform Target project, review the steps listed in Getting Started: Installation, in the "Hooking it All Together" section.

Getting Ladybug Up and Running

Once the project folders are in place, we'll need to decide how we're involving Ladybug in our project. The easiest way is to add it through the dotnet command:

dotnet add package WelcomeToMonday.Ladybug

The other option is to build it yourself and manually add a reference to ladybug.dll in xplatform.csproj.

These options are also covered in the Installation article linked above.

Once Ladybug has been added to your project, edit xplatform/Program.cs and get it set up with a Ladybug.Game instance. Open the file in your editor of choice and and replace its contents with the following:

using System;
using Ladybug;

namespace xplatform
{
	public static class Program
	{
		[STAThread]
		static void Main()
		{
			using (var game = new Game())
			{
				game.Run();
			}
		}
	}
}

At the moment, the Game does not have any Scenes to run, so the application will only produce an empty window. We'll be setting the Game instance up with a Scene in the next step.

Other .cs Files

The dotnet new command creates a number of files automatically when run. Among these files may be a Game1.cs in the xplatform and/or core folder.

These files are MonoGame/XNA template files that are useful when starting a new MonoGame project, but are not useful to us when using Ladybug. We will not be using them, so it is recommended to delete these files.

Step 1: Creating MainScene.cs

The first file we'll be creating in our new project is MainScene.cs -- the source file for the main scene that will be processing the bulk of our game's update and drawing logic.

While it is possible to create a Scene on the fly without creating our own derived Scene subclass, deriving a new class and defining our logic there will make for cleaner code and better organization in the long run -- especially for games that contain many scenes.

Create MainScene.cs at core/scene/MainScene.cs with the following content:

// core/scene/MainScene.cs
using System;
using Microsoft.Xna.Framework;
using Ladybug;

public class MainScene : Scene
{
	protected override void Initialize()
	{
		Console.WriteLine("Main Scene Initialized!");
	}
}

In the above code, we define a new class derived from Ladybug.Scene called MainScene. Within the class, we override the Initialize() method which, when invoked, will write "Main Scene Initialized" to the console when invoked, which will happen when it is loaded into a Game instance.

Speaking of which, now that we have our MainScene class defined, we can load it into our Game instance in Program.cs so that it is run when the game starts

// xplatform/Program.cs
// ...
static void Main()
{
	using (var game = new Game())
	{
		game.LoadScene<MainScene>();
		game.Run();
	}
}
// ...

Now, when the application is built and run, you should see Main Scene Initialized! printed to the console.

Step 2: Adding Update and Draw

Our MainScene class has a working Initialize() method that is appropriately being called as soon as it is loaded into the Game instance. This is a great start, but our game is still nothing but an empty window.

To fix this, we'll need to override MainScene's Update and Draw methods. We'll leave most of the details to the next chapter, but for now let's get our basic Update() and Draw() methods set up.

// core/scene/MainScene.cs
using System;
using Microsoft.Xna.Framework;
using Ladybug;

public class MainScene : Scene
{
	protected override void Initialize()
	{
		Console.WriteLine("Main Scene Initialized!");
	}

	protected override void Update(GameTime gameTime)
	{
		// Our update code will go here
	}

	protected override void Draw(GameTime gameTime)
	{
		// Our draw code will go here
	}
}

Conclusion

In this section, we reviewed setting up a Ladybug project and covered creating a custom MainScene class derived from Ladybug.Scene.

We added some basic initialization behavior to MainScene, loaded our MainScene into our Game instance, and saw that our Initialize() method was invoked when our game was built and run.

We also created some empty Update() and Draw() methods that we'll be using in the next chapter to extend MainScene's functionality and take another step closer to creating our Snake game.

Next Steps

With our MainScene in hand, it's time to head to the next chapter, Sprites and Player Input.

Complete code for this chapter can be found here.