j2me贪吃蛇

来源:互联网 发布:电商app行业数据 编辑:程序博客网 时间:2024/04/28 19:27

package airshow.com;
/*
 *  游戏画板框架类
 */
import javax.microedition.lcdui.game.GameCanvas;

public abstract class AGame extends GameCanvas {
 
 public AGame() {
  super(false);
 }

 public final void keyPressed(int keyCode) {// 键盘消息处理

  switch (keyCode) {
  case -1:
   actUp();
   break;
  case -2:
   actDown();
   break;
  case -3:
   actLeft();
   break;
  case -4:
   actRight();
   break;
  case -5:
   actSure();
   break;
  case KEY_NUM8:
   actUp();
   break;
  case KEY_NUM2:
   actDown();
   break;
  case KEY_NUM4:
   actLeft();
   break;
  case KEY_NUM6:
   actRight();
   break;
  case KEY_NUM5:
   actSure();
   break;

  case -7:
   micRight();
   break;
  case -6:
   micLeft();

  }
  this.repaint();
 }

 protected abstract void actDown();

 protected abstract void actLeft();

 protected abstract void actRight();

 protected abstract void actUp();

 protected abstract void micLeft();

 protected abstract void micRight();

 protected abstract void actSure();

 protected abstract void menuMessage(int menuId, int com);

}

————————————————————————————————————————

package airshow.snake;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class App extends MIDlet {
Game game;
Display dis;
 public App() {
  // TODO 自动生成构造函数存根
  game=new Game();
  dis=Display.getDisplay(this);
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO 自动生成方法存根

 }

 protected void pauseApp() {
  // TODO 自动生成方法存根

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO 自动生成方法存根
dis.setCurrent(game);
 }

}
————————————————————————————————

package airshow.snake;

import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

import airshow.com.AGame;

public class Game extends AGame  implements Runnable{

 protected void actDown() {
  // TODO 自动生成方法存根
  if(this.dir!=0) this.dir=2;
 }

 protected void actLeft() {
  // TODO 自动生成方法存根
  if(this.dir!=1) this.dir=3;
 }

 protected void actRight() {
  // TODO 自动生成方法存根
  if(this.dir!=3) this.dir=1;
 }

 protected void actSure() {
  // TODO 自动生成方法存根
  
 }

 protected void actUp() {
  // TODO 自动生成方法存根
  if(this.dir!=2) this.dir=0;
 }

 protected void menuMessage(int menuId, int com) {
  // TODO 自动生成方法存根
  
 }

 protected void micLeft() {
  // TODO 自动生成方法存根
  
 }

 protected void micRight() {
  // TODO 自动生成方法存根
  
 }

 private Image imgSnake;
 private LayerManager layers;
 private TiledLayer map;
 private Point head,tail;
 private int length=3;
 private int[][] snake;
 
 private Random r;
 
 private int dir=0;//方向 0;向上 1:向右 2;向下 3:向左
 Graphics g;
 int w=160;

 
 int gameState=0;
public Game(){
 g=this.getGraphics();
 head=new Point();
 tail=new Point();
 snake=new int[20][20];
 
 try {
  this.imgSnake=Image.createImage("/sn.png");
 } catch (IOException e) {
  // TODO 自动生成 catch 块
  e.printStackTrace();
 }
 map=new TiledLayer(20,20,imgSnake,8,8);
 
 layers=new LayerManager();
 layers.append(map);
 
 for(int i=0;i<20;i++){
  for(int j=0;j<20;j++){
   this.map.setCell(i,j,2);
   snake[i][j]=0;
  }
 }
 
 this.map.setCell(10,9,1); head.x=10;head.y=9;snake[10][9]=3;
 this.map.setCell(10,10,1);snake[10][10]=2;
 this.map.setCell(10,11,1);tail.x=10;tail.y=11;snake[10][11]=1;
 
 this.map.setCell(5, 5,1);//食物
 
 r=new Random();
 new Thread(this).start();
}

public void run() {
 // TODO 自动生成方法存根
 
 while(true){
  
  if(this.gameState==0)
   this.move();
  layers.setViewWindow(0,0,160,160);
  layers.paint(g,0,0);
  
  if(this.gameState==2){
   g.setColor(0xff0000);
   g.drawString("游戏结束",50,50,0);
  }
  
  this.flushGraphics();
  
  
  try {
   Thread.sleep(500);
  } catch (InterruptedException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
 }
}

private void move() {
 // TODO 自动生成方法存根

 switch(this.dir){
 case 0:
  this.head.y--;
 break;
 case 1:
  this.head.x++;
  break;
 case 2:
  this.head.y++;
  break;
 case 3:
  this.head.x--;
  break;
 }
 if(head.x<0||head.y<0||head.x>=20||head.y>=20||this.snake[head.x][head.y]!=0){//撞到了
  
  this.gameState=2;
  return;
 }
 if(this.map.getCell(head.x,head.y)==1){//蛇吃到食物了
  this.length++;
  snake[head.x][head.y]=this.length;
  food();
 }
 this.map.setCell(this.tail.x,this.tail.y, 2);
 this.map.setCell(head.x,head.y,1);
 for(int i=0;i<20;i++){
  for(int j=0;j<20;j++){
   if(this.snake[i][j]==2) {
    this.tail.x=i;this.tail.y=j;
   }
   if(this.snake[i][j]>0) snake[i][j]--;
  }
 }
 snake[head.x][head.y]=this.length;
 
 
}

private void food() {
 // TODO 自动生成方法存根
 boolean hasFood=false;
 int x=0,y=0;
 while(!hasFood){
  x=r.nextInt(20);
  y=r.nextInt(20);
  if(this.map.getCell(x,y)==2){
   this.map.setCell(x,y,1);
   hasFood=true;
  }
 }
}


}
class Point{
 int x=0;
 int y=0;
}