by date
TGB Card Deck Example
TGB Card Deck Example
| Name: | David Higgins | ![]() |
|---|---|---|
| Date Posted: | Jan 13, 2007 | |
| Rating: | 4.0 out of 5 | |
| Public: | YES | |
| Comments: | YES | |
| RSS Feed: | or Subscribe with . | |
| Profile Page: | View profile page for David Higgins |
Blog post
I just thought I'd post a cleaned up version of this code, along with the image used that allows the code to function properly.
This code allows you to create an ImageMap from the image below (or an image laid out the same way) and turn it into a usable Card Deck for just about any TGB related card game.
The below code is an example that displays some of the cards in a spread, dealt from the deck (top card out) and spread across the screen nicely.
To help explain the code, here's the imageMap I used:

Here is the ImageMapDataBlock that builds the deckImageMap referenced in the code above, using the PNG file above.

Update: Fixed a bug in the image map code, thanks Matthew Harris for pointing it out.
Update: Added example usage code and ImageMapDataBlock
Update: Updated the clearSimSet() function -- thanks again to Matt Harris for pointing out the flaw.
This code allows you to create an ImageMap from the image below (or an image laid out the same way) and turn it into a usable Card Deck for just about any TGB related card game.
$CARD_TYPE_CLUB = 0;
$CARD_TYPE_HEART = 1;
$CARD_TYPE_DIAMOND = 2;
$CARD_TYPE_SPADE = 3;
// $CARD_VALUE_1 -> $CARD_VALUE_10 can be created, or just assumed Static ;)
// the $CARD_VALUE_J-A is useful in case statements
// to perform specific logic based on those cards, usually 1-10 are not
// special in any way, so there's no need to pre-define them for code readability
$CARD_VALUE_J = 11;
$CARD_VALUE_Q = 12;
$CARD_VALUE_K = 13;
$CARD_VALUE_A = 1; // Ace is first, or last? :)
datablock t2dSceneObjectDatablock(CardDataBlock)
{
cardFace = $CARD_TYPE_HEART;
cardValue = $CARD_VALUE_A;
};
function CreateNewDeck(%sort, %scenegraph)
{
%cardDeck = new SimSet();
for(%x = 0; %x < 4; %x++)
{
for(%y = 1; %y < 14; %y++)
{
%card = new t2dStaticSprite()
{
defaultConfig = CardDataBlock;
scenegraph = %scenegraph;
size = "8.875 12.000";
imageMap = deckImageMap;
cardFace = %x;
cardValue = %y;
//cardSomething = %y;
//cardAnotherThing = %x;
position = deckHolder.getPosition();
frame = (%y - 1) + (%x * 13);
};
echo("Adding: " @ %card.cardSomething @ " -- " @ %card.cardFace);
%cardDeck.add(%card);
}
}
if(%sort $= "" || !%sort) return %cardDeck;
echo("Sorting Deck...");
// now to randomize the card deck
%cardDeckSorter = %cardDeck;
%cardDeck = new SimSet();
while(%cardDeckSorter.getCount() > 0)
{
%card = %cardDeckSorter.getObject(getRandom(0, %cardDeckSorter.getCount()-1));
%cardDeck.add(%card);
%cardDeckSorter.remove(%card);
}
return %cardDeck;
}
function clearSimSet(%set)
{
while(%set.getCount() > 0)
{
%obj = %set.getObject(0);
%set.remove(%obj);
%obj.delete();
}
}
function dealNext(%deck)
{
%card = %deck.getObject(0);
%deck.remove(%card);
return %card;
}
The below code is an example that displays some of the cards in a spread, dealt from the deck (top card out) and spread across the screen nicely.
echo("SCENEGRAPH: " @ sceneWindow2D.getSceneGraph());
$cardDeck = CreateNewDeck(true, sceneWindow2D.getSceneGraph());
// generic code to display the codes in a spread
// to show how dealing works -- echos to the console
// to show the cardValue and cardFace values -- should
// match the card spread
$lastX = -40;
$lastY = -9;
for(%x = 0; %x < 3; %x++)
{
for(%y = 0; %y < 7; %y++)
{
%card = dealNext($cardDeck);
%card.setPosition($lastX, $lastY);
%face = "Club";
switch(%card.cardFace)
{
case $CARD_TYPE_CLUB: %face = "Club";
case $CARD_TYPE_DIAMOND: %face = "Diamond";
case $CARD_TYPE_HEART: %face = "Heart";
case $CARD_TYPE_SPADE: %face = "Spade";
}
echo("Card: " @ %card.cardValue @ " -- Face: " @ %face);
$lastX += 1 + getWord(%card.getSize(), 1);
}
$lastX = -40;
$lastY += 4 + getWord(%card.getSize(),0);
}To help explain the code, here's the imageMap I used:

Here is the ImageMapDataBlock that builds the deckImageMap referenced in the code above, using the PNG file above.
new t2dImageMapDatablock(deckImageMap) {
imageName = "TGBCardDemo/Data/images/deck";
imageMode = "CELL";
frameCount = "-1";
filterMode = "SMOOTH";
filterPad = "1";
preferPerf = "1";
cellRowOrder = "1";
cellOffsetX = "0";
cellOffsetY = "0";
cellStrideX = "0";
cellStrideY = "0";
cellCountX = "13";
cellCountY = "4";
cellWidth = "71";
cellHeight = "96";
preload = "1";
allowUnload = "0";
};
Update: Fixed a bug in the image map code, thanks Matthew Harris for pointing it out.
Update: Added example usage code and ImageMapDataBlock
Update: Updated the clearSimSet() function -- thanks again to Matt Harris for pointing out the flaw.
Recent Blog Posts
| List: | 11/19/07 - Cacheable Web Resources... Oh My! 09/29/07 - The Adventures of Coco the Gorrila in: CocoNuts 09/23/07 - How's it all Add Up? 07/11/07 - Ever wondered how to get your game project update to the rest of the team? 06/30/07 - The dog ate my homework, I swear! 06/20/07 - My latest news, and the new site I just launched ... 05/09/07 - $5,000 sound interesting? 05/01/07 - What Time is It? |
|---|
Submit your own resources!| Andy Hawkins (Jan 13, 2007 at 15:46 GMT) |
| Michael Branin (Jan 13, 2007 at 16:20 GMT) |
| Tank Dork (Jan 14, 2007 at 01:55 GMT) |
| J Sears (Jan 14, 2007 at 09:19 GMT) |
| Ben Sparks (Warspawn) (Jan 14, 2007 at 14:47 GMT) Resource Rating: 5 |
| Matthew Spindle Harris (Jan 15, 2007 at 23:26 GMT) |
| Joshua Dallman (Jan 16, 2007 at 08:29 GMT) |
You must be a member and be logged in to either append comments or rate this resource.



4.0 out of 5


