Making an Android game in three hours

in #programming7 years ago

Pong header.jpg
About a week ago, I wrote that I had just started learning app development in Android Studio, utilizing the LibGDX framework. After spending a couple of hours doing simple animations and making objects bounce off each other, I started to feel confident about making an actual game.

Pong

As part of software architecture class, we were given the exercise of implementing the classic game pong.
Exciting_Pong.gif
(Source: http://uncyclopedia.wikia.com/wiki/Monitor)

We were, however, told that we were free to make any changes we would like, so rather than simulating table tennis as seen from above, I wanted to implement it as seen from the side. My main motivation for this was that I wanted to see how well I could simulate gravity.
Side Pong.jpg

More like badminton?

As you can see from the image above, my "realistic" side view doesn't really represent a good model of actual table tennis. There is no table for the ball to bounce on, so I guess my game is closer to representing badminton or volleyball.

Adding gravity

Making the ball travel in a nice parabolic trajectory proved to be way easier than I first thought. At any moment, the ball has a velocity in the x-direction and a velocity in the y-direction. For every frame of the game, the velocity in the y-direction is reduced (I have defined positive y-direction to be upwards) by a chosen gravity constant.

Initial condition

Having simulated gravity successfully would have been of little use if there was no way to get the ball in the air in the first place. I spent some time considering different ways to start the game. Would the ball just be dropped on either side of the net or would it be thrown from the middle of the screen? In the end I decided to throw it from the middle of the screen with a random velocity in the x-direction and a random positive velocity in the y-direction. Both of these velocities are within a range of defined start velocities.

Basic bouncing

The way I have implemented the game, the ball will bounce off the top of the screen, the sides of the screen, the net and the rackets. Initially, I just flipped the y-direction of the ball's velocity whenever it hit either the top of the screen, the top of the net or the rackets. Likewise, I flipped the x-direction of the ball's velocity whenever the ball hit the sides of the screen or the sides of the net.

More advanced bouncing

I wanted to let the player gain better control of how the ball would bounce off the racket, so I decided to introduce friction and conservation of momentum. What this basically means, is that the speed of the ball after it has bounced off the racket depends on the speed the racket had during the bounce. I implemented this simply by letting the ball's velocity in the x-direction after the bounce be the sum of the ball's velocity in the x-direction before the bounce and the racket's velocity in the x-direction before the bounce. I used the same logic in the y-direction, although I had to flip the ball's velocity in the y-direction first.

Defining how the ball would bounce off the corners of the net was difficult, because it wasn't always easy to determine whether it should bounce sideways, upwards or both. I guess I could have used something similar to the Cohen-Sutherland line clipping algorithm to determine the dominant side from which the ball hit the net, but I went for an easier approach and let the bounce-rate in the x-direction be proportional to the distance from the center of the net. This way I also reduced the chance of the ball getting stuck on top of the net.

The opponent and the need for inertia

On a smartphone, this isn't really the ideal game for multiplayer, unless it's online. On the desktop emulator, I used the mouse to control the racket, so this didn't lend itself to multiplayer either. The solution was to let the left racket be controlled by the app itself. In the beginning, I programmed the left racket to always position itself under the ball. This effectively meant that it could not lose, so I introduced inertia to both rackets. I did this by defining the velocity vector of the racket as an inertia factor multiplied by the distance vector between the racket and the registered touch of the user's finger (or the click of the mouse for the desktop version).

Opponent strength

To start out easy, I only gave the opponent the ability to move his racket in the x-direction. Since the ball's momentum was always conserved, this meant that as long as the ball had already come from above the net, the bounce off the racket would always be enough to bounce the ball high enough to get back over the net. I did, however, introduce air resistance (the velocity in each direction is reduced by a resistance factor multiplied by the velocity vector for each frame) and energy absorbation in each bounce (similar to air resistance, except the reduction of speed happens only during a bounce). I did this to make sure that the ball does not reach crazy speeds and also to make sure that the ball would not bounce up and down forever.

Having introduced these features, I suddenly ran the risk of the opponent not managing to bounce the ball hard enough to get it over the net. To solve this problem, I introduced the bounce strength constant. It defines the velocity in the y-direction with which the ball will travel after bouncing off the racket. This means that once the ball approaches the opponent's racket, it will adjust it's velocity in the y-direction to make sure that it's own velocity in the y-direction minus the ball's velocity in the y-direction are equal to the bounce strength constant.

A preview

Below I have included a gif showing the gameplay about two hours into developing. It probably lags a bit:p
pong compressed.gif

What's next?

Now that I've worked my way through simulating all the laws of physics in one simple game, I think it would be fun to make a game engine or framework from scratch to simplify the process of making games where objects have to interact with each other in a physical world. I also want to try moving on to 3D games. The possibilities are endless and it's exciting to have started on this journey. I hope I inspire some of you to do it too. I guess I could have written a lot more, but I want to go eat some spaghetti now.

Sort:  

Good going mate! I took a class on mobile dev using Xamarin to create a cross compatible DnD game and that took me forever

Very good game, very nice, pls upvote me Im totally not a hindu bot.

But seriously the paddles should be horizontal, and you could use the lower edge of screen as "board" while goingout on the sides gives points.

Yeah, ideally, I should also make sure that the ball hit the table exactly once before any of the players hit it as well. Pls upvote me too, I'm also totally not a hindu bot.

How did you manage the "AI"? All i did is take the vertical velocity of the ball minus 5 xD It is hard to score but possible :D

Once the ball falls below the top of the net on the bot's side, the bot racket's yVelocity is given by wantedBallYVelocity+ballYvelocity (where ballYVelocity is a negative number). This ensures that the ball always bounces up with a yVelocity given by wantedBallYVelocity. The yVelocity will vary slightly, though, since the inertia defined for the racket limits how quickly it can accelerate.

When it comes to the racket's xVelocity, it actually just matched that of the ball, which was actually a problem after I introduced air resistance. If the player did not hit the ball enough sideways, it might actually stop moving sideways, causing the bot to just bounce it up and down. I solved this by always moving the bot's racket slightly towards the net right before the ball hit the racket. This ensures that the bot will always move the ball towards the net and eventually get it over.

nice game . i want you play this again

Nice friend, success always for you

Nice !! Great work mate !!

Thanks, mate!:D

hi, nice game. i want to learn how to make one too. please post more on your progress. thanks!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.030
BTC 59527.28
ETH 2462.24
USDT 1.00
SBD 2.49