MonoGame Install & Walkthrough To Creating Your First 2d Game

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to another video my name is fizzle and i'm going to show you how to install model game and take you through a little tour and show you the ropes so if you've had model game in the past the version that you have is probably obsolete because they've moved all to a new get style um installer so to install it you want to open up your visual studios 2019 we're gonna want to go to if i can find it extensions and in here i'm gonna type in a mono game so you're going to download the project templates here and that's your first step and next you're going to want to exit out go to your command prompt and type in this command.net tool install mysg.net mgcb editor now this will what it will let you do is if i type in mgcb editor into my command prompt it will open up the pipeline editor which will allow you to have your textures your music your fonts everything like that so this is required next let's open up visual studios again and instead of opening without code we're going to create a new project as you can see in my recent project templates it's already here but if you just type in model game at the top you can see all of the packages and applications that you've installed so the one i'm going to pick is directx windows desktop application and i'm going to call it tutorial visual tutorial why not might take a second to load up you'll be brought to this solution you'll have dependencies which you're not really going to touch your content which we will talk about later an app manifest which every project comes standard with this um holds all your keys and everything like that you're not gonna touch that your game one which all of your code is gonna pretty much be in your icon so if you want to change your icon like your game icon when you launch or with the game when it builds you're going to edit this and your program cs which you're not going to touch this is your entry point to the project but monogame has already set this up for you so what do we talk about first okay we're in our game one we can rename our game one to anything by pressing ctrl r i can just call this tutorial game why not and in this we have two initialized global variables which is a graphics device manager and a sprite batch graphics device manager allows you to change windows focus windows change the aspect ratio and just pretty much things like that and sprite batch allows you to draw write do all of that cool stuff so because i'm not a fan of the underscore and i'm used to camel case i'm just going to press ctrl r and backspace and get rid of these underscores because i don't want to have to type an underscore every time i want to use the variable now we have our constructor an inner constructor it initializes our graphics field it initializes where our content is stored and with our content that's where all your files such as fonts music textures sprites all of that goes and it will show that mouse is visible if you want to have your own custom mouse um you can set this to false but when i launch it with f5 you're not going to have a mouse so it's pretty bleak so i recommend until you actually get a custom mouse setup or if you don't want to just leave it as is next is your initialization in your initialization whenever you have uh music fonts sprites all that crap you initialize it in here this will be ran only this will run once only when the program calls upon it and in your load content this is where you actually load your sprites and music and fonts so as an example let me say private texture 2d which is the texture 2d library and i'm just going to call this background since i don't need to initialize it i can just go straight into the content and say background equals content dot load texture 2d and it's a type t and any of these parameters here it will first go to the content folder so let's say i have a a item better way to do this let me let's go back to that in a second so here's your content mgcp when i double click it it'll automatically open up to the directory and that our file is so we don't have to go to file open blah blah blah in order for your you to be able to do that when you have the pipeline installed you're going to right click open with and set the mgc mgcb editor wpf as your default so let's talk about this for a little bit i like to be organized so i'm going to add a new folder and call this textures and one of fonts let's start instead of a texture let's start with a font because we don't need any textures to build upon that we can add a new item a spot sprite font description not localized and i'm going to call this text font now if i save and build it as you can see there's no errors and it's gone through i can we can see that in the content directory if we open up fonts we have this xml style font sheet we can change pretty much anything in here i can change the size let's make it a nice 24 i can change the font comic sans ms let's say ns and if we save it and we rebuild it uh oh we have an error change it to ms and it goes through so you'll know that if you've screwed up the font name this uses any local font in your directory um your system windows directory i'm not sure about linux i haven't tried it out but and we can bold we can italic let's say bold and rebuild so now we won't really have any font showing or anything really happening it's just a nice blue window but let's get this font working so in order to call it i'm going to use my modifier sprite font i'm going to call this text font and that's it now inside of our load content it's a text font close content load a sprite font so whenever you load something like a font a texture music you're going to pass through when you look and load it the name of the variable equal to content.load it takes the type so since ours is a font it's going to match that and in here it's going to ask for our directory so since it's already in content we can see that it's in font and its name is text font we don't need to add the xmb or whatever extension at the end because it's already converted to that let's say i add an s to that and there's no such local directory we will get an instant runtime error file is not found so you will know if your file is there or not i run it again everything worked let's go to our draw in order for you to draw this is applies to 2d games only if you're doing 3d it's a completely different system which i'm not going to get into we have to first initialize sprite batch begin as the entry point and sprite batch end as the end point everything you draw and everything you do will be in the middle of these two if you don't have one or the other your program will not run you'll get an error another important thing is your graphics device clear is so important because these two functions run 60 frames a second you need it to clear every second otherwise if you move an object it'll draw and overlap each other so let's say you want to move a character and you hold a to or d to hold to move it and it goes like that if you don't have this line of code it will draw 60 times a second the character being moved you will probably crash your computer if you get if the memory gets too high or crash the program so you always need to graphics device clear you can change the color though i can say pink we can run it we got a nice pink and you can change it to whatever color the color library has every color you can think of and every shade it's quite nice so let's say deep pink if we also tab in we can see other options so we don't usually mess with this but color options uh you know what i'm i don't even have that library in but we're not gonna mess with that we don't need to for the case of this tutorial so let's get some text to display on screen i am going to create another variable this is going to be a string and i'm going to say text message so instead of having your text just in there you're just going to want one variable that's initialized once so this should actually just be a constant string text message equals welcome to my game so let's actually draw the text to the screen we're also we're actually also missing one more piece of information which i totally forgot about we need a vector 2 what vector 2 is is a is point x and y and this tells it exactly where to draw so private vector 2 let's call this text um position we'll leave it as is let's initialize it in our load content text position equals new vector 2 and it's going to want a x and a y so let's say a screen or let's just do it at 0 10 so it'll be like there you can mess around with all these numbers so let's do sprite batch dot draw string the difference between drawstring and draw is drawstring only draws text if you try to use this on a different draw with the texture it will not work so it wants a font so we have that we have text font it wants the text we can write it here but we already have it stored in a constant variable a text message it wants a position which we have again and it wants a color we can do this with color dot and white we'll do one actually do black now if we were to run it as is wow we have text and looks nice with our comic sans and our deep pink background but this window is really small because monogame while xna was supported for the first original xbox uh and older monitors this was the resolution they used which is i believe 800x600 so i don't like that what i'm going to do is create a few more variables private const integer screen width equal to 1600 and screen height equal to 900 this is the usual bounds i use for my games unless i have a full screen option and inside of our initialize we can say graphics graphics device dot proof where is it viewport.preferred where is it ah they changed it a little okay graphics dot preferred back buffer width equal to screen width and graphics dot preferred back buffer height equal to screen height in order to apply these changes we're going to need to call the graphics apply changes void there's a few other cool things we can do in this initialize we can there's a window we can do window allow user resizing false window allow alt f4 true in case that our program crash or crashes but doesn't exit you can do window title um and now if we run the program we have a nice big screen we have our title we can alt f4 and it works perfectly two i'm gonna show you guys two other things here we're gonna do a texture now so let's do private texture 2d i'm gonna call this background and with the texture you're gonna need a rectangle like so these are the only two pieces of information you need for textures for the most part now let's instantiate these so let's say background so equals content load and it takes and it takes in the type so it is a texture 2d and we don't actually have a texture yet so let me go add an existing item um sure actually i think we already did that it should just be wrapped as well okay add it save it and build it perfect built with no errors so now that we know where our texture is it is in content textures map there it is and our background rectangle is going to be equal to a new rectangle and what are we going to want to do well we want it to for rectangle takes in four pieces of information a start x a start y the width and the height of the object so i want to draw for the whole screen so i'm going to do 0 0 we're also going to do let's scroll up and see graphics graphics fixed device viewport dot width i'm going to copy this over and go to height so when it loads in it whatever screen resolution you are it will adjust to that and that's all we need now we can uh draw it so because i want the text to be in front of our texture we're going to draw it back here so scrape batch dot draw we're going to say background background rectangle and a color dot white so what colors do in the case of textures is it tints it so if you say like red or gray or whatever it will tint it to that color let's run it here we go now let's say you want to have a little player let's go to paint and let me resize to about 100 by 100 a nice little except don't keep aspect ratio okay a nice little square let me save that on my desktop as player okay let's go and add a new folder and i'm going to call sprites let's build add an existing item and we're going to find player add it save it and we can see it got added uh with the sprites folder so let's draw and make the player move we need a texture for the player we're gonna need a rectangle for the player and we're gonna get him to move so we're gonna need a keyboard kb oh keyboard is it state okay private keyboard state kb let's load them in so we'll load them in after just for continuity player player text equals content load it's another texture it's in sprites and he's named player player wrecked is equal to a new rectangle we're going to have him first drawn uh let's say 500 by 500 and we're going to pass through player texture width and player texture height so now the player will be 100 by 100 pixels based off of this here so if that changes these will automatically change let's draw our guy sprite patch draw player texture player rect color white if we run the program you can see our black box but nothing's happening he's actually a little bit big for my liking give me a second here let's just divide it by two okay let's say key key b k b equals keyboard dot get state if kb is key down key code d player rectangle dot x plus equals i'll lose the decimal placement so since we're not working with the vector that's fine that's fine okay okay else if kb is key down key code let's do up and down and also fkb is key down keys dot s player rect plus equals player rec dot y plus equals speed run it and let's see what we got so this is basic movement but as you can see an issue is is that the player if you hold down two directional keys at once gains double the velocity in order to do this we'd have to come up with a more complex uh well it's not too complex but a different pattern of movement where you're basically adding velocity to the character's positions and checking it and checking their velocity make sure it doesn't go over but let's here let's say we only want them to be able to move one direction how would we do this now oh well this seems to work so whatever input we do only allows them to move one way even though i'm pressing all the buttons this is one way to fix it but it becomes static movement and some rpgs use this kind of movement but it's very unrealistic and kind of not smooth so yeah also i can move the text around if i wanted to as a final test we'll do that uh so i'm going to say texture rec or text position dot x plus equals speed and every frame it'll move so that's basic way how to get your monogames set up and a few things you can do i will be out with the next tutorial shortly depending on what you guys need thanks for watching
Info
Channel: Nicholas Chrysanthou
Views: 1,560
Rating: undefined out of 5
Keywords:
Id: 0Vm-o7elpnM
Channel Id: undefined
Length: 23min 46sec (1426 seconds)
Published: Thu Apr 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.