⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectorcanvas.java

📁 JME贪食蛇游戏,初学jme作品,不好不要见怪
💻 JAVA
字号:
package Src;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class VectorCanvas extends GameCanvas implements Runnable{
	protected Vector v=new Vector();
	int w,h;
	byte direction;
	public static final byte UP=0;
	public static final byte DOWN=1;
	public static final byte LEFT=2;
	public static final byte RIGHT=3;
	public static int SLEEP_TIME=250;
	private final int CELLSIZE=10;
	int foodX;
	int foodY;
	private int enemyNum=4;
	private int[][] enemy=new int[enemyNum][2];
	public boolean iscreateEnemy=false;
	boolean b=true;
	boolean e=true;
	boolean isRemove=true;
	Random rt=new Random();
	public static boolean gameOver=false;
	public boolean isPause=false;
	public MainMidlet mainmidlet;
	public int              score;
	public byte             level;
	public VectorCanvas(MainMidlet mainmidlet){
		super(true);
		this.mainmidlet=mainmidlet;
		init();
	}
	public void init(){
		w=getWidth();
		h=getHeight();
		for(int i=0;i<5;i++){
			v.addElement(new Cell(100-i*10,40));
		}
		foodX=100;
		foodY=100;
		direction=DOWN;
		score=0;
		level=1;
		new Thread(this).start();
	}
	public void createFood(){
		while(true){
			foodX=Math.abs(rt.nextInt()%(w-CELLSIZE+1))
			                        /CELLSIZE*CELLSIZE;
			foodY=Math.abs(rt.nextInt()%(h-CELLSIZE+1))
            /CELLSIZE*CELLSIZE;
			boolean b=true;
			for(int i=0;i<v.size();i++){
				if(foodX==((Cell)v.elementAt(i)).getX()&&((Cell)v.elementAt(i)).getY()==foodY){
					b=false;
					break;
				}
			}
			if(b){
				break;
			}
		}
	}
	public void eatFood(){
		if(((Cell)v.lastElement()).getX()==foodX&&((Cell)v.lastElement()).getY()==foodY){
			isRemove=false;
    		createFood();
    	}
	}
	public void move(){
		Cell l=(Cell)v.lastElement();
		int x=l.getX();
		int y=l.getY();
		System.out.println(""+x+" "+y);
		switch(direction){
		case UP:
			y-=10;
			break;
		case DOWN:
			y+=10;
			break;
		case LEFT:
			x-=10;
			break;
		case RIGHT:
			x+=10;
			break;
		}
		if(isRemove){
		Cell f=(Cell)v.firstElement();
		System.out.println(""+f.getX()+" "+f.getY());
		v.removeElement(f);
		}
		v.addElement(new Cell(x,y));
		isRemove=true;
	}
	public void draw(Graphics g,Cell cell){
		g.setColor(0xff0000);
		g.fillArc(cell.getX(), cell.getY(), CELLSIZE-1, CELLSIZE-1,0,360);
		if(b){
			g.fillArc(foodX, foodY, CELLSIZE-2, CELLSIZE-2,0,360);
    	}
	}
	public void drawString1(Graphics g){
		g.setColor(0xffffff);
		g.fillRect(0, 0, w, h);
		g.setColor(0);
		String msg="Game over";
		Font font=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
		g.setFont(font);
		int x=font.stringWidth(msg);
		g.drawString("Game over", (w/2)-(x/2), h/2, Graphics.TOP|Graphics.LEFT);
		this.flushGraphics();
	}
	public Cell getNode(int t){
		Cell temp=(Cell)v.elementAt(t);
		return temp;
	}
	public void drawSnake(Graphics g){
		g.setColor(0xffffff);
		g.fillRect(0, 0, w, h);
		for(int i=0;i<v.size();i++){
			Cell tt=(Cell)v.elementAt(i);
			draw(g,tt);
		}
		this.flushGraphics();
	}
	public void input(){
		int keystates=this.getKeyStates();
		if((keystates&LEFT_PRESSED)!=0){
			if(direction!=RIGHT&&isPause==false){
				direction=LEFT;
			}
		}
		if((keystates&RIGHT_PRESSED)!=0){
			if(direction!=LEFT&&isPause==false){
				direction=RIGHT;
			}
		}
		if((keystates&UP_PRESSED)!=0){
			if(direction!=DOWN&&isPause==false){
				direction=UP;
			}
		}
		if((keystates&DOWN_PRESSED)!=0){
			if(direction!=UP&&isPause==false){
				direction=DOWN;
			}
		}
		if((keystates&FIRE_PRESSED)!=0){
			isPause=!isPause;
		}
	}
	public boolean isGameOver(){
		Cell temp=(Cell)v.lastElement();
		int tempX=temp.getX();
		int tempY=temp.getY();
		if(tempX<0||tempX>(w-CELLSIZE)||tempY<0||tempY>(h-CELLSIZE)){
			gameOver=true;
			return true;
		}
		for(int i=v.size()-4;i>=0;i--){
			Cell temp2=(Cell)v.elementAt(i);
			if(tempX==temp2.getX()&&tempY==temp2.getY()){
				gameOver=true;
				return true;
			}
		}
		return false;
	}
	public void run(){
		Graphics g_p=this.getGraphics();
		while(true){
			long start=System.currentTimeMillis();
			input();
			if(!isPause){			
			eatFood();
			move();
			if(isGameOver()){
				this.drawString1(g_p);
				try{
					Thread.sleep(2000);
				}catch(Exception ee){}
				mainmidlet.d.setCurrent(mainmidlet.form);
				break;
			}
			}
			drawSnake(g_p);
			long end=System.currentTimeMillis();
			if(end-start<SLEEP_TIME){
			try{
				Thread.sleep(SLEEP_TIME-(end-start));
			}catch(Exception e){}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -