|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。
公司筹办做手机游戏方面的项目,第一次做DOJA相干的项目,实习了“贪吃蛇”这个游戏,现把本人做的源代码公布出来,但愿给才打仗DOJA的老手一点提醒和匡助。也接待人人提出更正。
//主运转类
packagegreedSnake;
importcom.nttdocomo.ui.Display;
importcom.nttdocomo.ui.IApplication;
publicclassGreedSnakeextendsIApplicationimplementsRunnable{
publicstaticfinalintGRID_WIDTH =20;
publicstaticfinalintGRID_HEIGHT=24;
publicstaticfinalintNODE_SIZE =10;
privateGrid grid=null;
privateFood food=null;
privateSnake snake=newSnake();;
privateboolean running;
privateint score;
privateint level;
privateint count;
privateint countMove;
privateint timeInterval;
//----------------------------------------
/**Setupgame.*/
publicvoidstart(){
reset();
grid=newGrid(GRID_WIDTH,GRID_HEIGHT,this);
Display.setCurrent(grid);
Threadrunner=newThread(this);
runner.start();
}
privatevoidreset(){
running=false;
score=0;
level=0;
count=0;
countMove=6;
timeInterval=200;
}
/**Runnableinterface.*/
publicvoidrun(){
food=grid.addFood();
grid.addSnake(snake);
grid.repaint();
for(;;){
if(food==null){
food=grid.addFood();
continue;
}
try{
Thread.sleep(timeInterval);
}catch(Exceptione){
break;
}
if(!running){
snake.move();
Nodehead=snake.getHead();
if(grid.overlapsWith(head)||(snake.ptInSnake(head))){
running=true;
continue;
}
if((head.x==food.getX())&&(head.y==food.getY())){
intscoreGet=(10000-200*countMove)/timeInterval;
score+=scoreGet>0?scoreGet:0;
countMove=0;
snake.eatFood(food);
grid.reduceFood(food);
food=null;
if(++count%5==0){
level++;
}
}else{
countMove++;
}
grid.repaint();
}else{
grid.setGameOver();
break;
}
}
grid.repaint();
}
publicintgetScore(){
returnscore;
}
/**Getthecurrentlevelofthegame.*/
publicintgetLevel(){
returnlevel;
}
/**Keyeventhandler.*/
publicvoidkeyPressed(intkey){
if(key==Display.KEY_SELECT){
grid.setGameOver();
}
if(key==Display.KEY_LEFT){
snake.setDirection(Snake.LEFT);
}
if(key==Display.KEY_RIGHT){
snake.setDirection(Snake.RIGHT);
}
if(key==Display.KEY_DOWN){
snake.setDirection(Snake.DOWN);
}
if(key==Display.KEY_UP){
snake.setDirection(Snake.UP);
}
}
}
//画面框架类
packagegreedSnake;
importcom.nttdocomo.ui.Canvas;
importcom.nttdocomo.ui.Display;
importcom.nttdocomo.ui.Font;
importcom.nttdocomo.ui.Frame;
importcom.nttdocomo.ui.Graphics;
//---------------------
/**
*Thisclassrepresentsthegridboxthatcatches
*thepiecesdroppingfromabove.Ifadroppedpiece
*fillsuponeormorehorizontallinesinthegrid,
*theselineswillberemovedfromit.
*/
publicclassGridextendsCanvas{
privateint width;
privateint height;
privateGreedSnake listener;
privateSnake snake;
privateFood food;
privateString pad="0000";
privateFont score_font;
privateFont game_over_font;
privateboolean blDisplay;
privateboolean game_over;
staticfinalint SOFT_LEFT =Frame.SOFT_KEY_1;
staticfinalint SOFT_RIGHT=Frame.SOFT_KEY_2;
/**
*Createanewinstanceofthisclass.
*@paramwitdththewidth(intiles)ofthisgrid
*@paramheighttheheight(intiles)ofthisgrid
*@paramscorethescoreobjecttokeeptrackofthescore
*@paramlistenerareferencetotheowningtetrisobject
*/
publicGrid(intwidth,intheight,GreedSnakelistener){
this.width =width;
this.height =height;
this.listener=listener;
reset();
setSoftLabel(SOFT_LEFT,"New");
setSoftLabel(SOFT_RIGHT,"Quit");
score_font =Font.getFont(Font.FACE_MONOSPACE|Font.SIZE_SMALL);
game_over_font=Font.getFont(Font.FACE_PROPORTIONAL|Font.SIZE_LARGE|Font.STYLE_BOLD);
}
/**Removeallpiecesfromthisinstance*/
privatevoidreset(){
synchronized(this){
food=null;
snake=null;
}
game_over=false;
blDisplay=false;
}
publicvoidsetGameOver(){
snake=null;
food=null;
game_over=true;
}
/**Getthewidth(intiles)ofthisinstance.*/
publicintgetGridWidth(){
returnwidth;
}
/**Gettheheight(intiles)ofthisinstance.*/
publicintgetGridHeight(){
returnheight;
}
/**Addthespecifiedpiecetothisinstance.*/
publicFoodaddFood(){
blDisplay=false;
intgs=GreedSnake.GRID_WIDTH;
if(snake==null){
this.food=Food.createRandomFood(gs,gs);
}else{
for(;;){
this.food=Food.createRandomFood(gs,gs);
NodenewNode=newNode(food.getX(),food.getY());
if(snake.ptInSnake(newNode)){
this.food=null;
continue;
}else{
break;
}
}
}
returnthis.food;
}
publicvoidaddSnake(Snakesnake){
this.snake=snake;
Nodehead=(Node)snake.getHead();
}
/**
*Returns<code>true</code>ifthespecified
*pieceoverlapswithanytileinthisinstance,
*<code>false</code>otherwise.
*/
publicbooleanoverlapsWith(Nodenode){
if((node.x<0)||(node.x>getGridWidth()-1)
||(node.y<0)||(node.y>getGridHeight()-1)){
returntrue;
}
returnfalse;
}
publicvoidreduceFood(Foodfood){
blDisplay=true;
}
privateStringformat(intn){
Stringraw=""+n;
returnpad.substring(0,4-raw.length())+raw;
}
/**Paintthisinstanceontothespecifiedgraphicscontext.*/
publicsynchronizedvoidpaint(Graphicsg){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.drawLine(0,0,0,getHeight());
g.drawLine(201,0,201,getHeight());
g.drawLine(0,getHeight(),201,getHeight());
if((food!=null)&&(!blDisplay)){
food.paint(g);
}
if(snake!=null){
snake.paint(g);
}
g.setColor(Graphics.getColorOfName(Graphics.SILVER));
g.setFont(score_font);
g.drawString("LEVEL",203,40);
g.drawString(String.valueOf(listener.getLevel()),220,70);
g.drawString("SCORE",202,100);
g.drawString(format(listener.getScore()),208,130);
if(game_over){
g.setColor(0x00ffff);
g.setFont(game_over_font);
g.drawString("GAME",70,110);
g.drawString("OVER",70,140);
}
g.unlock(true);
}
/**Processkeyevents.*/
publicvoidprocessEvent(inttype,intparam){
if(type==Display.KEY_PRESSED_EVENT){
listener.keyPressed(param);
}
}
}
//蛇对象类
packagegreedSnake;
importjava.util.Random;
importjava.util.Vector;
importjava.util.Enumeration;
importcom.nttdocomo.ui.Graphics;
publicclassSnake{
publicfinalstaticintUP=1;
publicfinalstaticintDOWN =3;
publicfinalstaticintLEFT =2;
publicfinalstaticintRIGHT =4;
privateintdirection;
// privateGrid grid;
privateNode node;
privateVector snakeData=null;
privatestaticRandom rnd=newRandom();
/**Privateinternalconstructor.*/
publicSnake(){
snakeData=newVector();
Nodenode=newNode(4,0);
snakeData.addElement(node);
node=newNode(3,0);
snakeData.addElement(node);
node=newNode(2,0);
snakeData.addElement(node);
node=newNode(1,0);
snakeData.addElement(node);
node=newNode(0,0);
snakeData.addElement(node);
setDirection(Snake.DOWN);
}
publicintgetDirection(){
returndirection;
}
publicvoidsetDirection(intdir){
if(direction%2!=dir%2){
direction=dir;
}
}
publicNodegetHead(){
return(Node)snakeData.elementAt(0);
}
publicNodegetNextHead(){
Nodenode=(Node)snakeData.elementAt(0);
node=newNode(node.x,node.y);
switch(this.direction){
caseSnake.UP:
node.y--;
break;
caseSnake.DOWN:
node.y++;
break;
caseSnake.LEFT:
node.x--;
break;
caseSnake.RIGHT:
node.x++;
break;
}
returnnode;
}
publicvoidmove(){
Nodenode=(Node)snakeData.elementAt(0);
NodenewNode=this.getNextHead();
for(inti=snakeData.size()-1;i>0;i--){
Nodenode1=(Node)snakeData.elementAt(i);
Nodenode2=(Node)snakeData.elementAt(i-1);
node1.x=node2.x;
node1.y=node2.y;
}
node.x=newNode.x;
node.y=newNode.y;
}
publicvoidaddNode(){
Nodenode=(Node)snakeData.lastElement();
NodenewNode=newNode(node.x,node.y);
snakeData.addElement(newNode);
}
publicvoidpaint(Graphicsg){
intSnakeColor=0xff0000;
intgn=GreedSnake.NODE_SIZE;
Enumeratione=snakeData.elements();
while(e.hasMoreElements()){
Nodenode=(Node)e.nextElement();
g.setColor(SnakeColor);
g.fillRect(node.x*gn,node.y*gn,gn,gn);
}
}
publicvoideatFood(Foodfood){
this.addNode();
}
publicbooleanptInSnake(Nodenode){
for(inti=1;i<snakeData.size();i++){
NodenodeInSnake=(Node)snakeData.elementAt(i);
if((nodeInSnake.x==node.x)&&(nodeInSnake.y==node.y)){
returntrue;
}
}
returnfalse;
}
}
//食品对象类
packagegreedSnake;
importjava.util.Random;
importcom.nttdocomo.ui.Graphics;
publicclassFood{
privateintx;
privateinty;
privatestaticRandomrnd=newRandom();
privateFood(intx,inty){
this.x =x;
this.y =y;
}
publicstaticFoodcreateRandomFood(intwidth,intheight){
returncreateFood(getRandomInt(width),getRandomInt(height));
}
privatestaticintgetRandomInt(intlimit){
returnMath.abs(rnd.nextInt()%limit);
}
privatestaticFoodcreateFood(intx,inty){
returnnewFood(x,y);
}
/**
*Getthexcoordinateofthebase
*locationofthisinstance.
*/
publicintgetX(){
returnx;
}
/**
*Gettheycoordinateofthebase
*locationofthisinstance.
*/
publicintgetY(){
returny;
}
/**PaintthisinstanceontothespecifiedGraphicscontext.*/
publicsynchronizedvoidpaint(Graphicsg){
intgn=GreedSnake.NODE_SIZE;
intFoodColor=0x0000ff;
g.setColor(FoodColor);
g.fillRect(this.getX()*gn,this.getY()*gn,gn,gn);
}
}
//节点对象类
/packagegreedSnake;
publicclassNode{
publicintx;
publicinty;
/**Createanewinstanceofthisclass.*/
publicNode(intx,inty){
this.x=x;
this.y=y;
}
}
最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net网页编程并驾齐驱的 |
|