Unity Experiment : Card game

25 Jun 2017

I've got a few interesting ideas for games, and might already have the domains for a little spinoff games company, but I don’t really want to write them from scratch. I've done that before and it’s not worth the effort with Unity being so cheap (e.g. free).

I‘m going to use Unity as I like C# and don’t agree with certain nameless Epic employees’ constant rants and their refusal to support UWP properly when pretty much all of their competitors do, so Unreal is out.

The thing is, there’s no point in trying to build something cool before you know how to use the tools, so I’m going to try and build something simple every week, e.g. a game mechanic, component, idea etc, just to see how stuff works.

As anyone with any experience programming know, sometimes you have to try making it to find out what works and what doesn’t, and it’s better to find out on something you expect to throw away, rather than after you’ve put a lot of hours in and then find out you’ve got to re-write a load of it.

I'll probably include making basic models and textures in here as well and if they’re any good I can always stick them in the Unity store.

I won’t be releasing any games made at the moment as they’ll be a collection of rough toys and obvious clones of existing stuff.

Basic mechanics : Card game

I know I’m going to end up building card games of some form so I thought I’d start there. This is also my first proper go at using Unity. I've had quick plays before but never really made anything, just installed it, started reading the manual then found something else to do.

I don’t want to waste time coming up with a game so I’m just going to implement Blackjack. The aim is to get a basic sprite based card game working with scoring and simple animation.

Storing of cards

Given that there’s going to be over 200 cards in play (4 full decks) it didn’t seem sensible to have them all being GameObjects. If they’re not visible to the player then why bother?

So I decided to split cards into two objects. There’s a Card class that just stores the suit and value and is used behind the scenes for everything.

Then I created a CardBehaviour which references a Card. This was combined with a Sprite object and made into a prefab so I could have a static method that took Card and created a version the player could interact with. The CardBehaviour also dealt with the game related aspects; where the card is currently on screen, the animation logic, current sprite image.

I didn’t do anything fancy with the animation, it just interpolates between a start and end position over a given time duration. I did write a simple class that maps an input float value between 0…1 to a difference range, so I could add basic easing in and out to make the animation look a little more natural.

Basic UI

You don’t need much for Blackjack, just two buttons for the players choice of Stick or Draw.

Once the hand reaches its end these get swapped out for a larger ‘Next hand’ button so the player can see the result of the hand, and the pot’s value, before it gets added to their balance if they won.

Originally all the on-screen text was on a single canvas but when I moved the camera from directly above to a more natural ‘sitting at a table’ view it looked weird to have the hand totals just floating in mid air. To fix this I created a second canvas, moved the totals onto that, and fixed it to the same plane as the cards.

Wrapping up

It works, and apart from the very quickly drawn card graphics, doesn’t look too bad. I tested it on my desktop, phone and an old tablet I have and it works fine on all three.

This post has been taged as Unity