Mon Feb 11, 2008 6:12 pm
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Transform;
public class GameLoop extends Canvas{
private Graphics3D iG3D;
private Camera iCamera;
private Light iLight;//A scene graph node that represents different kinds of light sources
private float iAngle=-45.0f;
private Background iBackground= new Background();
private Ground myg;
private SnakeBody mybody;
private Snakecube3D[] cubarr;
private SnakeFood skfood;
private boolean gameMode;
private Transform transform;
private boolean gameStarted;
private String[] choices=new String[]{"Easy","Normal","Hard"};
private short ch;
private myThread gameThread;
/** Creates a new instance of myCan */
public GameLoop() {
transform = new Transform();
transform.postTranslate(0.0f,10.0f,0.0f);
transform.postRotate(-35.0f,1.0f,0.0f,0.0f);
/*Camera is looking down with angle 35
*
*/
setCommandListener(new CommandListener() {
public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType()==command.EXIT){
if(!gameMode)
MIDletMain.quitApp();
else {
mybody.GameOver=true;
}
}
}
});
try{
addCommand(new Command("Exit",Command.EXIT,1));
} catch(Exception e){
e.printStackTrace();
}
}
protected void paint(Graphics g) {
if(gameMode) {
if(mybody.GameOver) {
// Game ends
gameMode=false;
}
iG3D.bindTarget(g,true,Graphics3D.TRUE_COLOR | Graphics3D.DITHER);
Transform camtans = new Transform();
iG3D.resetLights();//Resest Lights Computations
iG3D.clear(iBackground);// Refresh the Background
iG3D.addLight(iLight,transform);
camtans.postTranslate(mybody.getXcamera(),mybody.getYcamera(),mybody.getZcamera());
camtans.postRotate(iAngle,1.0f,0.0f,0.0f);
iG3D.setCamera(iCamera,camtans);
iG3D.render(myg.getIVbgd(),myg.getIIbgd(),myg.getIAppearancegd(),myg.getTransform());
if(!mybody.isGamePause()&&gameStarted)
mybody.update(skfood);
for(int i=0;i<mybody.getLen();i++) {
iG3D.render(cubarr[i].getIVbcube(),cubarr[i].getIIbcube(),cubarr[i].getIAppearancecube(),cubarr[i].getTransform());
}
if(!skfood.checkEaten())
iG3D.render(skfood.getIVbcube(),skfood.getIIbcube(),skfood.getIAppearancecube(),skfood.getTransform());
iG3D.releaseTarget();
drawHeader(g);
if(!gameMode) {
gameStarted=false;
// gameMode=false;
try {
Thread.sleep(1000);
gameThread.setGamemode(false);
gameThread=null;//destroy thread;
} catch (InterruptedException ex) {
}
}
} else {
drawList(g);
}
}
public void drawList(Graphics g) {
g.fillRect(0,0,300,300);
g.setColor(0xFF0000);
g.fillRect(40,100+(ch*20),50,15);
g.setColor(0xFFFFFF);//white
for(int i=0;i<choices.length;i++)
g.drawString(choices[i],50,100+(i*20),0);
g.setColor(0xFF0000);
g.fillRect(30,105+(ch*20),10,5);
// g.drawString(choices[i],50,100+(i*20),0);
g.setColor(0x00FF00);
g.drawString("Snake3D Game",60,10,0);
}
public void drawHeader(Graphics g) {
g.setColor(0xFFFF00);
g.drawRect(10,10,60,15);
g.drawString("Score :"+mybody.getScore(),12,10,0);
g.setColor(0x00FF99);
if(mybody.GameOver) {
g.setColor(0xFF0000);
g.drawRect(155,10,70,15);
g.drawString("Game Over",160,10,0);
} else if(mybody.isGamePause()) {
g.drawRect(155,10,70,15);
g.drawString(" Paused",160,10,0);
} else if(gameStarted) {
g.drawRect(155,10,70,15);
g.drawString("Level :"+choices[ch],160,10,0);
} else {
g.drawString("Press any key to start",100,10,0);
}
}
protected void keyPressed(int keyCode) {
short action=(short)getGameAction(keyCode);
if(gameMode) {
if(gameMode)
gameStarted=true;
if(gameStarted) {
/**
* Game Mode
*
**/
if(mybody.canChange) {
if(action==RIGHT) {
mybody.setDirection((short)3);
} else if(action==LEFT) {
mybody.setDirection((short)4);
} else if(action==UP) {
mybody.setDirection((short)1);
} else if(action==DOWN) {
mybody.setDirection((short)2);
}
}
if(action==GAME_A) {
iAngle+=5f;
} else if(action==GAME_B) {
iAngle-=5f;
}
if(action==FIRE) {
mybody.setGamePause(!mybody.isGamePause());
}
}
} else {
/**
* Main Menu
*
**/
if(action==UP) {
if(ch>0)
ch-=1;
} else if(action==DOWN) {
if(ch<2)
ch+=1;
} else if(action==FIRE) {
{
gameMode=true;
gameStarted=false;
SnakeBody.GameOver=false;
SnakeBody.canChange=true;
SnakeBody.GamePause=false;
mybody=new SnakeBody();
skfood=new SnakeFood();
cubarr=mybody.getMyBody();
iG3D=Graphics3D.getInstance();
iCamera= new Camera();
iCamera.setPerspective(100.0f,(float)getWidth()/(float)getHeight(),1.0f,1500.0f);
iLight=new Light();
iLight.setColor(0xFFFFFFFF);
iLight.setIntensity(1.20f);
myg=new Ground();
iBackground.setColor(0x000000);
gameThread=new myThread((short)(230-(ch*70)));
gameThread.start();
mybody.setValue((short)(2*(ch+1)));
}
}
repaint();
}
}
class myThread extends Thread {
private short sleeptime;
private boolean gamemode;
myThread(short time) {
sleeptime=time;
gamemode=true;
}
public void run() {
while(gamemode) {
try {
repaint();
Thread.sleep(sleeptime);
} catch (InterruptedException ex) {
//ex.printStackTrace();
}
}
}
public void setGamemode(boolean gamemode) {
this.gamemode = gamemode;
}
}
}
Sat Mar 21, 2009 2:40 pm
Sat Mar 21, 2009 5:11 pm
Tue Mar 24, 2009 10:59 am
Sun Jan 20, 2013 9:02 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.