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

本文介绍GameView组件如何根据游戏状态数据绘制游戏界面,包括方块和连接线的绘制过程。GameView通过重写onDraw方法实现绘制,并利用GameService获取游戏状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

GamaView的代码如下。

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

public class GameViewextends 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];

                               // 根据方块左上角XY坐标绘制方块

                               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是游戏的业务逻辑实现类。后面会详细介绍该类的实现,此处暂不讲解。

 

本文节选自《疯狂Android讲义(含CD光盘1张)》一书。

图书详细信息:http://blog.youkuaiyun.com/broadview2006/article/details/6609027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值