Optimize my code

fooshi

Member
Hello,
first sorry for my poor english. I start to develop on basic4ppc for 1 week.
I start to develop a pong game.
The game is fast on my computer but when i run it on my smartphone it's more slowly specialy when i touch the screen to move the player1. the speed of the ball is different too and the movement of the ball and all the application is slowly when i move the player1.

i post here my code to let you see if there is a problem in my code, and i wait some tip too ;-)

B4X:
Sub Globals    
    Dim sx         As Number                                 ' The position related to the image where the mouse was first down.
    Dim dep     As Number
    Dim Type(Haut,Right,Speed,Modulo) ball                ' The ball parameters
    Dim Type(onPlay,Tick,Score1,Score2) game            ' The game parameters
    Dim win     As Boolean
End Sub

Sub Init    
    game.onPlay   = False
    game.tick     = 0
    ball.Modulo   = 100
    Player1.Left  = (form1.Width / 2) - (Player1.Width / 2)    ' Player1
    Player1.Top   = form1.Height - player1.Height - 20    
    
    Player2.Left  = (form1.Width / 2) - (Player2.Width / 2)    ' Player2
    Player2.Top   = 20
    
    Balle.Left      = (form1.Width / 2) - (Balle.Width / 2)    ' Balle
    If win Then
        Balle.Top = form1.Height - Balle.Height - 40        ' The ball is for the player 1
        ball.Haut = True
    Else
        Balle.Top = 40                                        ' The ball is for the player 2
        ball.Haut = False
    End If    
End Sub

Sub App_start
    SetTransparentColor (cBlack)
    Sip(False)                            ' Hide the soft input panel
    Form1.ForeLayer = True
    Form1.Show    
    win            = True
    game.Score1 = 0
    game.Score2 = 0
    init    
    ' Ball    
    ball.Right     = True    
    ball.Speed     = 3
End Sub

Sub Form1_MouseDown (X,Y)    
    If game.onPlay = False Then            ' Check if the ball is on play
        game.onPlay     = True
        timer1.Enabled     = True
        timer1.Interval    = 1
    End If
    
    sx = X - Player1.Left
End Sub

Sub Form1_MouseMove (X,Y)
    dep = X - sx
    
    If dep < 0 Then                                     ' Player 1 reach the left screen
        Player1.Left = 0
    Else If dep + player1.Width > form1.Width Then        ' Player 1 reach the right screen
        Player1.Left = form1.Width - Player1.Width 
    Else
        Player1.Left = dep
    End If    
End Sub 


Sub Timer1_Tick
    game.tick = game.tick + 1
    If game.tick = 5000 Then ball.Speed = ball.Speed + 1    ' Speed up the ball
    
    ' --- Move the ball ---
    If (game.tick Mod ball.Modulo) = 0 Then
        If ball.right Then    
            Balle.Left = Balle.Left + ball.Speed                                ' Move to the right
            If Balle.Left + Balle.Width > form1.Width Then ball.Right = False    ' The ball reach the right screen
        Else         
            Balle.Left = Balle.Left - ball.Speed                                ' Move to the left
            If Balle.Left < 0 Then ball.Right = True                            ' The ball reach the left screen
        End If
    End If
    
    If ball.Haut Then
        ' --- Move the ball ---
        Balle.Top = Balle.Top - ball.speed                ' Move the ball to the top    
        
        If Balle.Top + Balle.Height < 0 Then            ' We win
            win            = True
            game.score1    = game.score1 + 1
            score1.Text    = game.score1
            timer1.Enabled = False
            init
        End If    
        
        ' --- The ball reach the player 2 ---
        If (Balle.Top < player2.Top + player2.Height) AND (Balle.Top + Balle.Height > Player2. top) AND (Balle.Left > player2.Left) AND (Balle.left < player2.Left + player2.Width) Then
            ball.Haut = False                             
            If Balle.Left < player2.Left + (player2.Width / 2) Then 
                ball.Right = False 
            Else 
                ball.Right = True
            End If
            If (Balle.Left - player2.Left <= 5) OR (Balle.Left - player2.Left >= 50) Then
                ball.Modulo = 1
            Else If (Balle.Left - player2.Left > 5 AND Balle.Left - player2.Left <= 10) OR (Balle.Left - player2.Left >= 45 AND Balle.Left - player2.Left < 50) Then
                ball.Modulo = 2
            Else If (Balle.Left - player2.Left > 10 AND Balle.Left - player2.Left <= 15) OR (Balle.Left - player2.Left >= 40 AND Balle.Left - player2.Left < 45) Then 
                ball.Modulo = 3
            Else If (Balle.Left - player2.Left > 15 AND Balle.Left - player2.Left <= 20) OR (Balle.Left - player2.Left >= 35 AND Balle.Left - player2.Left < 40) Then
                ball.Modulo = 4
            Else If (Balle.Left - player2.Left > 20 AND Balle.Left - player2.Left < 35) Then
                ball.Modulo = 100
            End If 
        End If
        
        ' --- Move the player 2 ---
        If Balle.Left + (Balle.Width / 2) > player2.Left + (player2.width / 2) Then player2.Left = player2.Left + 1    ' The ball is a the right of the player 2
        If Balle.Left + (Balle.width / 2) < player2.Left + (player2.Width / 2) Then player2.Left = player2.Left - 1    ' The ball is on the left of the player 2
    Else
        ' --- Move the ball ---
        Balle.Top = Balle.Top + ball.Speed                ' Move the ball to the bottom
        
        If Balle.Top + Balle.Height > form1.Height Then    ' We loose
            win            = False
            game.score2       = game.score2 + 1
            score2.Text    = game.score2
            timer1.Enabled = False
            init
        End If
        
        ' --- The ball reach he player 1 ---
        If (Balle.Top + Balle.Height > player1.Top) AND (Balle.Top < Player1.Top + Player1.Height) AND (Balle.Left > player1.Left) AND (Balle.left < player1.Left + player1.Width) Then    
            ball.Haut = True                            
            If Balle.Left < player1.Left + (player1.Width / 2) Then 
                ball.Right = False
            Else
                ball.Right = True
            End If
            If (Balle.Left - player1.Left <= 5) OR (Balle.Left - player1.Left >= 50) Then
                ball.Modulo = 1
            Else If (Balle.Left - player1.Left > 5 AND Balle.Left - player1.Left <= 10) OR (Balle.Left - player1.Left >= 45 AND Balle.Left - player1.Left < 50) Then
                ball.Modulo = 2
            Else If (Balle.Left - player1.Left > 10 AND Balle.Left - player1.Left <= 15) OR (Balle.Left - player1.Left >= 40 AND Balle.Left - player1.Left < 45) Then 
                ball.Modulo = 3
            Else If (Balle.Left - player1.Left > 15 AND Balle.Left - player1.Left <= 20) OR (Balle.Left - player1.Left >= 35 AND Balle.Left - player1.Left < 40) Then
                ball.Modulo = 4
            Else If (Balle.Left - player1.Left > 20 AND Balle.Left - player1.Left < 35) Then
                ball.Modulo = 100
            End If 
        End If 
        
        ' --- Move the player 2 ---
        If player2.Left + (player2.Width / 2) > (form1.Width / 2) Then player2.Left = player2.Left - 1        ' Center the player 2
        If player2.Left + (player2.Width / 2) < (form1.Width / 2) Then player2.Left = player2.Left + 1        ' Center the player 2
    End If 
        
    If player2.Left < 0 Then player2.Left = 0                                                        ' The player2 reach the left screen
    If player2.Left + player2.Width > form1.Width Then player2.Left = form1.Width - player2.Width    ' The player2 reach the right screen    
End Sub
thanks a lot
 
Hello
@Foshi you will get significantly better results if you use ImageLib....

Attached is what I have been able to pull off.A have added the Hardware Lib
to move the bar(BmpPlayer1) with hardkeys. I just can't figure how to
erase the BmpPlayer1.
Any help ,gentlemen?
 
I just can't figure how to
erase the BmpPlayer1.
Any help ,gentlemen?
.... no reply so far.
Is that supposed to mean that using ImageLib one cannot have two or more
bitmaps of different colors moving simultaniously?
:sign0163:
 
Top