none
Key Registration Slow for Graphics Window RRS feed

  • Question

  • Hello, In my addiction to the Snake Game i decided to recreate it.

    1 issues is that the key registration is delayed sometimes. Any way to fix that? 

    also Ignore the textwindow. its debugging.

    height = 500
    width = 600
    
    GraphicsWindow.Width = width
    GraphicsWindow.Height=height
    
    frameRate = 10
    frameMilliSecond = (1/frameRate) * 1000
    currentMilli = Clock.ElapsedMilliseconds
    
    movementy = (height/10 ) 
    movementx = (width/10 )
    currentframeRate = Shapes.addText("FrameRate: " + 0)
    Shapes.Move(currentframeRate, width *0.8, 5)
    GraphicsWindow.KeyDown = GetKeyDown
    GraphicsWindow.KeyUp = GetKeyUp
    
    
    
    
    '======Global=======
    createSnake()
    While 1=1
      
      motion()
      frameWait()
      
    endwhile
    
    Sub motion
      key=GraphicsWindow.LastKey
      TextWindow.WriteLine("Pressed key " +key)
    
      If(keyPress<> 1) Then
        
      Else
        
        If(key = "W") Then
          MoveUp()
        ElseIf (key = "A") then
          MoveLeft()
        ElseIf (key = "D") then
          MoveRight()
        ElseIf (key ="S")then
          
          MoveDown()
        endif
        
        
      EndIf
      
    EndSub
    
    Sub GetKeyDown
      keyPress = 1
      EndSub
      Sub GetKeyUp
        keyPress =0
        EndSub
    '=====================MOVEMENT========================
    Sub MoveUp
      TextWindow.WriteLine("Going up")
      toNodex = xpos[0]
      toNodey =ypos[0]-movementy
      
      Shapes.Move(SnakeBody[0],toNodex,toNodey)
     
      PreviousNodex = xpos[0]
      PreviousNodey = ypos[0]
      xpos[0] = toNodex
      ypos[0] = toNodey
      i=1
      
      While (i<lengthofSnake) 
        toNodex = PreviousNodex
        toNodey = PreviousNodey
        
        Shapes.Move(SnakeBody[i], toNodex,toNodey)
        PreviousNodex = xpos[i]
        PreviousNodey= ypos[i]
        
        xpos[i] = toNodex
        ypos[i] = toNodey
        
        i=i+1
        
      EndWhile
      
      
    EndSub
    Sub MoveDown
      TextWindow.WriteLine("Going up")
      toNodex = xpos[0]
      toNodey =ypos[0]+movementy
      
      Shapes.Move(SnakeBody[0],toNodex,toNodey)
     
      PreviousNodex = xpos[0]
      PreviousNodey = ypos[0]
      xpos[0] = toNodex
      ypos[0] = toNodey
      i=1
      
      While (i<lengthofSnake) 
        toNodex = PreviousNodex
        toNodey = PreviousNodey
        
        Shapes.Move(SnakeBody[i], toNodex,toNodey)
        PreviousNodex = xpos[i]
        PreviousNodey= ypos[i]
        
        xpos[i] = toNodex
        ypos[i] = toNodey
        
        i=i+1
        
      EndWhile
      
    EndSub
    Sub MoveLeft
      TextWindow.WriteLine("Going up")
      toNodex = xpos[0]-movementx
      toNodey =ypos[0]
      
      Shapes.Move(SnakeBody[0],toNodex,toNodey)
     
      PreviousNodex = xpos[0]
      PreviousNodey = ypos[0]
      xpos[0] = toNodex
      ypos[0] = toNodey
      i=1
      
      While (i<lengthofSnake) 
        toNodex = PreviousNodex
        toNodey = PreviousNodey
        
        Shapes.Move(SnakeBody[i], toNodex,toNodey)
        PreviousNodex = xpos[i]
        PreviousNodey= ypos[i]
        
        xpos[i] = toNodex
        ypos[i] = toNodey
        
        i=i+1
        
      EndWhile
      
    EndSub
    Sub MoveRight
      TextWindow.WriteLine("Going up")
      toNodex = xpos[0]+movementx
      toNodey =ypos[0]
      
      Shapes.Move(SnakeBody[0],toNodex,toNodey)
     
      PreviousNodex = xpos[0]
      PreviousNodey = ypos[0]
      xpos[0] = toNodex
      ypos[0] = toNodey
      i=1
      
      While (i<lengthofSnake) 
        toNodex = PreviousNodex
        toNodey = PreviousNodey
        
        Shapes.Move(SnakeBody[i], toNodex,toNodey)
        PreviousNodex = xpos[i]
        PreviousNodey= ypos[i]
        
        xpos[i] = toNodex
        ypos[i] = toNodey
        
        i=i+1
        
      EndWhile
      
    EndSub
    '=====================END MOVEMENT==========
    
    Sub createSnake
      SnakeBody[0] = Shapes.AddRectangle(width*.10, height *0.10)
      xpos[0] = Math.GetRandomNumber(width - (width*.10))
      ypos[0] = Math.GetRandomNumber(height - (height*.10))
      
      SnakeBody[1] = Shapes.AddRectangle(width*.10, height *0.10)
      xpos[1] = xpos[0] 
      ypos[1] = ypos[0] +height*0.1
      
      SnakeBody[2] = Shapes.AddRectangle(width*.10, height *0.10)
      xpos[2] = xpos[1] 
      ypos[2] = ypos[2] +height*0.1
      
      SnakeBody[3] = Shapes.AddRectangle(width*.10, height *0.10)
      xpos[3] = xpos[2] 
      ypos[3] = ypos[2] +height*0.1
     
     SnakeBody[4] = Shapes.AddRectangle(width*.10, height *0.10)
      xpos[4] = xpos[3] 
      ypos[4] = ypos[3] +height*0.1 
      
      
      lengthOfSnake = 5
      For i = 0 To lengthofSnake-1
        Shapes.Move(SnakeBody[i], xpos[i],ypos[i])
        EndFor
     
      
    EndSub
    
    
    '=========================FRAME RATE=================
    Sub frameWait
      afterSecond = Clock.ElapsedMilliseconds
      totalTime = afterSecond - currentMilli
    
      Shapes.SetText(currentframeRate,"FrameRate: " + totalTime)
      
      While totalTime < frameMilliSecond
        afterSecond = Clock.ElapsedMilliseconds
        totalTime = afterSecond - currentMilli
      EndWhile
      currentMilli = Clock.ElapsedMilliseconds
      
    endsub

    Thanks


    Matthew


    • Edited by JetFire3201 Saturday, June 24, 2017 2:48 AM
    Saturday, June 24, 2017 2:46 AM

Answers

  • Probably a combination of auto key repeat and your frame rate control, and possibly getting the last key at a different part of the code from detecting key press - not really certain.  However, here is how I would do it (MTV330) and it seems to be ok.
    • Marked as answer by JetFire3201 Saturday, June 24, 2017 2:08 PM
    Saturday, June 24, 2017 7:40 AM
    Moderator

All replies

  • Probably a combination of auto key repeat and your frame rate control, and possibly getting the last key at a different part of the code from detecting key press - not really certain.  However, here is how I would do it (MTV330) and it seems to be ok.
    • Marked as answer by JetFire3201 Saturday, June 24, 2017 2:08 PM
    Saturday, June 24, 2017 7:40 AM
    Moderator
  • Thank you. Whats the delay do? i understand the clock part, but what is the 100 for? is that like how long the frame rate should be. if its not 100 milliseconds long, to wait the rest??

    Also why does your code work and  mine doesnt? It seems that the delay would be the only thing that would have changed it. But i do like the getkeyup and down changes. Also i dont see the instantiation of the keys array. Do you not need to instantiate it? (I have a java background so...)


    Matthew

    Saturday, June 24, 2017 2:08 PM
  • the delay is 100ms i.e. frame rate 10 fps - any faster and the worm moved too fast to test.

    You could try swapping bits in and out to see what the reason was - I expect it was to do with key auto delay interacting with the fact that the key pressed was got in the main game loop, not when the key was pressed in conjunction with how you were creating the delay - sometimes the delay prevented a key being be caught somehow before the auto delay prevented the next key down from being detected - not a simple answer I'm afraid.

    Any variable or array not defined is assumed to be empty.  SB is very lax compared to most other languages about initialising variables.

    Saturday, June 24, 2017 2:29 PM
    Moderator
  • Im gonna assume that if you did increase the fps, you could affect also the movement by basing it on, lets say 2 spaces every second regardless of frame rate, but since this is a consistent 2 spaces, it does not matter how smooth it is since its a jump, not an ease into. it could be an ease into by doing shapes.animate but what would the point be... 


    Matthew

    Saturday, June 24, 2017 2:58 PM
  • Smaller movement distances per frame at higher frame rate might help - about 50 fps (20 ms delay) is usually good for smooth movement - don't use animate for this kind of movement where position is updated every frame.

    Slower frame rate for complex game logic or lots of graphics shapes.  If then too slow and fully optimised, then LD extension can help speeding some aspects.

    Saturday, June 24, 2017 3:14 PM
    Moderator
  • Right but the thing is, how im adding the movement is that im moving the cubes where the other cube was located in.

    Matthew

    Saturday, June 24, 2017 3:54 PM