Games [BANanoPhaser] Basic Phaser Games Development with BANano

Discussion in 'Game Development' started by Mashiane, Oct 26, 2019.

  1. Mashiane

    Mashiane Expert Licensed User


    Download BANanoPhaser

    Phaser is a popular game framework that enables one to quickly build games for the web. To do this we will make some assumptions

    1. You are comfortable with JavaScript
    2. You are comfortable with BANano esp BANanoObject class.

    Related Threads

    Star Catcher - game basics
    Dino Dash - sounds, tiles, logos, game restart, scrolling effect.
    Approximately - resource loading progress, using external fonts, scaling game.

    Game 1: BreakOut

    This has been ported from this StackAbuse article and it provides a nice introduction of how phaser and games kinda work. We are not reharshing anything here you can read the article in parallel with the code in this thread. With this game, only the basic Phaser implementation has been built just to build this game.


    For anyone willing to start creating games with Phaser, you can do so in BANanao with BANanoPhaser. This is rather a massive library so we just cover the basics here to get you going.

    With comments, the code for this game is just about 240 lines of code. To develop this game we are using Phaser 3. This will introduce one to Phaser.

    We need to remember 3 things here, 1. Preload (this for loading our game assets to the cache), 2. Create (this for where we create the actual game) and 3. Update (this is where movement, animation in the game happens and how we control that)

    So our game has such callback subs to control how everything works.
    Last edited: Nov 11, 2019 at 7:19 AM
  2. Mashiane

    Mashiane Expert Licensed User

    1. We have some global variables that we need to define first that are accessible across the game.

    Sub Process_Globals
    Private BANano As BANano
    Private body As BANanoElement
    Private game As BANanoPhaser
    Private gameDiv As BANanoElement
    'global variables
        Private player As BANanoObject
    Private ball As BANanoObject
    Private violetBricks As BANanoObject
    Private yellowBricks As BANanoObject
    Private redBricks As BANanoObject
        Private gameStarted As Boolean = False
    Private openingText As BANanoObject
    Private gameOverText As BANanoObject
    Private playerWonText As BANanoObject
        Private Scene As PhaserScene
    End Sub
    • We have a player - i.e. the paddle
    • We have a ball that we will hit
    • We have some assortements of bricks
    • We also have Start text, game over and game won text
    Johan Hormaza likes this.
  3. Mashiane

    Mashiane Expert Licensed User

    2. We initialize the game, build up the code for the 3 methods, preload, create and update and then start the game.

    We create a div inside the body where we will render the game. The code has been commented as much as possible to give us an idea of what is happening.

    Sub Init
    'set the body of the page
        body = BANano.GetElement("#body")
    "margin":"0 auto","padding":"0","width":"100%""height":"100%")))
    'add the div to set the game in
        'we will use inline style
        gameDiv = body.Append($"<div id="game"></div>"$).Get("#game")
    "margin""10px auto""padding""0""width""800px""height""640px")))
    'lets set up the game
        'initialze the game, use game element and set size
    'The Type can be Phaser.CANVAS, Phaser.WEBGL Or Phaser.AUTO. AUTO means that
        'Phaser will Try To render with WebGL, And fall back To Canvas If it fails
    'element to inject the game in
    'Ensure the canvas is resized to fit the parent div's dimensions
    'Center the game canvas both horizontally and vertically within the parent
    'The physics engine determines how objects interact with the world. Phaser
        'supports three physics engines out of the box: arcade, impact and matter.
        ' Arcade is understood to be the simplest one to implement
    'create a scene there can be multiple scenes in the same game
        Scene = game.CreateScene("breakout")
    'steps in the game scene to execute
        Scene.SetOnPreload(Me, "onPreLoad")
    End Sub
    Next we look at the callback methods that make the game tick / work!
    Johan Hormaza likes this.
  4. Mashiane

    Mashiane Expert Licensed User

    3. Preload Resources.


    There are some resources that we will use in our game, we have to preload these so that they are available in our game world!.

    ' The function loads assets as Phaser begins to run the Scene. The images are
    ' loaded as key value pairs, we reference the assets by their keys of course
     Sub onPreload
    End Sub
    Johan Hormaza likes this.
  5. Mashiane

    Mashiane Expert Licensed User

    4. We now create the game... this involves placing the bricks, ball, paddle etc. This sub will also have some callback for when the paddle collides with the ball and the ball collides with the brick and the respective logic.

    ' We create our game world in this function. The initial state of our game is
    ' defined here. We also set up our physics rules here
    ' Coordinates start at 0,0 from the top left
    ' As we move rightward, the x value increases
    ' As we move downward, the y value increases.
    Sub onCreate
    'add a sprite
        player = Scene.PhysicsAddSprite(400600"paddle")
        ball = Scene.PhysicsAddSprite(
    'add sprite grounds
        violetBricks = Scene.PhysicsAddGroup("brick1"98014070True)
        yellowBricks = Scene.PhysicsAddGroup(
        redBricks = Scene.PhysicsAddGroup(
    'Manage key presses
    'Ensure that the player and ball can't leave the screen
        Scene.SetCollideWorldBounds(player, True)
    'The bounce ensures that the ball retains its velocity after colliding with an object.
        Scene.SetBounceXY(ball, 11)
    ' Disable collision with the bottom of the game world. This needs to be added so the ball falls to the bottom, which means that the game is over
    'Add collision for the bricks
        Dim currentBall As BANanoObject
    Dim currentPlayer As BANanoObject
    Dim cbHitBrick As BANanoObject = BANano.CallBack(Me, "hitBrick"Array(currentBall, currentPlayer))
        Scene.PhysicsAddCollider5(ball, violetBricks, cbHitBrick, 
    Null, Scene.Scene)
        Scene.PhysicsAddCollider5(ball, yellowBricks, cbHitBrick, 
    Null, Scene.scene)
        Scene.PhysicsAddCollider5(ball, redBricks, cbHitBrick, 
    Null, Scene.scene)
    'Make the player immovable
        Scene.SetImmovable(player, True)
    'Add collision for the player
        Dim cbhitPlayer As BANanoObject = BANano.CallBack(Me, "hitPlayer"Array(currentBall, currentPlayer))
        Scene.PhysicsAddCollider5(ball, player, cbhitPlayer, 
    Null, Scene.scene)
    'Create opening text
        Dim txtX As Int = Scene.PhysicsWorldBoundsWidth / 2
    Dim txtY As Int = Scene.PhysicsWorldBoundsHeight / 2
        openingText = Scene.SceneAddText(txtX, txtY, "Press SPACE to Start", _
    "fontFamily""Monaco, Courier, monospace""fontSize""50px""fill""#fff"))
    'The origin of the text object is at the top left, change the origin to the center so it can be properly aligned
        Scene.SetOrigin(openingText, 0.5)
    ' Create game over text
        gameOverText = Scene.SceneAddText(txtX, txtY, "Game Over", _
    "fontFamily""Monaco, Courier, monospace""fontSize""50px""fill""#fff"))
    'Make it invisible Until the player loses
        Scene.SetVisible(gameOverText, False)
    'Create the game won text
        playerWonText = Scene.SceneAddText(txtX, txtY, "You won!", _
    "fontFamily""Monaco, Courier, monospace""fontSize""50px""fill""#fff"))
    'make invisible until the player wins
        Scene.SetVisible(playerWonText, False)
    End Sub
    Johan Hormaza likes this.
  6. Mashiane

    Mashiane Expert Licensed User

    5. Running the game. This happens during the FPS (frames per second execution / ticking)

    Here a check of whether the game is over or a game is won happens. This also enables one to press space bar to start the game.

    'happens during the game loop phase
    Sub onUpdate
        'check if the game is over
        Dim isOver As Boolean = isGameOver
    Dim IsWon As Boolean = isGameWon
        If isOver Then
    else if IsWon Then
    'Put this in so that the player doesn't move if no key is being pressed
            Scene.BodySetVelocityX(player, 0)
    'check the cursor and move the velocity accordingly, with arcade we adjust velocity for movement
            'to maniputing xy values directly
            If Scene.LeftIsDown Then
                Scene.BodySetVelocityX(player, -
    Else if Scene.RightIsDown Then
    End If
    'the game starts only when the user presses the spacebar
            If gameStarted = False Then
    'the ball should follow the paddle while the user selects to start
                Dim px As Int = Scene.GetX(player)
                Scene.SetX(ball, px)
                If Scene.SpaceIsDown Then
                    gameStarted = 
                    Scene.SetVelocityY(ball, -
    End If
    End If
    End If
    End Sub
    Johan Hormaza likes this.
  7. Mashiane

    Mashiane Expert Licensed User

  8. Toky Olivier

    Toky Olivier Member Licensed User

    Thank you for your work. Very good one. Is it possible one day to use it with B4A (for example with the webview)? Will the performance be good?
    Mashiane likes this.
  9. Mashiane

    Mashiane Expert Licensed User

    Pleasure, yes I believe it can be used with a webview too. The game engine itself has mobile in mind as there is a plugin in it for Facebook games.

    I think its my gif recorder that didnt do justice to some of the graphics. otherwise so far I am happy with the performance.
    Toky Olivier likes this.
  10. Mashiane

    Mashiane Expert Licensed User

    Game 4: Emitters (Phaser 0.2)

    This is code from the Phaser site that demostrates emitters.


    The game code is simple enough.

    Sub onPreload
    End Sub

    Sub onCreate
    Dim particles As PhaserObject = Scene.add.particles("red")
    Dim emitter As PhaserObject = Scene.ToPhaserObject(particles.createEmitter(10010"NORMAL"))
    Dim logo As PhaserObject = Scene.physics.add.image(400100"logo")
    End Sub
    See the Emitters folder in the Github
    Last edited: Nov 11, 2019 at 8:03 AM
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice