Android Example Simple Pong Game

wonder

Expert
Licensed User
Hey guys!!

Here's a simple example of a 2 player Pong game, using nothing more than the core library.
Please keep in mind that this is just the frame-work for what could be a more advanced game.

Merry Xmas and happy coding!! :D

B4X:
#Region  Project Attributes
    #ApplicationLabel: 2 Player Pong
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: Landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim Main_Engine As Timer
 
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private P1_Controls As Panel
    Private P2_Controls As Panel 
    Private P1 As Panel
    Private P2 As Panel 
    Private Ball As Panel
 
    Dim ball_speed = 10dip As Float
    Dim ball_direction_x = ball_speed As Float
    Dim ball_direction_y = ball_speed As Float
 
    Dim P1startX As Float
    Dim P1startY As Float 
    Dim P1deltaX As Float
    Dim P1deltaY As Float
 
    Dim P2startX As Float
    Dim P2startY As Float 
    Dim P2deltaX As Float
    Dim P2deltaY As Float
 
    Dim p1_score = 0 As Int
    Dim p2_score = 0 As Int
 
    Dim Pause = True As Boolean
     
    Private Player1_Score As Label
    Private Player2_Score As Label
     
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Game")

    P1_Controls.Width = 50%x
    P1_Controls.Height = 100%y
    P1_Controls.Top = 0%y
    P1_Controls.Left = 0%x

    P2_Controls.Width = 50%x
    P2_Controls.Height = 100%y
    P2_Controls.Top = 0%y
    P2_Controls.Left = 50%x
 
    P1.Width = 2%x
    P1.Height = 24%y
    P1.Left = 10%x
    P1.Top = 50%y - (P1.Height / 2)
 
    P2.Width = P1.Width
    P2.Height = P1.Height
    P2.Left = 90%x - P1.Width
    P2.Top = P1.Top
 
    Ball.Width = 4%x
    Ball.Height = Ball.Width
    Ball.Left = 50%x - (Ball.Width / 2)
    Ball.Top = 50%y - (Ball.Height / 2)
 
    Player1_Score.Width = 30%x
    Player1_Score.Height = 20%y 
    Player1_Score.Left = 25%x - (Player1_Score.Width / 2)
    Player1_Score.Top = 50%y - (Player1_Score.Height / 2)

    Player2_Score.Width = 30%x
    Player2_Score.Height = 20%y 
    Player2_Score.Left = 75%x - (Player2_Score.Width / 2)
    Player2_Score.Top = 50%y - (Player2_Score.Height / 2)

    P1_Controls.BringToFront
    P2_Controls.BringToFront

    Main_Engine.Initialize("Main_Engine", 16)
    Main_Engine.Enabled = True

End Sub

Sub Main_Engine_Tick

    Player1_Score.Text = p1_score
    Player2_Score.Text = p2_score
     
        'Colision detector for Player 1
            If Ball.Left < P1.Left + P1.Width _
            AND (Ball.Top + Ball.Height < P1.Top OR Ball.top > P1.Top + P1.Height) Then
                Ball.Left = 50%x - (Ball.Width / 2)
                Ball.Top = 50%y - (Ball.Height / 2)
                p2_score = p2_score + 1
                Pause = True
            End If     
     
        'Colision detector for Player 2
            If Ball.Left + Ball.Width > P2.Left _
            AND (Ball.Top + Ball.Height < P2.Top OR Ball.top > P2.Top + P2.Height) Then
                Ball.Left = 50%x - (Ball.Width / 2)
                Ball.Top = 50%y - (Ball.Height / 2)
                p1_score = p1_score + 1
                Pause = True
            End If     
     
        'Colision detector for ball against the screen boundaries
            If (Ball.Left + Ball.Width) > P2.Left Then ball_direction_x = -ball_direction_x
            If Ball.Left < (P1.Left  + P1.Width) Then ball_direction_x = -ball_direction_x
            If (Ball.Top + Ball.Height) > 100%y Then ball_direction_y = -ball_direction_y
            If Ball.Top < 0%y Then ball_direction_y = -ball_direction_y
     
        If Pause = False Then
            Ball.Left = Ball.Left + ball_direction_x
            Ball.Top = Ball.Top + ball_direction_y
        End If
     
End Sub


Sub P1_Controls_Touch (Action As Int, X As Float, Y As Float)
        Select Action
            Case Activity.ACTION_DOWN         
                P1startX = X
                P1startY = Y
             
            Case Activity.ACTION_MOVE
                Pause = False
                P1deltaX = X - P1startX
                P1deltaY = Y - P1startY
                P1startX = X
                P1startY = Y
             
                If (P1.Top + P1deltaY) > 0%y AND (P1.Top + P1.Height + P1deltaY) < 100%y Then
                    P1.Top = P1.Top + P1deltaY * 1.5
                End If         
             
        End Select     
End Sub


Sub P2_Controls_Touch (Action As Int, X As Float, Y As Float)
        Select Action
            Case Activity.ACTION_DOWN         
                P2startX = X
                P2startY = Y
             
            Case Activity.ACTION_MOVE
                Pause = False
                P2deltaX = X - P2startX
                P2deltaY = Y - P2startY
                P2startX = X
                P2startY = Y
             
                If (P2.Top + P2deltaY) > 0%y AND (P2.Top + P2.Height + P2deltaY) < 100%y Then
                    P2.Top = P2.Top + P2deltaY * 1.5
                End If         
             
        End Select     
End Sub

Sub Activity_Resume
    Pause = True
    Main_Engine.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Pause = True
    Main_Engine.Enabled = False
End Sub
 

Attachments

Last edited:

KMatle

Expert
Licensed User
Nice but the ball runs (too) fast on my S4. :D Good work (simple and effective). Any reason why you used panels instead of f.e. labels?

Make a few levels (increasing the ball speed) and the put it on the market. 50K downloads for sure!

@Erel What about a contest? A complete game. Only the basic views are allowed. No libraries.
 

wonder

Expert
Licensed User
Indeed, the game speed won't be constant across all devices, since I didn't include the "step method" as described by Informatix on his "How To Make Games" tutorial. About the panels, I just like their flexibility in terms of parameters, but there was no special reason for choosing them.
Properly done, I would utilize libGDX as the renderer and the "step method" in order to achieve a constant speed across all devices.

I created this example as a spin-off of another thread were I consider implementing Bluetooth multiplayer on my "Bulldozer" game engine. The first step would be turning this simple Pong game into a two player Bluetooth experience, as a proof of concept. Hence the simplicity on this project. :)
 
Last edited:

ibra939

Active Member
Licensed User
thanks for your
 
Top