🆕 New Here? Read This! [Now with Menu]

Welcome

The Navigation Menu, click here to expand and close

1- How to input Code to your messages on a thread

2- Finding hidden gems

3- Sample Code

4- How to search first before asking for help

5- Using Github CopilotXChat in your Projects


Hey there, first and foremost, Welcome to the Community, we hope you settle down, – find a hut somewhere, and start the BBQ, I mean, – get comfortable, get coding, and enjoy coding like we all do.

Why this thread exists:
I noticed something was missing, something for visitors looking for their next challenge or inspiration, and to understand what MonoGame is so they can get to choosing it and getting started already.

So, with that said, if that is you, and you want a one stop shop to understand whether you should or not use MonoGame for your course or project, read on.

I have once been fishing around for something to move into from my previous coding language, you might say it was my search for my move into a C like language. You may be looking for your first foray into programming or game development or just started a university thing, keep reading.

Long ago I knew I wanted to use C#, so MonoGame ticked that box immediately. I was moving from one programming language to a level up.
MonoGame uses C#.

I had previously utilised a framework-like toolset, where you had something to display things, but you had to tell it what to draw, MonoGame ticked this box too.
MonoGame is a Framework, which handles Inputs, Display, Sound, and more…

I also wanted something which had plenty of documentation or tutorials, as MonoGame is XNA based, those books and online resources all explained how to do things in XNA/MonoGame, another box ticked.

You can find a lot of XNA books, and even a handful of MonoGame related books on Amazon or a good book store, as well as across the internet.

Microsoft XNA – which was discontinued around a decade ago – was so loved, communities grew to keep it alive, communities such as this one. And for me, a helpful community was essential to find help when it was needed, MonoGame – you guessed it – ticked this box too.

MonoGame’s Community, while it may seem quiet around here, is very much alive and kicking, – however the BBQ, I mean activities have shifted to a more real time experience – over on Discord, link here: MonoGame Discord

I realised MonoGame was a community project, which leads to the question, how is the website funded?

You can learn more about supporting MonoGame here: Donate | MonoGame and also find the modern thing people do, over here: https://www.patreon.com/MonoGame

Now, whenever you look at a new tool, you do find yourself wondering, how does this hammer work? Can it nail and un-nail things? Well, while your local hardware store may not have live demonstrations, MonoGame on the other hand, does have a showcase to give you ideas of what you can achieve with it. We used to call this; Box Art.

__


__

You can find the MonoGame Box Art over on the Showcase page, found here: Showcase | MonoGame [Yes, I side stepped to make the box art you see above, yes, the QR Code works.]

So, it has a structure to its code, right? Yes, it does, somewhat, you in the end decide how it glues together.

You can seek the official documentation here: https://docs.monogame.net/ more specifically you want to look at Getting Started | MonoGame Documentation.

Remember, if you need help with the ability to back trace your help, post a thread on the forum, I recommend using a GitHub account to sign in.

You are reading this on the forum, look at the top right of the forum to see a button to Sign Up, then click ‘+ New Topic’ in the same area once signed in. Set the title to be something relating to your issue, and then write the description in the body of the topic box, and select a category close to the issue, and optionally add a tag such as UWP, Linux, or, MacOS / iOS, remember to click ‘Create Topic’ at the bottom.

You may also find yourself thinking, who works on this? There is a page for that too.

You can learn more about MonoGame and the people involved over on the about page, which is here: About | MonoGame.

Hey, but what about video tutorials?

Check YouTube for those, there are plenty for everyone’s taste.

And finally, the moment you have been reading for, how do you get started with MonoGame? Well, technically that was already directed earlier, but you could also take a look over on the Downloads page: Downloads | MonoGame.

So, you find yourself wondering; who wrote all this? Well, I am just like you, a member of an enthusiastic community. So, welcome, take a seat, the BBQ is good here.

Thank you for reading, and I welcome the community to share their story of how they got here, and show their works, and if you have the time, share your experiences with MonoGame and why you chose it.

6 Likes

How to input Code to your messages on a thread

So, you have joined up, and started writing your first support thread topic, but now your code looks all weird when you paste it into the message window
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here

        #region Update Base
        // base update call
        base.Update(gameTime);
        #endregion
    }

Notice how the top three lines where protected override starts?

One way to fix this is to press the return key twice after the line above like this

    protected override void Update(GameTime gameTime)
    {
        // TODO: Add your update logic here

        #region Update Base
        // base update call
        base.Update(gameTime);
        #endregion
    }

And then hitting the return key twice once you have CTRL+V’d your code…

But what if you want to just emphasise some code? or enter code mid text like that?

Take a look at this website:

How to type the backtick character on Windows, Ubuntu, Mac OS? - Techwetrust

Content copied here for future readers in case the link above dies, please visit the link before clicking this open

QWERTY and QWERTZ keyboard layout

AZERTY France keyboard layout

image

AZERTY Belgium keyboard layout

image

Alt + 96
Alt + 096
Alt + 0060
Example: Hold left Alt, press 9, release 9, press 6, release 6 and release Alt.

It will help you find the backtick ` button on your keyboard, or the ALT input code.

Begin by typing three backticks ` ` ` and then typing what you want to be emphasised followed by three backticks again ` ` `, note that you do not space them apart, I did that so you can see where I placed them.

To do this for a code block where you want it, always start with a newline in the message box, but with this method, you can do it immediately after a line without needing to hit return twice.

code

image


it will look like that, the line on the left is the message window, I included it so you can see where the example image is.

Just to clarify what I mean by the next line:



I included separation lines so you can see where the images begin and end if you are in dark mode.

Moving on, there are just a few more things to bring to your attention concerning the backticks code blocks.

        protected override void Draw(GameTime gameTime)
        {
            #region Draw Base
            // base draw call
            base.Draw(gameTime);
            #endregion
        }

The text for the previous code block looks like this:



Now, what happens when you want to have certain key terms highlighted? It would be a preference, but you can add special terms immediately after the backticks…

This is the same code above with cpp added to the leading backticks:

        protected override void Draw(GameTime gameTime)
        {
            #region Draw Base
            // base draw call
            base.Draw(gameTime);
            #endregion
        }


If you want the block to be clean looking, you can also do the following:

        protected override void Draw(GameTime gameTime)
        {
            #region Draw Base
            // base draw call
            base.Draw(gameTime);
            #endregion
        }


So, you have some options to display it how you wish to, experiment with various code, you can use html, javascript, java, cs, csharp, fsharp, cpp, css, etc. just remember to add it after the backticks, without a space.

This also works on the discord site.

And to answer the question you may have asked, how did I do this?

Hidden stuff

HELLO WORLD!


Well, I will keep some secrets but the box can be created like so:

[details=Enter text here to give it a description]
Type anything here, even a code block
` ` `
Debug.WriteLine("HELLO WORLD!");
` ` `
[/details]
Enter text here to give it a description

Type anything here, even a code block

Debug.WriteLine("HELLO WORLD!");

And finally, the line separator,

___

it is just three joined underscores _ _ _, handy to draw emphasis to something.


See how it has lines above and below it?

And that concludes this helper guide for you fresh, or even long term members, I had to do a lot of research to learn about [BBCODE] and then find the types that worked here… there are still other secrets, if you know any, do share below.

I hope this was helpful.

Happy Coding.

4 Likes

Finding hidden gems

Here is a short one for you, if you click a users name/picture, click their name again, and it will take you to their Activity page, click Topics on the left menu so see their threads, you can often find rare gems in there.

Happy Coding!

Sample Code

I was forced at keyboard point to point out the Samples and Demos code page:

https://docs.monogame.net/articles/samples.html

There it is.

If you struggle with the samples, post a help request here on the forum or ask for help in real time on the discord.

Happy Coding!

1 Like

How to search first before asking for help

Here is an important piece of advice for all beginners, always attempt to find the solution first, before asking for help, as this not only teaches you how to be proactive in solving problems, but it also shows when you ask for help eventually, by you explaining what you found, your methodology for finding the solution, and your experience level too. This helps people feel at ease with taking time out to assist, remember, when asking for help, you will be using up valuable time of another.

Many a time, a request for help has begun with rather obvious irritated language at something being difficult to do or discover [Many if not all of us have been there], however, this eventually leads down to the meme:

click here to open/close the gif

And you don’t want to be that person, instead let us take a more nuanced approach.

First, let us explore what you need help with:

Is it really MonoGame Related

click here to open/close

Such as how do you draw a sprite to the screen and animate it, or is it C# related [Which you could get away with asking for help with on the discord, link in first post] such as how do you loop through a list of objects.


Is it a question to do with a GitHub repository/extension to MonoGame

click here to open/close

Such as MonoGame.Extended or a kit like Nez, in which case, please approach the relevant areas for support, or ask on the MG Discord, or post a correctly tagged forum post. It helps if you identify this factor in either the title of the thread, or at the very beginning of your support thread post, it saves time getting to the middle or bottom of a post that can sometimes be a dissertation long, only to find out we were reading about something we do not currently or intend to use. I have seen this many times, and as a result, nobody wishes to respond, and the people who you need may glance over the thread without knowing you require their attention.


Is it a Shader question?

This is answered below in General Advice and The SIP.

Other examples of questions asked will appear here in future

General Advice

It helps if you first understand that MonoGame is a framework which continues the legacy of Microsoft XNA, thereby, a lot of the answers you are looking for fall under the XNA resources searches.

This means you can search for XNA support to find your issue if you cannot find anything using MonoGame in the search terms.

The Solving Issues Process [The SIP]:


1-

Check your favourite search engine

click here to open/close
Use variations of the question, 
include either XNA or MonoGame, or both
Examples:
_How to draw a part of an image in XNA
_How to animate a sprite in MonoGame
_How to load a sprite in XNA/MonoGame

2-

Perform a check on specific documentation

click here to open/close

If it feels like a basic function, such as adding sounds to your project, check the Documentation, or perform a search [This opens a special page with advanced filters] on the forum for Adding SFX or Adding music or Adding Sound Effects in the search box at the top of the forum.


3-

Asking on the Forum or the Discord, or both.

click here to open/close

Here comes the interesting step, Once you have tried to discover the solution yourself, and feel you are still stuck, or cannot really be sure what to search for, try the following:

In as few words as possible, explain to yourself, 
what your intent is, then phrase it like a question 
and post a thread post, and then either immediately 
or after a period of time, pop over to the Help 
channel on the discord [linked in first thread post] 
and link the thread there and re-iterate on the 
discord what it is you are seeking help for, 
so that people can help there and then, 
instead of having to pop over to the forum, 
help them help you.

It also helps if you find a title that specifically explains in as few words as possible what your thread body text explains further, for example:

[Title]
Rendering a group of objects in the background
[Thread body text]
I am looking to draw a group of objects in 
my 2D project, where the objects can be 
drawn at specific intervals.
How do I make sure this is not using too
many resources or slowing down the fps.

Try to avoid repeating the question in the title, just because it is short enough to fit in the title, if it is, then the question has not been thought through enough.


4-

Things to include in your Thread Post

click here to open/close
If you have not specified using the tags, 
such as Windows, Linux, Android, Mac, iPhone.
it would go a long way if you have not already,
include in the opening statement of your post
What OS you are on, Windows, MacOS, Linux Distro
\ Each have different things to point out
\ such as key commands or settings menus

Your hardware, in as much detail as you feel
is needed to aid with your issue, such as
if the issue pertains to memory issues then
be sure to include details such as:

System Memory
\ 4GB 8GB 12GB 16GB etc.
\ such as: 64.0 GB (63.8 GB usable)

GPU Memory
\ [Internal Shared State System RAM] 
\ Dedicated 4GB, 8GB+ etc.
\ [Dedicated means a Graphics Card]

CPU Make and model number such as:
\ Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz

Operating System and Release Version Number
\ Such as Edition: Windows 11 Pro, Version: 22H2

If the question is Input related,
\ state your hardware:
\ is it a standard device?
\ Microsoft XBOX Controller
\ or an uncommon device...
\ Logitech [something] Controller etc.
\ or a different Console gamepad
\ Switch Pro Controller, 
\ PlayStation Dual Shock Gamepad

What Platform are you targeting?
This also relates to the input somewhat
as Input is handled differently in DX and OGL
[DX= DirectX, OGL= OpenGL]
\ State you are targeting Windows, or MacOS, iOS, 
\ Android, Linux [if Linux, state which Distro]
\ If Windows, ensure you mention you are using 
\ Desktop DX or Desktop OGL. Windows UWP uses DX only.

And any other relevant information you can think of, 
it helps if people assisting understand your setup, 
intentions, and then see if they can help you. 
A Non-Linux user, reading a dissertation worth 
of information, only to reach the bottom, 
to then find out you use Linux, is an exhausting 
approach to help.

Closing Notes

While this is not a thorough guide, I may add to it in future.

Happy Coding!

1 Like

Using Github CopilotXChat in your Projects

So, you want to use the power of AI to speed things up?

Want to discover mysterious coding ways?

Wish to enter the Matrix?

Ok, that last one was a joke but you get the idea, using something like GitHub, take a breath, CopilotXChat [GHCPXC], assuming that is the correct name for it… can drastically speed up your coding endeavours, however, I find you need to be well versed in coding with whatever language you use, in our case, C#.

What do I mean with that?

I would argue you need to be somewhat an intermediate C# programmer to catch the mistakes GHCPXC creates, and also, understand how to get it to fix those errors too.

I will begin with a simple example using a Console app.

Now, I did generate the base code with GHCPXC, henceforth CPXC, but I had to fill in some details and modify the code a little, but essentially I have this:

click to show/hide code block
using System;

internal class Program
{
    private static void Main(string[] args)
    {
        var person = new Person();
        person.Name = "John";
        person.Age = 20;
        person.Introduce("Hot Chocolate");

        var person2 = new Person();
        person2.Name = "July";
        person2.Age = 22;
        person2.Introduce("");

        var person3 = new Person();
        person3.Name = "John";
        person3.Age = 20;
        person3.Introduce("Pineapples");
    }

    private class Person
    {
        public Person()
        {
            Name = null;
            Age = 0;
        }
        public string? Name { get; internal set; }
        public int Age { get; internal set; }
        internal void Introduce(string v)
        {
            Console.WriteLine("Hi, my name is " + Name);
            Console.WriteLine("I am " + Age + " years old");
            if (v == "")
                Console.WriteLine("I don't like anything");
            else
                Console.WriteLine("I like " + v);          
            Console.WriteLine();
        }
    }
}

The v variable was defined by CPXC

What I did next, was select the following lines:

        var person = new Person();
        person.Name = "John";
        person.Age = 20;
        person.Introduce("Hot Chocolate");

And selected the Ask Copilot option in the right click context menu… [Alt+/]

And asked it to:

simplify this code

Which it then spat out the following code:

click to show/hide code block
using System;

internal class Program
{
    private static void Main(string[] args)
    {
        var person1 = new Person("John", 20, "Hot Chocolate");
        person1.Introduce();

        var person2 = new Person("July", 22);
        person2.Introduce();

        var person3 = new Person("John", 20, "Pineapples");
        person3.Introduce();
    }

    private class Person
    {
        public Person(string name, int age, string likes = "")
        {
            Name = name;
            Age = age;
            Likes = likes;
        }

        public string Name { get; internal set; }
        public int Age { get; internal set; }
        public string Likes { get; internal set; }

        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);
            Console.WriteLine("I am " + Age + " years old");
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");
            else
                Console.WriteLine("I like " + Likes);
            Console.WriteLine();
        }
    }
}

Can you spot the differences? pretty neat right?

We can make it more neat…

Select the space above internal class Program and press ALT+/ and ask:

add comments to this code

Which gave me:

click to show/hide code block
using System;

internal class Program
{
    // Main method that starts execution.
    private static void Main(string[] args)
    {
        var person1 = new Person("John", 20, "Hot Chocolate");
        person1.Introduce();

        var person2 = new Person("July", 22);
        person2.Introduce();

        var person3 = new Person("John", 20, "Pineapples");
        person3.Introduce();
    }

    // Class defining a Person.
    private class Person
    {
        // Constructor for the Person class.
        public Person(string name, int age, string likes = "")
        {
            Name = name;
            Age = age;
            Likes = likes;
        }

        // The person's name.
        public string Name { get; internal set; }

        // The person's age.
        public int Age { get; internal set; }

        // The person's likes.
        public string Likes { get; internal set; }

        // Method to introduce the person.
        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);
            Console.WriteLine("I am " + Age + " years old");
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");
            else
                Console.WriteLine("I like " + Likes);
            Console.WriteLine();
        }
    }
}

I was not happy with the granularity, and wanted to speed up adding comments to:

click to show/hide code block
        // Create a new Person object and call the Introduce() method.
        var person1 = new Person("John", 20, "Hot Chocolate");
        person1.Introduce();

        // Create another new Person object and call the Introduce() method.
        var person2 = new Person("July", 22);
        person2.Introduce();

        // Create a third new Person object and call the Introduce() method.
        var person3 = new Person("John", 20, "Pineapples");
        person3.Introduce();

So I asked it to… [I skipped showing the snippet and added the end result to save repeated reading for the reader]

I asked it to add comments to:

        public string Name { get; internal set; }
        public int Age { get; internal set; }
        public string Likes { get; internal set; }

And it got a little annoyed at me so it gave me this:

click to show/hide code block
using System;

internal class Program
{
    // Main method that starts execution.
    private static void Main(string[] args)
    {
        var person1 = new Person("John", 20, "Hot Chocolate");  // Create a new instance of a person with name, age, and likes arguments.
        person1.Introduce();  // call the introduce method of person1 to print out its details.

        var person2 = new Person("July", 22);  // Create a new instance of a person with only the name and age arguments.
        person2.Introduce();  // call the introduce method of person2 to print out its details.

        var person3 = new Person("John", 20, "Pineapples");  // Create a new instance of a person with name, age, and likes arguments.
        person3.Introduce();  // call the introduce method of person3 to print out its details.
    }

    // Class defining a Person.
    private class Person
    {
        // Constructor for the Person class.
        public Person(string name, int age, string likes = "")
        {
            Name = name;  // Set the name of the person instance.
            Age = age;  // Set the age of the person instance.
            Likes = likes;  // Set the likes of the person instance (defaults to empty string).
        }

        // The person's name.
        public string Name { get; internal set; }

        // The person's age.
        public int Age { get; internal set; }

        // The person's likes.
        public string Likes { get; internal set; }

        // Method to introduce the person.
        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);  // Prints out the person's name.
            Console.WriteLine("I am " + Age + " years old");  // Prints out the person's age.
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");  // Checks to see if the person doesn't have any likes, and prints accordingly.
            else
                Console.WriteLine("I like " + Likes);  // Prints out the person's likes.
            Console.WriteLine();  // Adds space between introductions.
        }
    }
}

Anyway, joking aside, note how much time that saved me, I did not need to explain what my code [or anyone else’s code for that matter] did, or how it works, it was done for me, and that can be minutes to hours for a longer chunk of code, and remember, you should be breaking things down to small chunks.

Moving on, now that we have a small idea or wait a moment…

click to show/hide code block
        public Person(string name, int age, string likes = "")
        {
            Name = name;  // Set the name of the person instance.
            Age = age;  // Set the age of the person instance.
            Likes = likes;  // Set the likes of the person instance (defaults to empty string).
        }

I selected this block of code and asked it to:

add a height variable to the entire code

What do you think happened?

click to show/hide code block
    // Class defining a Person.
    private class Person
    {
        // Constructor for the Person class.
        public Person(string name, int age, string likes = "", int height = 0)
        {
            Name = name;  // Set the name of the person instance.
            Age = age;  // Set the age of the person instance.
            Likes = likes;  // Set the likes of the person instance (defaults to empty string).
            Height = height; // Set the height of the person instance (defaults to 0).
        }

        // The person's name.
        public string Name { get; internal set; }

        // The person's age.
        public int Age { get; internal set; }

        // The person's likes.
        public string Likes { get; internal set; }

        // The person's height.
        public int Height { get; internal set; }

        // Method to introduce the person.
        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);  // Prints out the person's name.
            Console.WriteLine("I am " + Age + " years old");  // Prints out the person's age.
            Console.WriteLine("I am " + Height + "cm tall");  // Prints out the person's height.
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");  // Checks to see if the person doesn't have any likes, and prints accordingly.
            else
                Console.WriteLine("I like " + Likes);  // Prints out the person's likes.
            Console.WriteLine();  // Adds space between introductions.
        }
    }

Notice anything missing?

click to show/hide code block
        var person1 = new Person("John", 20, "Hot Chocolate");  // Create a new instance of a person with name, age, and likes arguments.

See it yet?

include the height

I asked it to add the height on this section and got the following code:

click to show/hide code block
using System;

internal class Program
{
    // Main method that starts execution.
    private static void Main(string[] args)
    {
        var person1 = new Person("John", 20, "Hot Chocolate", 180);  // Create a new instance of a person with name, age, height, and likes arguments.
        person1.Introduce();  // call the introduce method of person1 to print out its details.

        var person2 = new Person("July", 22);  // Create a new instance of a person with only the name and age arguments.
        person2.Introduce();  // call the introduce method of person2 to print out its details.

        var person3 = new Person("John", 20, "Pineapples", 165);  // Create a new instance of a person with name, age, height, and likes arguments.
        person3.Introduce();  // call the introduce method of person3 to print out its details.
    }

    // Class defining a Person.
    private class Person
    {
        // Constructor for the Person class.
        public Person(string name, int age, string likes = "", int height = 0)
        {
            Name = name;  // Set the name of the person instance.
            Age = age;  // Set the age of the person instance.
            Height = height; // Set the height of the person instance.
            Likes = likes;  // Set the likes of the person instance (defaults to empty string).
        }

        // The person's name.
        public string Name { get; internal set; }

        // The person's age.
        public int Age { get; internal set; }

        // The person's height.
        public int Height { get; internal set; }

        // The person's likes.
        public string Likes { get; internal set; }

        // Method to introduce the person.
        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);  // Prints out the person's name.
            Console.WriteLine($"I am {Age} years old and I am {Height} cm tall.");  // Prints out the person's age and height.
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");  // Checks to see if the person doesn't have any likes, and prints accordingly.
            else
                Console.WriteLine("I like " + Likes);  // Prints out the person's likes.
            Console.WriteLine();  // Adds space between introductions.
        }
    }
}

You might notice a problem remaining…

Hi, my name is John
I am 20 years old and I am 180 cm tall.
I like Hot Chocolate

Hi, my name is July
I am 22 years old and I am 0 cm tall.
I don't like anything

Hi, my name is John
I am 20 years old and I am 165 cm tall.
I like Pineapples

July has a height issue, lets finish this section off and see how well CPXC does…

I selected:

click to show/hide code block
            Console.WriteLine($"I am {Age} years old and I am {Height} cm tall.");  // Prints out the person's age and height.

Asked:

add a null check for height and exclude it from the output

And got the most amusing fix:

click to show/hide code block
Hi, my name is John
I am 20 years old
and I am 180 cm tall.
I like Hot Chocolate

Hi, my name is July
I am 22 years old
and I don't have a height.
I don't like anything

Hi, my name is John
I am 20 years old
and I don't have a height.
I like Pineapples

Complete code:

click to show/hide code block
using System;

internal class Program
{
    // Main method that starts execution.
    private static void Main(string[] args)
    {
        var person1 = new Person("John", 20, "Hot Chocolate", 180);  // Create a new instance of a person with name, age, height, and likes arguments.
        person1.Introduce();  // call the introduce method of person1 to print out its details.

        var person2 = new Person("July", 22);  // Create a new instance of a person with only the name and age arguments.
        person2.Introduce();  // call the introduce method of person2 to print out its details.

        var person3 = new Person("John", 20, "Pineapples", null);  // Create a new instance of a person with name, age, height, and likes arguments with height set to null.
        person3.Introduce();  // call the introduce method of person3 to print out its details (height excluded).
    }

    // Class defining a Person.
    private class Person
    {
        // Constructor for the Person class.
        public Person(string name, int age, string likes = "", int? height = null)
        {
            Name = name;  // Set the name of the person instance.
            Age = age;  // Set the age of the person instance.
            Height = height; // Set the height of the person instance.
            Likes = likes;  // Set the likes of the person instance (defaults to empty string).
        }

        // The person's name.
        public string Name { get; internal set; }

        // The person's age.
        public int Age { get; internal set; }

        // The person's height (nullable).
        public int? Height { get; internal set; }

        // The person's likes.
        public string Likes { get; internal set; }

        // Method to introduce the person.
        internal void Introduce()
        {
            Console.WriteLine("Hi, my name is " + Name);  // Prints out the person's name.
            Console.WriteLine($"I am {Age} years old");
            if (Height.HasValue)
                Console.WriteLine($"and I am {Height.Value} cm tall.");  // Prints out the person's height if not null.
            else
                Console.WriteLine("and I don't have a height.");  // Prints out that the person doesn't have a height if null.
            if (string.IsNullOrEmpty(Likes))
                Console.WriteLine("I don't like anything");  // Checks to see if the person doesn't have any likes, and prints accordingly.
            else
                Console.WriteLine("I like " + Likes);  // Prints out the person's likes.
            Console.WriteLine();  // Adds space between introductions.
        }
    }
}

I did notice I accidently left two John’s in there…

Moving on…

We now have a good idea of how involved you might need to be or how little you can be, but do not misunderstand, you do need to review the code, and ensure it is not doing something you did not expect, and always look for thrown exceptions added before you build and run.

Let’s try a MonoGame example, a brief one at that…

Spinning up a new MonoGame Desktop DirectX project, I asked CPXC to:

create a black pixel texture and add it to the content manager

inside the LoadContent() method and received the following back:

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            blackPixelTexture = new Texture2D(GraphicsDevice, 1, 1);
            blackPixelTexture.SetData(new Color[] { Color.Black });

            // Add texture to content manager
            Content.Load<Texture2D>("blackPixelTexture", blackPixelTexture);

            // TODO: use this.Content to load your game content here
        }

Which brought about some issues, but we can resolve those using just CPXC, select the first mention of blackPixelTexture using your right click on your mouse and select Ask Copilot below it should show the text Explain and fix CS0103 click it.

We find it throws an error saying the solution was redacted…

This is a good example of the AI being completely useless but not by its own initiative… I found this issue to occur far too often.

Let’s select a fix using the VS Hover options… or ALT+Enter fixes…

I selected the Generate Property option which added:

    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        // The following line
        public Texture2D blackPixelTexture { get; private set; }

Which left a remaining error with the load method following… I selected the red underlined code Load<Texture2D> and asked it to Explain and fix CS1501 which gave me the fix of:

Content.Load<Texture2D>("blackPixelTexture");

Which was wrong…

So I deleted it…

Your LoadContent() should look like this so far:

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            blackPixelTexture = new Texture2D(GraphicsDevice, 1, 1);
            blackPixelTexture.SetData(new Color[] { Color.Black });

            // TODO: use this.Content to load your game content here

        }

Let’s move along to the Draw() method…

just after the TODO comment, ask CPXC to:

Draw the black pixel at 30x scale in the centre of the screen

Which will spit out:

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            var scale = 30f;
            var position = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
            var origin = new Vector2(0.5f, 0.5f);

            _spriteBatch.Begin();
            _spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);
            _spriteBatch.End();

            base.Draw(gameTime);
        }

So, copy this and replace your original method… Optional to re-add the TODO comment…

Running this code will give the following output:

If you thought I was done there, then you do not know my boredom levels…

Highlight the following lines:

            _spriteBatch.Begin();
            _spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);
            _spriteBatch.End();

And ask:

Make this sprite move in a radial circle randomly

which should spit out the following code:

// Define random offsets for the x and y axis from the center of the screen
var xOffset = new Random().Next(50, 150);
var yOffset = new Random().Next(50, 150);

// Calculate the new position of the sprite based on the elapsed game time
var angle = (float)(gameTime.TotalGameTime.TotalSeconds * Math.PI);
var newX = (float)(Math.Cos(angle) * xOffset) + (GraphicsDevice.Viewport.Width / 2);
var newY = (float)(Math.Sin(angle) * yOffset) + (GraphicsDevice.Viewport.Height / 2);

// Update the sprite position and redraw it
position = new Vector2(newX, newY);
_spriteBatch.Begin();
_spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);
_spriteBatch.End();

Complete Draw() code thus far:

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            var scale = 30f;
            var position = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
            var origin = new Vector2(0.5f, 0.5f);

            // Define random offsets for the x and y axis from the center of the screen
            var xOffset = new Random().Next(50, 150);
            var yOffset = new Random().Next(50, 150);

            // Calculate the new position of the sprite based on the elapsed game time
            var angle = (float)(gameTime.TotalGameTime.TotalSeconds * Math.PI);
            var newX = (float)(Math.Cos(angle) * xOffset) + (GraphicsDevice.Viewport.Width / 2);
            var newY = (float)(Math.Sin(angle) * yOffset) + (GraphicsDevice.Viewport.Height / 2);

            // Update the sprite position and redraw it
            position = new Vector2(newX, newY);
            _spriteBatch.Begin();
            _spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);
            _spriteBatch.End();


            base.Draw(gameTime);
        }

Now, this is a little too jittery for our liking, so I highlighted the code that related to the drawing, and asked:

The movement is too jittery, can you smooth it out?

Which gave me:

var scale = 30f;
var position = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
var origin = new Vector2(0.5f, 0.5f);

// Define random offsets for the x and y axis from the center of the screen
var xOffset = new Random().Next(50, 150);
var yOffset = new Random().Next(50, 150);

// Calculate the new position of the sprite based on the elapsed game time
var angle = (float)(gameTime.TotalGameTime.TotalSeconds * Math.PI);
var newX = (float)(Math.Cos(angle) * xOffset) + (GraphicsDevice.Viewport.Width / 2);
var newY = (float)(Math.Sin(angle) * yOffset) + (GraphicsDevice.Viewport.Height / 2);

// Smoothly interpolate between the current position and the new position
position = Vector2.Lerp(position, new Vector2(newX, newY), MathHelper.SmoothStep(0f, 1f, 0.5f));

// Update the sprite position and redraw it
_spriteBatch.Begin();
_spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);
_spriteBatch.End();

Not an impressive improvement but you now have some slightly complex code to learn from… and it could not be simpler than our best friend:

add detailed comments to this code

With the Draw() Method highlighted:

protected override void Draw(GameTime gameTime)
{
    // Clear the screen to a visually appealing color
    GraphicsDevice.Clear(Color.CornflowerBlue);

    // Set scaling of the sprite
    var scale = 30f;

    // Set position of the sprite at the center of the screen
    var position = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);

    // Set origin of the sprite, specifying its alignment point at the center of the sprite texture
    var origin = new Vector2(0.5f, 0.5f);

    // Define random offsets for the x and y axis from the center of the screen
    var xOffset = new Random().Next(50, 150);
    var yOffset = new Random().Next(50, 150);

    // Calculate the new position of the sprite based on the elapsed game time
    var angle = (float)(gameTime.TotalGameTime.TotalSeconds * Math.PI);
    var newX = (float)(Math.Cos(angle) * xOffset) + (GraphicsDevice.Viewport.Width / 2);
    var newY = (float)(Math.Sin(angle) * yOffset) + (GraphicsDevice.Viewport.Height / 2);

    // Smoothly interpolate between the current position and the new position
    position = Vector2.Lerp(position, new Vector2(newX, newY), MathHelper.SmoothStep(0f, 1f, 0.5f));

    // Begin drawing using spritebatch
    _spriteBatch.Begin();

    // Draw the black pixel texture using spritebatch with the settings defined above
    _spriteBatch.Draw(blackPixelTexture, position, null, Color.White, 0f, origin, scale, SpriteEffects.None, 0f);

    // End drawing using spritebatch
    _spriteBatch.End();

    // Call the base class's Draw method
    base.Draw(gameTime);
}

Now, as this was simply a demonstration, you would not keep logical code in the draw method, instead logical code would be in the update method.

As CPXC is ever evolving, I may come back to this guide at some point.

Try to experiment and find issues to fix yourself, try highlighting all the code relating to the pixel and asking:

with the random movement, follow the mouse around the screen as the centre of the position

And have fun!

Happy Coding!

1 Like

Re:

Sample Code

https://docs.monogame.net/articles/samples.html

note: see the branch on these two samples, the 3.8.1 is marked as the default but is not selected as the current branch, if you are looking in the browser, that is 3.8.0.

Make sure you clone the latest 3.8.1 on the 2 samples if you want the performance of NetCore 6, 7, or 8 and are using the latest compilers.

The advantage of the structure is one shared gamecore.dll, for example, NeonShooter.Core.Dll , one set of shaders, with multiple thin EXE launchers each with links to the Content.mgcb with the core. No repeat assets, shared code between platforms

(https://github.com/MonoGame/MonoGame.Samples/blob/3.8.1/NeonShooter/NeonShooter.Core/NeonShooter.Core.csproj)

1 Like

You mean this:

Good catch! @Damian_Eaglestein

1 Like