none
Can you see what is wrong with this code sample, which moves a square on the screen using the cursor keys? RRS feed

  • Question

  • please help me. i was trying to make a square that moves around using the arrow keys however it does not. i checked it multiple times and it is correct. this is my code:-

    GRAPHICS WINDOW
    gh = 600
    gw = 1000
    GraphicsWindow.Height = gh
    GraphicsWindow.Width = gw
    GraphicsWindow.Top = 84
    GraphicsWindow.Left = 183
    GraphicsWindow.BackgroundColor = "black"
    'EVENT FLAGS


    speed = 5

    playerup = 0
    playerdown = 0
    playerleft = 0
    playerright = 0


    'PLAYER'
    player = Shapes.AddRectangle(200, 200)
    Shapes.Move(player, 400, 200)


    'SUBROUTINES'

    GraphicsWindow.KeyDown = kd
    GraphicsWindow.KeyUp = ku
    'GAMEPLAY'
    If playerup = 1 Then
              Shapes.Move(player, x, y - speed)
            ElseIf playerdown = 1 then 
              Shapes.Move(player, x, y + 5)
            elseif playerleft = 1 then
              Shapes.Move(player, x - 5, y)
            elseif playerright = 1 then
              Shapes.Move(player, x + 5, y)
            EndIf
    'SUBROUTINES
    Sub kd
      x = Shapes.GetLeft(player)
      y = Shapes.GetTop(player)
      If GraphicsWindow.LastKey = "Up" Then 
        playerup = 1
        elseIf GraphicsWindow.LastKey = "down" Then 
          playerdown = 1
         elseIf GraphicsWindow.LastKey = "left" Then
            playerleft = 1
            elseIf GraphicsWindow.LastKey = "right" Then 
              playerright = 1
            EndIf
          EndSub
          
          
          Sub ku
            If GraphicsWindow.LastKey = "Up" Then 
        playerup = 0
        elseIf GraphicsWindow.LastKey = "down" Then 
          playerdown = 0
         elseIf GraphicsWindow.LastKey = "left" Then
            playerleft = 0
            elseIf GraphicsWindow.LastKey = "right" Then 
              playerright = 0
            EndIf
          EndSub



    Sunday, November 3, 2019 4:52 PM

Answers

  • You have to put the lines from "GAMEPLAY" to "SUBROUTINES" inside a    While "True"  ....  EndWhile, loop.

    Your program runs the gameplay lines only once.

    And: the names of the keys all start with a capital  Down  Left  Right


    Jan [ WhTurner ] The Netherlands

    Sunday, November 3, 2019 6:21 PM
    Answerer

All replies

  • You have to put the lines from "GAMEPLAY" to "SUBROUTINES" inside a    While "True"  ....  EndWhile, loop.

    Your program runs the gameplay lines only once.

    And: the names of the keys all start with a capital  Down  Left  Right


    Jan [ WhTurner ] The Netherlands

    Sunday, November 3, 2019 6:21 PM
    Answerer
  • Here is another basic approach using a game loop, import BCZ053.
    Sunday, November 3, 2019 7:19 PM
    Moderator