技术开发 频道

疯狂连连看之游戏界面组件:GamaView

  【IT168技术】GameView主要就是根据游戏的状态数据来绘制界面上的方块,GameView继承了View组件,重写了View组件上onDraw(Canvas canvas)方法,重写该方法主要就是绘制游戏里剩余的方块;除此之外,它还会负责绘制连接方块的连接线。

  GamaView的代码如下。

  程序清单:codes\18\Link\src\org\crazyit\link\view\GameView.java

public class GameView extends View
{
    
// 游戏逻辑的实现类
    
private GameService gameService;          //
    
// 保存当前已经被选中的方块
    
private Piece selectedPiece;
    
// 连接信息对象
    
private LinkInfo linkInfo;
    
private Paint paint;
    
// 选中标识的图片对象
    
private Bitmap selectImage;
    
public GameView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.paint
= new Paint();
        
// 设置连接线的颜色
        this.paint.setColor(Color.RED);
        
// 设置连接线的粗细
        this.paint.setStrokeWidth(
3);
        this.selectImage
= ImageUtil.getSelectImage(context);
    }
    
public void setLinkInfo(LinkInfo linkInfo)
    {
        this.linkInfo
= linkInfo;
    }
    
public void setGameService(GameService gameService)
    {
        this.gameService
= gameService;
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        
if (this.gameService == null)
            return;
        Piece[][] pieces
= gameService.getPieces();            //
        
if (pieces != null)
        {
            
// 遍历pieces二维数组
            
for (int i = 0; i < pieces.length; i++)
            {
                
for (int j = 0; j < pieces[i].length; j++)
                {
                    
// 如果二维数组中该元素不为空(即有方块),将这个方块的图片画出来
                    
if (pieces[i][j] != null)
                    {
                        
// 得到这个Piece对象
                        Piece piece
= pieces[i][j];
                        
// 根据方块左上角X、Y坐标绘制方块
                        canvas.drawBitmap(piece.getImage().getImage(),
                            piece.getBeginX(), piece.getBeginY(),
null);
                    }
                }
            }
        }
        
// 如果当前对象中有linkInfo对象, 即连接信息
        
if (this.linkInfo != null)
        {
            
// 绘制连接线
            drawLine(this.linkInfo, canvas);
            
// 处理完后清空linkInfo对象
            this.linkInfo
= null;
        }
        
// 画选中标识的图片
        
if (this.selectedPiece != null)
        {
            canvas.drawBitmap(this.selectImage, this.selectedPiece.
            getBeginX(),
                this.selectedPiece.getBeginY(),
null);
        }
    }
    
// 根据LinkInfo绘制连接线的方法
    
private void drawLine(LinkInfo linkInfo, Canvas canvas)
    {
        
// 获取LinkInfo中封装的所有连接点
        List
<Point> points = linkInfo.getLinkPoints();
        
// 依次遍历linkInfo中的每个连接点
        
for (int i = 0; i < points.size() - 1; i++)
        {
            
// 获取当前连接点与下一个连接点
            Point currentPoint
= points.get(i);
            Point nextPoint
= points.get(i + 1);
            
// 绘制连线
            canvas.drawLine(currentPoint.x, currentPoint.y,
                nextPoint.x, nextPoint.y, this.paint);
        }
    }
    
// 设置当前选中方块的方法
    
public void setSelectedPiece(Piece piece)
    {
        this.selectedPiece
= piece;
    }
    
// 开始游戏方法
    
public void startGame()
    {
        this.gameService.start();
        this.postInvalidate();
    }
}

  上面的GameView中第一段粗体字代码用于根据游戏的状态数据来绘制界面中的所有方块,第二段粗体字代码则用于根据LinkInfo来绘制两个方块之间的连接线。

疯狂连连看之开发游戏界面组件二

  上面的程序中①号代码处定义了GameService对象,②号代码则调用了GameService的getPieces()方法来获取游戏中剩余的方块,GameService是游戏的业务逻辑实现类。后面会详细介绍该类的实现,此处暂不讲解。

0
相关文章