Creating TetrisView

So far, so good. We have successfully implemented classes to model blocks, frames, and shapes of different tetrominoes that will be used within the application, as well as implemented an AppModel class to coordinate all the interactions between views and these programmatic components created. Without this view existing, there is no means by which a user can interact with AppModel. If a user cannot interact with the game, the game might as well not exist. In this section, we will implement TetrisView, the user interface by which a user will play Tetris.

Create a package named view in your source package and add a TetrisView.kt file in it. As we want TestrisView to be a View, we must declare it to extend the View class. Add the code below to TetrisView.kt

package com.mydomain.tetris.views

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.View
import android.widget.Toast
import com.mydomain.tetris.constants.CellConstants
import com.mydomain.tetris.GameActivity
import com.mydomain.tetris.constants.FieldConstants
import com.mydomain.tetris.models.AppModel
import com.mydomain.tetris.models.Block

class TetrisView : View {

private val paint = Paint()
private var lastMove: Long = 0
private var model: AppModel? = null
private var activity: GameActivity? = null
private val viewHandler = ViewHandler(this)
private var cellSize: Dimension = Dimension(0, 0)
private var frameOffset: Dimension = Dimension(0, 0)

constructor(context: Context, attrs: AttributeSet) :
super(context, attrs)

constructor(context: Context, attrs: AttributeSet, defStyle: Int) :
super(context, attrs, defStyle)

companion object {
private val DELAY = 500
private val BLOCK_OFFSET = 2
private val FRAME_OFFSET_BASE = 10
}
}

The TetrisView class is declared to extend View. View is a class that all application view elements must extend. As the View type has a constructor that must be initialized, we are declaring two secondary constructors for TetrisView that initialize two distinct constructors of the view class, depending on which secondary constructor is called.

The paint property is an instance of android.graphics.Paint. The Paint class holds style and color information concerning how to draw texts, bitmaps, and geometries. lastMove will be used to keep track of the last time in milliseconds that a move was made. The model instance will be used to hold an AppModel instance that will be interacted with by TetrisView to control gameplay. Activity is an instance of the GameActivity class we created. The cellSize and frameOffset are properties that will hold dimensions for the size of cells in the game and the frame offset, respectively.

Neither ViewHandler nor Dimension is a class provided to us by the Android application framework. We must implement these two classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236