Finishing up GameActivity

So far, you have successfully implemented the views, handlers, helper functions, classes, and models necessary to put the Tetris game together. Now we are going to finish up the work we started by putting it all together in GameActivity. The first thing on our agenda is to add the newly created tetris view to the game activity's layout. We can easily add TetrisView as a child element anywhere within a layout file by utilizing the <com.mydomain.tetris.views.TetrisView> layout tag:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mydomain.tetris.GameActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
android:background="#e8e8e8">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="32dp"
android:paddingBottom="32dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current_score"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_current_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin_top"
android:text="@string/high_score"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_high_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
<Button
android:id="@+id/btn_restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_restart"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9">
<!-- Adding TetrisView -->
<com.mydomain.tetris.views.TetrisView
android:id="@+id/view_tetris"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Once you have added the tetris view to activity_game.xml, open the GameActivity class and employ the changes to the class shown in the following code block:

package com.mydomain.tetris

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.mydomain.tetris.models.AppModel
import com.mydomain.tetris.storage.AppPreferences
import com.mydomain.tetris.views.TetrisView

class GameActivity: AppCompatActivity() {

var tvHighScore: TextView? = null
var tvCurrentScore: TextView? = null
private lateinit var tetrisView: TetrisView

var appPreferences: AppPreferences? = null
private val appModel: AppModel = AppModel()

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
appPreferences = AppPreferences(this)
appModel.setPreferences(appPreferences)

val btnRestart = findViewById<Button>(R.id.btn_restart)
tvHighScore = findViewById<TextView>(R.id.tv_high_score)
tvCurrentScore = findViewById<TextView>(R.id.tv_current_score)
tetrisView = findViewById<TetrisView>(R.id.view_tetris)
tetrisView.setActivity(this)
tetrisView.setModel(appModel)

tetrisView.setOnTouchListener(this::onTetrisViewTouch)
btnRestart.setOnClickListener(this::btnRestartClick)

updateHighScore()
updateCurrentScore()
}

private fun btnRestartClick(view: View) {
appModel.restartGame()
}

private fun onTetrisViewTouch(view: View, event: MotionEvent):
Boolean {
if (appModel.isGameOver() || appModel.isGameAwaitingStart()) {
appModel.startGame()
tetrisView.setGameCommandWithDelay(AppModel.Motions.DOWN)

} else if(appModel.isGameActive()) {
when (resolveTouchDirection(view, event)) {
0 -> moveTetromino(AppModel.Motions.LEFT)
1 -> moveTetromino(AppModel.Motions.ROTATE)
2 -> moveTetromino(AppModel.Motions.DOWN)
3 -> moveTetromino(AppModel.Motions.RIGHT)
}
}
return true
}

private fun resolveTouchDirection(view: View, event: MotionEvent):
Int {
val x = event.x / view.width
val y = event.y / view.height
val direction: Int

direction = if (y > x) {
if (x > 1 - y) 2 else 0
}
else {
if (x > 1 - y) 3 else 1
}
return direction
}

private fun moveTetromino(motion: AppModel.Motions) {
if (appModel.isGameActive()) {
tetrisView.setGameCommand(motion)
}
}

private fun updateHighScore() {
tvHighScore?.text = "${appPreferences?.getHighScore()}"
}

private fun updateCurrentScore() {
tvCurrentScore?.text = "0"
}
}

We added an object reference to the tetris view layout element in activity_game.xml in the form of the tetrisView property; we also created an instance of AppModel that will be used by GameActivity. In oncreate(), we set the activity in use by tetrisView to the current instance of the GameActivity and set the model in use by tetrisView to appModel – the AppModel instance property we created. In addition, the on-touch listener for tetrisView was set to the onTetrisViewTouch() function.

If tetrisView is touched and the game is in an AWAITING_START or OVER state, a new game is started. If tetrisView is touched and the game is in its ACTIVE state, the direction in which the touch on tetrisView occurred is resolved with the help of resolveTouchDirection(). moveTetromino() is used to move a tetromino block based on the action passed to it. If a left touch occurred, moveTetromino() is called with AppModel.Motions.LEFT set as its argument. This results in the movement of the tetromino to the left on the field. Right, down, and up touches on tetrisView result in rightward, downward, and rotational motions.

Having made all the additions, build and run the project. Once the project launches on your desired device, navigate to game activity and touch the tetris view to the right of the screen. The game will start:

Feel free to play around with the game you created. You deserve it!

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

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