Now you can Subscribe using RSS

Submit your Email

Saturday

How to make a split screen game

Vishal Shrestha
split-screen-game

Brief Intro- (If you are impatient, you can directly skip to the example below)
Making split screen games might seem a complex at first, but it's definitely not so if you can make single screen versions of the same game. Think about it, the mobile device doesn't know if two player are playing or one player or many players for the matter of fact. Only the players playing recognize that and usually this two player illusion is created by dividing the screen into two parts by drawing a boundary in between the screen. That's it. Now all you gotta do is just handle the scores. That's pretty simple in itself. Let's look at an example-

Let's assume you are making a game that shows two different type of ball on two half of screen and the player who tap his/her respective ball more in the given time wins.
Let's get into the algorithm-

First of all let's draw a boundary dividing the upper and lower half of the screen.

Bitmap boundary = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.boundary), getWidth(), getHeight()/50, true);



//Drawing the bitmap with X=0, Y=height/2 i.e middle of the screen

canvas.drawBitmap(boundary, 0, getHeight()/2, null); 


Now let's draw a ball in upper half of screen, let's call it ball1 and let's draw ball2 in lower half of the screen.

//Getting ball for players
        ball1 = BitmapFactory.decodeResource(getResources(), R.drawable.player1);

        ball2 = BitmapFactory.decodeResource(getResources(), R.drawable.player2);

//Calling function that will draw the ball

      draBall(ball1, 1);  drawBall(ball2, 2);



Now in drawBall lies the main logic of the game,
void drawBall(Bitmap ball, int i)

{

   int x, y;

// Checking to choose the region in which we need to draw ball.

   if(i==1)

   {

      // x can have any value but y it only upto top half of the screen, nextDouble returns value

      // from 0-0.9 so multiplying it with getHeight()/2 can't give y greater than half the screen.

      x = (int) (random.nextDouble() * getWidth());

      y = (int) (random.nextDouble() * (getHeight() / 2));

   }

   else

   {

      // x can have any value but y it only upto bottom half of the screen 

      x = (int) (random.nextDouble() * getWidth());

      y = (int) (random.nextDouble() * (getHeight() / 2));

      y = getHeight() - y;

   }

   canvas.drawBitmap(ball, x, y, null); 

}


Now all we gotta do is, recognize the ball tap and then remove it and draw new ball according to the tapped ball. i.e if ball1 is tapped then, call drawBall(ball1, 1) else call drawBall(ball2, 2).
This is a very basic algorithm for making split screen games however you can build up from this and create you own complex games. Do comment if you have any queries.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha