none
Array Issues RRS feed

  • Question

  • Hi! I am currently making a bit more advanced Minecraft 2D. It uses arrays to check for the tree's locations and save the level (soon to come). Every time the player moves, I need it to check to see if the player has ran into a tree. Do you have any suggestions? I already have a array checker, but it will not work. Thanks for any help! 

    'Loads Game GraphicsWindow.Title = "Minecraft 2D" GraphicsWindow.Height = 700 GraphicsWindow.Width = 700 GraphicsWindow.Top = 0 GraphicsWindow.BackgroundColor = "Green" GraphicsWindow.BrushColor = "Gray" 'Loads Player into Memory mex = 1 mey = 1 player = Shapes.AddRectangle(30, 30) drawTrees() GraphicsWindow.TextInput = playerControls 'Draw Trees Sub drawTrees 'Gets a Random Number With to Total Number of Trees to Draw totalTrees = Math.GetRandomNumber(10) 'Sets the Current tree that is in the Loop curTreeToDraw = 1 'Creates Two Arrays Used to Check if the Player Ran into One treePositionX = LDArray.create(10) treePositionY = LDArray.create(10) While curTreeToDraw < totalTrees GraphicsWindow.BrushColor = "LimeGreen" treeX = Math.GetRandomNumber(650) treeY = Math.GetRandomNumber(650) tree = Shapes.AddEllipse(20, 20) Shapes.Move(tree, treeX, treeY) LDArray.SetValue(treePositionX, curTreeToDraw, treeX) LDArray.SetValue(treePositionY, curTreeToDraw, treeY) curTreeToDraw = curTreeToDraw + 1 EndWhile EndSub Sub playerControls If GraphicsWindow.LastText = "w" Then mey = mey - 10 Shapes.Move(player, mex, mey) EndIf If GraphicsWindow.LastText = "a" Then mex = mex - 10 Shapes.Move(player, mex, mey) EndIf If GraphicsWindow.LastText = "s" Then mey = mey + 10 Shapes.Move(player, mex, mey) EndIf If GraphicsWindow.LastText = "d" Then mex = mex + 10 Shapes.Move(player, mex, mey) EndIf

    'Checks if Run In-To Tree
            For i = 1 To 10
              checkX = LDArray.GetValue(treePositionX, i)
              checkY = LDArray.GetValue(treePositionY, i)
              If mex = checkX Then
                 If mey = checkY Then
                   mex = mex - 50
                  mey = mey - 50
                  Shapes.Move(player, mex, mey)
                  EndIf
                EndIf
                
              If mey = checkY Then
                If mex = checkX Then
                mex = mex - 50
                mey = mey - 50
                Shapes.Move(player, mex, mey)
              EndIf
              endif
            EndFor

    EndSub



    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."



    Wednesday, September 19, 2012 11:22 PM

Answers

  • Hi Noah,

    I have taken the time to write you a simplish example. This works by storing the tiles (sprites) in a two dimensional array and checking the players position (the ball) within the array (map) to determine whether or not it can travel there.

    Please make sure you add two images into the programs directory one called wall.png and the other grass.png these have to be 32*32 pixels.

    The collision detection can be changed to checking if a wall is in the way before the player is moved - I just wanted to see how fast SmallBasic could handle returning the ball to its original position.

    'Setup window
    GraphicsWindow.Width = 320
    GraphicsWindow.Height = 320
    GraphicsWindow.CanResize = "false"
    'File directory
    Dir = Program.Directory
    'load images
    WallImg = ImageList.LoadImage(Dir + "\wall.png")
    GrassImg = ImageList.LoadImage(Dir + "\grass.png")
    'Load and position player
    BallImg = Shapes.AddEllipse(25, 25)
    PlayerX = 32
    PlayerY = 64
    Shapes.Move(BallImg, PlayerX, PlayerY)
    'Tiles
    Tile[1] = WallImg
    Tile[2] = GrassImg
    'Create a 2D array to store tiles on map. (This data would normally be stored in a file.)
    Map[0][0] = 1
    Map[1][0] = 1
    Map[5][5] = 1
    Map[4][3] = 1
    Map[4][4] = 2
    Map[5][4] = 2
    'Draw map
    For x = 0 To GraphicsWindow.Width / 32
      For y = 0 To GraphicsWindow.Height / 32
        TileToDraw = Map[x][y]
        GraphicsWindow.DrawImage(Tile[TileToDraw], x * 32, y * 32)
      EndFor
    EndFor
    'Player movement
    GraphicsWindow.KeyDown = OnKeyDown
    Sub OnKeyDown
      If GraphicsWindow.LastKey = "Left" Then
        PlayerDirection = 1
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey = "Right" Then
        PlayerDirection = 3
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey = "Up" Then
        PlayerDirection = 2
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey =  "Down" Then
        PlayerDirection = 4
        PlayersPosition()
      EndIf
    EndSub  
    'Move player when key is pressed then check map position if wall is there move back.
    Sub PlayersPosition
      If PlayerDirection = 1 Then 
        PlayerX = PlayerX - 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerX = PlayerX + 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 2 Then
        PlayerY = PlayerY - 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerY = PlayerY + 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 3 Then
        PlayerX = PlayerX + 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerX = PlayerX - 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 4 Then
        PlayerY = PlayerY + 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerY = PlayerY - 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      EndIf
    EndSub

    If you have any questions please let me know. Please note this is the first time I have written a tile based graphical program so there may be better ways to do things.
    • Edited by Mainchip Thursday, September 20, 2012 4:42 AM
    • Marked as answer by Qazwsxedc Qazwsxedc Thursday, September 27, 2012 10:24 PM
    Thursday, September 20, 2012 4:41 AM

All replies

  • Hi Noah,

    I have taken the time to write you a simplish example. This works by storing the tiles (sprites) in a two dimensional array and checking the players position (the ball) within the array (map) to determine whether or not it can travel there.

    Please make sure you add two images into the programs directory one called wall.png and the other grass.png these have to be 32*32 pixels.

    The collision detection can be changed to checking if a wall is in the way before the player is moved - I just wanted to see how fast SmallBasic could handle returning the ball to its original position.

    'Setup window
    GraphicsWindow.Width = 320
    GraphicsWindow.Height = 320
    GraphicsWindow.CanResize = "false"
    'File directory
    Dir = Program.Directory
    'load images
    WallImg = ImageList.LoadImage(Dir + "\wall.png")
    GrassImg = ImageList.LoadImage(Dir + "\grass.png")
    'Load and position player
    BallImg = Shapes.AddEllipse(25, 25)
    PlayerX = 32
    PlayerY = 64
    Shapes.Move(BallImg, PlayerX, PlayerY)
    'Tiles
    Tile[1] = WallImg
    Tile[2] = GrassImg
    'Create a 2D array to store tiles on map. (This data would normally be stored in a file.)
    Map[0][0] = 1
    Map[1][0] = 1
    Map[5][5] = 1
    Map[4][3] = 1
    Map[4][4] = 2
    Map[5][4] = 2
    'Draw map
    For x = 0 To GraphicsWindow.Width / 32
      For y = 0 To GraphicsWindow.Height / 32
        TileToDraw = Map[x][y]
        GraphicsWindow.DrawImage(Tile[TileToDraw], x * 32, y * 32)
      EndFor
    EndFor
    'Player movement
    GraphicsWindow.KeyDown = OnKeyDown
    Sub OnKeyDown
      If GraphicsWindow.LastKey = "Left" Then
        PlayerDirection = 1
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey = "Right" Then
        PlayerDirection = 3
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey = "Up" Then
        PlayerDirection = 2
        PlayersPosition()
      ElseIf GraphicsWindow.LastKey =  "Down" Then
        PlayerDirection = 4
        PlayersPosition()
      EndIf
    EndSub  
    'Move player when key is pressed then check map position if wall is there move back.
    Sub PlayersPosition
      If PlayerDirection = 1 Then 
        PlayerX = PlayerX - 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerX = PlayerX + 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 2 Then
        PlayerY = PlayerY - 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerY = PlayerY + 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 3 Then
        PlayerX = PlayerX + 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerX = PlayerX - 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      ElseIf PlayerDirection = 4 Then
        PlayerY = PlayerY + 32
        Shapes.Move(BallImg, PlayerX, PlayerY)
        If Map[PlayerX/32][PlayerY/32] = 1 Then
          PlayerY = PlayerY - 32
          Shapes.Move(BallImg, PlayerX, PlayerY)
        EndIf
      EndIf
    EndSub

    If you have any questions please let me know. Please note this is the first time I have written a tile based graphical program so there may be better ways to do things.
    • Edited by Mainchip Thursday, September 20, 2012 4:42 AM
    • Marked as answer by Qazwsxedc Qazwsxedc Thursday, September 27, 2012 10:24 PM
    Thursday, September 20, 2012 4:41 AM
  • Thanks! I will use your example to fix my program ASAP! :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Thursday, September 20, 2012 12:13 PM