/*
Author : RK
Description : A Simple Game to Move Towards a Particaluar @ symbol and Increase the Score
Date : 23/05/2010
*/
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MyFirstGame extends MIDlet
{
Display disp=null;
public void startApp()
{
disp=Display.getDisplay(this);
MyGame game=new MyGame(this);
game.start();
disp.setCurrent(game);
}
public void pauseApp()
{
}
public void destroyApp(boolean condition)
{
}
public void exitMidlet()
{
System.gc();
destroyApp(false);
notifyDestroyed();
}
}
class MyGame extends GameCanvas implements Runnable,CommandListener
{
boolean isPlay;
long delay;
int timer=100;
int curX,curY;
int rX,rY;
int width;
int height;
int color=1;
Form form=null;
Command exit=null;
Command spdCmd=null;
MyFirstGame main=null;
String line="('.')";
int keyStates;
String symbol="@";
int score=0;
int speed=3;
Random r=null;
int time=0;
public MyGame(MyFirstGame main)
{
super(true);
this.main=main;
width=getWidth();
height=getHeight();
curX=width/2;
curY=height/2;
r=new Random();
rX=r.nextInt(width-5);
rY=r.nextInt(height-5);
delay=20;
form=new Form("");
exit=new Command("Exit",Command.EXIT,1);
spdCmd=new Command("Speed++",Command.OK,0);
addCommand(exit);
addCommand(spdCmd);
setCommandListener(this);
}
public void start()
{
isPlay=true;
Thread t=new Thread(this);
t.start();
}
public void stop()
{
isPlay=false;
}
public void run()
{
Graphics g=getGraphics();
while(isPlay)
{
input();
drawScreen(g);
try
{
Thread.sleep(delay);
time++;
if((curX>=rX-5 && curX<=rX+5) && (curY>=rY-5 && curY<=rY+5))
{
score=score+(curX+curY);
time=timer+10;
delay=0;
}
if(time>timer)
{
time=0; rY=r.nextInt(height-5);
rX=r.nextInt(width-5);
delay=20;
}
}catch(InterruptedException e){}
}
}
public void input()
{
keyStates=getKeyStates();
switch(keyStates)
{
case LEFT_PRESSED:
curX=Math.max(0,curX-speed);
break;
case RIGHT_PRESSED:
curX=Math.min(width,curX+speed); break;
case UP_PRESSED:
curY=Math.max(0,curY-speed);
break;
case DOWN_PRESSED:
curY=Math.min(height-1,curY+speed);
break;
}
}
public void drawScreen(Graphics g)
{
g.setColor(57,154,222);
g.fillRect(0,0,width,height);
g.setColor(255,255,255);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString(line,curX,curY,Graphics.TOP|Graphics.LEFT);
g.drawString(symbol,rX,rY,Graphics.TOP|Graphics.LEFT);
g.drawString("Score : "+score+" Speed : "+speed,1,1,Graphics.TOP|Graphics.LEFT);
g.drawString("[RK]",1,height-27,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
public void putSymbol(Graphics g)
{
g.fillRect(0,0,width,height);
g.setColor(255,255,255);
g.drawString(symbol,rX,rY,Graphics.TOP|Graphics.LEFT);
g.drawString(line,curX,curY,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
public void commandAction(Command c,Displayable d)
{
if(c==exit)
{
main.exitMidlet();
}
else
{
speed++;
if(speed>7)
{
speed=3;
timer=100;
delay=20;
}
else
{
timer=timer-9;
delay=delay-2;
}
}
}
}
Author : RK
Description : A Simple Game to Move Towards a Particaluar @ symbol and Increase the Score
Date : 23/05/2010
*/
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MyFirstGame extends MIDlet
{
Display disp=null;
public void startApp()
{
disp=Display.getDisplay(this);
MyGame game=new MyGame(this);
game.start();
disp.setCurrent(game);
}
public void pauseApp()
{
}
public void destroyApp(boolean condition)
{
}
public void exitMidlet()
{
System.gc();
destroyApp(false);
notifyDestroyed();
}
}
class MyGame extends GameCanvas implements Runnable,CommandListener
{
boolean isPlay;
long delay;
int timer=100;
int curX,curY;
int rX,rY;
int width;
int height;
int color=1;
Form form=null;
Command exit=null;
Command spdCmd=null;
MyFirstGame main=null;
String line="('.')";
int keyStates;
String symbol="@";
int score=0;
int speed=3;
Random r=null;
int time=0;
public MyGame(MyFirstGame main)
{
super(true);
this.main=main;
width=getWidth();
height=getHeight();
curX=width/2;
curY=height/2;
r=new Random();
rX=r.nextInt(width-5);
rY=r.nextInt(height-5);
delay=20;
form=new Form("");
exit=new Command("Exit",Command.EXIT,1);
spdCmd=new Command("Speed++",Command.OK,0);
addCommand(exit);
addCommand(spdCmd);
setCommandListener(this);
}
public void start()
{
isPlay=true;
Thread t=new Thread(this);
t.start();
}
public void stop()
{
isPlay=false;
}
public void run()
{
Graphics g=getGraphics();
while(isPlay)
{
input();
drawScreen(g);
try
{
Thread.sleep(delay);
time++;
if((curX>=rX-5 && curX<=rX+5) && (curY>=rY-5 && curY<=rY+5))
{
score=score+(curX+curY);
time=timer+10;
delay=0;
}
if(time>timer)
{
time=0; rY=r.nextInt(height-5);
rX=r.nextInt(width-5);
delay=20;
}
}catch(InterruptedException e){}
}
}
public void input()
{
keyStates=getKeyStates();
switch(keyStates)
{
case LEFT_PRESSED:
curX=Math.max(0,curX-speed);
break;
case RIGHT_PRESSED:
curX=Math.min(width,curX+speed); break;
case UP_PRESSED:
curY=Math.max(0,curY-speed);
break;
case DOWN_PRESSED:
curY=Math.min(height-1,curY+speed);
break;
}
}
public void drawScreen(Graphics g)
{
g.setColor(57,154,222);
g.fillRect(0,0,width,height);
g.setColor(255,255,255);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString(line,curX,curY,Graphics.TOP|Graphics.LEFT);
g.drawString(symbol,rX,rY,Graphics.TOP|Graphics.LEFT);
g.drawString("Score : "+score+" Speed : "+speed,1,1,Graphics.TOP|Graphics.LEFT);
g.drawString("[RK]",1,height-27,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
public void putSymbol(Graphics g)
{
g.fillRect(0,0,width,height);
g.setColor(255,255,255);
g.drawString(symbol,rX,rY,Graphics.TOP|Graphics.LEFT);
g.drawString(line,curX,curY,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
public void commandAction(Command c,Displayable d)
{
if(c==exit)
{
main.exitMidlet();
}
else
{
speed++;
if(speed>7)
{
speed=3;
timer=100;
delay=20;
}
else
{
timer=timer-9;
delay=delay-2;
}
}
}
}
No comments:
Post a Comment