Making Minesweeper in Unity (Simple 2D game for Beginners)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to make minesweeper in unity this is a great game with some simple logic which makes it perfect for beginners to learn let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with in-depth tutorials made by a professional indie game developer so if you find the video helpful consider subscribing ok so this is what we want to make first of all if you're not familiar with my sweeper here's a quick rundown of how the game goes we have our play area in here and as you can see it's using a nice grid now hidden under this grid we have certain mines the mines are randomly generated so here we can use a debug visual this reveals the entire map and every time we play we get a different map so there it is by reloading the scene we have a new map and just like that a new map the goal of the game is to clear the entire map without triggering any of the minds and way we do that is with the help of mine indicators now we can click to explore our map so I click in here and there it goes in the position underneath it is a mine indicator it reveals just that position in this case the indicator is a number and the number means that there is this many mines in the area surrounding this great position so there are up to 8 positions total neighboring a grid position above below left-right and diagonals but since this one is on the corner it only has these ones so in these neighboring positions there is going to be one possible mind so in this case I can use the debug visual and see yep indeed there is that mine in there so this one has one mine neighboring and this one has two so that one and that one yep then we also have some areas that don't have any neighboring mines and when we click on those they show up all of the connected positions with our mind as you see and finally we can right click in order to place a flag to indicate where we think of mine might be then the game ends when either we head on a mine so there you go hit and there you go we have a game over and nice button to retry so hit retry and again we got a new nice map and the timer goes back to zero and then we can also win and when I click to win there you go it asks me for my player name so I put in the player name and my score gets added to a very nice high score table so just like this we have our very nice and simple complete game alright so this is our goal let's get to it now minesweeper is a game which is played on a grid so that makes it perfect for us use our previously made grid map if you haven't seen that video yet go check out the link in the description we created this grid map entirely from scratch then we added generics which lets us hold a custom object type in each grid sound position so using that as our base we can really speed up the development for this game so let's begin by setting up our map so a new script this won't be our map now in here let's make this a basic class so no mana behavior and on the constructor in here we want to make our grid map so we define a private grid and now in here we need to input our grid object so let's make that back in here let's make a new c-sharp script call this our map grid object so this is the grid object that is present on every position in our grid and in here we're going to make the default fields just like we did when we created our grid map okay so we have our simple grid object now let's go back into the map and in here we can now define our grid map as a grid of map grade object and here on the constructor we simply inset here okay all right so here's our grid being instantiated again if this seems weird to you make sure you watch the previous videos on making the grid and making the grid with generics and here we are instantiating our grid with the size of 10 by 10 decent size is 10 units or origin on vector 3.0 and we are creating our grid object very simply with the grid DX and dy ok so now we need to make the main script that won't create our map so here and let's make a new c-sharp script call this the minesweeper game Hamlet and let's make a game object to run this script and we drag the script onto it okay and in here we simply go into our start then we create a new map all right there it is very simple now let's just make sure that on our grid script that in here we have the debug visuals enabled so let's set this to true so we can visually see it alright let's test any of their days we have our grid map being accentuated and as you see every single grid position contains our map quick object alright great so far so good now let's set up our grid object correctly based on our game design we only have a limited number of possible types for each grid position we can have an empty a mine or an indicator so we're going to represent those different types using a new so here we have a Pollock innum let's call this our type and for the various types we have empty we have the mind then the indicators just like that and finally we have a type for our current type now here on the constructor let's set our tank so just for testing let's set it to random and let's also override our to string and make it return a string of our time okay so it says and we should be able to see our grid with random types everywhere what C and yep there it is our grid does have random types on every grid position so you can see over here we have a mine here another mine here a mine new 5 and so on and so forth alright great now let's make a proper visual instead of using this debug view over here I have a class that takes in a grid and instantiate a prefab on each grid position we first made a grid visual in the heatmap video then we also expand upon it in the talmud video so if you've seen both of those videos you'll be able to easily understand how all of this works we simply have a setup function and then we are subscribed to the on grid object changed event and when that happens we call this function which updates the various prefabs related to every great position so we can customize those prefabs in order to fit our game design so let's first going to the editor to make our visual prefab let's make an empty game object inside let's add a sprite you need to select the empty background sprite there it is then we're going to have another sprite this one will be above the background let's drag the mine sprite okay like that and finally we're also going to have a text object and we're going to use the text in order to display the number of minds and now just drag the game object on to the prefabs folder in order to make it a proven okay so we have the background sprite that on top we have an icon sprite so we can I'll put the mine or the flag and then on top we also have the mine indicator text and finally on top we can also add another sprite for this texture right here alright that's our prefab now let's make a game object to run our visual code let's put it on zero zero and let's drag the grid prefab visual okay and now here you can see that we have a field for the prefab visual so let's remove this one from the scene and in here drag our prefab references okay now we need to see this working is to call our setup function so here on the game handler script let's add a field for our visual script so here we just need to call our setup and pass in our grid and finally let's go into the editor and here on the game hanwen let's pass in the reference to our script okay let you do it let's test any of there it is we can see the grid and we have a prefab spawned on each grid position so we can pause and look at the hierarchy and there it is we have all of our grid node visuals right awesome now with this working let's set up the display to be based on the underlying a known value so here on our visual script here we have the update visual and in here we are cycling through every single node position on our grid we grab the visual node we set it to active and when we set it up so we can go here and do all of our setup let's pass individual node and as well as the grid object so now here we can do a simple switch on our map grid object and we get the grid time okay so here we are grabbing references for all of our elements inside of our visual node transform again here is the prefab and this is how we set it up so you have the background the icon the indicator and the hidden sprite so here if it's empty we're going to hide the indicator text and we're also going to hide the icon then in case we have a mine if we have mine then first we hide the text then we show the icon then we have all of our indicators so for the indicators we hide the sprite and we show the indicator so case we have an indicator we show the text and then we set the text to the correct number and for right now we're not going to deal with revealing so let's set the hidden transform to always be hidden okay so far so good this should be working with this we should be able to see the correct visuals on each grid position let's see and if there it is we have all of our nice visuals so over here we have an empty we have an eight seven over here we have some mines and all of them are working exactly as intended great so right now with visuals and the underlying objects working we can finally generate a map so in our grid object instead of having a random type let's start off at empty okay and now let's go into the map constructor and in here after we instantiate our grid let's randomly place some mines all right so we are going through this cycle 10 times and we're simply randomizing the X and the y then we get the grid object on that X&Y position and we set the grid type to be a mine so just like this we should have randomly placed ten months let's test any of their it is we have some random minds let's reload and there we go they're in a different position try again and you have another random position awesome so our randomness is working correctly however we do have a slight issue we're placing the mines randomly and we're coming up to ten however this script is completely random so we might be overriding in mind so at the end we might not actually end up with ten months in here for example you can see one two three four five six seven and only eight months so let's make sure that we only set the exact number of mines that we want so here we get the grid object then we do if the map grid objects we get the grid type if it is not a mind then we set it and increase the counter and if it is already in mind then we're simply going to go again so let's see now any of there it is we have our random mines in our map and we can place an exact number okay great now after we place our mind it's time to place the indicators so here we are placing our mines okay and then afterwards let's cycle through the whole map so we are cycling through the width and height and we check if this is an empty position if it is an empty position then we want to come late how many neighbors we have with a mod so let's make a function to get those neighbors all right so here we have a function to get all of the neighbors of a certain x and y position and we're similarly doing some checks to make sure that the numbers are valid so we make sure that the X minus one is above zero and download width and so on and so forth so we get the in left left down up right so on and so forth okay so we have this function to get our neighbors now we can go up here and get our list of neighbors and here we define an int for the mine count we start off at zero then we cycle through all the neighbors and if the neighbor grid type equals a mine then we're going to increase the mine count and after going through all the neighbors then we can now do a switch on our mind count to switch on the mine count and based on how many we have we set our correct map with object type right great so with this let's test and see our mind indicators and okay there it is and everything seems film let's see if it's correct so for example on this corner we have a mine and yep it is correct empty empty an indicator indicator and we got a mine here this one says we have two mines and yep we have these two this one says three one two three and yep all of them appear to be correct so here we have a three so one two three here we have a 2 1 2 and so on all right so our algorithm is working correctly and again this is all being created dynamically so if we run this code again their days we get a component different map and again the algorithm cell works so this one has two so one to another 2 1 2 and so on and there you go as you can see the algorithm is working correctly alright awesome now let's quickly try this out with more mines so here instead of generating 10 mines and let's try putting 30 let's see and yep there it is we have our map full of Mines and now let's see so this one s 2 1 2 yep that's correct this one s 4 1 2 3 4 this one has 5 1 2 3 4 5 right awesome so we have the indicators correctly displaying how many mines seroma so we have pretty much our imagine correctly working now let's deal with player input so let's go into the game Handler and then here on let's make a credit ROI update and on update let's assess for the mouse button down and if we have the unless mouse button down then we're going to tell the map to reveal the grid position on this world position so let's grab the world position so here I'm using a function from the utilities in order to get the mouse on position as long as you can download the utilities for free from unity code monkey comm here is the function in case you want to employ yourself only does is it takes the world camera the screen to run point on the screen position okay so with this let's go to the map and we're going to make a function let's call it reveal grid position and let's pass in our world position okay so here and let's make that function down here a public void reveal grid position and we receive a vector3 for the world position and we are going to grab the map grade object so we have the grid object underneath our world position now we want to reveal it so to do that let's go into our grid object definition and in here let's also add a boolean called is revealed by default it's not revealed so let's put it to false and let's make a function to reveal it we're simply set it to true and again due to the way that we created our print we need to go into grid and call trigger where the object changed passing this x and y so this allows us to fire the event so we can then update our visual and again back into our set up visual node now we can deal with our hidden transform so if the map grid object dot is revealed let's make this listen here a public ball is revealed and we simply return our boolean okay so if it is Ruby on then we do on this and if not then the node is hidden so on we're going to do is set the hidden transform to visible all right that's it very simple so let's review how our code is running over here we have our main game handler script we are testing for an input on the mouse button down so when we click with the mouse button we call the function to reveal the grid position and we pass in the world position then the map gets the map right object underneath our warm position and we're going to call the reveal function so that reveal function is on the credit object and over here all we're doing is setting this boolean to true and then we fire the event on our grid when we fire that event the visual captures that event and calls the function here to update the visual which calls the set up visual node and then in here we set up this visual node we ask if it's revealed if so then we show it like we were doing previously and if not we simply show the hidden transform okay so let's test and yep for starters everything is completely hidden now let's put the mouse here and click and there you go that position was revealed now click some more and there we go that one is revealed and that one down down down so for example like here and that's a mine click here ok and let's see a indicator there I give it two so that means there's two mines within these eight positions and let's see which one it is that one has a 1 so there's a mine here here here or here and it's probably in here and nope it's not this one has two so there's one here and so on and there you go just by clicking we can now uncover our map awesome so now the next step is to identify what we actually clicked on so for example if I click on a mine we should be able to get a nice game over so over here on the map on the reveal grid position let's make this return a map great object type and we go into that one we reveal it and we return the great object type in there okay so now we can go into the game hammer and in here we revealed ok and this one turn em up with object type and then with this type we can simply test if this one is a mine so if we had a mind we're going to spawn a pop-up saying game over all right so let's see if this happens whenever we reveal a mine okay here we're on let's reveal and nope that's empty empty empty okay there's someone so there's a mine at some point in here and there you go that's our mind and we have our nice game over puppet right great so we are now correctly identifying what we reveal when we reveal a position in our grid map now let's just make a quick debug visual to show the entire world so we can easily test clicking on different objects so we're here in our visual script let's make a boolean and a couple of functions so we're going to have a private ball let's call this reveal entire map and let's also make it function okay so we can set this boolean and now here in our cycle when we go through the setup visual node let's see if this one is revealed or we are supposed to reveal the entire map and now let's go into the game hammer and here let's add some debug keys okay so here on key down when I press the T key we're going to reveal the entire map when I let go it's going to go back to normal okay so let's test ok here we are with everything nice and hidden press key and there you go I can reveal the whole map and let go and it goes back to normal so this is excellent for us to easily test what's going on under our map so for example I can see there's a bomb here so now I'm going to click and there you go instant game over so in here we add a three and there you go all right so this is very useful for testing now let's handle the logic for revealing an empty position when we do that for example this position when I click on it it should reveal on that one but also every other empty position that is surrounding it so let's add that logic on our map over here in the map what's going to the reveal grid position okay so we are revealing a grid position now let's test if this one is an empty position so this one is an empty then we want to reveal all of the notes that are connected that are not lines okay so we reveal on the current grid object under the warm position so we get that one on the warm position we call this function with our grid object we count to reveal then we test if it's an empty then we sight on through all the neighbors check if they are not mines and if not then we reveal them however with this we are going to have infinite recursion so we need to keep track of which nodes we've already revealed okay so here's the algorithm for revealing of our connected nodes so we start off just revealing this object then we check if it's an empty word object if so then we want to reveal all of our connected nodes so we set up two lists to keep track of the ones already checked and the ones that we need to check so we first add to the queue this current map grid object and then we have our second where we're going to run once we have nodes check we grab the first one removed from the queue added to the already checked then we check the neighbors then we test the neighbors from that great object if it's not a mind then we reveal it and then we check if it's an empty and if so then we add the queue which will then run and so on and so forth so with this we should be able to reveal all of our connected neighbors when we click on an empty node let's test alright so in here we can use our debug view okay so we can see that over here we have a whole bunch of empty nodes so when I click in here it should reveal this one all of these all these and also the number ones connected so mighty and click in here and there you go it revealed all of the connected empty nodes and number nodes exactly as intended awesome so let's try this one on this side and we click and there yo it reveals all of them so it stops when it finds a mine or when it finds an number pass another number so it only keeps going for the connected ones once the current node is empty so just like this we already have a lot of our game working we can click to reveal positions and we have all of our map generation correctly working so we spawned a bunch of mines and they correctly indicate how many mines are surrounding it right awesome now let's add our mine flag feature flags are a way for the player to place a marker where he thinks there might be a mine don't actually do anything in terms of game design they are merely a visual object so it's actually very simple to add let's go in here to our grid object and let's add another boolean simply called is flying let's make some functions to set this value all right we have our two functions okay now it's going to the visual and in here on our visual node if the node is still hidden we show the hidden transform okay but then let's also go into the map grid object and test if it is flag and if so then we want to show our five so you know into the icon sprite renderer and we're going to set the sprite so now we need a reference to the flag sprite so if here let's add another serie lights field okay we have a flag sprite and a mine sprite so if it is flag then we set the sprite to the flag sprite and then in here in case we modify that one then we need to set it back into the mine sprite alright so just like this now we can go into the minesweeper game hammer and in here we're testing on left mouse button to reveal okay and now it's also test for the right mouse button so on the right mouse button we want to flag our current world position so let's make that function on the map and now we do is go into the map great object and concept fun okay so that should do it let's see if we can see the visuals okay here we are now when I right click and there you go this position has been filed again this really is just the visual mechanics so if I left click I can see on revealment so the goal with this mechanic is for example over here in the corner you can see a one so there's a mine connected to it so it's not on this one or this one so there has to be a mine here and if we use the debug we can see yep indeed there's a mine in there so in this case the player would flag it in order to know okay there's a mine in there so don't reveal it that's going for this mechanic okay so all of our player actions are already implemented all that's left is to hand on the game over and game when states now we already identified game over so if I click in this one there you go we got our pop up so now let's the only way the game win now the player wins the game when all the nodes have been revealed except for the mines so let's deal with that logic here on the map after we reveal a certain position let's make a function to test if the entire map is already reveal all right so here we have our code we sight on through the entire grid we check if it's not a mine and if it's not mine we check if it's not revealed and if not revealed then we return false otherwise if we go through this entire cycle then the entire map is revealed okay so here after we reveal let's call this function and if the entire map is revealed let's fire an event so let's go all the way up here to make our nice event okay so if the entire map is revealed we're going to fire off this event so now let's go to the game Handler and in here and let's subscribe to that event so the map dot on entire map revealed we subscribe to it and if so let's do a nice puppet alright the logic should be working let's test okay so here we are now let's first check out the unloose scenario so we can use the debug okay there's a mine here let's check our loose click and there we go there's a nice pop-up saying game over okay now let's refresh now let's use the debug okay and let's reveal all of it alright so using the debug I can see that this is the only one left so when I click there you go we have the game when event fired all right awesome so all of our logic is correctly working and we can test for a game when and gave loose okay now let's make a simple game over and game win window okay so here we have a nice game over window just says game over and they retry button now let's make the script okay so we simply have the retry button going into the scene manager and calling loads in now for this to work we need to go into the build options over here we need to make sure we add this scene okay and now in here in order to show and hide this window and let's make some static functions okay here we have a simple singleton with show and hide functions and now we can go into the game handler and in here when we have our game over let's simply go into the UI game over grab the instance and kancho alright let's test okay here we are now let's use the debug to make sure that we lose okay there's the mine there now when I click there you go we got the pop-up and we got the game over window so let's click retry and there you go we reload the quarantine and we can play again so mighty clean this one this one and boom there you go game over and retry and there it is and we have a nice new map right awesome now that we have the game over working let's see on with the wind state so when we when we want to input our time into a high score table so first let's count the time okay here we have a nice text field now let's go to the game panel and here on let's say move the timer while the game is active alright so here it is very simple whilst the game is active we simply increase the timer and we update the text and there it is and you can see these seconds counting up okay great now when we win we want to show a text input window okay here is a nice input window now this window was fully created in a previous video so go check the link in the description to see how this was made over here is the input window script and as you can see we have various buttons various actions and we can essentially call a function in order to show it so let's use this here when the player wins let's first get the current time so we're just going to for the timer to get the time score then we're going to go into the input window and call show going to ask the player for a player name okay so we asked for a player name with three characters only of uppercase if the player cancels let's simply retry so you send one load the scene just like previously okay and if the player hits okay then we want to submit that player name and the score into a high score table here is the high score table which again this was fully created in the previous video so go check that video out to see this whole thing created from scratch here is the high score table script and we have a nice function to set a new high score entry so let's use this so we simply go into the high score and we simply call an high score entry now we're going to pass in our time score and our me only received player name and finally let's súplica T game over window in order to make a play again window okay so here we are and everything so far looks correct we have the timer increasing and we have our grid now let's win the game okay so I just have to review on this one and we should be able to win so it's quick and there it is it's asking for our player name let's put ABC and click and there you go we have our high score table with our score and right here at the bottom so we just need to polish this up and reverse the order let's do that okay so here we are and let's try to win so let's use a debug in order to clear all this up all right when I click here I should win so click and there you go i won now I got the player name my input and there you go the player name is added into the high score table and as you can see it is now correctly sorted so I'm number one with a score of twenty three seconds so just like this we have the save scores and we also have the play again button and there you go we click them again and it reloads our scene which reloads with a completely new map okay so now let's just polish this up a bit first let's make the bombs be revealed on loose okay so when we lose we're calling revealing the entire map I know it does is exactly that it goes through the entire grid map and reveals every single grid object let's see okay so let's click on this bomb and see what happens and there you go we have the entire map being revealed as our game over window shows up so hit retry and see there's a bomb here click em yep there you go we can see the entire map all right great so just like this we have our completed final game we have a grid with randomly generated mines we can use our debug vision to reveal the underlying map to see how the generation is working so here is another map there you go and here's another one and yep our map is constantly being generated randomly so we can play which means we can click to reveal a single position so quick if it's an indicator it just shows that position however we can also reveal an empty position and if we do there you go it reveals all of the connected positions and we can also right click to fly potential mines so this one has a mind neighboring it so the mine has to be here so just like that we have a nice visual flag then if we happen to hit a mind so I click there you go we have the game over and choose entire map and we can easily restart however if we successfully clear the whole map just like this area we win and we can out do a player name given a player and there you go we get added to the nice high score table so just like this we have a very nice completed simple mini game as always you can download the project files and utilities from Unity code marcom subscribe to the channel for more unity tutorials plus any questions you have in the comments and I'll see you next time [Music] you
Info
Channel: Code Monkey
Views: 27,365
Rating: undefined out of 5
Keywords: unity minesweeper, unity minesweeper tutorial, unity simple game, unity simple 2d game tutorial, unity beginner tutorial 2019, unity beginner tutorial, unity simple mobile game, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming
Id: ty_sM-UAFYk
Channel Id: undefined
Length: 34min 32sec (2072 seconds)
Published: Fri Dec 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.