A.I, Data and Software Engineering

re-dev the first video game pong with SurfaceView and gameloop

Pong game, android Kotlin
Pong game (prototype)

In this article, we recreate the two-player game Pong – the first arcade video game 1972 – for Android using Kotlin. We will implement the game SurfaceView, game loop design pattern, and code optimization for better performance.

Game loop

Game loop is a common design pattern in game development. It is already integrated into different game engines. Nevertheless, we won’t use one in this project. Therefore, we can define a game loop interface in the newly created project.

However, if you want to create a more convenient interface with some more variables, we can have something like below.

  • The frameRate variable (or update rate) is to control the time interval between each update.
  • The timeToUpdate and shouldUpdate variables are to determine it is time to update.
  • The render and update are two standard method that are used to draw and update relevant elements.

Sprite, Ball, and player classes

For easier to handle the drawing of the ball and players, we will create an abstract class named Sprite. The benefit of doing this is to manage all objects’ collision and draw on the screen and help the code project easier to maintain.

The variables, such as location, shape, and movVec, are to control the drawing and movement of the sprite. Also, it can accan accept any

Next, the ball and player classes will then inherit Sprite. The player class is implemented as follow:

Similarly, we create the ball class. However, we handle some collision The ball class is implemented as follow:

The game view

The game view is a surface view and it also implements the game loop interface. Using SurfaceView will let you have more control over animation but you also to work harder than using normal View.

The result of Ping the Pong

The result

Video tutorial will be available on Petamind channel.

Useful links

Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest


You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.