欢迎来到天天文库
浏览记录
ID:38086920
大小:43.50 KB
页数:6页
时间:2019-05-28
《贪吃蛇源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//文件名:SnakeMIDlet.javapackagesnake;importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;publicclassSnakeMIDletextendsMIDlet{SnakeCanvasdisplayable=newSnakeCanvas();publicSnakeMIDlet(){Display.getDisplay(this).setCurrent(displayable);}publicvoidstartApp(){}publicvoi
2、dpauseApp(){}publicvoiddestroyApp(booleanunconditional){}}//文件名:SnakeCanvas.javapackagesnake;importjava.util.*;importjavax.microedition.lcdui.*;/***贪吃蛇游戏*/publicclassSnakeCanvasextendsCanvasimplementsRunnable{/**存储贪吃蛇节点坐标,其中第二维下标为0的x坐标,第二维下标是1的y坐标*/int[][]snake=newint[200][2];/*
3、*已经使用的节点数量*/intsnakeNum;/**贪吃蛇运动方向,0向上,1向下,2向左,3向右*/intdirection;/*移动方向*//**向上*/privatefinalintDIRECTION_UP=0;/**向下*/privatefinalintDIRECTION_DOWN=1;/**向左*/privatefinalintDIRECTION_LEFT=2;/**向右*/privatefinalintDIRECTION_RIGHT=3;/**游戏区域宽度*/intwidth;/**游戏区域高度*/intheight;/**蛇身单元宽度*
4、/privatefinalbyteSNAKEWIDTH=4;/**是否处于暂停状态,true暂停*/booleanisPaused=false;/**是否处于运行状态,true运行*/booleanisRun=true;/**时间间隔*/privatefinalintSLEEP_TIME=300;/**食物的X坐标*/intfoodX;/**食物的Y坐标*/intfoodY;/**食物的闪烁控制*/booleanb=true;/**Random对象*/Randomrandom=newRandom();publicSnakeCanvas(){//初始化i
5、nit();width=this.getWidth();height=this.getHeight();//启动线程newThread(this).start();}/***初始化开始数据*/privatevoidinit(){//初始化节点数量snakeNum=7;//初始化节点数据for(inti=0;i6、ctedvoidpaint(Graphicsg){//清屏g.setColor(0xffffff);g.fillRect(0,0,width,height);g.setColor(0);//绘制蛇身for(inti=0;i7、inti=snakeNum-1;i>0;i--){snake[0]=snake[i-1][0];snake[1]=snake[i-1][1];}//第一个单元格移动switch(direction){caseDIRECTION_UP:snake[0][1]=snake[0][1]-SNAKEWIDTH;break;caseDIRECTION_DOWN:snake[0][1]=snake[0][1]+SNAKEWIDTH;break;caseDIRECTION_LEFT:snake[0][0]=snake[0][0]-SNAKEWIDTH;break;c8、aseDIRECTION_RIGHT:snake[0][0]=snake[0][0]+SNAK
6、ctedvoidpaint(Graphicsg){//清屏g.setColor(0xffffff);g.fillRect(0,0,width,height);g.setColor(0);//绘制蛇身for(inti=0;i7、inti=snakeNum-1;i>0;i--){snake[0]=snake[i-1][0];snake[1]=snake[i-1][1];}//第一个单元格移动switch(direction){caseDIRECTION_UP:snake[0][1]=snake[0][1]-SNAKEWIDTH;break;caseDIRECTION_DOWN:snake[0][1]=snake[0][1]+SNAKEWIDTH;break;caseDIRECTION_LEFT:snake[0][0]=snake[0][0]-SNAKEWIDTH;break;c8、aseDIRECTION_RIGHT:snake[0][0]=snake[0][0]+SNAK
7、inti=snakeNum-1;i>0;i--){snake[0]=snake[i-1][0];snake[1]=snake[i-1][1];}//第一个单元格移动switch(direction){caseDIRECTION_UP:snake[0][1]=snake[0][1]-SNAKEWIDTH;break;caseDIRECTION_DOWN:snake[0][1]=snake[0][1]+SNAKEWIDTH;break;caseDIRECTION_LEFT:snake[0][0]=snake[0][0]-SNAKEWIDTH;break;c
8、aseDIRECTION_RIGHT:snake[0][0]=snake[0][0]+SNAK
此文档下载收益归作者所有