Using BitmapData to manage a deck of cards

Read all posts about "" game

December 28th update: 2nd part online

I have already published a tutorial about BitmapData in the post Shuffle an image with BitmapData, but this time I want to show you how to manage a deck of cards using BitmapData.

Managing a deck of cards is very useful because there are tons of card games you can publish once you have a deck of cards in your library.

The first thing you need is an image with all cards like this one:

Deck of cards

The image is scaled down for blogging reasons, but you can view/download it at its original size here.

Now, with the same method seen in Shuffle an image with BitmapData, I’ll cut off all cards starting from the original image.

I am publishing some raw actionscript because this is just a prototype, but a full tutorial will come shortly.

The prototype is about the simplest card game ever: you draw a card and have to say if the next card will be higher on lower than the card you have on the table.

Being a prototype it’s not complete yet, but I want to share this code to you.

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.DropShadowFilter;
var distance:Number = 4;
var angleInDegrees:Number = 45;
var color:Number = 0x000000;
var alpha:Number = .5;
var blurX:Number = 4;
var blurY:Number = 4;
var strength:Number = 1;
var quality:Number = 3;
var inner:Boolean = false;
var knockout:Boolean = false;
var hideObject:Boolean = false;
var my_shadow_filter:DropShadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);
big_picture = BitmapData.loadBitmap("cardz");
_root.attachMovie("table","table",_root.getNextHighestDepth());
_root.attachMovie("higher","higher",_root.getNextHighestDepth());
_root.attachMovie("lower","lower",_root.getNextHighestDepth());
_root.createEmptyMovieClip("big_pic_obj",_root.getNextHighestDepth());
big_pic_obj.attachBitmap(big_picture,_root.getNextHighestDepth());
big_pic_obj._visible = false;
pictures = new Array();
sequence = new Array();
Array.prototype.shuffle = function() {
	for (x=0; x<52; x++) {
		var from = Math.floor(Math.random()*52);
		var to = this[x];
		this[x] = this[from];
		this[from] = to;
	}
};
for (x=0; x<52; x++) {
	card = _root.createEmptyMovieClip("small_pic_obj_"+x, _root.getNextHighestDepth());
	sequence[x] = x;
	small_picture = new BitmapData(79, 123);
	card.attachBitmap(small_picture,_root.getNextHighestDepth());
	small_picture.copyPixels(big_picture,new Rectangle(0+(x%13)*79, 0+Math.floor(x/13)*123, 79, 123),new Point(0, 0));
	card._visible = false;
	card.filters = new Array(my_shadow_filter);
	card.onEnterFrame = function() {
		switch (this.action) {
			case "come" :
				this._x += 40;
				if (this._x>210) {
					this._x = 210;
					this.action = "stay";
				}
				break;
			case "move" :
				this._x += 20;
				if (this._x>310) {
					this._x = 310;
					this.action = "dissolve";
				}
				break;
			case "dissolve" :
				this._alpha -= 4;
				if (this._alpha<0) {
					can_draw=true;
					this.removeMovieClip();
				}
				break;
		}
	};
}
sequence.shuffle();
cards_drawn = 0;
can_draw = true;
draw_card();
higher.onRelease = function() {
	if(can_draw){
		can_draw=false;
		draw_card();
	}
};
lower.onRelease = function() {
	if(can_draw){
		can_draw=false;
		draw_card();
	}
};
function draw_card() {
	_root["small_pic_obj_"+sequence[cards_drawn]]._x = 0;
	_root["small_pic_obj_"+sequence[cards_drawn]]._y = 30;
	_root["small_pic_obj_"+sequence[cards_drawn]]._visible = true;
	_root["small_pic_obj_"+sequence[cards_drawn]].action = "come";
	_root["small_pic_obj_"+sequence[cards_drawn-1]].action = "move";
	cards_drawn++;
}

and the result is...

I think simple and stupid games like this one can have a good replay value if I provide a high score table like in Christmas Couples, another simple game that entered in the "million games played" club.

But I will talk about it in the next post about the experiment, meanwhile take the source code.

Read 2nd part

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers