Frog Throwing in PlayBasic Tutorial

Not only is frog throwing fun, it's an essential type of player control for Frogger-style games.

Frog Throwing in PlayBasic Tutorial

"Frog throwing" gets its name from the game Frogger. Technically it's a form of grid movement, with the exception that you actually "throw" the player object in units like a grid. This technique is necessary for Frogger-like games because when the frog hitches a ride on a log, normal grid movement would break down because the log itself doesn't move on a grid.

Instead of stopping movement in the squares of a grid, frog throwing uses a target, or landing pixel. The "frog" will be set up to move towards this landing area, so it's important to first set the landing area variables to the initial x# and y# variables when the game starts.

FrogX# = 368
FrogY# = 268
FrogLandX# = FrogX#
FrogLandY# = FrogY#

There's two modes to frog throwing, like grid movement. In grid movement, the player can only start the player object moving if the player object is centered on the grid. Otherwise, player control is disabled and the player object is in the process of moving between grid squares. Frog throwing is the same but the condition to allow the player to start movement is that the "frog" has "landed". In other words, :

If frog has landed
    Can throw frog
Else
    Move frog through air
EndIf

It's easy to check whether the frog "has landed." Just check if the landing target is the same as the frog's current location.

If FrogLandX# = FrogX# And FrogLandY# = FrogY

If the frog has landed, then it can be thrown. This is done by setting FrogLandX# or FrogLandY# to where you want to throw the frog to when the player presses a key :

If LeftKey()
    FrogLandX# = FrogX# - 40
ElseIf RightKey()
    FrogLandX# = FrogX# + 40
ElseIf UpKey()
    FrogLandY# = FrogY# - 40
ElseIf DownKey()
    FrogLandY# = FrogY# + 40
EndIf

After FrogLandX# or FrogLandY# have been set, the above if statement will no longer be true. This allows us to use Else to move the frog and at the same time disable player control :

Else
    If FrogLandX# < FrogX# Then FrogX# = FrogX# - 2
    If FrogLandX# > FrogX# Then FrogX# = FrogX# + 2
    If FrogLandY# < FrogY# Then FrogY# = FrogY# - 2
    If FrogLandY# > FrogY# Then FrogY# = FrogY# + 2
EndIF

We can determine which way to move the frog by comparing the landing location to the frog's current location.

And that's all there is to it. Here's an entire code example :

FrogX# = 368
FrogY# = 268
FrogLandX# = FrogX#
FrogLandY# = FrogY#

SetFPS 60

Do
    If FrogLandX# = FrogX# and FrogLandY# = FrogY#
        If LeftKey()
            FrogLandX# = FrogX# - 40
        ElseIf RightKey()
            FrogLandX# = FrogX# + 32
        ElseIf UpKey()
            FrogLandY# = FrogY# - 32
        ElseIf DownKey()
            FrogLandY# = FrogY# + 32
        EndIf    
    Else
        If FrogLandX# < FrogX# Then FrogX# = FrogX# - 2
        If FrogLandX# > FrogX# Then FrogX# = FrogX# + 2
        If FrogLandY# < FrogY# Then FrogY# = FrogY# - 2
        If FrogLandY# > FrogY# Then FrogY# = FrogY# + 2
    EndIf

    Cls 0
        BoxC FrogX#, FrogY#, FrogX# + 40, FrogY# + 40, 1, $00FF00
    Sync    

Loop

End
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.