Skip to main content
 

How to make a 2D Game in Android Studio Using Koltin 002


Hi Guys, In the previous article, we have learned how to develop the game loop in the android studio using Kotlin. So, In this article, we will learn how to draw characters on the surface. If you have not read the previous article yet, click here to read the previous article tutorial of "How to make a 2D Game in Android Studio Using Koltin 002". So, let get started with our tutorial.

Create an Interface

  • Now, create an Interface in the project.
  • Left Click on the "com.example.user.game".


  • Then, select "new" and after that, click on "Kotlin File/Class". 


  • After that, Name it "GameObject" and In the kind section, select Interface and Click Ok.




We are using interface in the project because when we add methods in the interface and implement the interface in the class, we want to add the same method in the class as an override method. In the next paragraph, I will explain it in more detail.

Add two function in the Interface. The first function is "update" and the second function is drawn.

1
2
3
4
interface GameObject {
    fun draw(canvas: Canvas)
    fun update()
}

Create A New Class

Now, create a new class. Name it "RectPlayer", and extend it to Game Object and we want to add the method as override method in the "Rect Player", which we have added in the game object. In the Rect Player, we will draw a rectangle and give the dimension of the rectangle. The rectangle is our first character in the game. So, add the codes below in your project.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class RectPlayer(var rectangle: Rect, private var color:Int ):GameObject {

    init{
        this.rectangle = rectangle
        this.color = color
    }

    override fun draw(canvas: Canvas) {
        val paint = Paint()
        paint.setColor(color)
        canvas.drawRect(rectangle,paint)

    }

    override fun update() {
    }

    fun update(point: Point) {
        rectangle.set(point.x - rectangle.width()/2,
            point.y - rectangle.width()/2,
            point.x + rectangle.width()/2,
            point.y + rectangle.width()/2)
    }
}

Now, we have completed with our "RectPlayer" and Now, you want to add some more codes in the GamePanel class. In the game panel class, we will describe the touch event, the color of the rectangle and background color in the game panel class. So, add below codes in the game panel class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class GamePanel(context: Context): SurfaceView(context), SurfaceHolder.Callback{
    private val thread:MainThread
    private val player:RectPlayer
    private var playerPoint: Point? = null

    init {
        holder.addCallback((this))
        thread = MainThread(holder,this)
        isFocusable = true
        player = RectPlayer(Rect(100,100,200,200), Color.rgb(255,0,0))
        playerPoint = Point(150,150)

    }


    override fun surfaceChanged(holder: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
        thread.setRunning(true)
        thread.start()

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        var retry =true
        while(retry) {
            try{
                thread.setRunning(false)
                thread.join()

            }catch (e:Exception) {
                e.printStackTrace()
            }
            retry = false
        }

    }

    override fun surfaceCreated(holder: SurfaceHolder) {

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event!!.action){
            MotionEvent.ACTION_DOWN -> {
            }
            MotionEvent.ACTION_MOVE ->  playerPoint!!.set(event.x.toInt(),event.y.toInt())
        }
        return true
        //return super.onTouchEvent(event)
    }

    override fun draw(canvas: Canvas?) {
        super.draw(canvas)

        canvas!!.drawColor(Color.WHITE)
        player.draw(canvas)

    }

    fun update() {
        player.update(playerPoint!!)

    }
}

Conclusion

Let, try the game in the emulator. You can try the game in the real device also.


We have successfully created the rectangle on the screen. In the next article, We will create obstacles and remove the action bar from the screen. If you found this article helpful, then please visit next time in the website. Thank you. 

Comments

Popular posts from this blog

How to Create Calculator in Android Studio Using Kotlin

Hi Friends, calculator app is very easy to create in the android studio using Kotlin language as a language. In this article, I will write " How to Create Calculator in Android Studio Using Kotlin ".In this calculator app, I will design a beautiful user interface which you can publish in the play store. Generally, people only write about the logic of the app, but here I will write logic with an elegant user interface. So, let jump into the android studio. Let get started with calculator android app: Step 1: Create a new project in the android studio. Now, give the name of the application. Make sure you check the box of Kotlin support. Step 2: Select the API Level as per your wish. Select the Empty Activity in the project, And click next. Then, click finish and your project will be loaded. Now, for designing a beautiful User Interface. We will use Linear layout in our project. In the layout, there will be seven horizontal linear layouts. A

How to make a 2D Game in Android Studio Using Koltin

2D Game in Android Studio Using Koltin Hello guys, There are many games in the Play Store, which are developed by the much big company. If you want to develop, that type of games, then you surely move to Unity, which is a platform to developed 3D games. But if don't want to develop 3D games, then you can try android studio to develop 2D games. I completely agree that android studio is not for game development, but if you started with game development in the android studio, it will be very easy to develop an application in the android studio. In this tutorial post, we will learn " How to make a 2D Game in Android Studio Using Koltin ".We will use kotlin language for this tutorial. Our game will look like this after development: So, Let get started with the tutorial: Create a new project in Android Studio Create an empty project in the android studio. Name your application. Then select the Kotlin support. After that, select the API l

How to make image slider by view flipper in the android studio(kotlin)

Hi Guys, you have seemed professional android application which usually having image slider, So, in this, we will learn " How to make image slider by view flipper in the android studio(kotlin) ". activity_main.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" xmlns:app= "http://schemas.android.com/apk/res-auto" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" android:orientation= "vertical" > <ViewFlipper android:id= "@+id/v_flipper" android:layout_width= "match_parent" android:layout_height= "220dp" /> </LinearLayout> MainActivity.kt 1 2