|Top Previous Next|
I really don't know why I have never created a card game until now. Fortunately, this month's plug and play article will teach us how to play with the cards. It's a memory game, where you have to click identical pair of cards in order to remove them from the screen; you will have to face more and more difficult challenges as the level number increases, of course.
This piece of code can be of great help for any card game, because it shows you how to generate unique, random numbers (cards), how to shuffle them, how to compare various entities without using pointers (based on their file name), and so on.
The game comes with 3 levels, but adding many more levels is very easy; simply call the function that sets the number of rows and columns, position the camera properly, and that's pretty much it! The only limitation comes from having a maximum of 104 cards appear on the screen at the same time (52 unique pairs) - a limit imposed by the number of cards that are available in any standard set, and not by the code. I guarantee that you won't need more than 108 cards for a regular game, though.
Don't forget that the rows * columns product must be an even number; this means that having 7x6 rows / columns is a good combination, while 7x5 is a bad one because one of the cards wouldn't have a pair.
The code is very well commented, as always.