J2ME_贪吃蛇
程序员文章站
2022-05-15 19:22:50
...
/* *以下是MySnakeCanvas.java源码 */ package com.ming; import java.util.Random; 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; /* * 一个简单的贪吃蛇小游戏 */ public class MySnakeCanvas extends Canvas implements Runnable, CommandListener { //蛇身长度 int snakeSize; //蛇身宽度 int snakeWidth; //蛇头坐标 int snakeHeadX,snakeHeadY; //蛇身坐标 int[] snakeX=new int[100]; int[] snakeY=new int[100]; //蛇运动方向 String fx; //间隔时间 int spaceTime; //食物坐标 int foodX,foodY; //分数 int score; //控制游戏的暂停与继续 boolean isSpace; //控制游戏胜负 boolean isLose=false; boolean isWin=false; //初始化一个线程 private Thread thread; Command startC=new Command("开始", Command.OK, 1); Command spaceC=new Command("暂停", Command.CANCEL, 1); Random rdm=new Random(); public MySnakeCanvas(){ addCommand(startC); addCommand(spaceC); //添加监听器 setCommandListener(this); initGame(); } //游戏初始化 public void initGame(){ score=0; snakeSize=5; snakeWidth=6; snakeHeadX=getWidth()/2; snakeHeadY=getHeight()/2-1; //暂停时间,根据spaceTime改变游戏难度,spaceTime值越小,蛇的移动速度越快 spaceTime=300; isSpace=true; fx="left"; for (int i = 0; i < snakeSize; i++) { snakeX[i]=snakeHeadX+snakeWidth*i; snakeY[i]=snakeHeadY; } getNewFood(); } //获取一个随机食物 public void getNewFood(){ do { foodX=rdm.nextInt(getWidth()/snakeWidth)*snakeWidth; foodY=rdm.nextInt(getHeight()/snakeWidth)*snakeWidth; } while (isBadFood()); } //判断食物是否符合规定(若食物与蛇身重合则视为违规) public boolean isBadFood(){ for (int i = 0; i < snakeSize; i++) { if (foodX==snakeX[i]&&foodY==snakeY[i]) { return true; } } return false; } //移动蛇的位置 public void moveSnake(){ //指定蛇头坐标(将蛇身的第一个节点设为蛇头) snakeX[0]=snakeHeadX; snakeY[0]=snakeHeadY; //蛇身跟随蛇头移动(将蛇蛇身的后一个节点移动到前一个节点的坐标位置) for (int i = snakeSize; i >0; i--) { snakeX[i]=snakeX[i-1]; snakeY[i]=snakeY[i-1]; } //移动蛇头 if (fx=="left") { snakeHeadX-=snakeWidth; }else if (fx=="right") { snakeHeadX+=snakeWidth; }else if (fx=="up") { snakeHeadY-=snakeWidth; }else if (fx=="down") { snakeHeadY+=snakeWidth; } //重新指定蛇头位置 snakeX[0]=snakeHeadX; snakeY[0]=snakeHeadY; //测试位置偏差 //System.out.println("foodX:"+foodX+"*********snakeHeadX:"+snakeHeadX); //System.out.println("foodY:"+foodY+"*********snakeHeadY:"+snakeHeadY); //System.out.println(); if (snakeHeadX==foodX&&snakeHeadY==foodY) { eatFood(); } if (snakeHeadX>getWidth()||snakeHeadY>getHeight()||snakeHeadX<0||snakeHeadY<0) { youLose(); } for (int i = 1; i < snakeSize; i++) { if (snakeHeadX==snakeX[i]&&snakeHeadY==snakeY[i]) { youLose(); } } //过关条件 if (snakeSize>100) { youWin(); } repaint(); } public void eatFood(){ snakeSize++; score+=5; getNewFood(); repaint(); } public void youLose(){ //提示消息 System.out.println("you Lose"); isLose=true; isWin=false; } public void youWin(){ //提示消息 System.out.println("you Win"); isLose=false; isWin=true; //进入下一关 } public void commandAction(Command c, Displayable d) { // TODO Auto-generated method stub if (c==startC) { //开始 isSpace=false; thread=new Thread(this); thread.start(); }else if (c==spaceC) { //暂停 isSpace=true; } } public void run() { // TODO Auto-generated method stub while (!isSpace) { moveSnake(); try { //线程暂停 Thread.sleep(spaceTime); } catch (Exception e) { // TODO: handle exception } } } //根据按键操作改变蛇的相对运动方向 protected void keyPressed(int keyCode) { //方向 上 下 左 右 switch(getGameAction(keyCode)) { case Canvas.LEFT: if (fx!="right") {fx="left";} break; case Canvas.RIGHT: if (fx!="left") {fx="right";} break; case Canvas.DOWN: if (fx!="up") {fx="down";} break; case Canvas.UP: if (fx!="down") {fx="up";} break; } } protected void paint(Graphics g) { // TODO Auto-generated method stub //清屏 g.setColor(0xffffff); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0x111111); g.fillRect(foodX, foodY, snakeWidth,snakeWidth ); g.setColor(0x333333); for (int i = 0; i < snakeSize ; i++) { g.fillRect(snakeX[i], snakeY[i], snakeWidth,snakeWidth ); } if (isLose) { //清屏 g.setColor(0xffffff); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0xff00000); g.drawString("You Lose!", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.LEFT); } if (isWin) { //清屏 g.setColor(0xffffff); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0xff00000); g.drawString("You Win!", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.LEFT); } g.drawString("score:"+score, 10, 10, Graphics.TOP|Graphics.LEFT); } } ////////////////////////////////////////////////////////////// /* *以下是MyAction.java源码 */ package com.ming; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class MyAction extends MIDlet { Display display; protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub display=Display.getDisplay(this); MySnakeCanvas msc=new MySnakeCanvas(); display.setCurrent(msc); } }
上一篇: vue组件间的八种通信方式
下一篇: J2ME_下载图片