Planning the Game Story
Every game start with a story. So you need to think. Not actually 😛 because I have already decided what we will be creating.
So In our game there will be a Space Jet, and the jet has to kill the enemies by hitting them. There will also be some good guys and our Space Jet should not kill them. So basically if our Space Jet touches other character that will be destroyed. So we have to only kill the enemies.
The Game Rules
- Player ship has to kill the enemies by colliding with them.
- If player lets 3 enemies to go safely then the game is over.
- If player kills a good guy the game is over.
Android Game Development Tutorial
We have decided the game story and the rules. Now its time to create the game. We will use Android Studio. And I am assuming all of you got Android Studio and know the basics of it.
Android Game Development Tutorial – Video Demo
Before going ahead in this tutorial you can check out this video to know what you will get at the end of this Android Game Development Tutorial part 1.
Download the Images Used
You can design and create your own character using Adobe Photoshop or other photo editing program. But if you don’t want to do it yourself you can download the images I used in this project from the link given below.
Creating a New Project
- Open Android Studio and create a new project.
- Once the project is loaded you will get MainActivity.java and activity_main.xml
- First paste all the images that you downloaded inside the drawable folder of your project.
@Override
public void onClick(View v) {
//starting game activity
startActivity(new Intent(this, GameActivity.class));
}
- Now you need to create a new activity named GameActivity. To create a new activity right click on the package name -> new -> activity -> empty activity
- The above class is our GameView class. It is the actual game panel where we will play the game. The class is implementing Runnable interface. We have a volatile boolean type variable running that will track whether the game is running or not. After that we have our gameThread, it is the main game loop. Then we have the constructor to the class. We are not doing anything inside the constructor right now. Then we have the overriden method run(), here we are running a loop until the playing variable running is true. Inside the loop we are calling the following methods.
update() -> Here we will update the coordinate of our characters.
draw() -> Here we will draw the characters to the canvas.
control() -> This method will control the frames per seconds drawn. Here we are calling the delay method of Thread. And this is actually making our frame rate to aroud 60fps.
After these we have two more methods.
pause() -> To pause the game, we are stopping the gameThread here.
resume() -> To resume the game, here we are starting the gameThread.
Adding GameView to GameActivity
- After tapping the play now button we are launching the GameActivity. We will set our GameView to the content of this activity. So go to GameActivity.java and modify the code as below.
- The above code is very easy to understand. I have written comments to explain everything. So lets move ahead.
Drawing Player to GameView
- To draw the player to our GameView you need to come back to the GameView.java class and modify it as below.
Adding Controls
We will now add control to the player’s Space Jet. When the player will tap on the screen the Space Jet will boost up and after releasing the screen the Space Jet will boost down. The movement is inspired by the most popular game Flappy Bird.
To add this control we will need to detect touches of the screen. So lets begin.
- Come inside GameView.java file and override the method onTouchEvent().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
//When the user presses on the screen
//we will do something here
break;
case MotionEvent.ACTION_DOWN:
//When the user releases the screen
//do something here
break;
}
return true;
}
|
- Currently we need these two events only which are ACTION_UP and ACTION_DOWN. What we need to do is we need to boost up the Space Jet on ACTION_UP and boost down the Space Jet on ACTION_DOWN.
Adding Booster to Space Jet
Now we will add the booster to our Space Jet so that our player can control the Space Jet. Follow these steps to do this.
- Modify the code of Player.java file as follows.
沒有留言:
張貼留言