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

📄 kuma.java

📁 j2me 的小游戏
💻 JAVA
字号:
/*
 * Kuma.java
 *
 * Copyright 1999 JJKING Software, Junichi Ito <jun1@mailhost.net>
 * Permission to use, copy, modify, and distribute this software and its
 * documentation without fee for NON-COMMERCIAL is free.
 */
 
import com.sun.kjava.Bitmap;
import com.sun.kjava.Graphics;

/**
 * This class represents a animation character "Kuma". 
 *
 * @version 1.0  1999/10/2
 */
public class Kuma extends AnimChar
{
	/**
	 * The bitmap patterns.
	 */
	static final short[][] pattern = {
		{	// ID=1000 kuma_f1
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x1c1c, (short)0x2222, (short)0x47e2
			, (short)0x4c1a, (short)0x5004, (short)0x2002, (short)0x4662
			, (short)0x4662, (short)0x4002, (short)0x4182, (short)0x2392
			, (short)0x16e4, (short)0x1008, (short)0x0c1c, (short)0x33e2
			, (short)0x4001, (short)0x980d, (short)0xe80b, (short)0x0808
			, (short)0x1004, (short)0x33e6, (short)0x2412, (short)0x380e
		}
		,{	// ID=1001 kuma_f2
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x1c1c, (short)0x2222, (short)0x47e2, (short)0x4c1a
			, (short)0x5004, (short)0x2002, (short)0x4662, (short)0x4662
			, (short)0x4002, (short)0x4182, (short)0x2392, (short)0x56e4
			, (short)0xb00b, (short)0x9c1d, (short)0x43e1, (short)0x2002
			, (short)0x180c, (short)0x0808, (short)0x0808, (short)0x0804
			, (short)0x11c4, (short)0x1224, (short)0x1224, (short)0x0c18
		}
		,{	// ID=1002 kuma_walk_l1
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x0038, (short)0x1844, (short)0x27c4
			, (short)0x2c34, (short)0x300c, (short)0x2002, (short)0x4cc2
			, (short)0x4cc2, (short)0x4002, (short)0x4302, (short)0x2722
			, (short)0x1dc4, (short)0x1008, (short)0x0c1c, (short)0x33e2
			, (short)0x4001, (short)0x980d, (short)0xe80b, (short)0x0808
			, (short)0x0b08, (short)0x1e38, (short)0x20c4, (short)0x3f3c
		}
		,{	// ID=1003 kuma_walk_l2
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x0038, (short)0x1844, (short)0x27c4
			, (short)0x2c34, (short)0x300c, (short)0x2002, (short)0x4cc2
			, (short)0x4cc2, (short)0x4002, (short)0x4302, (short)0x2722
			, (short)0x1dc4, (short)0x1008, (short)0x0c18, (short)0x13e4
			, (short)0x1004, (short)0x18e4, (short)0x0908, (short)0x09fc
			, (short)0x1004, (short)0x33e6, (short)0x2412, (short)0x380e
		}
		,{	// ID=1004 kuma_walk_r1
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x1c00, (short)0x2218, (short)0x23e4
			, (short)0x2c34, (short)0x300c, (short)0x4004, (short)0x4332
			, (short)0x4332, (short)0x4002, (short)0x4062, (short)0x44e4
			, (short)0x23b8, (short)0x1008, (short)0x3830, (short)0x47cc
			, (short)0x8002, (short)0xb019, (short)0xd017, (short)0x1010
			, (short)0x10d0, (short)0x1c78, (short)0x2304, (short)0x3cfc
		}
		,{	// ID=1005 kuma_walk_r2
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x1c00, (short)0x2218, (short)0x23e4
			, (short)0x2c34, (short)0x300c, (short)0x4004, (short)0x4332
			, (short)0x4332, (short)0x4002, (short)0x4062, (short)0x44e4
			, (short)0x23b8, (short)0x1008, (short)0x1830, (short)0x27c8
			, (short)0x2008, (short)0x2718, (short)0x1090, (short)0x3f90
			, (short)0x2008, (short)0x67cc, (short)0x4824, (short)0x701c
		}
		,{	// ID=1006 kuma_bye1
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x1c1c, (short)0x2222, (short)0x47e2
			, (short)0x4c1a, (short)0x5004, (short)0x2002, (short)0x4662
			, (short)0x4662, (short)0x4002, (short)0x4182, (short)0x2392
			, (short)0x56e4, (short)0xb008, (short)0x9c1c, (short)0x43e2
			, (short)0x2001, (short)0x180d, (short)0x080b, (short)0x0808
			, (short)0x1004, (short)0x33e6, (short)0x2412, (short)0x380e
		}
		,{	// ID=1007 kuma_bye2
			  (short)0x0010, (short)0x0018, (short)0x0002, (short)0x0000
			, (short)0x0101, (short)0x0000, (short)0x0000, (short)0x0000
			, (short)0x0000, (short)0x1c1c, (short)0x2222, (short)0x47e2
			, (short)0x4c1a, (short)0x5004, (short)0x2002, (short)0x4662
			, (short)0x4662, (short)0x4002, (short)0x4982, (short)0x3592
			, (short)0x26e4, (short)0x4808, (short)0x4c1c, (short)0x23e2
			, (short)0x2001, (short)0x180d, (short)0x080b, (short)0x0808
			, (short)0x1004, (short)0x33e6, (short)0x2412, (short)0x380e
		}
	};

	/**
	 * Kuma bitmaps.
	 */
	static Bitmap[] kumaBitmap = {
		  new Bitmap(pattern[0])
		, new Bitmap(pattern[1])
		, new Bitmap(pattern[2])
		, new Bitmap(pattern[3])
		, new Bitmap(pattern[4])
		, new Bitmap(pattern[5])
		, new Bitmap(pattern[6])
		, new Bitmap(pattern[7])
	};

	/**
	 * The bitmap information.
	 */
	static final int BITMAP_WIDTH = 16;
	static final int BITMAP_HEIGHT = 24;

	/**
	 * The active state of Kuma.
	 */
	static final int STATE_WAIT = 1;
	static final int STATE_HAPPY = 2;
	static final int STATE_WALK = 3;
	static final int STATE_GOAWAY = 4;
	static final int STATE_BYE = 5;

	/**
	 * The x and y step for kuma movement.
	 */
	static final int[] X_STEP = { 2,2,2,-2,-2,-2 };
	static final int[] Y_STEP = { 2,0,-2,-2,0,2 };

	/**
	 * The old x coordinate of top-left corner of a character.
	 */
	int ox;

	/**
	 * The old y coordinate of top-left corner of a character.
	 */
	int oy;

	/**
	 * The x coordinate of top-left corner of a character.
	 */
	int x;

	/**
	 * The y coordinate of top-left corner of a character.
	 */
	int y;

	/**
	 * The x step to move kuma.
	 */
	int vx;

	/**
	 * The y step to move kuma.
	 */
	int vy;

	/**
	 * The duration of the current state continues.
	 */
	int duration;
	
	/**
	 * The flag which indicate Kuma is on the edge of field. 
	 */
	boolean edge;
	
	/**
	 * The flag which indicate Kuma bitmap appears on the screen. 
	 */
	boolean appear;
	
	/**
	 * This method is invoked when manager starts to manage this character.
	 * @param mgr the animation manager.
	 */
	public void start(AnimManager mgr)
	{
		super.start(mgr);
		Rectangle bounds = mgr.getFieldBounds();
		x = bounds.x + AnimManager.random((bounds.width - BITMAP_WIDTH) / 2) * 2;
		y = bounds.y + bounds.height - BITMAP_HEIGHT;
		setState(STATE_WALK);
		vy = -2;
		appear = true;
	}

	/**
	 * Sets the state.
	 * @param the state of character.
	 */
	public void setState(int state)
	{
		this.state = state;
		switch(state)
		{
		case STATE_WAIT:
			vx = 0;
			vy = 0;
			duration = 8;
			break;

		case STATE_WALK:
			randOrient();
			duration = AnimManager.random(10) * 4 + 16;
			break;

		case STATE_GOAWAY:
			if (vx == 0 && vy == 0) {
				randOrient();
			}
			else {
				vx = -vx;
				vy = -vy;
			}
			duration = 1000;
			break;
			
		case STATE_HAPPY:
		case STATE_BYE:
			vx = 0;
			vy = 0;
			duration = 16;
			break;
		}
	}

	/**
	 * Sets kuma moving step in random.
	 */
	public void randOrient()
	{
		int r = AnimManager.random(6);

		vx = X_STEP[r];
		vy = Y_STEP[r];
	}

	/**
	 * Kuma go away.
	 */
	public void bye()
	{
		if (state != STATE_GOAWAY			&& state != STATE_HIDE			&& state != STATE_BYE) {
			setState(STATE_GOAWAY);
		}
	}

	/**
	 * Move the character.
	 */
	public void move()
	{
		// calc new position
		ox = x;
		oy = y;
		x += vx;
		y += vy;
	
		// detect field edge
		Rectangle bounds = mgr.getFieldBounds();
		boolean edge = false;
		if (x < bounds.x || x > bounds.x + bounds.width - BITMAP_WIDTH) {
			vx = -vx;
			x = ox;
			edge = true;
		}
		if (y < bounds.y || y > bounds.y + bounds.height - BITMAP_HEIGHT) {
			vy = -vy;
			y = oy;
			edge = true;
		}
		
		// if edge then bye.
		if (edge && state == STATE_GOAWAY) {
			setState(STATE_BYE);
			return;
		}
		
		// decrease counter
		duration --;
		if (duration > 0) { return; }
		
		// random number to determine kuma's behavior.
		int r = AnimManager.random(5);

		// change state
		switch(state)
		{
		case STATE_WALK:
			if (r == 0) { setState(STATE_HAPPY); }
			else { setState(STATE_WAIT); }
			break;
		
		case STATE_HAPPY:
		case STATE_WAIT:
			setState(STATE_WALK);
			break;

		case STATE_BYE:
			setState(STATE_HIDE);
			break;
		}
	}
	
	/**
	 * Draw the character on the given graphics.
	 * @param g the graphics.
	 */
	public void paint(Graphics g)
	{
		// clear bitmap
		if (state == STATE_HIDE && appear) {
			appear = false;
			g.drawRectangle(ox, oy, BITMAP_WIDTH, BITMAP_HEIGHT
				, Graphics.ERASE, 0);
		}
		else if (x != ox || y != oy) {
			g.drawRectangle(ox, oy, BITMAP_WIDTH, BITMAP_HEIGHT
				, Graphics.ERASE, 0);
		}
		
		// draw bitmap
		int bmid;
		switch(state)
		{
		case STATE_WALK:
		case STATE_GOAWAY:
			if (vx <= 0) {
				bmid = 2 + (duration & 0x01);
			}
			else {
				bmid = 4 + (duration & 0x01);
			}
			break;
		case STATE_WAIT:
			bmid = 0;
			break;
		case STATE_HAPPY:
			bmid = (duration & 0x02) >> 1;
			break;
		case STATE_BYE:
			bmid = 6 + ((duration & 0x02) >> 1);
			break;
		default:
			return;
		}
		g.drawBitmap(x, y, kumaBitmap[bmid]);
	}
}

⌨️ 快捷键说明

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