' 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
Log("creating...")
'add a sprite
player = Scene.PhysicsAddSprite(400, 600, "paddle")
ball = Scene.PhysicsAddSprite(400, 565, "ball")
'add sprite grounds
violetBricks = Scene.PhysicsAddGroup("brick1", 9, 80, 140, 70, True)
yellowBricks = Scene.PhysicsAddGroup("brick2", 9, 80, 90, 70, True)
redBricks = Scene.PhysicsAddGroup("brick3", 9, 80, 40, 70, True)
'Manage key presses
Scene.CreateCursorKeys
'Ensure that the player and ball can't leave the screen
Scene.SetCollideWorldBounds(player, True)
Scene.SetCollideWorldBounds(ball, True)
'The bounce ensures that the ball retains its velocity after colliding with an object.
Scene.SetBounceXY(ball, 1, 1)
' 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
Scene.SetPhysicsWorldCheckCollisionDown(False)
'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", _
CreateMap("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", _
CreateMap("fontFamily": "Monaco, Courier, monospace", "fontSize": "50px", "fill": "#fff"))
Scene.SetOrigin(gameOverText, 0.5)
'Make it invisible Until the player loses
Scene.SetVisible(gameOverText, False)
'Create the game won text
playerWonText = Scene.SceneAddText(txtX, txtY, "You won!", _
CreateMap("fontFamily": "Monaco, Courier, monospace", "fontSize": "50px", "fill": "#fff"))
Scene.SetOrigin(playerWonText, 0.5)
'make invisible until the player wins
Scene.SetVisible(playerWonText, False)
End Sub