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

📄 serpentgamecanvas.java

📁 Position.java :辅助类 SerpentGameCanvas.java :游戏主控类 Maze.java :迷宫 SerpentGame.java :应用主控类 Serpent.java
💻 JAVA
字号:
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;class SerpentGameCanvas extends Canvas{	final int PLAYER_HEAD=1;	final int PLAYER_BODY=2;	final int CELL_WIDTH=9;	SerpentGame midlet;	String action = "" ;	protected Serpent player;	protected Maze maze;	protected Frog frog;	protected Vector npc=new Vector();	public Vector eggList=new Vector();	public Egg eggp=null;	public static Random rand=new Random((new Date()).getTime());	private int mapid;	public int score=0;	public int life=5;	public int stage=1;		int track;		SerpentGameCanvas(SerpentGame mid)	{		midlet=mid;		maze=new Maze();		mapid=1;		maze.loadMaze(mapid);		player=new Serpent(this, maze, new Position(0,8), 5);		frog=new Frog(this, maze, new Position(5,5));		npc.addElement(new RunnerSerpent(this, maze, new Position(10,4), 5));		npc.addElement(new RunnerSerpent(this, maze, new Position(10,6), 5));		npc.addElement(new RunnerSerpent(this, maze, new Position(10,8), 5));		player.start();		frog.start();		for( int i=0;i<npc.size();i++ )			((Serpent)npc.elementAt(i)).start();	}		public void paint(Graphics g)	{		//清除螢幕		g.setColor(255,255,255) ;		g.fillRect(0,0,getWidth(),getHeight()) ;		g.setColor(0,0,0) ;		drawMaze(g, maze);		if(player.bLive)			drawSerpent(g, player);		drawSerpents(g, npc);//		for( int i=0;i<npc.size();i++ ) {//			if(((Serpent)npc.elementAt(i)).bLive) //				drawSerpent(g, ((Serpent)npc.elementAt(i)));//		}		if(frog.bLive)			drawFrog(g, frog);		if( eggp!=null )			drawEggP(g, eggp);		for( int i=0;i<eggList.size();i++ ) {			drawEgg(g, (Egg)eggList.elementAt(i));		}		drawScore(g);		drawLife(g);	}		public void notifyFrogEaten()	{		frog=new Frog( this, maze, new Position( Math.abs( rand.nextInt()) % Maze.WIDTH, Math.abs( rand.nextInt()) % Maze.HEIGHT ));	//16x21		frog.start();	}		//	nextStage	synchronized public void checkNpcKilled()	{		//	a little confusing		for( int i=0;i<npc.size();i++ ) {			if( !((Serpent)npc.elementAt(i)).bLive ) {				npc.removeElementAt(i);				i--;			}		}		if( npc.size()!=0 ) return;		//		for( int i=0;i<npc.size();i++ ) {//			if( ((Serpent)npc.elementAt(i)).bLive ) return;//		}				mapid++;		stage++;		if( mapid>10 ) mapid=1;				System.err.println("New map start "+mapid);		maze.loadMaze(mapid);		player.changeMap( maze, new Position(0,8));		player.award(Serpent.NEXT_STAGE);		if( eggp!=null ) {			//	add life			eggp=null;			life++;			player.award(Serpent.NEW_LIFE);		}		player.killImmatureEgg();		player.newEgg();		frog=new Frog(this, maze, new Position(5,5));		synchronized(eggList) {			for( int i=0;i<eggList.size();i++ )				((Egg)eggList.elementAt(i)).getKilled();			eggList.removeAllElements();		}		npc.removeAllElements();		npc.addElement(new RunnerSerpent(this, maze, new Position(10,4), 6));		npc.addElement(new RunnerSerpent(this, maze, new Position(10,6), 6));		npc.addElement(new RunnerSerpent(this, maze, new Position(10,8), 6));		frog.start();		for( int i=0;i<npc.size();i++ )			((Serpent)npc.elementAt(i)).start();		repaint();	}	private void gameOver()	{//		frog.getKilled();//		player.getKilled();//		for( int i=0;i<npc.size();i++ )//			((Serpent)npc.elementAt(i)).getKilled();//		eggp.getKilled();//		for( int i=0;i<eggList.size();i++ )//			((Egg)eggList.elementAt(i)).getKilled();		midlet.showGameOver(score, stage);//		midlet.notifyDestroyed();	}		//	restart	public void notifyPlayerKilled()	{		life--;		if( life<=0 ) 			gameOver();					track=0;		maze.loadMaze(mapid);		player=new Serpent(this, maze, new Position(0,8), 5);		frog=new Frog(this, maze, new Position(5,5));		synchronized(eggList) {			for( int i=0;i<eggList.size();i++ )				((Egg)eggList.elementAt(i)).getKilled();			eggList.removeAllElements();		}		track=1;		for( int i=0;i<npc.size();i++ ) {			int startpoint=8;			if( i==0 ) startpoint=4;			if( i==1 ) startpoint=6;			if( ((Serpent)npc.elementAt(i)).bLive ) {				((Serpent)npc.elementAt(i)).changeMap( maze, new Position(10, startpoint));				if( ((Serpent)npc.elementAt(i)).egg!=null && ((Serpent)npc.elementAt(i)).egg.bLive ) {					((Serpent)npc.elementAt(i)).killImmatureEgg();				}			}			track++;		}		player.start();		track=5;		frog.start();		track=6;		repaint();	}		private void drawMaze(Graphics g, Maze maze)	{		int i,j;		g.setColor(226,71,5);		Position wallPos=new Position();		for( i=0;i<maze.horizontalWalls.length;i++ )			for( j=0;j<maze.horizontalWalls[i].length;j++ ) {				wallPos.setValue(j,i);				drawHorizontalWall( g, wallPos, maze.horizontalWalls[i][j] );			}					for( i=0;i<maze.verticalWalls.length;i++ )			for( j=0;j<maze.verticalWalls[i].length;j++ ) {				wallPos.setValue(j,i);				drawVerticalWalls( g, wallPos, maze.verticalWalls[i][j] );			}				}		private void drawHorizontalWall(Graphics g, Position pos, int bDraw)	{		if( bDraw != 0 ) {			Rect rt=new Rect( pos, CELL_WIDTH );			g.drawLine( rt.left, rt.top+rt.height, rt.left+rt.width, rt.top+rt.height);		}	}		private void drawVerticalWalls(Graphics g, Position pos, int bDraw)	{		if( bDraw != 0 ) {			Rect rt=new Rect( pos, CELL_WIDTH );			g.drawLine( rt.left+rt.width, rt.top, rt.left+rt.width, rt.top+rt.height);		}	}		private void drawSerpent(Graphics g, Serpent serpent)	{		int i;		for( i=0;i<serpent.body.size();i++ ) {			drawSerpentSegment(g, (Position)serpent.body.elementAt(i), PLAYER_BODY, serpent.direction, serpent.type);		}		drawSerpentSegment(g, serpent.head, PLAYER_HEAD, serpent.direction, serpent.type);	}		private void drawSerpents(Graphics g, Vector serpents)	{		int i, sep;		for( sep=0;sep<serpents.size();sep++ ) {			Serpent serpent=(Serpent)serpents.elementAt(sep);			if( !serpent.bLive ) continue;			for( i=0;i<serpent.body.size();i++ ) {				drawSerpentSegment(g, (Position)serpent.body.elementAt(i), PLAYER_BODY, serpent.direction, serpent.type);			}		}		for( sep=0;sep<serpents.size();sep++ ) {			Serpent serpent=(Serpent)serpents.elementAt(sep);			if( !serpent.bLive ) continue;			drawSerpentSegment(g, serpent.head, PLAYER_HEAD, serpent.direction, serpent.type);		}	}		private void drawSerpentSegment(Graphics g, Position pos, int desc, int direction, int type)	{		Rect rt=new Rect( pos, CELL_WIDTH );				if( desc==PLAYER_BODY )			if( type==Serpent.PLAYER )				g.setColor(174,242,180);//				g.setColor(126,234,134);			else				g.setColor(255,255,255);//				g.setColor(144,128,202);		else			if( type==Serpent.PLAYER )				g.setColor(66,154,74);			else				g.setColor(114,98,252);		g.fillArc(rt.left, rt.top, rt.width, rt.height, 0, 360);				if( type==Serpent.PLAYER )			g.setColor(66,154,74);		else			g.setColor(114,98,252);		g.drawArc(rt.left, rt.top, rt.width, rt.height, 0, 360);				if( type==Serpent.PLAYER )			g.setColor(244,154,2);		else			g.setColor(224,150,233);		if( desc==PLAYER_BODY )			drawSerpentTexture(g, pos, desc, direction);	}		private void drawSerpentTexture(Graphics g, Position pos, int desc, int direction)	{		Rect rt=new Rect( pos, CELL_WIDTH );		if( direction==Serpent.MOVE_UP || direction==Serpent.MOVE_DOWN )			g.drawLine(rt.left+rt.width/2, rt.top, rt.left+rt.width/2, rt.top+rt.height);		else			g.drawLine(rt.left, rt.top+rt.height/2, rt.left+rt.width, rt.top+rt.height/2);	}		private void drawFrog(Graphics g, Frog frog)	{		if( !frog.bLive ) return;				Rect rt=new Rect( frog.pos, CELL_WIDTH );		g.setColor(71,114,1);		g.drawLine(rt.left+rt.width/6, rt.top+rt.height/6, rt.left+rt.width*5/6, rt.top+rt.height*5/6);		g.drawLine(rt.left+rt.width*5/6, rt.top+rt.height/6, rt.left+rt.width/6, rt.top+rt.height*5/6);		g.drawLine(rt.left+rt.width/2, rt.top+rt.height/6, rt.left+rt.width/2, rt.top+rt.height/2);	}	public void drawScore(Graphics g)	{		g.setFont(null);		g.setColor(0,200,0) ;		g.drawString("SC:"+score, 5,0, Graphics.TOP|Graphics.LEFT);	}	public void drawLife(Graphics g)	{		g.setFont(null);		g.setColor(0,200,0) ;		g.drawString("LF: "+life, 70,0, Graphics.TOP|Graphics.LEFT);	}	private void drawEgg(Graphics g, Egg egg)	{		if( !egg.bLive ) return;		Rect rt=new Rect( egg.pos, CELL_WIDTH );		g.setColor(252,172,65);		g.fillArc(rt.left, rt.top+rt.height/6, rt.width, rt.height*2/3, 0, 360);		g.setColor(255,255,255);		g.fillArc(rt.left+rt.height/3, rt.top+rt.height*7/18, rt.width/3, rt.height*2/9, 0, 360);	}		private void drawEggP(Graphics g, Egg egg)	{		if( egg==null || !egg.bLive ) return;		Rect rt=new Rect( egg.pos, CELL_WIDTH );		g.setColor(252,172,65);		g.fillArc(rt.left, rt.top+rt.height/6, rt.width, rt.height*2/3, 0, 360);	}		protected void keyPressed(int keyCode)	{		setDirection(keyCode);//		System.err.println(String.valueOf(keyCode));//		repaint();	}	private void setDirection(int keyCode)		{		int moveDirection=Serpent.MOVE_UP;		switch(keyCode) {			case Canvas.KEY_NUM2:			case -1:				moveDirection=Serpent.MOVE_UP;				break;			case Canvas.KEY_NUM8:			case -2:				moveDirection=Serpent.MOVE_DOWN;				break;			case Canvas.KEY_NUM4:			case -3:				moveDirection=Serpent.MOVE_LEFT;				break;			case Canvas.KEY_NUM6:			case -4:				moveDirection=Serpent.MOVE_RIGHT;				break;			default:				return;		}				player.setDirection(moveDirection);	}}

⌨️ 快捷键说明

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