android-Snake Games-analysis and implementation-kotlin language description

Source: Internet
Author: User

android-Snake Games-analysis and Implementation-kotlin Language description overview

The main content of this chapter is the analysis and implementation of the Snake games, the specific code for implementation can be found in the GitHub link at the bottom of the article.

The entire game is implemented through continuous drawing through in, view.

Look first.

Analyze which game objects are available

No doubt, whether Kotlin or Java are object-oriented programming language, then we want to realize the first step of the snake game is to analyze the game of what objects, already have the object are analyzed completely, we can then develop, from which we can find a minimum of two objects.

    • Food, the snake will grow after eating the food
    • Snake, you can clearly see that the snake is made up of a small block, then each one of the block is an object, all the block objects into a set, the collection is composed of the snake. The first element of the collection is 蛇头 to show the difference, using a different color to distinguish the head from the body.
    • Extracting the parent class, whether it is the block or the food object that makes up the snake, is essentially a game object, then we can extract the two objects to a common parent class, defining common properties and methods in the parent class.

Well, whether it is greedy and food, it needs to be drawn out, then a reference to the drawing then have to mention the coordinates, the calculation of coordinates is quite cumbersome and error-prone, then we have to find a way to abstract the coordinates. From the example of the game we can find that all the game objects are the two-dimensional planar structure that can be determined by the index of a row, and the index of a class, then we can abstract the original coordinates into a row and column structure. The members of the public parent class here are basically OK, and the structure of our classes can be determined. The structure of the extracted classes is as follows.

/** * 贪吃蛇游戏中所有游戏的父类,所有的游戏对象都将会从此类继承 * */open class GameObject(var row: Int, var column: Int) {    /**     * 绘制游戏对象     * @param canvas 画布对象     * @param paint 画笔对象     * */    open fun draw(canvas: Canvas, x: Float, y: Float, paint: Paint) {        canvas.drawRect(x, y, x + SnakeGameConfiguration.GRID_WIDTH, y + SnakeGameConfiguration.GRID_HEIGHT, paint)    }}/** * 贪吃蛇吃的食物 * */class Food(row: Int, column: Int) : GameObject(row, column) {    override fun draw(canvas: Canvas, x: Float, y: Float, paint: Paint) {        val gw = SnakeGameConfiguration.GRID_WIDTH        val gh = SnakeGameConfiguration.GRID_HEIGHT        canvas.drawCircle(x + (gw / 2), y + (gh / 2), gw / 2, paint)    }}/** * 因为蛇的组成是块状的,此对象就是组成蛇的块 * */class SnakeBlock(row: Int, column: Int, val isHead: Boolean) : GameObject(row, column) {}
Extracting the game's configuration parameters

In the game of snake, the length of the map is fixed (that 行 列 is, the number is fixed), and the width and height of each game object are calculated according to the size of the view and the number of rows and columns. These values can be stored as global variables for later modification and invocation.

object SnakeGameConfiguration {    var GRID_WIDTH = 0F    var GRID_HEIGHT = 0F    val GAME_COLUMN_COUNT = 20    val GAME_ROW_COUNT = 20}
How the Snake moves

This logic in gluttonous and swim is a difficult point, because the snake can be many times turn, moving the head of the realization of the idea below.

    1. First, start with the tail of the snake and move forward to the position of the object that makes up the block of the snake.
    2. And then the second to last, the third position to move, in turn, the class diagram
    3. After the position of the snake head, move the snake head according to the moving direction

when the snake's position is moved, the collision detection can be carried out .

Collision detection

Collision detection for ordered objects is divided into three cases, as to how collision detection only needs to detect if rows and columns are outside the game map range, or if they are coincident with other game objects.

    1. The snake is beyond the boundaries of the map-can be seen as a wall
    2. Gluttonous and eat yourself
    3. The snake's late food needs to be the length of the snake +1, that is, in the tail of the snake to increase the formation of a snake block

For the three cases above, we can extract two callback functions.

interface OnCrashListener {    fun onCrash()}/** * 贪吃蛇吃到食物的回调 * */interface OnEatenFoodListener {    fun onEaten()}
SOURCE download

Some of the rest of the relatively simple places, there is no article on the explanation, you can go to my github download all the source code.

Greedy Snake Games Source download

android-Snake Games-analysis and implementation-kotlin language description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.