WELCOME TO RD-PROJECT WEB BLOG's

Tampilkan postingan dengan label pengantar teknologi game (tugas 6). Tampilkan semua postingan
Tampilkan postingan dengan label pengantar teknologi game (tugas 6). Tampilkan semua postingan

Minggu, 11 April 2010

Implementasi Source Code Game

//Author Jeremiah McLeod xdebugx
package rubixRedux;
import javax.microedition.rms.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import rubixRedux.About;
import rubixRedux.highScoresAlert;

public class rubixReduxCanvas extends Canvas {

private static long menuTimeStart;

public static int width,height;
private static Display myDisplay;

private static Image imgDoubleBuffer;
private static Graphics doubleBuffer;
private static boolean buffered;
private static Graphics saved;
private static int buttonWidth,buttonHeight,buttonX,buttonY;
private static boolean showBtnBack,showBtnMenu;
private static Image btnBack,btnMenu;
rubixRedux myRubixRedux;

public boolean go,start,loading,scramble,menu,started,level,solved,gotName,showHighS,savePrompt,loadPrompt;

public long time1,timeDone;

private static Random myRandom;

private static boolean myInitialized;

private static boolean higher;
private static RecordStore db;

private static int moves,diffLevel,score;
public long timeStart;
private static boolean about=false,found;

public String highName;

private static int reso,splX,splY,curTime,curH,curM,curS,selX,selY,selInc,selWidth,selHeight,levelToShow;
private static String highSName[], highSScore[], highSMoves[], highSTime[];
private static int highSValue[];

private static Font FONT;

private static int headerHeight,boardY;

private static int selector;

private static Image sqImage [],sq2Image [];

private static int boardWidth,boardHeight,lenBoard,squareWidth,squareHeight,spotX,spotY,scrambleSize;
private static int board[];

private static int left,right,up,down,fire;

private static boolean boardLatLon;

private static Image splash;

public rubixReduxCanvas(rubixRedux midlet) {
myDisplay = Display.getDisplay(midlet);
myRubixRedux = midlet;
myInitialized=false;
go=false;
}

void start() {
myDisplay.setCurrent(this);
repaint();
}

//-------------------------------------------------------
// graphics methods

/**
* paint the game graphic on the screen.
*/
public void paint(Graphics g) {
int p,t,a;

// perform the calculations if necessary:
if(!myInitialized) {

myRubixRedux.setFullScreen ();

if (isDoubleBuffered()) buffered=true; else buffered=false;

showBtnBack=false;
showBtnMenu=false;
about=false;
go=false;
started=false;

left=getKeyCode(LEFT);
right=getKeyCode(RIGHT);
up=getKeyCode(UP);
down=getKeyCode(DOWN);
fire=getKeyCode(FIRE);
selector=0;
higher=false;
level=false;
highSName=new String [12];
highSScore=new String [12];
highSValue=new int[12];
highSMoves=new String [12];
highSTime=new String [12];

FONT = g.getFont();
if (g.getClipWidth()>width) width = g.getClipWidth();
if (g.getClipHeight()>height) height = g.getClipHeight();

buttonWidth=30;
buttonHeight=20;
buttonX=width-buttonWidth;
buttonY=height-buttonHeight;
headerHeight=height/7;
boardY=headerHeight;

if (buffered==false) {
imgDoubleBuffer=Image.createImage (width,height);
doubleBuffer=imgDoubleBuffer.getGraphics ();
}

reso=0;

int small=0; if (width>=height) small=height; else small=width;
if (small>160) reso=1;
if (small>196) reso=2;

if (reso==0) {
a=width-120;
if (a<0) a=0;
splX=0+(a/2);
a=height-90;
if (a<0) a=0;
splY=0+(a/2);
selX=splX+20;
selY=splY+20;
selInc=12;
selWidth=80;
selHeight=10;
}
if (reso==1) {
a=width-170;
if (a<0) a=0;
splX=0+(a/2);
a=height-170;
if (a<0) a=0;
splY=0+(a/2);
selX=splX+31;
selY=splY+41;
selInc=22;
selWidth=111;
selHeight=18;
}
if (reso==2) {
a=width-200;
if (a<0) a=0;
splX=0+(a/2);
a=height-200;
if (a<0) a=0;
splY=0+(a/2);
selX=splX+37;
selY=splY+49;
selInc=26;
selWidth=128;
selHeight=20;
}

myRandom = new Random(System.currentTimeMillis());

FONT=Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);

sqImage=new Image [6];
sq2Image = new Image [6];

loading=true;

//loadimages


myInitialized = true;
}

if (buffered==false) {
saved=g;
g=imgDoubleBuffer.getGraphics();
}
g.setFont (FONT);

if (loading==true) {
g.setColor (255,255,255);
g.fillRect (0,0,width,height);
g.setColor (255,0,0);
g.drawString ("Loading",(width/2)-(g.getFont().stringWidth("Loading")/2),height/2,0);
}

if (go==true) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (200,100,225);
g.fillRect (0,0,width,headerHeight);
g.fillRect (boardWidth*squareWidth,0,width,height);
g.fillRect (0,headerHeight+(squareHeight*boardHeight),width,height);

for (p=0;p

g.setColor (255,255,255);
if (boardLatLon==false) {
g.drawRect (0,headerHeight+(spotY*squareHeight),squareWidth*boardWidth-1,squareHeight);
g.drawRect (1,headerHeight+(spotY*squareHeight)+1,(squareWidth*boardWidth)-3,squareHeight-2);
}
else {
g.drawRect (spotX*squareWidth,headerHeight,squareWidth,boardHeight*squareHeight-1);
g.drawRect (spotX*squareWidth+1,headerHeight+1,squareWidth-2,boardHeight*squareHeight-3);
}

//paint game here
g.setColor (255,255,255);
curTime=(int) ((System.currentTimeMillis()-timeStart)/1000);
g.drawString ("Moves "+moves,5,(headerHeight/2)-(FONT.getHeight()/2),0);
curH=curTime/3600;
curM=(curTime-(curH*3600))/60;
curS=curTime-(curH*3600)-(curM*60);
g.drawString ("Time "+curH+":"+curM+":"+curS,width-FONT.stringWidth ("Time "+curH+":"+curM+":"+curS)-5,(headerHeight/2)-(FONT.getHeight()/2),0);
}//gotrue

if (savePrompt==true) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (255,255,255);
g.drawString ("CHOOSE SAVE GAME SLOT",width/2-(FONT.stringWidth("CHOOSE SAVE GAME SLOT")/2),0,0);
g.drawString ("Will overwrite game,",width/2-(FONT.stringWidth("Will overwrite game,")/2),0+(FONT.getHeight()*1),0);
g.drawString ("in that slot.",width/2-(FONT.stringWidth("in that slot.")/2),0+(FONT.getHeight()*2),0);
g.drawString ("Press 1,2 or 3 for,",width/2-(FONT.stringWidth("Press 1,2 or 3 for,")/2),0+(FONT.getHeight()*3),0);
g.drawString ("save game slot #.",width/2-(FONT.stringWidth("save game slot #.")/2),0+(FONT.getHeight()*4),0);
}

if (loadPrompt==true) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (255,255,255);
g.drawString ("CHOOSE SLOT TO LOAD",width/2-(FONT.stringWidth("CHOOSE SLOT TO LOAD")/2),0,0);
g.drawString ("Press 1,2 or 3 for,",width/2-(FONT.stringWidth("Press 1,2 or 3 for,")/2),0+(FONT.getHeight()*1),0);
g.drawString ("save game slot to load.",width/2-(FONT.stringWidth("save game slot to load.")/2),0+(FONT.getHeight()*2),0);
}

if (solved==true && menu==false) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (200,100,225);
g.fillRect (0,0,width,headerHeight);
g.fillRect (boardWidth*squareWidth,0,width,height);
g.fillRect (0,headerHeight+(squareHeight*boardHeight),width,height);

for (p=0;p
g.setColor (255,255,255);
if (boardLatLon==false) {
g.drawRect (0,headerHeight+(spotY*squareHeight),squareWidth*boardWidth-1,squareHeight);
g.drawRect (1,headerHeight+(spotY*squareHeight)+1,(squareWidth*boardWidth)-3,squareHeight-2);
}
else {
g.drawRect (spotX*squareWidth,headerHeight,squareWidth,boardHeight*squareHeight-1);
g.drawRect (spotX*squareWidth+1,headerHeight+1,squareWidth-2,boardHeight*squareHeight-3);
}

//paint game here
g.setColor (255,255,255);
g.drawString ("Moves "+moves,5,(headerHeight/2)-(FONT.getHeight()/2),0);
curTime=(int) (timeDone);
curH=curTime/3600;
curM=(curTime-(curH*3600))/60;
curS=curTime-(curH*3600)-(curM*60);

g.drawString ("Time "+curH+":"+curM+":"+curS,width-FONT.stringWidth ("Time "+curH+":"+curM+":"+curS)-5,(headerHeight/2)-(FONT.getHeight()/2),0);
g.setColor (getRandomInt (255),getRandomInt (255),getRandomInt (255));
g.drawString ("Solved",0+(width/2)-(FONT.stringWidth ("Solved")/2),0+(height/2)-FONT.getHeight(),0);
g.drawString ("Score: "+score,0+(width/2)-(FONT.stringWidth ("Score: "+score)/2),0+(height/2)+FONT.getHeight(),0);
}//gotrue

if (scramble==true) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (200,100,225);
g.fillRect (0,0,width,headerHeight);
g.fillRect (boardWidth*squareWidth,0,width,height);
g.fillRect (0,headerHeight+(squareHeight*boardHeight),width,height);

for (p=0;p
g.setColor (getRandomInt (255),getRandomInt (255),getRandomInt (255));
g.drawString ("Scrambling",width/2-(FONT.stringWidth("Scrambling")/2),height/2-(FONT.getHeight()/2),0);
}

if (showHighS==true) {

highScoresAlert.copyright="Rubix Redux! Copyright (c) 2007 XdebugX All rights reserved.\n\n"
+"Visit me at http://www.xdebugx.net for more free mobile games.\n\n\n"+"HIGH SCORES\n\n"
+"Level 1:\n"+highSName[0]+" Score:"+highSScore[0]+" Moves:"+highSMoves[0]+" Time:"+highSTime[0]+"\n"+highSName[1]+" Score:"+highSScore[1]+" Moves:"+highSMoves[1]+" Time:"+highSTime[1]+"\n"+highSName[2]+" Score:"+highSScore[2]+" Moves:"+highSMoves[2]+" Time:"+highSTime[2]+"\n\n"
+"Level 2:\n"+highSName[3]+" Score:"+highSScore[3]+" Moves:"+highSMoves[3]+" Time:"+highSTime[3]+"\n"+highSName[4]+" Score:"+highSScore[4]+" Moves:"+highSMoves[4]+" Time:"+highSTime[4]+"\n"+highSName[5]+" Score:"+highSScore[5]+" Moves:"+highSMoves[5]+" Time:"+highSTime[5]+"\n\n"
+"Level 3:\n"+highSName[6]+" Score:"+highSScore[6]+" Moves:"+highSMoves[6]+" Time:"+highSTime[6]+"\n"+highSName[7]+" Score:"+highSScore[7]+" Moves:"+highSMoves[7]+" Time:"+highSTime[7]+"\n"+highSName[8]+" Score:"+highSScore[8]+" Moves:"+highSMoves[8]+" Time:"+highSTime[8]+"\n\n"
+"Level 4:\n"+highSName[9]+" Score:"+highSScore[9]+" Moves:"+highSMoves[9]+" Time:"+highSTime[9]+"\n"+highSName[10]+" Score:"+highSScore[10]+" Moves:"+highSMoves[10]+" Time:"+highSTime[10]+"\n"+highSName[11]+" Score:"+highSScore[11]+" Moves:"+highSMoves[11]+" Time:"+highSTime[11]+"\n\n";


highScoresAlert.showHighScoresAlert(Display.getDisplay(myRubixRedux));
menu=true;
showHighS=false;

}

if (level==true) {
g.setColor (0,0,0);
g.fillRect (0,0,width,height);
g.setColor (255,255,255);
g.drawString ("Press 1, 2, 3, or 4",0+((width-FONT.stringWidth("Press 1, 2, 3 or 4"))/2),(height/2)-(FONT.getHeight()),0);
g.drawString ("For difficulty level",0+((width-FONT.stringWidth("For difficulty level"))/2),(height/2)+(FONT.getHeight()),0);
}

if (menu==true) {
g.setColor (40,40,80);
g.fillRect (0,0,width,height);
g.drawImage (splash,splX,splY,0);
g.setColor (getRandomInt (255),getRandomInt (255),getRandomInt (255));
g.drawRect (selX,selY+(selInc*selector),selWidth,selHeight);
g.drawRect (selX-1,selY+(selInc*selector)-1,selWidth+2,selHeight+2);
}

if (about==true) {
About.showAbout(Display.getDisplay(myRubixRedux));
menu=true;
about=false;
myRubixRedux.setFullScreen();
}

if (showBtnMenu==true) g.drawImage (btnMenu,buttonX,buttonY,0);
if (showBtnBack==true) g.drawImage (btnBack,buttonX,buttonY,0);

if (buffered==false) saved.drawImage (imgDoubleBuffer,0,0,0);

} //paint

public void advance() {
int p,t;


if (loading==true) {
loadImages();
loading=false;
menu=true;
}

if (scramble==true) {
int a,b,c,spotX,spotY,temp;
scrambleSize--;

a=getRandomInt (2);
b=getRandomInt (2);
c=getRandomInt (boardWidth);
if(a==0) {
if (b==0) {
spotY=c;
temp=board[spotY*boardWidth];

for (p=0;p board[(spotY*boardWidth)+p]=board[(spotY*boardWidth)+p+1];
board[(spotY*boardWidth)+boardWidth-1]=temp;
}

}
if(a==0) {
if (b==1) {
spotY=c;
temp=board[boardWidth-1+(spotY*boardWidth)];

for (p=boardWidth-1;p>0;p--)
board[(spotY*boardWidth)+p]=board[(spotY*boardWidth)+p-1];
board[spotY*boardWidth]=temp;
}
}
if(a==1) {
if (b==0) {
spotX=c;
temp=board[spotX];

for (p=0;p board[spotX+(p*boardWidth)]=board[spotX+((p+1)*boardWidth)];
board[spotX+((boardHeight-1)*boardWidth)]=temp;
}
}

if(a==1) {
if (b==1) {
spotX=c;
temp=board[spotX+((boardHeight-1)*boardWidth)];

for (p=boardHeight-1;p>0;p--)
board[spotX+(p*boardWidth)]=board[spotX+((p-1)*boardWidth)];
board[spotX]=temp;
}
}

if (scrambleSize<0) {
found=true;
for (p=0;pif (found==false) {
found=true;
for (p=1;p}

if (found==true) scrambleSize=10; else {

scramble=false;
go=true;
started=true;
spotX=boardWidth/2;
spotY=boardHeight/2;
boardLatLon=false;
timeStart=System.currentTimeMillis();
}//found
}//scramble size
}//scramble

if (go==true) {
found=true;
for (p=0;pif (found==false) {
found=true;
for (p=1;p}

if (found==true) {
solved=true;
started=false;
gotName=false;

go=false;
timeDone=(System.currentTimeMillis()-timeStart)/1000;
score=(moves*5)+(int)(timeDone);
higher=false;
getHighScores ();
for (p=0+(diffLevel*3);p<0+(diffLevel*3)+3;p++) {
if (score higher=true;

myRubixRedux.getName ();
}
}
}

}//go

if (higher==true) {
if (gotName==true) {
for (p=0+(diffLevel*3);p<0+(diffLevel*3)+3;p++) if (score for (t=((diffLevel+1)*3)-1;t>p;t--) {
highSName[t]=highSName[t-1];
highSScore[t]=highSScore[t-1];
highSValue[t]=highSValue[t-1];
highSTime[t]=highSTime[t-1];
highSMoves[t]=highSMoves[t-1];
}
highSName[p]=highName;
highSScore[p]=""+score;
highSValue[p]=score;
highSTime[p]=""+(int) (timeDone);
highSMoves[p]=""+moves;
p=10000;
}

if (diffLevel==0) {
try {
db=RecordStore.openRecordStore("level1", true);
writeHighS (db,0);
closeRecStore (db);
showHighS=true;
solved=false;
higher=false;
gotName=false;

} catch (Exception e) {System.out.println (e);}

}//difflevel0

if (diffLevel==1) {
try {

db=RecordStore.openRecordStore("level2", true);
writeHighS (db,3);
closeRecStore (db);
showHighS=true;
solved=false;
higher=false;
gotName=false;

} catch (Exception e) {System.out.println (e);}

}//difflevel0

if (diffLevel==2) {
try {
db=RecordStore.openRecordStore("level3", true);
writeHighS (db,6);
closeRecStore (db);
showHighS=true;
higher=false;
gotName=false;
solved=false;

} catch (Exception e) {System.out.println (e);}

}//difflevel0

if (diffLevel==3) {
try {
db=RecordStore.openRecordStore("level4", true);
writeHighS (db,9);
closeRecStore (db);
showHighS=true;
higher=false;
gotName=false;
solved=false;

} catch (Exception e) {System.out.println (e);}

}//difflevel0

}//got name
}//higher

repaint ();
}

private static void getHighScores () {
int p=0,t=0;

for (p=0;p<12;p++) {
highSName[p]="none";
highSScore[p]="9999999";
highSValue[p]=9999999;
highSTime[p]=" ";
highSMoves[p]=" ";

}
try {

db=RecordStore.openRecordStore("level1", true);
readRecords (db,0);
closeRecStore(db);

db=RecordStore.openRecordStore("level2", true);
readRecords (db,3);
closeRecStore(db);

db=RecordStore.openRecordStore("level3", true);
readRecords (db,6);
closeRecStore(db);

db=RecordStore.openRecordStore("level4", true);
readRecords (db,9);
closeRecStore(db);

} catch (Exception e) {System.out.println (""+e+" getHighs");}

}///gethighscores

private static void writeHighS (RecordStore recStore,int pOffset) {
int i=1;
for (int p=0+(pOffset);p<0+pOffset+3;p++) {

try
{

String tempp=highSName[p];
byte[] rec = tempp.getBytes();

if (recStore.getNextRecordID()>i) {
recStore.setRecord(i++,rec, 0, rec.length);
} else {
recStore.addRecord (rec,0,rec.length);
i++;
}

tempp=highSScore[p];
rec = tempp.getBytes();

if (recStore.getNextRecordID()>i) {
recStore.setRecord(i++,rec, 0, rec.length);
} else {
recStore.addRecord (rec,0,rec.length);
i++;
}

tempp=highSMoves[p];
rec = tempp.getBytes();

if (recStore.getNextRecordID()>i) {
recStore.setRecord(i++,rec, 0, rec.length);
} else {
recStore.addRecord (rec,0,rec.length);
i++;
}

tempp=highSTime[p];
rec = tempp.getBytes();

if (recStore.getNextRecordID()>i) {
recStore.setRecord(i++,rec, 0, rec.length);
} else {
recStore.addRecord (rec,0,rec.length);
i++;
}

}
catch (Exception e) {
System.err.println(e.toString());
}
}

}//write high s

public static void readRecords(RecordStore recStore,int pOffset)
{
try
{
// Intentionally small to test code below
byte[] recData = new byte[5];
int len;
int a=0;

for (int i = 1; i <= recStore.getNumRecords(); i++)
{
// Allocate more storage if necessary

if (recStore.getRecordSize(i) > recData.length)
recData = new byte[recStore.getRecordSize(i)];
len = recStore.getRecord(i++, recData, 0);
String tempp=new String(recData, 0, len);
highSName[a+pOffset]=tempp;

if (recStore.getRecordSize(i) > recData.length)
recData = new byte[recStore.getRecordSize(i)];
len = recStore.getRecord(i++, recData, 0);
tempp=new String(recData, 0, len);
highSScore[a+pOffset]=tempp;
highSValue[a+pOffset]=Integer.parseInt(highSScore[a+pOffset]);

if (recStore.getRecordSize(i) > recData.length)
recData= new byte[recStore.getRecordSize(i)];
len = recStore.getRecord(i++, recData, 0);
tempp=new String(recData, 0, len);
highSMoves[a+pOffset]=tempp;

if (recStore.getRecordSize(i) > recData.length)
recData = new byte[recStore.getRecordSize(i)];
len = recStore.getRecord(i, recData, 0);
tempp=new String(recData, 0, len);
highSTime[a+pOffset]=tempp;

a++;

}//i
}//try
catch (Exception e)
{
System.err.println(e.toString()+"reading");
}
} //readrecords

private static void closeRecStore(RecordStore recStore)
{
try
{
recStore.closeRecordStore();
}
catch (Exception e)
{
System.err.println(e.toString());
}
}

public void keyReleased (int keyCode) {

}//keyReleased

public void keyPressed (int keyCode) {
int oldSpotX,oldSpotY,temp,p,t;
if (keyCode==-7 || keyCode==-21) {
if (solved==true) {
menu=!menu;
solved=false;
showBtnBack=false;
showBtnMenu=false;
}else

if (go==true) {
go=false;
menu=true;
showBtnBack=true;
showBtnMenu=false;
menuTimeStart=System.currentTimeMillis();
} else
if (menu==true) {
if (started==true) {
showBtnMenu=true;
showBtnBack=false;

go=true;
menu=false;
timeStart=timeStart+(System.currentTimeMillis()-menuTimeStart);
}
} else

if (level==true)
{
if (started==false) {
showBtnBack=false;
showBtnMenu=true;
go=false;
menu=true;
level=false;
} else {
menu=true;
level=false;
showBtnBack=true;
showBtnMenu=false;
}
} else

if (savePrompt==true) {
savePrompt=false;
menu=true;
if (started==true) showBtnBack=true;
} else

if (loadPrompt==true) {
loadPrompt=false;
menu=true;
if (started==true) showBtnBack=true;
}
}
if (go==true) {

if(keyCode==-3 || keyCode==KEY_NUM4 || keyCode==-13 || keyCode==LEFT || keyCode==left) {
if (boardLatLon==false) {
temp=board[spotY*boardWidth];

for (p=0;p board[(spotY*boardWidth)+p]=board[(spotY*boardWidth)+p+1];
board[(spotY*boardWidth)+boardWidth-1]=temp;
moves=moves+1;
} else {
spotX=spotX-1;
if (spotX<0) {
boardLatLon=false;
spotY=0;
}
}

//handle left
}
if(keyCode==-4 || keyCode==KEY_NUM6 || keyCode==-12 || keyCode==RIGHT || keyCode==right) {
if (boardLatLon==false) {
temp=board[boardWidth-1+(spotY*boardWidth)];

for (p=boardWidth-1;p>0;p--)
board[(spotY*boardWidth)+p]=board[(spotY*boardWidth)+p-1];
board[spotY*boardWidth]=temp;
moves=moves+1;

} else {
spotX=spotX+1;
if (spotX>=boardWidth) {
boardLatLon=false;
spotY=boardHeight-1;
}
}
}

if(keyCode==-1 || keyCode==KEY_NUM2 || keyCode==-10 || keyCode==UP || keyCode==up) {
if (boardLatLon==true) {
temp=board[spotX];

for (p=0;p board[spotX+(p*boardWidth)]=board[spotX+((p+1)*boardWidth)];
board[spotX+((boardHeight-1)*boardWidth)]=temp;
moves=moves+1;
} else {

spotY=spotY-1;
if (spotY<0) {
boardLatLon=true;
spotX=0;
}
}
}

if(keyCode==-2 || keyCode==KEY_NUM8 || keyCode==-11 || keyCode==DOWN || keyCode==down) {
if (boardLatLon==true) {
temp=board[spotX+((boardHeight-1)*boardWidth)];

for (p=boardHeight-1;p>0;p--)
board[spotX+(p*boardWidth)]=board[spotX+((p-1)*boardWidth)];
board[spotX]=temp;
moves=moves+1;
} else {

spotY++;
if (spotY>=boardHeight) {
boardLatLon=true;
spotX=boardWidth-1;
}
}
}
if (keyCode==-5 || keyCode==KEY_NUM5 || keyCode==-14 || keyCode==FIRE || keyCode==fire) {
spotX=boardWidth/2;
spotY=boardHeight/2;
boardLatLon=!boardLatLon;
}
} else //go =true

if (menu==true) {

if(keyCode==-2 || keyCode==KEY_NUM8 || keyCode==-11 || keyCode==DOWN || keyCode==down) {
selector++; if (selector>5) selector=0;
}

if(keyCode==-1 || keyCode==KEY_NUM2 || keyCode==-10 || keyCode==UP || keyCode==up) {
selector--; if (selector<0) selector=5;
}

if (keyCode==-5 || keyCode==KEY_NUM5 || keyCode==-14 || keyCode==FIRE || keyCode==fire) {
if (selector==0) about=true;
if (selector==1) {
level=true;
loadPrompt=false;
savePrompt=false;
menu=false;
showBtnMenu=true;
}

if (selector==2) {
if (started==true) {
savePrompt=true;
go=false;
menu=false;
showBtnMenu=true;
showBtnBack=false;
}
}//selector 2

if (selector==3) {
loadPrompt=true;
go=false;
menu=false;
showBtnMenu=true;
showBtnBack=false;
}//selector 2

if (selector==4) {
getHighScores ();
menu=false;
showHighS=true;

}
if (selector==5) {
try {
myRubixRedux.destroyApp(false);
myRubixRedux.notifyDestroyed();
} catch (Exception ex) {
}
}//selector==5
}

} else //menu

if (level==true) {
if (keyCode==KEY_NUM1 || keyCode==-2 || keyCode==-11 || keyCode==DOWN || keyCode==down) {
newGame (0);
}

if (keyCode==KEY_NUM2 || keyCode==-1 || keyCode==-10 || keyCode==UP || keyCode==up) {
newGame (1);
}
if (keyCode==KEY_NUM3 || keyCode==-5 || keyCode==-14 || keyCode==FIRE || keyCode==fire) {
newGame (2);
}

if (keyCode==KEY_NUM4 || keyCode==-3 || keyCode==-13 || keyCode==LEFT || keyCode==left) {
newGame (3);
}
} else //level

if (savePrompt==true) {
if (keyCode==KEY_NUM1 || keyCode==-2 || keyCode==-11 || keyCode==DOWN || keyCode==down) {
saveGame (0);
menu=true;
savePrompt=false;
showBtnMenu=false;
showBtnBack=true;
}

if (keyCode==KEY_NUM2 || keyCode==-1 || keyCode==-10 || keyCode==UP || keyCode==up) {
saveGame (1);
menu=true;
savePrompt=false;
showBtnMenu=false;
showBtnBack=true;

}
if (keyCode==KEY_NUM3 || keyCode==-5 || keyCode==-14 || keyCode==FIRE || keyCode==fire) {
saveGame (2);
menu=true;
savePrompt=false;
showBtnMenu=false;
showBtnBack=true;
}

}else //save prompt

if (loadPrompt==true) {
if (keyCode==KEY_NUM1 || keyCode==-2 || keyCode==-11 || keyCode==DOWN || keyCode==down) {
loadGame (0);
}

if (keyCode==KEY_NUM2 || keyCode==-1 || keyCode==-10 || keyCode==UP || keyCode==up) {
loadGame (1);
}

if (keyCode==KEY_NUM3 || keyCode==-5 || keyCode==-14 || keyCode==FIRE || keyCode==fire) {
loadGame (2);
}

}//load prompt

}//keys

private void saveGame (int slot) {
int p,t;
String record;
byte[] rec;
try {
if (slot==0) {
db=RecordStore.openRecordStore("saveGame1", true);
db.closeRecordStore ();
RecordStore.deleteRecordStore("saveGame1");
db=RecordStore.openRecordStore("saveGame1", true);
}
if (slot==1) {
db=RecordStore.openRecordStore("saveGame2", true);
db.closeRecordStore ();
RecordStore.deleteRecordStore("saveGame2");
db=RecordStore.openRecordStore("saveGame2", true);
}
if (slot==2) {
db=RecordStore.openRecordStore("saveGame3", true);
db.closeRecordStore ();
RecordStore.deleteRecordStore("saveGame3");
db=RecordStore.openRecordStore("saveGame3", true);
}

record=""+diffLevel;
rec = record.getBytes();
db.addRecord (rec,0,rec.length);
int tim=(int) (menuTimeStart-timeStart);
record=""+tim;
rec = record.getBytes();
db.addRecord (rec,0,rec.length);
record=""+moves;
rec = record.getBytes();
db.addRecord (rec,0,rec.length);

for (p=0;p record=""+board[p];
rec = record.getBytes();
db.addRecord (rec,0,rec.length);
}

db.closeRecordStore();
} catch (Exception e) {System.out.println (""+e+" saving game");}

}//save game

private void loadGame (int slot) {
int p,t;
boolean error=false;
try {
db=null;
if (slot==0) {
try {
db=RecordStore.openRecordStore("saveGame1", false);
}catch (RecordStoreNotFoundException d) { error=true;}
}
if (slot==1) {
try {
db=RecordStore.openRecordStore("saveGame2", false);
}catch (RecordStoreNotFoundException d) { error=true;}
}
if (slot==2) {
try {
db=RecordStore.openRecordStore("saveGame3", false);
}catch (RecordStoreNotFoundException d) { error=true;}
}
if (error==true) {
menu=true;
savePrompt=false;
} else {
byte[] recData = new byte[5];
int len;
String tempp;

if (db.getRecordSize(1) > recData.length) recData = new byte[db.getRecordSize(1)];
len = db.getRecord(1, recData, 0);
tempp=new String(recData, 0, len);
diffLevel=Integer.parseInt (tempp);

if (db.getRecordSize(2) > recData.length) recData = new byte[db.getRecordSize(2)];
len = db.getRecord(2, recData, 0);
tempp=new String(recData, 0, len);
timeStart=System.currentTimeMillis()-(long) (Integer.parseInt (tempp));

if (db.getRecordSize(3) > recData.length) recData = new byte[db.getRecordSize(3)];
len = db.getRecord(3, recData, 0);
tempp=new String(recData, 0, len);
moves=Integer.parseInt (tempp);

int size=diffLevel;

if (size==0) {
boardWidth=3;
boardHeight=3;
}

if (size==1) {
boardWidth=4;
boardHeight=4;
}

if (size==2) {
boardWidth=8;
boardHeight=8;
}

if (size==3) {
boardWidth=12;
boardHeight=12;
}

lenBoard=boardWidth*boardHeight;
board=new int [lenBoard];

for (p=0;p squareWidth=width/boardWidth;
squareHeight=(height-boardY)/boardHeight;

if (size!=1) {
sq2Image[0]=scaleImageFast (sqImage[0],squareWidth,squareHeight);
sq2Image[1]=scaleImageFast (sqImage[1],squareWidth,squareHeight);
sq2Image[2]=scaleImageFast (sqImage[2],squareWidth,squareHeight);
if (size>0) sq2Image[3]=scaleImageFast (sqImage[3],squareWidth,squareHeight);
if (size>2) sq2Image[4]=scaleImageFast (sqImage[4],squareWidth,squareHeight);
if (size>2) sq2Image[5]=scaleImageFast (sqImage[5],squareWidth,squareHeight);
} else {

sq2Image[0]=scaleImageFast (sqImage[5],squareWidth,squareHeight);
sq2Image[1]=scaleImageFast (sqImage[4],squareWidth,squareHeight);
sq2Image[2]=scaleImageFast (sqImage[3],squareWidth,squareHeight);
sq2Image[3]=scaleImageFast (sqImage[1],squareWidth,squareHeight);
}

spotX=boardWidth/2;
spotY=boardHeight/2;
boardLatLon=false;


int a=0;
if (diffLevel==0) a=3*3;
if (diffLevel==1) a=4*4;
if (diffLevel==2) a=8*8;
if (diffLevel==3) a=12*12;

for (p=4;p<4+a;p++) {
if (db.getRecordSize(p) > recData.length) recData = new byte[db.getRecordSize(p)];
len = db.getRecord(p, recData, 0);
tempp=new String(recData, 0, len);
board[p-4]=Integer.parseInt (tempp);
}

db.closeRecordStore();
size=diffLevel;
loadPrompt=false;
solved=false;
showHighS=false;
started=true;
scramble=false;
menu=false;
go=true;

}//error true

} catch (Exception e) {System.out.println (""+e);};
} //load game


private void newGame (int size) {
go=false;
level=false;
solved=false;
showBtnBack=false;
showBtnMenu=true;
int p,t,a;
myRandom = new Random(System.currentTimeMillis());
moves=0;
timeStart=0;
diffLevel=size;

if (size==0) {
boardWidth=3;
boardHeight=3;
}

if (size==1) {
boardWidth=4;
boardHeight=4;
}

if (size==2) {
boardWidth=8;
boardHeight=8;
}

if (size==3) {
boardWidth=12;
boardHeight=12;
}

lenBoard=boardWidth*boardHeight;
board=new int [lenBoard];

squareWidth=width/boardWidth;
squareHeight=(height-boardY)/boardHeight;
for (p=0;p
if (size<3) for (p=0;pif (size!=1) {
sq2Image[0]=scaleImageFast (sqImage[0],squareWidth,squareHeight);
sq2Image[1]=scaleImageFast (sqImage[1],squareWidth,squareHeight);
sq2Image[2]=scaleImageFast (sqImage[2],squareWidth,squareHeight);
if (size>0) sq2Image[3]=scaleImageFast (sqImage[3],squareWidth,squareHeight);
if (size>2) sq2Image[4]=scaleImageFast (sqImage[4],squareWidth,squareHeight);
if (size>2) sq2Image[5]=scaleImageFast (sqImage[5],squareWidth,squareHeight);
} else {

sq2Image[0]=scaleImageFast (sqImage[5],squareWidth,squareHeight);
sq2Image[1]=scaleImageFast (sqImage[4],squareWidth,squareHeight);
sq2Image[2]=scaleImageFast (sqImage[3],squareWidth,squareHeight);
sq2Image[3]=scaleImageFast (sqImage[1],squareWidth,squareHeight);
}

spotX=boardWidth/2;
spotY=boardHeight/2;
boardLatLon=false;
scrambleSize=(size+1)*55;
menu=false;
scramble=true;
solved=false;
showHighS=false;
}

public int getRandomInt(int upper) {
int retVal = myRandom.nextInt() % upper;
if(retVal < 0) {
retVal += upper;
}
return(retVal);
}

private static void loadImages () {
int p,t;

try {

sqImage[0]=Image.createImage ("/sq1.png");
sqImage[1]=Image.createImage ("/sq2.png");
sqImage[2]=Image.createImage ("/sq3.png");
sqImage[3]=Image.createImage ("/sq4.png");
sqImage[4]=Image.createImage ("/sq5.png");
sqImage[5]=Image.createImage ("/sq6.png");
if (reso==0) splash=Image.createImage ("/menuLR.png");
if (reso==1) splash=Image.createImage ("/menuMR.png");
if (reso==2) splash=Image.createImage ("/menuHR.png");
btnMenu=Image.createImage ("/btnMenu.png");
btnBack=Image.createImage ("/btnBack.png");

} catch (Exception e) {System.out.println ("Error loading images: "+e);}

}// loadimages

private static Image scaleImageFast (Image originalImage, int width, int height) {
//if (height <= 0 || width <= 0)
//throw new Exception("ERROR_INVALID_ARGUMENT");
if (originalImage == null)
return Image.createImage(height, width);
int orig_h = originalImage.getHeight();
int orig_w = originalImage.getWidth();
if (orig_h==height && orig_w==width) return originalImage;
//if (orig_w == width && orig_h == height)
// return originalImage;
//int index;
//int loc, oldloc = 0;
Image newImage = Image.createImage (width,height);

Graphics newImageG=newImage.getGraphics();
//long before = System.currentTimeMillis();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//System.arraycopy(orig,index,newbuffer,loc,1);
// newbuffer[loc] = orig[index];
newImageG.setClip (j,i,1,1);
// newImageG.drawRegion (originalImage,(int) (j*orig_w/width),(int) (i*orig_h/height),1,1,Sprite.TRANS_NONE,j,i,0);
newImageG.drawImage (originalImage,j-(int) (j*orig_w/width),i-(int) (i*orig_h/height),0);
}
}
//System.out.println("time:"+(System.currentTimeMillis()-before));
//Image newImage = Image.createRGBImage(newbuffer, width, height, false);
// orig = null;
// newbuffer = null;
originalImage=null;
System.gc(); //
return newImage;
}
}
Read More..
free counters