Talking about Bejeweled game, Actionscript 3, Flash and Game development.
You should know Bejeweled from PopCap Games.
It’s a game which have been played millions, if not billions, of times. Now PopCap just released the 3rd version of his blockbuster.
To celebrate this event, just like I made when I coded a complete Flash Sokoban game in less than 2KB, I made a Bejeweled version in less than 2KB. The swf file is 2035 bytes, less than the 2048 need to make 2KB.
Do you think it does not have any feature? Take a look:
* colorful representation of all 7 kinds of jewel
* select a jewel clicking on it
* swap jewels only if they make a chain
* combo chains recognizing
* falling jewels animation
* complex (let’s say “not that basic”) score system
* hint on a possible move when clicking outside the game area
Everything in less than 2KB. The code was mode with the only purpose of making the game fit in less than 2KB, there’s not readability nor optimization.
Here it is the source:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
public class n extends Sprite {
private var jw:Array=new Array();
private var g:Sprite;
private var s:Sprite=new Sprite();
private var pr:int=-10;
private var pc:int=-10;
private var cl:Array=new Array(0xFF0000,0xFF00,0xFF,0XFFFF00,0xFF00FF,0xFFFF,0xFFFFFF);
private var cp:Boolean=false;
private var ts:TextField=new TextField();
private var th:TextField=new TextField();
private var sc:uint=0;
private var m:uint=0;
public function n() {
addChild(ts);
ts.textColor=0xFFFFFF;
ts.x=500;
addChild(th);
th.textColor=0xFFFFFF;
th.x=550;
for (var i:uint=0; i<8; i++) {
jw[i]=new Array();
for (var j:uint=0; j<8; j++) {
do {
jw[i][j]=Math.floor(Math.random()*7);
} while (rk(i,j)>2||ck(i,j)>2);
g=new Sprite();
g.graphics.beginFill(cl[jw[i][j]]);
g.graphics.drawCircle(30,30,29);
g.graphics.endFill();
g.name=i+"_"+j;
g.x=j*60;
g.y=i*60;
addChild(g);
}
}
addChild(s);
s.graphics.lineStyle(2,0xff0000,1);
s.graphics.drawRect(0,0,60,60);
s.visible=false;
stage.addEventListener(MouseEvent.CLICK,ci);
addEventListener(Event.ENTER_FRAME,ef);
}
private function ef(e:Event):void {
var f:Boolean=false;
for (var i:int=6; i>=0; i--) {
for (var j:uint=0; j<8; j++) {
if (jw[i][j]!=-1&&jw[i+1][j]==-1) {
f=true;
jw[i+1][j]=jw[i][j];
jw[i][j]=-1;
getChildByName(i+"_"+j).y+=60;
getChildByName(i+"_"+j).name=(i+1)+"_"+j;
break;
}
}
if (f) {
break;
}
}
if (! f) {
var h:Boolean=false;
for (i=7; i>=0; i--) {
for (j=0; j<8; j++) {
if (jw[i][j]==-1) {
h=true;
jw[0][j]=Math.floor(Math.random()*7);
g=new Sprite();
g.graphics.beginFill(cl[jw[0][j]]);
g.graphics.drawCircle(30,30,29);
g.graphics.endFill();
g.name="0_"+j;
g.x=j*60;
g.y=0;
addChild(g);
break;
}
}
if (h) {
break;
}
}
if (! h) {
var r:Boolean=false;
for (i=7; i>=0; i--) {
for (j=0; j<8; j++) {
if (rk(i,j)>2||ck(i,j)>2) {
r=true;
var tr:Array=[i+"_"+j];
var u:uint=jw[i][j];
var t:int;
if (rk(i,j)>2) {
t=j;
while (chk(u,i,t-1)) {
t--;
tr.push(i+"_"+t);
}
t=j;
while (chk(u,i,t+1)) {
t++;
tr.push(i+"_"+t);
}
}
if (ck(i,j)>2) {
t=i;
while (chk(u,t-1,j)) {
t--;
tr.push(t+"_"+j);
}
t=i;
while (chk(u,t+1,j)) {
t++;
tr.push(t+"_"+j);
}
}
for (i=0; i0&&mouseY<480&&mouseY>0) {
var sr:uint=Math.floor(mouseY/60);
var sc:uint=Math.floor(mouseX/60);
if (!(((sr==pr+1||sr==pr-1)&&sc==pc)||((sc==pc+1||sc==pc-1)&&sr==pr))) {
pr=sr;
pc=sc;
s.x=60*pc;
s.y=60*pr;
s.visible=true;
} else {
swp(pr,pc,sr,sc);
if (rk(pr,pc)>2||ck(pr,pc)>2||rk(sr,sc)>2||ck(sr,sc)>2) {
th.text="";
cp=false;
getChildByName(pr+"_"+pc).x=sc*60;
getChildByName(pr+"_"+pc).y=sr*60;
getChildByName(pr+"_"+pc).name="t";
getChildByName(sr+"_"+sc).x=pc*60;
getChildByName(sr+"_"+sc).y=pr*60;
getChildByName(sr+"_"+sc).name=pr+"_"+pc;
getChildByName("t").name=sr+"_"+sc;
} else {
swp(pr,pc,sr,sc);
}
pr=-10;
pc=-10;
s.visible=false;
}
} else {
for (var i:uint=0; i<8; i++) {
for (var j:uint=0; j<8; j++) {
if (i<7) {
swp(i,j,i+1,j);
if ((rk(i,j)>2||ck(i,j)>2||rk(i+1,j)>2||ck(i+1,j)>2)) {
th.text = i.toString()+","+j.toString()+"->"+(i+1).toString()+","+j.toString();
}
swp(i,j,i+1,j);
}
if (j<7) {
swp(i,j,i,j+1);
if ((rk(i,j)>2||ck(i,j)>2||rk(i,j+1)>2||ck(i,j+1)>2) ) {
th.text = i.toString()+","+j.toString()+"->"+(i).toString()+","+(j+1).toString();
}
swp(i,j,i,j+1);
}
}
}
}
}
}
private function swp(r1:uint,c1:uint,r2:uint,c2:uint):void {
var t:uint=jw[r1][c1];
jw[r1][c1]=jw[r2][c2];
jw[r2][c2]=t;
}
private function rk(r:uint,c:uint):uint {
var u:uint=jw[r];
var stk:uint=1;
var t:int=c;
while (chk(u,r,t-1)) {
t--;
stk++;
}
t=c;
while (chk(u,r,t+1)) {
t++;
stk++;
}
return (stk);
}
private function ck(r:uint,c:uint):uint {
var u:uint=jw[r];
var stk:uint=1;
var t:int=r;
while (chk(u,t-1,c)) {
t--;
stk++;
}
t=r;
while (chk(u,t+1,c)) {
t++;
stk++;
}
return (stk);
}
private function chk(g:uint,r:int,c:int):Boolean {
if (jw[r]==null) {
return false;
}
if (jw[r]==null) {
return false;
}
return g==jw[r];
}
}
}
This class was compiled with “Compress movie” checked and “Include XMP metadata” unchecked. There’s still room to save some bytes, but not enough to develop power jewels I am afraid… anyway you are free to try to reduce the code some more.
Download the source code. What’s your best score?
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.