返回

在Kotlin中构建打方块小游戏:趣味编程体验

Android

用Kotlin来实现一个打方块的小游戏

今天来做个打方块的小游戏,继续熟悉kotlin的语法,看下要实现的效果图

看着效果图好像挺难的样子,但理清思绪后,你会发现特别的简单,还是那句话,学习方法最重要

构造

先创建一个TextView显示分数,我这里再加一个显示最高分,方便大家扩展(有朋友会写数据库之类),定义id分别为score_tv,high_score_tv

    score_tv = findViewById(R.id.score_tv)
    high_score_tv = findViewById(R.id.high_score_tv)

然后创建一个视图view来显示方块,id设为square,并在layout的根部加一个相对布局,用来控制view的位置

    square = findViewById(R.id.square)
    square_parent = square.parent as RelativeLayout

接下来定义变量来记录各种状态,比如下落速度speed,方向direction,以及判定方块是否越界越界标志out,碰到了没有碰到block等

    var speed = 1 //下落速度
    var direction = 1 //0 left 1 right
    var out = false //是否越界
    var block = false //是否碰撞

接下来编写监听器,监听手机屏幕的触摸事件,来控制方块左右移动

    square.setOnTouchListener { _, event ->
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                if (event.x < square.x + square.width / 2) {
                    direction = 0
                } else {
                    direction = 1
                }
            }
        }
        true
    }

这个监听器比较好理解,当触摸事件发生的时候,根据触摸的位置来判断是往左还是往右

然后编写游戏循环,这个游戏循环是游戏的核心,用于控制方块的下落,还有随机生成新方块,还有随机下落小球

    object : CountDownTimer(Int.MAX_VALUE.toLong(), 15L) {
        override fun onTick(millisUntilFinished: Long) {
            //计算坐标
            val squareX = square.x.toInt()
            val squareY = square.y.toInt()
            val squareW = square.width
            val squareH = square.height

            //控制左右移动
            if (direction == 0 && squareX > 0) {
                square.x -= 5
            } else if (direction == 1 && squareX < (square_parent.width - squareW)) {
                square.x += 5
            }
            //碰撞判定
            if (!out && squareY >= (square_parent.height - squareH)) {
                out = true
            }
            //生成方块
            if (square_list.size < 5) {
                var top = 0
                if (square_list.size > 0) {
                    top = square_list[square_list.size - 1].top + square_list[square_list.size - 1].bottom
                }
                square_list.add(Square(Random().nextInt(square_parent.width - 100), top, 100, 30))
            }
            //碰撞判定
            square_list.forEach {
                if (it.y + it.bottom >= squareY && it.y <= squareY + squareH && it.x <= squareX + squareW && it.x + it.width >= squareX) {
                    block = true
                    if (it.y + it.bottom < squareY + squareH / 2) {
                        speed = 2
                    }
                }
            }
            //下落
            if (!block) {
                square.y += speed
            }
            //方块碰撞处理
            if (block) {
                score++
                if (score > high_score) {
                    high_score = score
                }
                score_tv.text = score.toString()
                high_score_tv.text = high_score.toString()
                square.y -= speed
                speed = 1
                direction = 1
                block = false
                out = false
            }
            //方块掉落处理
            if (out) {
                out = false
                direction = 1
                square.y = square_parent.height - square.height - 2
                speed = 1
                if (score > 0) {
                    score--
                }
                score_tv.text = score.toString()
            }
        }

        override fun onFinish() {
        }
    }.start()

这段代码就比较长了,但是逻辑很简单,首先我们获取到方块的x,y坐标,以及方块的宽高

然后根据触摸事件来改变方向,接着通过碰撞判定来判断方块是否碰到下方障碍,如果是就停止下落

接着随机生成新方块,方块的高度是随机的,方块的宽度是固定的,然后通过遍历方块列表来检测是否有方块与当前方块相撞

如果有就判定为碰到方块,并增加分数,然后重置方块的位置和速度

最后如果方块掉出了屏幕,就判定为失败,并减少分数

整个游戏就是这样,很简单,代码也非常简单,基本就是一些逻辑判断和数学运算,感兴趣的读者可以自己动手写一下,还是挺有意思的