Skip to main content
 

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


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

Hi Guys, So, we have completed the "Game Loop" and we have successfully created a rectangle in our screen. Now in this article, we will describe our obstacles size, width, height, and the gap from where our player can pass. And In the next article, we will draw obstacles on the screen. If you have not read, previous articles yet, then click here. So, Let's get started with our tutorial.

Create an Object

Create an object and Name it "Constants"




In the constants object, we will store the screen height, screen width, and the context, init time of the game. Copy below codes and paste it into your constants object.

1
2
3
4
5
6
7
object Constants {
    var SCREEN_WIDTH:Int = 0
    var SCREEN_HEIGHT:Int = 0
    var CURRENT_CONTEXT: Context? = null

    var INIT_TIME:Long = 0
}

Now, describe the screen width and screen height in the main activity. Just copy the below codes and paste it on your main activity.class.

1
2
3
4
   val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        Constants.SCREEN_WIDTH = dm.widthPixels
        Constants.SCREEN_HEIGHT = dm.heightPixels

Create a Class

Create a Kotlin class, Name it "Obstacle". In the obstacle class, we stored the obstacle height, width, and player gap. In the obstacle, we describe the two rectangles, which intersects each another. And In the next article, I will draw obstacles on the surface of the screen. Copy the below codes and paste it on your obstacle.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
class Obstacle(rectHeight :Int,private val color:Int,startX:Int,startY:Int,playerGap:Int):GameObject {
    val rectangle:Rect
    private val rectangle2:Rect

    init{
        rectangle = Rect(0,startY,startX,startY + rectHeight)
        rectangle2 = Rect(startX+ playerGap,startY,Constants.SCREEN_WIDTH,startY+rectHeight)

    }

    fun playerCollide(player:RectPlayer):Boolean {
        return Rect.intersects(rectangle,player.rectangle) || Rect.intersects(rectangle2,player.rectangle)
    }

    override fun update() {

    }

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


    }
 }

Remove the Action Bar

Click on the res folder.


Then, click on the values folder.
After, that click on the styles.xml
Now, change the name from "DarkActionBar" to "NoActionBar".


Conclusion

So, In this article, we have created the obstacle height, width, dimension, and the player gap. In the next article, I will draw obstacles on the surface of the screen. I hope you found this article helpful. If you like this article then, visit again. 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