shape.java

来自「培训老师写的nb代码,midp1.0画出来的游戏」· Java 代码 · 共 106 行

JAVA
106
字号
import javax.microedition.lcdui.Graphics;

public class Shape 
{
	private ShapeListener listener;
	
	private int[][] body;
	private int status;
	
	private int left;
	private int top;
	
	public static final int ROTATE 	= 1;
	public static final int DOWN 		= 2;
	public static final int LEFT 			= 3;
	public static final int RIGHT		= 4;
	
	public int getLeft(){return left;}
	public int getTop(){return top;}
	
	public Shape()
	{
		new Thread(new ShapDriver()).start();
	}
	public void rotate()
	{ 
		System.out.println("rect rotate");
		status = (status + 1)%body.length;
	}
	public void moveDown()
	{ 
		System.out.println("rect move down");
		top++;
	}
	public void moveLeft()
	{
		System.out.println("rect move left");
		left--;
	}
	public void moveRight()
	{
		System.out.println("rect move right");
		left++;
	}
	
	public void addListener(ShapeListener lis)
	{
		listener = lis;
	}
	public static final int CELL_SIZE = 20;
	public void paint(Graphics g)
	{
		System.out.println("Shape`s paint");
		g.setColor(0xFF0000);
		for(int x=0; x<4; x++)
		{
			for(int y=0; y<4; y++)
			{
				if(getFlagByPoint(x, y))
				{
					g.fillRoundRect((left+x)*CELL_SIZE, 
							(top+y)*CELL_SIZE,CELL_SIZE,CELL_SIZE,5,5);
				}
			}
		}
	}
	
	private boolean getFlagByPoint(int x, int y)
	{
		return body[status][y*4+x] == 1;
	}
	
	public boolean isMember(int x, int y, boolean rotate)
	{
		int tmpStatus = status;
		if(rotate)
		{
			tmpStatus = (tmpStatus+1)%body.length;
		}
		return body[tmpStatus][y*4+x] == 1;
	}

	public void setStatus(int status)
	{
		this.status = status;
	}
	
	public void setBody(int[][] body)
	{
		this.body = body;
	}
	
	private class ShapDriver implements Runnable
	{
		public void run()
		{
			while(listener.isShapeMoveable(Shape.this))
			{
				moveDown();
				listener.shapeMoveDown(Shape.this);
				try{Thread.sleep(1000);}catch(Exception e){}
			}
		}
	}
}

⌨️ 快捷键说明

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