Android Question tile game move hero help

Douglas Farias

Expert
Licensed User
Longtime User
hi all i m really want to learn about games
i need some help to understantd this code

i never used sensor and i dont want to user sensor

i want to add buttons on the screen left right up and down

but i dont know convert sensor to buttons

what move the hero ?
Hero.DestinationY = MaxSize - 1
u have tryed this but this dont move hero total to next tile

here is my 2 buttons

Sub baixo_Click
Hero.DestinationY = MaxSize - 1
End Sub

Sub cima_Click
NextX = Ceil(X)
End Sub

can someone help me understand how to move the hero using buttons?
without Sensor_AccelerometerChanged

princess_tiles-png.16980



B4X:
#Region  Project Attributes
    #ApplicationLabel: AS_PrincessTiles
    #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
    Type typHero(X As Float, Y As Float, DestinationX As Int, DestinationY As Int, _
                Speed As Float, Gems As Int, Keys As Int)
    Type typTile(GroundTile As Byte, ObjectOnTile As Byte, Animation As Int)
    Type typAnimator(Bmp As Bitmap, X As Int, Y As Int, Scale As Float, Offset As Int)
End Sub

Sub Globals
    Dim AcSf As AcceleratedSurface
    Dim IU As AS_ImageUtils
    Dim PA As PhoneAccelerometer
    Dim PWS As PhoneWakeState

    Dim Hero As typHero
    Dim bmpHero, bmpPrincess As Bitmap
    Dim Score, ScoreFontSize, ScoreHeight As Int

    Dim bmpDirt, bmpGrass, bmpWater As Bitmap
    Dim bmpBush1, bmpBush2, bmpTree, bmpRock As Bitmap
    Dim bmpChest, bmpGem, bmpKey As Bitmap
    Dim bmpRamp1, bmpRamp2, bmpStone As Bitmap
    Dim drwZoomIn, drwZoomOut As Object

    Dim MaxSize As Int = 60
    Dim Landscape(MaxSize, MaxSize) As typTile
    Dim FirstTileX, FirstTileY As Float
    Dim Zoom As Float
    Dim BeforeNextZoom As Long
    Dim DoZoom As Short
    Dim MotionThreshold As Float
    Dim NewX, NewY, tmpX As Float
    Dim ScreenX, ScreenY, Progression As Float
    Dim DestRect As Rect
    Dim NextX, NextY As Int
    Dim NextTile As typTile

    Dim Animators As List
    Private cima As Button
    Private baixo As Button
End Sub


Sub Activity_Create(FirstTime As Boolean)
    'Generates a random map
    Dim Random As Int
    For i = 0 To MaxSize - 1
        For j = 0 To MaxSize - 1
            Dim Tile As typTile
            Tile.Initialize
            Tile.Animation = -1
            Random = Rnd(0, 256)
            If Random < 128 Then
                Tile.GroundTile = 0 'Grass
            Else
                Tile.GroundTile = 1 'Dirt
            End If
            If Random Mod 30 = 0 AND (i > 6 OR j > 6) Then
                'Object = bush1, bush2, tree or rock
                Tile.ObjectOnTile = Rnd(1, 5)
            Else If Random Mod 20 = 0 AND (i > 6 OR j > 6) Then
                'Object = chest, gem or key
                Tile.ObjectOnTile = Rnd(10, 13)
            End If
            Landscape(i, j) = Tile
        Next
    Next

    'Adds a river and a bridge near the starting point
    For j = 0 To MaxSize - 1
        Dim Tile As typTile
        Tile.Initialize
        Tile.Animation = -1
        Tile.GroundTile = 2 'Water
        Landscape(4, j) = Tile
        Landscape(4 + 1, j) = Tile
    Next
    For j = 5 To 6
        Dim Tile As typTile
        Tile = Landscape(3, j)
        Tile.ObjectOnTile = 20 'Ramp west
        Tile = Landscape(4, j)
        Tile.ObjectOnTile = 21 'Stone
        Tile = Landscape(5, j)
        Tile.ObjectOnTile = 21 'Stone
        Tile = Landscape(6, j)
        Tile.ObjectOnTile = 22 'Ramp east
    Next

    'The furthest tile is occupied by the princess
    Dim PrincessTile As typTile
    PrincessTile = Landscape(MaxSize - 1, MaxSize - 1)
    PrincessTile.ObjectOnTile = 99

    'Loads the bitmaps for the hero and the princess
    bmpHero = IU.LoadScaledBitmap(File.DirAssets, "tiles/characters/hero.png", 64dip, 108dip, True)
    bmpPrincess = IU.LoadScaledBitmap(File.DirAssets, "tiles/characters/princess.png", 64dip, 108dip, True)

    'Loads the ground bitmaps
    bmpDirt = IU.LoadScaledBitmap(File.DirAssets, "tiles/ground/dirt.png", 64dip, 108dip, True)
    bmpGrass = IU.LoadScaledBitmap(File.DirAssets, "tiles/ground/grass.png", 64dip, 108dip, True)
    bmpWater = IU.LoadScaledBitmap(File.DirAssets, "tiles/ground/water.png", 64dip, 108dip, True)

    'Loads the objects bitmaps
    bmpBush1 = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/bush1.png", 64dip, 108dip, True)
    bmpBush2 = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/bush2.png", 64dip, 108dip, True)
    bmpTree = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/tree.png", 64dip, 108dip, True)
    bmpRock = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/rock.png", 64dip, 108dip, True)
    bmpChest = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/chest.png", 64dip, 108dip, True)
    bmpGem = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/gem.png", 64dip, 108dip, True)
    bmpKey = IU.LoadScaledBitmap(File.DirAssets, "tiles/objects/key.png", 64dip, 108dip, True)

    'Loads the buildings bitmaps
    bmpRamp1 = IU.LoadScaledBitmap(File.DirAssets, "tiles/buildings/ramp1.png", 64dip, 108dip, True)
    bmpRamp2 = IU.LoadScaledBitmap(File.DirAssets, "tiles/buildings/ramp2.png", 64dip, 108dip, True)
    bmpStone = IU.LoadScaledBitmap(File.DirAssets, "tiles/buildings/stone.png", 64dip, 108dip, True)

    'Loads the zoom drawables
    drwZoomIn = IU.LoadSystemDrawable("btn_plus_default")
    drwZoomOut = IU.LoadSystemDrawable("btn_minus_default")

    'Initializes all settings
    Hero.Initialize
    Hero.Speed = 1dip + 0.2%x
    FirstTileX = 0
    FirstTileY = 0
    Zoom = 1
    DoZoom = 0 '1 = Zoom in, -1 = Zoom out
    MotionThreshold = 1.5
    Dim FontScale As Float = (100%x + 100%y) / (320dip + 480dip)
    ScoreFontSize = 18 * FontScale
    ScoreHeight = 17dip * FontScale
    Score = 0
    Animators.Initialize

    'Creates the accelerated surface
    AcSf.Initialize("AcSf", True)
    Activity.AddView(AcSf, 0, 0, 100%x, 100%y)
  
  
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    'Prevents the device from going to sleep
    PWS.KeepAlive(True)

    'Displays the rules
    Msgbox("Collect as many gems as possible and bring them to the princess. You can open chests with keys." & _
    CRLF & "Use the accelerometer to move the hero.", "Rules")

    'Starts the game
    PA.StartListening("Sensor")
    AcSf.StartRegularUpdateAndDraw(16)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    'Stops the game
    PA.StopListening
    AcSf.StopRegularDraw

    'Allows the device to go to sleep
    PWS.ReleaseKeepAlive
End Sub

Sub Sensor_AccelerometerChanged (X As Float, Y As Float, Z As Float)
    'X and Y are inverted in the landscape orientation
    If Activity.Width > Activity.Height Then
        tmpX = X
        X = Y
        Y = tmpX
    End If

    'Move detected on the X axis?
    If Abs(X) > MotionThreshold Then
        If X > 0 Then
            Hero.DestinationX = MaxSize - 1
        Else
            Hero.DestinationX = 0
        End If
    Else
        StopMoveX
    End If

    'Move detected on the Y axis?
    If Abs(Y) > MotionThreshold Then
        If Y > 0 Then
            Hero.DestinationY = MaxSize - 1
        Else
            Hero.DestinationY = 0
        End If
    Else
        StopMoveY
    End If
End Sub


Sub baixo_Click

End Sub
Sub cima_Click
  
End Sub



Sub StopMoveX
    Hero.DestinationX = Round(Hero.X)
End Sub

Sub StopMoveY
    Hero.DestinationY = Round(Hero.Y)
End Sub

Sub CheckMove(X As Float, Y As Float)
    'Checks if the next move is allowed
    'If the hero is not blocked by a water tile or a big object, then the move is done.
    Dim DeltaX As Float = Hero.X - X
    Dim DeltaY As Float = Hero.Y - Y
    If DeltaX > 0 Then
        'To the left
        NextX = Floor(X)
    Else If DeltaX < 0 Then
        'To the right
        NextX = Ceil(X)
    Else
        NextX = X
    End If
    If DeltaY > 0 Then
        'To the top
        NextY = Floor(Y)
    Else If DeltaY < 0 Then
        'To the bottom
        NextY = Ceil(Y)
    Else
        NextY = Y
    End If
    NextTile = Landscape(NextX, NextY)
    If NextTile.ObjectOnTile >= 20 AND NextTile.ObjectOnTile <> 99 Then
        'The hero crosses the bridge
        Hero.X = X
        Hero.Y = Y
    Else If NextTile.GroundTile = 0 OR NextTile.GroundTile = 1 Then
        'If the ground is grass or dirt...
        If NextTile.ObjectOnTile = 0 Then
            Hero.X = X
            Hero.Y = Y
        Else If NextTile.ObjectOnTile = 10 Then
            'The hero has found a chest
            If Hero.Keys > 0 Then
                'He has a key to open the chest -> the chest becomes a gem
                Hero.Keys = Hero.Keys - 1
                NextTile.ObjectOnTile = 11
                Hero.X = X
                Hero.Y = Y
            End If
        Else If NextTile.ObjectOnTile = 11 Then
            'The hero has found a gem
            Hero.Gems = Hero.Gems + 1
            NextTile.ObjectOnTile = 0
            Hero.X = X
            Hero.Y = Y
            NextTile.Animation = CreateAnimator(bmpGem, 11dip)
        Else If NextTile.ObjectOnTile = 12 Then
            'The hero has found a key
            Hero.Keys = Hero.Keys + 1
            NextTile.ObjectOnTile = 0
            Hero.X = X
            Hero.Y = Y
            NextTile.Animation = CreateAnimator(bmpKey, 0)
        Else If NextTile.ObjectOnTile = 99 Then
            'The hero gives all his gems to the princess
            Score = Score + Hero.Gems
            Hero.Gems = 0
        End If
    End If
End Sub

Sub CreateAnimator(Bmp As Bitmap, Offset As Int) As Int
    'Creates an animated object
    Dim Animator As typAnimator
    Animator.Initialize
    Animator.Bmp = Bmp
    Animator.X = NextX
    Animator.Y = NextY
    Animator.Scale = 1
    Animator.Offset = Offset
    Animators.Add(Animator)
    Return Animators.Size - 1
End Sub

Sub AcSf_Update(ElapsedTime As Long)
    'Is a zoom in progress?
    If BeforeNextZoom = 0 Then
        If DoZoom <> 0 Then
            If DoZoom = 1 Then
                Zoom = Min(1.5, Zoom + 0.05)
            Else
                Zoom = Max(0.5, Zoom - 0.05)
            End If
            BeforeNextZoom = 40
        End If
    Else
        BeforeNextZoom = Max(0, BeforeNextZoom - ElapsedTime)
    End If

    'Is a move in progress?
    Progression = Hero.Speed * ElapsedTime / 1000 * Zoom
    If Hero.X < Hero.DestinationX Then
        NewX = Min(Hero.X + Progression, Hero.DestinationX)
    Else If Hero.X > Hero.DestinationX Then
        NewX = Max(Hero.X - Progression, Hero.DestinationX)
    Else
        NewX = Hero.X
    End If
    If Hero.Y < Hero.DestinationY Then
        NewY = Min(Hero.Y + Progression, Hero.DestinationY)
    Else If Hero.Y > Hero.DestinationY Then
        NewY = Max(Hero.Y - Progression, Hero.DestinationY)
    Else
        NewY = Hero.Y
    End If
    CheckMove(NewX, NewY)

    'Checks if the landscape must be scrolled
    ScreenX = (Hero.X - FirstTileX) * 64dip * Zoom
    If ScreenX + 64dip * Zoom > 75%x Then FirstTileX = FirstTileX + Progression
    If ScreenX < 25%x Then FirstTileX = Max(0, FirstTileX - Progression)
    ScreenY = (Hero.Y - FirstTileY) * 52dip * Zoom
    If ScreenY + 84dip * Zoom > 75%y Then FirstTileY = FirstTileY + Progression
    If ScreenY < 25%y Then FirstTileY = Max(0, FirstTileY - Progression)

    'Computes the new scale of animated objects
    For i = Animators.Size - 1 To 0 Step -1
        Dim Animator As typAnimator
        Animator = Animators.Get(i)
        Animator.Scale = Animator.Scale + (ElapsedTime / 60)
        If Animator.Scale >= 6 Then
            Dim Tile As typTile
            Tile = Landscape(Animator.X, Animator.Y)
            Tile.Animation = -1
            Animators.RemoveAt(i)
        End If
    Next
End Sub

Sub DrawLandscape(AC As AS_Canvas)
    'Draws the tiles
    Dim Tile As typTile
    For X = Floor(FirstTileX) To MaxSize - 1
        NewX = (X - FirstTileX) * 64dip * Zoom
        If NewX >= 100%x Then Exit
        For Y = Max(0, Floor(FirstTileY) - 1) To MaxSize - 1
            NewY = (Y - FirstTileY) * 52dip * Zoom
            If NewY >= 100%y Then Exit
            DestRect.Initialize(NewX, NewY, NewX + 64dip * Zoom, NewY + 108dip * Zoom)
            Tile = Landscape(X, Y)
            Select Tile.GroundTile
                Case 0 'Tile = grass
                    AC.DrawBitmap(bmpGrass, Null, DestRect)
                Case 1 'Tile = dirt
                    AC.DrawBitmap(bmpDirt, Null, DestRect)
                Case 2 'Tile = water
                    AC.DrawBitmap(bmpWater, Null, DestRect)
            End Select
        Next
    Next

    'Draws the bridge
    For X = 3 To 6
        If X < Floor(FirstTileX) Then Continue
        NewX = (X - FirstTileX) * 64dip * Zoom
        For Y = 5 To 6
            If Y < Floor(FirstTileY) - 1 Then Continue
            NewY = (Y - FirstTileY) * 52dip * Zoom
            DestRect.Initialize(NewX, NewY, NewX + 64dip * Zoom, NewY + 108dip * Zoom)
            Tile = Landscape(X, Y)
            Select Tile.ObjectOnTile
                Case 20 'Tile = ramp east
                    AC.DrawBitmap(bmpRamp1, Null, DestRect)
                Case 21 'Tile = stone
                    AC.DrawBitmap(bmpStone, Null, DestRect)
                Case 22 'Tile = ramp west
                    AC.DrawBitmap(bmpRamp2, Null, DestRect)
            End Select
        Next
    Next
End Sub

Sub DrawObjects(AC As AS_Canvas)
    'Draws the objects
    Dim Tile As typTile
    For X = Floor(FirstTileX) To MaxSize - 1
        NewX = (X - FirstTileX) * 64dip * Zoom
        If NewX >= 100%x Then Exit
        For Y = Max(0, Floor(FirstTileY) - 1) To MaxSize - 1
            NewY = (Y - FirstTileY) * 52dip * Zoom - (24dip * Zoom)
            If NewY >= 100%y Then Exit
            DestRect.Initialize(NewX, NewY, NewX + 64dip * Zoom, NewY + 108dip * Zoom)
            Tile = Landscape(X, Y)
            Select Tile.ObjectOnTile
                Case 1 'Object = bush 1
                    AC.DrawBitmap(bmpBush1, Null, DestRect)
                Case 2 'Object = bush 2
                    AC.DrawBitmap(bmpBush2, Null, DestRect)
                Case 3 'Object = tree
                    AC.DrawBitmap(bmpTree, Null, DestRect)
                Case 4 'Object = rock
                    AC.DrawBitmap(bmpRock, Null, DestRect)
                Case 10 'Object = chest
                    AC.DrawBitmap(bmpChest, Null, DestRect)
                Case 11 'Object = gem
                    AC.DrawBitmap(bmpGem, Null, DestRect)
                Case 12 'Object = key
                    AC.DrawBitmap(bmpKey, Null, DestRect)
            End Select
            If Tile.Animation >= 0 Then
                'Draws the growing object
                Dim Animator As typAnimator
                If Tile.Animation < Animators.Size Then
                    Animator = Animators.Get(Tile.Animation)
                    AC.MatrixSetScale(Animator.Scale * Zoom, Animator.Scale * Zoom)
                    AC.DrawBitmapWithMatrixAt(IU.AlterColors(Animator.Bmp, 255 - (Animator.Scale * 40), 0, 1), _
                                              NewX + (Bit.ShiftRight(Animator.Bmp.Width, 1) - (Bit.ShiftRight(Animator.Bmp.Width, 1) * Animator.Scale)) * Zoom, _
                                              NewY + (Bit.ShiftRight(Animator.Bmp.Height, 1) - ((Bit.ShiftRight(Animator.Bmp.Height, 1) + Animator.Offset) * Animator.Scale)) * Zoom, True)
                Else
                    Tile.Animation = -1 'Error
                End If
            End If
        Next
    Next

    'Draws the princess on the furthest tile
    ScreenX = (MaxSize - 1 - FirstTileX) * 64dip * Zoom
    ScreenY = (MaxSize - 1 - FirstTileY) * 52dip * Zoom
    DestRect.Initialize(ScreenX, ScreenY, ScreenX + 64dip * Zoom, ScreenY + 84dip * Zoom)
    If NewX < 100%x AND NewY < 100%y Then
        AC.DrawBitmap(bmpPrincess, Null, DestRect)
    End If

    'Draws the hero's keys, gems and score
    For i = 0 To Hero.Gems - 1
        DestRect.Initialize(i * 3.1%x, -1.5%x, (i * 3.1%x) + 3%x, 3.5%x)
        AC.DrawBitmap(bmpGem, Null, DestRect)
    Next
    For i = 0 To Hero.Keys - 1
        DestRect.Initialize(i * 3.1%x, 2.2%x, (i * 3.1%x) + 3%x, 7.2%X)
        AC.DrawBitmap(bmpKey, Null, DestRect)
    Next
    AC.DrawText(Score, 100%x - 5dip, ScoreHeight + 4dip, Typeface.DEFAULT_BOLD, ScoreFontSize, Colors.Red, AC.ALIGN_RIGHT)

'    For debugging (draws the next tile in the motion direction):
'    NewX = ((NextX - FirstTileX) * 64dip * Zoom)
'    NewY = ((NextY - FirstTileY) * 52dip * Zoom) + 31dip * Zoom
'    DestRect.Initialize(NewX, NewY, NewX + 64dip * Zoom, NewY + 52dip * Zoom)
'    AC.DrawRoundRect(DestRect, Colors.Red, 5dip, False, 2dip, True)
End Sub

Sub AcSf_Draw(AC As AS_Canvas)
    'Draws the landscape
    DrawLandscape(AC)

    'Draws the hero
    ScreenX = (Hero.X - FirstTileX) * 64dip * Zoom
    ScreenY = (Hero.Y - FirstTileY) * 52dip * Zoom
    DestRect.Initialize(ScreenX, ScreenY, ScreenX + 64dip * Zoom, ScreenY + 84dip * Zoom)
    AC.DrawBitmap(bmpHero, Null, DestRect)

    'Draws the objects
    DrawObjects(AC)

    'Draws the zoom buttons
    DestRect.Initialize(0, 100%y - 80dip, 113dip, 100%y - 14dip)
    AC.DrawDrawable(drwZoomIn, DestRect)
    DestRect.Initialize(100%x - 113dip, 100%y - 80dip, 100%x, 100%y - 14dip)
    AC.DrawDrawable(drwZoomOut, DestRect)
End Sub

Sub AcSf_Touch(Action As Int, X As Int, Y As Int, Event As Object)
    'Is one of the zoom buttons touched?
    If Action = 0 Then 'Down
        If Y >= 100%y - 80dip AND Y <= 100%y - 14dip Then
            If X <= 70dip Then
                DoZoom = 1
                BeforeNextZoom = 0
            Else If X >= 100%x - 70dip Then
                DoZoom = -1
                BeforeNextZoom = 0
            End If
        End If
    Else If Action = 1 OR Action = 3 Then 'Up or Cancel
        DoZoom = 0
    End If
End Sub


i make this and this move to the next tile right nut back to old tile *-*

Sub cima_Click
Hero.X = Hero.X + 1
End Sub
 
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
done if help someone
i have removed this
B4X:
    'Is a move in progress?
    Progression = Hero.Speed * ElapsedTime / 1000 * Zoom
    If Hero.X < Hero.DestinationX Then
        NewX = Min(Hero.X + Progression, Hero.DestinationX)
    Else If Hero.X > Hero.DestinationX Then
        NewX = Max(Hero.X - Progression, Hero.DestinationX)
    Else
        NewX = Hero.X
    End If
    If Hero.Y < Hero.DestinationY Then
        NewY = Min(Hero.Y + Progression, Hero.DestinationY)
    Else If Hero.Y > Hero.DestinationY Then
        NewY = Max(Hero.Y - Progression, Hero.DestinationY)
    Else
        NewY = Hero.Y
    End If
    CheckMove(NewX, NewY)

and make my buttons
B4X:
Sub baixo_Click
Hero.x = Hero.X + 1
End Sub
Sub cima_Click
Hero.Y = Hero.Y + 1
End Sub
 
Last edited:
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
@Informatix how can i make to camera folow the hero now using keys to move and no aceleration

i m using
B4X:
Sub baixo_Click
Hero.x = Hero.X + 1
End Sub
Sub cima_Click
Hero.Y = Hero.Y + 1
End Sub

to move the hero.

but the camera
i m trying
B4X:
 Progression = Hero.Speed +1

this works move the camera with bug
i think because this
B4X:
    'Checks if the landscape must be scrolled
    ScreenX = (Hero.X - FirstTileX) * 64dip * Zoom
    If ScreenX + 64dip * Zoom > 75%x Then FirstTileX = FirstTileX + Progression
    If ScreenX < 25%x Then FirstTileX = Max(0, FirstTileX - Progression)
    ScreenY = (Hero.Y - FirstTileY) * 52dip * Zoom
    If ScreenY + 84dip * Zoom > 75%y Then FirstTileY = FirstTileY + Progression
    If ScreenY < 25%y Then FirstTileY = Max(0, FirstTileY - Progression)

what the correct way to make this? i dont go use zoom
 
Upvote 0
Top