hero.java

来自「这是一个用JAVA写的很好玩的好游戏!是更新版!加了碰撞上面都有的!希望大家喜欢」· Java 代码 · 共 104 行

JAVA
104
字号
import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class Hero {
	int state;
	int dir;
	int x;
	int y;
	int speed;
	int nowPic;
	int hp;
	int mp;
	Image img;
	int w;
	int h;
	int count;
	//state
	static final int STOP=0;
	static final int MOVE=1;
	static final int DEAD=2;
	//dir
	static final int UP=0;
	static final int DOWN=1;
	static final int LEFT=2;
	static final int RIGHT=3;
	
	int action[][]={
			{12,13,14,15},
			{0,1,2,3},
			{4,5,6,7},
			{8,9,10,11}
	};
	
	public Hero(){
		try {
			img=Image.createImage("/039-Mage07.png");
			w=img.getWidth()/4;
			h=img.getHeight()/4;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void initHero(int state,int dir,int x,int y){
		this.state=state;
		this.dir=dir;
		this.x=x;
		this.y=y;
		this.speed=4;
	}
	
	public void paint(Graphics g){
		if(state==STOP){
			nowPic=action[dir][0];
			g.setClip(x, y, w, h);
			g.drawImage(img, x-nowPic%4*w, y-nowPic/4*h, 0);
		}
		if(state==MOVE){
			nowPic=action[dir][count%4];
			g.setClip(x, y, w, h);
			g.drawImage(img, x-nowPic%4*w, y-nowPic/4*h, 0);
		}
	}
	
	public void move(){
		if(state==MOVE){
			count++;
			if(count>999){
				count=0;
			}
			switch(dir){
			case UP:
				y-=speed;
				if(y<=0){
					y=0;
				}
				break;
			case DOWN:
				y+=speed;
				if(y>=GameCanvas.screen_h-h){
					y=GameCanvas.screen_h-h;
				}
				break;
			case LEFT:
				x-=speed;
				if(x<=0){
					x=0;
				}
				break;
			case RIGHT:
				x+=speed;
				if(x>=GameCanvas.screen_w-w){
					x=GameCanvas.screen_w-w;
				}
				break;
			}
		}
	}
	
}

⌨️ 快捷键说明

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