Small Basic Sample: JetLag

This is a video game where you have to avoid obstacles. Start the game by hitting the space bar, move left and right with the arrow keys.

You can import the program with ID RCB513

Code

Initialize()
While (GameState<>"End")
    DoGameLoop()
EndWhile
Program.End()
 
Sub Initialize
    'Initialize Variables
    Score = 0
    Direction = 0
    GameState = "GameOver"
    GameDelay = 100
     
    'Initialize the GraphicsWindow
    GraphicsWindow.Show() 
    UpdateScore()
    GraphicsWindow.Width = 640 
    GraphicsWindow.Height = 480 
    GraphicsWindow.BackgroundColor = "#000000"
    GraphicsWindow.KeyDown = OnKeyDown
    
    'Initialize Blocks
    GraphicsWindow.PenWidth = 0
    GraphicsWindow.BrushColor = "#FFFFFF"
    For Index=0 To 29
        Temp = Shapes.AddRectangle(16,16)
        Shapes.Move(Temp,0,Index*16)
        Array.SetValue("Blocks",Index,Temp)
        Array.SetValue("BlockPositions",Index,0)
    EndFor
    
    'Initialize Walls
    GraphicsWindow.PenWidth = 0
    GraphicsWindow.BrushColor = "#0000FF"
    Temp = Shapes.AddRectangle(16,480)
    shapes.Move(Temp,624,0)
    shapes.AddRectangle(16,480)
    
    'Initialize Tail
    GraphicsWindow.PenWidth = 0
    GraphicsWindow.BrushColor = "#FFFF00"
    For Index = 0 To 4
        Temp = Shapes.AddRectangle(16,16)
        Shapes.Move(Temp,320,Index*16)
        Array.SetValue("Tail",Index,Temp)
        Array.SetValue("TailPositions",Index,20)
    EndFor
    
    'Initialize Head
    GraphicsWindow.PenWidth = 0
    GraphicsWindow.BrushColor = "#FF0000"
    Temp = Shapes.AddRectangle(16,16)
    Shapes.Move(Temp,320,5*16)
    Array.SetValue("Tail",5,Temp)
    Array.SetValue("TailPositions",5,20)
    
EndSub
 
Sub UpdateScore
    GraphicsWindow.Title = "SBJetLag - Score: " + Score
EndSub
 
Sub OnKeyDown
    Temp = GraphicsWindow.LastKey
    If (GameState = "GameOver") Then
        If (GraphicsWindow.LastKey = "Space") Then
            StartGame()
        Else
            If (GraphicsWindow.LastKey = "Escape") Then
                GameState = "End"
            Else
                 If (GraphicsWindow.LastKey = "D1") Then
                    GameDelay = 100
                    GraphicsWindow.ShowMessage("Game delay is set to 100ms","Speed Changed!")
                Else
                    If (GraphicsWindow.LastKey = "D2") Then
                        GameDelay = 200
                        GraphicsWindow.ShowMessage("Game delay is set to 200ms","Speed Changed!")
                    Else
                        If (GraphicsWindow.LastKey = "D3") Then
                            GameDelay = 300
                            GraphicsWindow.ShowMessage("Game delay is set to 300ms","Speed Changed!")
                        Else
                            If (GraphicsWindow.LastKey = "D4") Then
                                GameDelay = 400
                                GraphicsWindow.ShowMessage("Game delay is set to 400ms","Speed Changed!")
                            Else
                                If (GraphicsWindow.LastKey = "D5") Then
                                    GameDelay = 500
                                    GraphicsWindow.ShowMessage("Game delay is set to 500ms","Speed Changed!")
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            EndIf
        Endif
    Else
        If (GraphicsWindow.LastKey = "Left") Then
            Direction = -1
        Else
            If (GraphicsWindow.LastKey = "Right") Then
                Direction = 1
            EndIf
        EndIf
        
    EndIf
EndSub
 
Sub StartGame
    Score = 0
    Direction = 1
    UpdateScore()
    For Index=0 To 29
        Temp = Array.GetValue("Blocks",Index)
        Shapes.Move(Temp,0,Index*16)
        Array.SetValue("BlockPositions",Index,0)
    EndFor
    For Index=0 To 5
        Temp = Array.GetValue("Tail",Index)
        Shapes.Move(Temp,20*16,Index*16)
        Array.SetValue("TailPositions",Index,20)
    EndFor
    GameState = "Play"
EndSub
 
Sub DoGameLoop
    If (GameState = "Play") Then
        Score = Score + 1
        UpdateScore()
        For Index=0 To 28
            Temp = Array.GetValue("BlockPositions",Index+1)
            Array.SetValue("BlockPositions",Index,Temp)
        EndFor
        Array.SetValue("BlockPositions",29,Math.GetRandomNumber(38))
        For Index=0 To 29
            Temp = Array.GetValue("Blocks",Index)
            Shapes.Move(Temp,Array.GetValue("BlockPositions",Index)*16,Index*16)
            
        EndFor
        For Index=0 To 4
            Temp = Array.GetValue("TailPositions",Index+1)
            Array.SetValue("TailPositions",Index,Temp)
        EndFor
        Array.SetValue("TailPositions",5,Array.GetValue("TailPositions",5)+Direction)
        For Index=0 To 5
            Temp = Array.GetValue("Tail",Index)
            Shapes.Move(Temp,Array.GetValue("TailPositions",Index)*16,Index*16)
        EndFor
        Temp = Array.GetValue("TailPositions",5)
        If (Temp=0 Or Temp = 39 Or Temp = Array.GetValue("BlockPositions",5)) Then
            GameState = "GameOver"
        EndIf
        Program.Delay(GameDelay)
    EndIf
EndSub

See Also

Other Languages