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
Post a Comment