Let's Make Solitaire In Unity Part 2: Dealing to the Tableau

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody this is Matt from Megillah mobile and welcome to part two of this mini series let's make solitaire in unity in part 1 we set up the scene generated a deck of cards shuffled them and printed the results out to the console if you missed episode 1 I'd recommend checking that out first as we will pick up right where we left off in this video we will assign sprites to the cards sort the cards into their correct piles and deal them out to a tableau which as I'm not sure I can pronounce correctly we'll just call the bottom row as usual all the scripts and unity packages are available in the description and the most recent version can be played in a browser let's crack on [Music] now that we can create a list of card names and shuffle it we can use that list to create cards that we can see on the screen as we know the order of a nun shuffled deck will always be the same alphabetically arranged by certain value we can use an array of sprites in the same order to make sure we always show the correct card public sprite array card faces back in the editor we can select the soil test script expand the card faces and give the array 52 locations we can then go through and attach the correct sprite to each spot this is a little mind numbing but it only needs to be done once we can also take the opportunity to tidy up a bit I've created a new folder for the scripts and a new folder that'll house any prefabs created as we're spawning multiple copies of the card it will need to be a prefab so we can drag it in there the solo test script will handle dealing the cards so we create a new public game object the card prefab and attach the card prefab we just created to the script in the editor next we need a method to deal the cards void solitaire deal for each string card in deck we instantiate a new card prefab as this is just going to be a quick test we can spawn them at the transform position of this game object which as we've reset the transform will just be the center of the screen and quaternion dot identity which matches the rotation the name of the new card game object created is going to be the string of the Kart name so new card don't name equals card the solitaire deal method is then added to the play cards method save and hit play and it looks like a single card has appeared on the screen however if we take a look at the hierarchy we can see that there are 52 individually named and shuffled cards here they just all share the same position and currently only display the cards back to get a better visual representation of the cards we can give them an offset in the y&z axis in the solitaire Zeile method will create a float y offset which we can start equal to zero and a float Zed offset which we can start equal to not 0.3 float then we can amend the instantiate position to be a new vector3 and build it from a transformed opposition x y&z of the game object we can then subtract the appropriate offset from the Z and Y values and on each iteration through the loop which instantiates the objects increase the offsets I'll increase the Y offset by not point one float to start with and there's head offset by not point not three float now saving and heading back to the unity editor we can see that all 52 cards are now spawned in a single row entering 3d mode we can see that the lower our card is the closer it is to the camera this can help with rendering issues and hit detection now that we have cards on the screen we can update the sprites to match the cards to their names we already created an update sprite script and attached it to our card so within the update sprite script we can handle the Spry swaps we create public sprite can't face public sprite card back and attach the back of the cards to the card back slot in the editor I want interactive objects to have a unifying script that looks after things like their values to help tract of the gameplay so I'm going to add a new script selectable to the cart prefab within the selectable script we can create a new public pool face-up and set it to be equal false over start back within the update script we need access to the sprite renderer the selectable script that we just added and with solitaire script as that is where we added the sprite away the houses all the card faces drivers sprite renderer sprite renderer private selectable selectable and private solitaire solitaire to assign the correct face card for newly instantiate cards we can use the array is created in the solitaire script I have named the instance of the card prefab based on the string from the deck and also out of the card faces in order of an empty deck therefore the list location where the name of the card matches the string in an empty deck is the same number as the location of the correct sprite in the special ray as the generate deck method is inside the soil test script we can just call it to build a quick empty deck to test against in the start method we can add list of type string deck equals solitaire don't generate deck we also need access to the sprites so we can create a quick variable to help with that solitaire equals find object of type solitaire now we can build a loop that matches the name of the card with the strings in the deck int I equals 0 for each string card in deck if this not name is equal to card card-- face equals solitaire dock card faces at location I brick we don't need to keep going here as we found the correct card already then we increment I sprite renderer equals get components sprite renderer selectable equals get components selectable then in the update method we can say that if the card is face up in the selectable script than to show the cards face otherwise show the cards back if selectable dot face up is equal to true sprite renderer DOS prime equals card face else sprite renderer dot sprite equals card back active a solitaire script and we can add a line that makes any doubt card face up at the time being new card don't get component selectable don't face up equals true we can also increase the y offset to allow us to see more at the card faces save the scripts and remember to update the prefab when we play on the editor we can see that all 52 cards are shuffled and dealt with the correct faces they fall off the bottom of the screen but we can head back over to the scene panel to take a better look at our creation selecting a card and toggling the face up pool in the selectable script allows us to see that if face up is false the back of the we'll be displayed instead spawning all the cards in the middle of the screen isn't really part of the game so we need a way to position them correctly when they are dealt out to help with this we can create some arrays to look after the positions of the empty spaces in the solitaire script we can add a public game object array bottom pause and a public game object array top pots a couple of arrays of lists to look after what is where public lists of type string array bottoms and the public lists of type string array tops and a list of what card is currently in what position create a private list of type string autumn zero I must get to equal a new list of type string and we do the same for bottom one bottom to bottom three bottom four bottom five and bottom six in the start method we can build the array of lists bottoms from the lists we just made autumn's equals a new list of type string array and that's going to contain autumn zero bottom one autumn two bottom three bottom four bottom five bottom six we are now able to sort the initial cards dealt out into their correct positions we can do this with a new method called solitaire sort this method will run a couple of nested loops to put the cards in the right places the first loop counts from zero to six and saves the current count as I within each iteration a second loop is run that counts from the current value of I to six and this saves account as J within this loop we can find a list at location J of the bottoms array and AB the last string in the deck of cards we can then remove that string from the deck as the card is no longer in it avoid solitaire sort for int I equals zero while I is less than seven increment I for n j equals i while j is less than seven increment J bottoms at location J don't add deck top last off type string the don't last functionality for finding the last entry in the list is not a standard function to be able to use it we do need to add an additional library using system link then we can say deck dot remove at deck count minus one so when I is zero seven cards are added to the bottoms list one at each location but the second time through J starts counting at one so location zero is skipped over this repeats adding one less card to each row until all seven rows that are complete heading back over to the editor we can connect the bottom pause and top pause arrays with the appropriate game objects pop pause has a size of four we can just drag and drop the respective game objects autumn paused as a size of seven hoping you do the same back in the solitaire script instead of dealing the cards all in the same place we can use the positions of the game objects we've just added and the contents of the bottoms array of lists of strings to populate the play field correctly within the solitaire deal method we create a new loop that counts from 0 to 6 inclusive for int I equals 0 while I is less than 7 increment I we can then change the instantiation position to instead be the bottom position app location off the count bottom pause application I don't transform the position to X and we do the same for y and we do the same visit the instantiated cards should also be children of the game objects they're being dealt from top off so we can add an extra argument to the instantiate method autumn pause at location I transform back in the play cards method before dealing the cards we call the solitaire sort method and as we're no longer looping through the entire deck to deal all the cards we've changed this loop to only deal cards they're in the lists of cards at the bottoms save and play and the cards have been dealt out correctly but there is a small glitch by setting the new cards to be children of the transforms they inherit the properties of the parent as the scale of the cards is no point form and the scale of the empty rectangles is also not point for the cards have been shrunk twice luckily this is an easy fix drag the empty positions so that they are no longer children of the top and bottom game objects all we need to do is set the scale their parent game objects to null point four then drag them back over the size remains the same but if we look at the individual scales we can see that they've reverted to one now we just need to update the scale of the card prefab from not point four to one and press play and the right number of cards are dealt in the right places at the right scale of course in solitaire only the bottom cards of each row are initially dealt face-up so heading back over to the solitaire script with a module function we can add some extra logic to make this happen instead of making all the cards face-up we will only make them face up if the card string is equal to the last string in the current list if card is equal to autumns at location I at location autumn's at location I minus one new can't doggie components off type selectable dot face up equals true now when we save and hit play in the is really starting to look like solitaire however I don't like the way the cards appear all at once I would rather that they were quickly doubt out one at a time to do this we can change our solitaire deal method to be a core team replacing void with ienumerator allows us to add a yield statement to force a method to wait before completing we can put this wait statement inside the loop to slow it down I still want the deal to be quite fast so I'll set the wait time between each cards to only a tenth of a second yield return new waitforseconds not point one flicked up in the play cards method where we call solitaire deal we need to identify that we're running a Korra team by enclosing it in a start core team method now when we save and play the cards are quickly dealt one at a time that's it for part two next time we will look at creating a way for users to click on game objects and then move on to dealing cards three at a time from the deck pile thanks for watching if you enjoyed this video then please hit like please subscribe and feel free to leave a message in the comments okay [Music]
Info
Channel: Megalomobile
Views: 25,960
Rating: undefined out of 5
Keywords: lets make unity, solitaire, make solitaire, card game, unity2d cards, Unity card game, unity game tutorial, unity beginner tutorial, unity tutorial, unity2d tutorial, game jam, rapid game prototyping, game design, shuffle unity, lets make, unity series
Id: IfG6YtqJoLk
Channel Id: undefined
Length: 11min 12sec (672 seconds)
Published: Sat Jan 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.