Let's Make Solitaire in Unity Part 1: Set Up and Shuffle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
👍︎︎ 1 👤︎︎ u/megalomatt 📅︎︎ Jan 05 2019 🗫︎ replies
Captions
hello everybody this is Matt from Megillah mobile and welcome to part 1 of this mini series let's make solitaire in unity solitaire is a classic card game from the 1700s that has seen its fair share of video game adaptations it first appeared as an 8-bit Atari game and a version was included on every edition of Microsoft Windows up until Windows 7 if you google solitaire a playable game is embedded within the search results page with so many different versions several coding solutions are available so as part of this challenge I am going to use none of them this means that the implementation we create a Miss series will be a unique version that plays specifically on functionality provided by the Unity game engine but it also means that it would not be the cleanest or most concise implementation possible I did not set out to make the most cohesive refactored or neatest version of solitaire but instead I treated this project like a game jam what I did have a loose plan of what I wanted to create I did not spend much time planning out the exact design of the software so from a software engineering perspective it is a bit of a mess however solitaire is not the most complicated game so I have confidence that we'll get a half-decent version that we can mate together and hopefully learn something along the way a word of warning I will explain what I'm doing as we go along but we will be doing a fair amount of juggling arrays and lists so having a basic understanding of them is probably a prerequisite to your enjoyment of this series additionally I'd recommend that you've taken the time to do some basic unity tutorials as although this is not tricky stuff it is not aimed at an absolute novice either each episode will contain links to any assets used and as usual all the scripts and unity packages are available in the description you can follow along download the individual unity packages for each episode skip ahead and download the complete package or even play the current version that we're creating in the series in a browser let's get started [Music] before we start building solitaire and unity we need to be aware of the basic rules of the game the version we're building is the basic conduct version it's played with a standard 52-card deck which is shuffled and then partially Delta to seven powers from left to right each power contains one more card than the last and only the final card in each pile is dealt face-up the remainder of the deck is placed facedown at the top at the playfield the aim of the game is to move all the cards from the seven spaces in the bottom row to the four spaces in the top room cards on the bottom row may be sequentially built down by holding the colors from King to ace and the top row is built up I suit Ace to King only Kings can be moved to empty spaces on the bottom row only aces can be moved to empty spaces on the top row stacks of cards and the bottom row can carry the cards beneath them when they're moved to another space on the bottom row and if the back of the car is exposed on the bottom row then that card may be flipped over to end to play the remaining cards in the deck are made available to the player in different ways depending on the version that you're playing but we will start with the most popular and maybe add additional functionality later in this version the player mesial three cards at a time from the top deck face-up it can only access the last card to use in the game we will place no limits on the number of reels additionally if a player double clicks a card and it is eligible to enter the top row it should automatically fly up there we are starting this project from scratch so the first thing we need to do is create a brand new unity project I'm calling mine let's make solitaire but you can call yours whatever you want the important thing is that you set the template to 2d as this is going to be a 2-d game once UNC has loaded hop on over to the game tab and select your desired aspect ratio I have gone with 16:9 as it is the most common aspect ratio for computer monitors instead of adding a background image I'm just going to tint the background color generated by my camera to do this select the main camera and click on the background color I have made mine felt green for that classic solitaire look whilst you're here double check that your camera is set to orthographic mode and check the Zed position at the transform is set to negative 10 world units we're going to need some assets for this game and I got my playing card sprites from open game art torgue which is an excellent resource the cards I'm using are listed as being in the public domain and the pack contains both pngs and vector file formats we will only need the pngs once downloaded and unzipped the cards can be dragged straight into the unity editor I've made a folder called sprites to house them unfortunately this deck did not come with a card back so I had to look elsewhere the pixabay is another great place to royalty free assets I settled on this design for my card backs i resized it and tinted it read an image editor you can make your own or you can use mine all the assets are linked in the description this can be added to the spirits folder the last asset I created myself as it is just a simple rectangle that indicates where the powers of cards can go I drew this myself as it was just a simple shape and you're welcome to use mine if you want to next we can add some game objects to the scene the first one I have labeled deck the second top and the third bottom adding the rectangle to the scene by dragging it from the sprites folder to the scene shows that it is a little big there are a few ways to resize it but for now I'm just going to set the scale to not point 4 in the X and y-axis then we build the scene as we want it to appear I use world units to make my spaces line up neatly as I intend to use the location of these rectangles to handle the position of the cards being placed on them it doesn't need to be accurate I just prefer the look of it I rename the rectangles sequentially starting of 0 so I can keep track of which is which easily this will make more sense if I have to link them to arrays or lists which I am planning on doing later I then set the four top positions as children or the top game object and the seven bottom positions as children of the bottom game objects make sure that the transforms of the top and bottom game objects are reset before you do this as it could cause headaches later if you don't I then add a rectangle to the deck position child to a deck game object and switch out is sprite for the back of the card picture that I created earlier this is going to be the button that deals cards from the deck so I've called it deck button I don't want to have to create 52 different cards so I'm going to create a prefab that can handle all of that for me whenever we deal a card we can just deal a copy of this prefab and update the sprite depending on whatever the card is meant to be I want the car to be able to handle this itself so I'll create a new script update sprite and attach to the card game object although we'll come back to that later as pretty as a playfield is looking the game doesn't really do anything yet so we need to start introducing some functionality I created a game object called solitaire game to handle my logic and the first component that I add is a solitaire script I also want this game object to handle any input from the player so I create a second script called user input and attach that to the solitaire script is going to handle the gameplay so I need it to be able to do a few key things it will generate a deck of cards shuffle the deck and deal the cards out correctly I'm going to use arrays loops and lists to handle a lot of this so the thing I need is an array of suits there will never be more suits than clubs diamonds hearts and spades so I can make this a static string array public static string array suits equals new string array C D H and s for clubs diamonds hearts and spades public static string array values equals new string array ace 2 3 4 5 6 7 8 9 10 Jack Queen King I also create a public list of type string and I'm going to call that deck as I will be automatically generating the deck from these arrays of strings I create a new public static method that returns a list of strings called generate deck public static list off type string generate deck list of type string new deck equals a new list of type string for each string s in suits then for each string V in values new deck door add s + V this concatenates the suit string to the valley string and adds it to the list of cards then I can return new deck I then want to create a method that handles the start of my game so I creation a method public void play cards within that I can call the method we just created deck Eckles generate deck the test of the cards are all contained within the deck we can loop through it and print the contents of the deck to the console for each string card in deck print card we can then call the play cards method from the start method saving the scripts and attaching to unity we can move back over to the editor and hit play the console then fills up with printer statements representing each card in my deck suited in order from CA which is the ace of clubs C - the two clubs and so on in order all the way to the 50 second entry which is SK the King of Spades next we want to shuffle the cards shuffling is a bit of a rabbit hole topic especially when it comes to accuracy and there are a number of methods of generating the most random shuffle possible but this is just solitaire it's not an online casino we will go with a quick and easy shuffle the one we are using is based on the officiate shuffle and credit where credit is due I turned to Stack Overflow for help getting this right I used a code snippet uploaded by a user called grenade and edited by you eek I'm a link is in the description we create a method called shuffle of an arbitrary type T that takes in a list where the thing was lists are also arbitrary as an argument we call list void shuffle T takes in list T which we only call list we can use the length or cow for lists and randomly reorder it system dot random random equals new system dot random int N equals list dot count while n is greater than 1 do the following into K equals random dot next and decrement n tea temp equals list at location K lists a location K equals list at location M list at location N equals temp then back in the play cards method all we need to do is call the shuffle method and give it the deck now if we save go back to the editor and hit play we can see that all the cards are jumbled up nicely and we get a different order each time we play the game that's it for part 1 next time we will assign sprites to the cards sort the cards into their correct piles and deal them out just like in a real game of solitaire thanks for watching if you enjoyed this video then please hit like please it subscribe and feel free to leave a message in the comments I'll catch you next time [Music]
Info
Channel: Megalomobile
Views: 52,786
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: 1Cmb181-quI
Channel Id: undefined
Length: 9min 24sec (564 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.