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

📄 hero.java

📁 本j2me坦克游戏是在Nokia平台下开发的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Hero.java	0.20 02/11/07 * Copyright (c) 2002 InterScape Creations. All Rights Reserved. */import javax.microedition.lcdui.*;class Hero implements Runnable {    private static Graphics G = null;    int posX, posY;    int status;		//1--normal 2--start 7--explode    int direction;    int speed;	boolean shootFlag = false;	int bulletNo;	int count;	Image heroImage;	Image drawHero;	Image heroLeft;	Image heroRight;	Image heroUp;	Image heroDown;	Image heroLeft1;	Image heroRight1;	Image heroUp1;	Image heroDown1;    Image heroLeft2;    Image heroRight2;    Image heroUp2;    Image heroDown2;	Image explodeImage;    int frameWidth, frameHeight;    int bulletLevel;   //symbol the hero's bullet speed    boolean capped;    boolean grassPaint = false;    boolean overstart;    Hero() {      // posX = 70;//sony 108;      //  posY = 190;//sony 148;        status = 0;        count = 40;        bulletLevel = 0;        direction = 3; // 1--left 2--right 3--up 4--down		// Load the object images        try {            heroImage = Image.createImage("/tank_sh.png");           	frameWidth = heroImage.getWidth()/4;           	frameHeight = heroImage.getHeight()/2;            heroLeft = Image.createImage(frameWidth, frameHeight);            G = heroLeft.getGraphics();            G.drawImage(heroImage, 0, 0, 20);            heroRight = Image.createImage(frameWidth, frameHeight);            G = heroRight.getGraphics();            G.drawImage(heroImage, -frameWidth, 0, 20);            heroUp = Image.createImage(frameWidth, frameHeight);            G = heroUp.getGraphics();            G.drawImage(heroImage, -frameWidth*2, 0, 20);            heroDown = Image.createImage(frameWidth, frameHeight);            G = heroDown.getGraphics();            G.drawImage(heroImage, -frameWidth*3, 0, 20);            heroLeft1 = Image.createImage(frameWidth, frameHeight);            G = heroLeft1.getGraphics();            G.drawImage(heroImage, 0, -frameHeight, 20);            heroRight1 = Image.createImage(frameWidth, frameHeight);            G = heroRight1.getGraphics();            G.drawImage(heroImage, -frameWidth, -frameHeight, 20);            heroUp1 = Image.createImage(frameWidth, frameHeight);            G = heroUp1.getGraphics();            G.drawImage(heroImage, -frameWidth*2, -frameHeight, 20);            heroDown1 = Image.createImage(frameWidth, frameHeight);            G = heroDown1.getGraphics();            G.drawImage(heroImage, -frameWidth*3, -frameHeight, 20);            heroLeft2 = Image.createImage(frameWidth, frameHeight);            G = heroLeft2.getGraphics();            G.drawImage(heroImage, 0, 0, 20);            heroRight2 = Image.createImage(frameWidth, frameHeight);            G = heroRight2.getGraphics();            G.drawImage(heroImage, -frameWidth, 0, 20);            heroUp2 = Image.createImage(frameWidth, frameHeight);            G = heroUp2.getGraphics();            G.drawImage(heroImage, -frameWidth*2, 0, 20);            heroDown2 = Image.createImage(frameWidth, frameHeight);            G = heroDown2.getGraphics();            G.drawImage(heroImage, -frameWidth*3, 0, 20);            G = null;	       	drawHero = heroUp;			explodeImage = Image.createImage("/explode.png");        }        catch(Exception exception) {            System.out.println(exception);        }    }	public void run() {	}    synchronized void paint(Graphics g) {    	int fillposX = posX;    	int fillposY = posY;    	int fillwidth = frameWidth;    	int fillheigh = frameHeight;    	speed = SuperTank.canvas.tileW;		g.setColor(0x0);		grassPaint = false;        if ( ((direction & 0x10) > 0) && (status < 7) ){			switch(direction & 0x0f) {		        case 1:		//left		        	drawHero = heroLeft;			        if ((posX > SuperTank.canvas.mapOffsetX) && !ismapCollide()){				        posX -= speed;			        	fillposX = posX + frameWidth;			        	fillposY = posY;			        	fillwidth = speed;			        	fillheigh = frameHeight;		        	}		            break;		        case 2:		//right		        	drawHero = heroRight;			        if ((posX < (SuperTank.canvas.mapOffsetX+SuperTank.canvas.mapWidth - frameWidth)) && !ismapCollide()){				        posX += speed;			        	fillposX = posX - speed;			        	fillposY = posY;			        	fillwidth = speed;			        	fillheigh = frameHeight;			    	}		            break;		        case 3:		//up		        	drawHero = heroUp;			        if ((posY > SuperTank.canvas.mapOffsetY) && !ismapCollide()){				        posY -= speed;			        	fillposX = posX;			        	fillposY = posY + frameHeight;			        	fillwidth = frameWidth;			        	fillheigh = speed;		        	}	    	        break;		        case 4:		//down		        	drawHero = heroDown;			        if ((posY<(SuperTank.canvas.mapOffsetY+SuperTank.canvas.mapHeight - frameHeight)) && !ismapCollide()){				        posY += speed;			        	fillposX = posX;			        	fillposY = posY - speed;			        	fillwidth = frameWidth;			        	fillheigh = speed;					}		        default:		            break;			}			g.fillRect(fillposX, fillposY, fillwidth, fillheigh);		}		if ( status == 0 ){			if ( count > 0 ){				if ( (count & 1) > 0 ){//                  drawHero = heroImage1;					switch(direction & 0x0f) {				        case 1:		//left				        	drawHero = heroLeft1;				            break;				        case 2:		//right				        	drawHero = heroRight1;				            break;				        case 3:		//up				        	drawHero = heroUp1;				            break;				        case 4:		//down				        	drawHero = heroDown1;				        default:				            break;					}				} else {//                  drawHero = heroImage2;					switch(direction & 0x0f) {				        case 1:		//left				        	drawHero = heroLeft;			    	        break;				        case 2:		//right				        	drawHero = heroRight;			        	    break;				        case 3:		//up				        	drawHero = heroUp;			            	break;				        case 4:		//down				        	drawHero = heroDown;				        default:			            	break;					}				}				count --;			} else {				// Load the object images		        try {		            heroImage = Image.createImage("/tank.png");		            heroLeft = Image.createImage(frameWidth, frameHeight);		            G = heroLeft.getGraphics();		            G.drawImage(heroImage, 0, 0, 20);		            heroRight = Image.createImage(frameWidth, frameHeight);		            G = heroRight.getGraphics();		            G.drawImage(heroImage, -frameWidth, 0, 20);		            heroUp = Image.createImage(frameWidth, frameHeight);		            G = heroUp.getGraphics();		            G.drawImage(heroImage, -frameWidth*2, 0, 20);		            heroDown = Image.createImage(frameWidth, frameHeight);		            G = heroDown.getGraphics();		            G.drawImage(heroImage, -frameWidth*3, 0, 20);		            G = null;		        }		        catch(Exception exception) {		            System.out.println(exception);		        }				status = 1;/*            }//            DirectGraphics dg = DirectUtils.getDirectGraphics(g);            switch(direction & 0x0f) {              case 1:		//left//                dg.drawImage(drawHero, posX, posY, g.TOP|g.LEFT, DirectGraphics.ROTATE_90);                g.drawImage(heroLeft, posX, posY, g.TOP|g.LEFT);                  break;              case 2:		//right//                dg.drawImage(drawHero, posX, posY, g.TOP|g.LEFT, DirectGraphics.ROTATE_270);                g.drawImage(heroRight, posX, posY, g.TOP|g.LEFT);                  break;              case 3:		//up//                g.drawImage(drawHero, posX, posY, g.TOP|g.LEFT);                g.drawImage(heroUp, posX, posY, g.TOP|g.LEFT);                  break;              case 4:		//down//                dg.drawImage(drawHero, posX, posY, g.TOP|g.LEFT, DirectGraphics.ROTATE_180);                g.drawImage(heroDown, posX, posY, g.TOP|g.LEFT);              default:                  break;          }*/                    switch (direction & 0x0f) {                        case 1: //left                            drawHero = heroLeft;                            break;                        case 2: //right                            drawHero = heroRight;                            break;                        case 3: //up                            drawHero = heroUp;                            break;                        case 4: //down                            drawHero = heroDown;                        default:                            break;                    }                }			g.drawImage(drawHero, posX, posY, g.TOP|g.LEFT);		} else if ( status == 7 ) {			g.drawImage(explodeImage, posX, posY, g.TOP|g.LEFT);			if (count > 0)				count--;			else{				g.fillRect(posX, posY, explodeImage.getWidth(), explodeImage.getHeight());				if ( SuperTank.canvas.life > 1 ){	//restart a hero					SuperTank.canvas.life --;			        try {			            heroImage = Image.createImage("/tank_sh.png");			            heroLeft = Image.createImage(frameWidth, frameHeight);			            G = heroLeft.getGraphics();			            G.drawImage(heroImage, 0, 0, 20);			            heroRight = Image.createImage(frameWidth, frameHeight);			            G = heroRight.getGraphics();			            G.drawImage(heroImage, -frameWidth, 0, 20);			            heroUp = Image.createImage(frameWidth, frameHeight);			            G = heroUp.getGraphics();			            G.drawImage(heroImage, -frameWidth*2, 0, 20);			            heroDown = Image.createImage(frameWidth, frameHeight);			            G = heroDown.getGraphics();			            G.drawImage(heroImage, -frameWidth*3, 0, 20);			            heroLeft1 = Image.createImage(frameWidth, frameHeight);			            G = heroLeft1.getGraphics();			            G.drawImage(heroImage, 0, -frameHeight, 20);			            heroRight1 = Image.createImage(frameWidth, frameHeight);			            G = heroRight1.getGraphics();			            G.drawImage(heroImage, -frameWidth, -frameHeight, 20);			            heroUp1 = Image.createImage(frameWidth, frameHeight);			            G = heroUp1.getGraphics();			            G.drawImage(heroImage, -frameWidth*2, -frameHeight, 20);			            heroDown1 = Image.createImage(frameWidth, frameHeight);			            G = heroDown1.getGraphics();			            G.drawImage(heroImage, -frameWidth*3, -frameHeight, 20);			            G = null;			        }			        catch(Exception exception) {

⌨️ 快捷键说明

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