Using BitmapData to manage a deck of cards – part 2
Talking about Card Game game, Flash and Game development.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
After a couple of days, the prototype for the management of a card deck is almost complete.
Now I have a full game, you draw a card and have to say if the next card will be higher or lower.
At the moment the prototype has all messages and a game over screen with the score recap.
Now I want to include Kongregate’s highscores API and then I’ll release the complete tutorial.
Meanwhile, here it is the code:
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
var card_shadow:DropShadowFilter = new DropShadowFilter(4, 45, 0x000000, .5, 4, 4, 1, 3, false, false, false);
var text_glow:GlowFilter = new GlowFilter(0x000000, .6, 4, 4, 2, 3, false, false);
big_picture = BitmapData.loadBitmap("cardz");
_root.attachMovie("table", "table", _root.getNextHighestDepth());
_root.createEmptyMovieClip("game", _root.getNextHighestDepth());
game.attachMovie("higher", "higher", game.getNextHighestDepth());
game.attachMovie("lower", "lower", game.getNextHighestDepth());
game.createEmptyMovieClip("big_pic_obj", game.getNextHighestDepth());
big_pic_obj.attachBitmap(big_picture, game.getNextHighestDepth());
big_pic_obj._visible = false;
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 = game.createEmptyMovieClip("small_pic_obj_"+x, game.getNextHighestDepth());
sequence[x] = x;
small_picture = new BitmapData(79, 123);
card.attachBitmap(small_picture, game.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(card_shadow);
card.onEnterFrame = function() {
switch (this.action) {
case "come" :
this._x += 40;
if (this._x>210) {
this._x = 210;
this.action = "stay";
if (cards_drawn>1) {
game.ok_ko._visible = true;
}
}
break;
case "move" :
this._x += 20;
if (this._x>310) {
this._x = 310;
this.action = "dissolve";
}
break;
case "dissolve" :
this._alpha -= 4;
game.lap_score._alpha -= 4;
if (this._alpha<0) {
can_draw = true;
game.ok_ko._visible = false;
if (cards_drawn == 52) {
endgame();
}
this.removeMovieClip();
}
break;
}
};
}
game.attachMovie("ok_ko", "ok_ko", game.getNextHighestDepth(), {_x:250, _y:230, _visible:false});
game.attachMovie("score", "score", game.getNextHighestDepth(), {_x:0, _y:140});
game.attachMovie("lap_score", "lap_score", game.getNextHighestDepth(), {_x:-50, _y:0, _alpha:0});
game.score.filters = new Array(text_glow);
game.lap_score.filters = new Array(text_glow);
sequence.shuffle();
cards_drawn = 0;
points = 0;
can_draw = true;
draw_card();
game.higher.onRelease = function() {
if (can_draw) {
can_draw = false;
higher = true;
draw_card();
}
};
game.lower.onRelease = function() {
if (can_draw) {
can_draw = false;
higher = false;
draw_card();
}
};
function draw_card() {
game.ok_ko.gotoAndStop(2);
lap = 0;
if (((sequence[cards_drawn]%13)<=(sequence[cards_drawn-1]%13) and (!higher)) or ((sequence[cards_drawn]%13)>=(sequence[cards_drawn-1]%13) and (higher))) {
game.ok_ko.gotoAndStop(1);
game.lap_score.lap_text.textColor = 0x40ff40;
if (cards_drawn>0) {
if (!higher) {
lap = 13-(sequence[cards_drawn-1]%13);
} else {
lap = sequence[cards_drawn-1]%13+1;
}
}
} else {
lap = -5;
game.lap_score.lap_text.textColor = 0x900000;
}
game.lap_score.lap_text.text = lap;
if (cards_drawn>0) {
game.lap_score._alpha = 100;
}
points += lap;
game["small_pic_obj_"+sequence[cards_drawn]]._x = 0;
game["small_pic_obj_"+sequence[cards_drawn]]._y = 10;
game["small_pic_obj_"+sequence[cards_drawn]]._visible = true;
game["small_pic_obj_"+sequence[cards_drawn]].action = "come";
game["small_pic_obj_"+sequence[cards_drawn-1]].action = "move";
cards_drawn++;
game.score.textscore.text = "Cards left: "+(52-cards_drawn)+" - Score: "+points;
}
function endgame() {
game.removeMovieClip();
_root.attachMovie("game_over","game_over",_root.getNextHighestDepth());
game_over.gameovertext.filters = new Array(text_glow);
_root.game_over.gameovertext.text = "Your score:\n"+points;
}
and this is the result
When it’s game over, at the moment you must reload the page to play again. How much did you score?
Download the source code, and wait for the full tut…
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.