Programming Art, LLC
Products Web Services Free stuff Forums
100% Satisfaction Guarantee
Credit Cards Accepted
Programming Art's Tutorials

Director Card Game Tutorial
This tutorial will show you how to get started making a card game in Director using the Lingo programming language. It may seem like a strange topic for a tutorial, but we hope that not only will you get an idea on how to make a card game, but also get some experience with objects, behaviors, and other Director basics. Sometimes the best way to learn is to simply observe a solution, so don't forget to open up the FREE Director movie below and check out all the source code. If you need help, please post your questions on the boards.

Download the FREE Director source movie here! [Director 8.0, 632Kb]

When you run the sample movie, you will see a stack of cards on your left, a discard pile on your right, a message bar at the bottom, and a shuffle button. Press the shuffle button and the cards in the stack will be shuffled. In a more elaborate game, this would be a good time to insert a shuffling animation. Pull a card from the stack by clicking on the stack and dragging a card onto the table. Once you release the card, it's face will be shown. Pull 2 or 3 more cards from the stack and place them on the table. Next, place one of the cards from the table on the discard pile by clicking and dragging it to the discard pile. Place another card on the discard pile. Now, take your cards back from the discard pile and notice that they come off in the reverse order in which you put them on. The discard pile is behaving just the way you would expect. The first card placed on the pile is the last card off. Now, drag a card from the table to the card stack. The card will be placed at the bottom of the stack, so you would have to remove all the cards from the stack in order to see that card again. The card stack is behaving the way you would expect because in most card games, when a card is returned to the stack it is almost always placed on the bottom.

The structure of this demo starts with the "cardDeck object". This is a script that represents a deck of cards. To create a new deck of cards, you must tell the object how many decks you wish to use and what sprite channels are available for it to utilize. In this demo, we decided to use 1 deck of 52 cards, so we did the following to create our object.

set spriteList = []
   repeat with n=10 to 62
   end repeat

 --initialize card deck with one deck of cards and 52 available sprite channels
 set gDeck = new (script "cardDeck object", 1, spriteList)

Be sure you have enough sprite channels allocated to support how ever many decks of cards you plan on using. For example, 2 decks of cards will require 104 sprite channels in case every card is ever placed on the table at the same time. To allocate a sprite channel, just place a cast member in that channel and then drag it off the stage so it doesn't get in the way.

To make the deck of cards usable, we had to create behaviors so we could move the cards from the stack to the table, from the table to the discard pile, and so on. We have 3 behaviors in action. The first behavior can be seen when you take a card from the stack. This is the "topCard behavior". It allows you to drag the card about the screen and either place it on the table or back on the stack. The second behavior is for the cards that have already been placed on the table. It is the "activeCard behavior". This behavior should be attached to any sprite channels you've allocated for the "cardDeck object". It allows you to move cards about the table, place them on the bottom of the stack, or place them on top of the discard pile. The third behavior is for the cards in the discard pile. This is the "topDiscard behavior". It allows you to take cards from the discard pile and place them on the table or back on the discard pile. When you go through the source code for these behaviors, notice that we are careful to get available sprite channels from the "cardDeck object" when we need them, and put them back once a card is either returned to the stack or the discard pile. How this is done will be very clear after you view the source code.

The "cardDeck object" also makes use of a simple naming scheme for the card graphics. If you plan on using this object to start your own game, be sure you name your graphics the same way they are named here. Once you read and understand the source code for the "cardDeck object", the naming scheme should seem intuitive.

If you're looking for ideas on where to go from here, try creating your own "hand of cards" object and incorporating it into this demo. When you take cards from the stack, you could place them in different "hands of cards". If you wanted to implement a poker game, you could then add functions to your new object that would check a hand and report it's highest value. For example, a hand consisting of all spades would report a flush, a hand with 3 nines and 2 jacks would report a full-house, etc.

A quick note regarding the graphics in this demo... The graphics in this demo are property of Programming Art and are made available in this format for learning purposes. Please do not use them in your own commercial product without our permission. Thank you.

Good Luck!

Your Shopping Cart