Develop Android 2D Game of Spinning The Bottle
I hope you guys are enjoying the android tutorial so far and are building some exciting apps using android. Through this article, we will cover another interesting app, which would be a 2D game. Many of us are fascinated by games, whether PUBG Call of Duty or some other well-known game. Games are the way to refresh your mind and provide a kind of entertainment to various people.
From this, we can understand that games have a vast market around the globe and people love to explore new types of games. In that scenario, if you know game development in android, you can target such an audience and make a earning out of that app.
In this article, we will try to build a 2D spinning a bottle game that helps us while playing games like Truth or Dare. So, let’s not waste time and just dig into the methods you need to follow to build your spinning the bottle game.
About Spinning The Bottle Game
Spinning the bottle, as the name suggests, is a game where the bottle rotates and stops after some time when you tap on the bottle. We can use this to play several games like Truth or Dare. In the case of Truth or Dare, you can use this spinning of the bottle to choose a random friend among your group.
Prerequisites:
The tutorial is for any beginner starting with android development and doesn’t require much knowledge in android. However, knowing the following concepts beforehand would be an upper hand in understanding the steps clearly.
- Android Studio and its tools
- Android Activities
- Android Views and ViewGroups
- Android Event Listeners
- XML
- Kotlin Programming Basics
Project Assets and Source Code
Before proceeding with the actual steps to implement the game, you need to download a few things mentioned below. These files include the source code and the assets(like images) essential for building up the project. So, go ahead and download the below files and then proceed with the steps.
Source Code of How to develop 2D game using Android:2D Game Android Project
Steps to build Spinning a Bottle Game in Android
1: Start your Android Studio and create a new project named “SpinningABottle”, as shown below.
2: Now, you need to place the downloaded images into the drawable folder. To do so, navigate to res/drawable and paste the pictures there as shown below.
3: First, we will style our layout and provide the background and bottle in the activity_main.xml file.
Code: activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" tools:context="com.techvidvan.spinningabottle.MainActivity"> <ImageView android:id="@+id/bottleView" android:layout_width="310dp" android:layout_height="310dp" android:src="@drawable/bottle" android:layout_centerInParent="true" android:onClick="spinTheBottle"/> </RelativeLayout>
4: Now come back to your MainActivity.kt file and add the below code there.
Code: MainActivity.kt
package com.techvidvan.spinningabottle import android.os.Bundle import android.view.View import android.view.animation.Animation import android.view.animation.RotateAnimation import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { //Declaring Variables private lateinit var bottleObj: ImageView private val random: Random = Random() private var endDir = 0 private var rotation = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //Binding the variable to the bottle view bottleObj = findViewById(R.id.bottleView) } fun spinTheBottle(v: View?) { if (!rotation) { //Specifying the position of the bottle val currDir: Int = random.nextInt(1800) val pivotX: Float = (bottleObj.getWidth() / 2).toFloat() val pivotY: Float = (bottleObj.getHeight() / 2).toFloat() //Rotating the bottle provided the pivots val rotate: Animation = RotateAnimation( endDir.toFloat(), currDir.toFloat(), pivotX, pivotY ) //setting the duration of rotation rotate.duration = 3000 rotate.fillAfter = true //Setting Animation Listener rotate.setAnimationListener(object : Animation.AnimationListener { override fun onAnimationStart(animation: Animation) { rotation = true } override fun onAnimationEnd(animation: Animation) { rotation = false } override fun onAnimationRepeat(animation: Animation) {} }) //Locating the end direction of the bottle after rotation endDir = currDir //calling the Animation method to start animation bottleObj.startAnimation(rotate) } } }
Now, your application is ready. Go ahead and run it on your device or emulator and notice the output.
Summary
Through this article, you came across a 2D game on android. You saw how building 2D games are more accessible in android studio. Later, you saw steps to build your spinning bottle game in android. I hope you enjoyed the tutorial and found it quite simple to implement. Finally, I would say that you can self develop several games by using some simple animations.