Page 1 of 1

Draw Text and move with arrow keys

Posted: Thu Apr 02, 2009 12:04 pm
by PeterGams
I am having trouble drawing text on the screen and using arrow keys to move it up or down.

So near but yet so far..

Posted: Thu Apr 02, 2009 12:14 pm
by srod
If you're close then it may be that someone can add the finishing touch for you.

Course that does require you to post your code first.

I have the text moving but.

Posted: Thu Apr 02, 2009 12:42 pm
by PeterGams
I would like to press a arrow key and the text would move a set amount.
eg. press down arrow and the text moves down 10 pixels smoothly.

I have changed this code arounf to suit but don't remember where the original came from sorry...

Code: Select all

  xx = 140:yy = 120
  If InitSprite() And InitKeyboard() And OpenScreen(800,600,16,"")
    Repeat
      FlipBuffers()
      ClearScreen(RGB(0, 0, 0))
      
      ExamineKeyboard()
 
        Title$ = "XXXXXXXXXXXXXX"
      
  If KeyboardPushed(#PB_Key_Down) 
    xx + 2
    
  EndIf
      
    If KeyboardPushed(#PB_Key_Up) 
    xx - 2 
  EndIf    
      
   If KeyboardPushed(#PB_Key_Right) 
    yy + 3
  EndIf
      
    If KeyboardPushed(#PB_Key_Left) 
    yy - 3 
  EndIf     
            ; Display the result
      ;
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(128, 255, 0))
          DrawText(yy, xx, Title$)
        StopDrawing()
      EndIf
    
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf

Posted: Thu Apr 02, 2009 12:50 pm
by srod
I don't understand... works okay here!

Posted: Thu Apr 02, 2009 1:03 pm
by Ollivier
Attention!

You update the content of your string IN the main loop. So it erases all modifications you would add in this same string. I moved the line 'Title$ = "xxxx" ':

Code: Select all

  xx = 140:yy = 120
  If InitSprite() And InitKeyboard() And OpenScreen(800,600,16,"")
  Title$ = "XXXXXXXXXXXXXX"
    Repeat
      FlipBuffers()
      ClearScreen(RGB(0, 0, 0))
     
      ExamineKeyboard()
 
     
  If KeyboardPushed(#PB_Key_Down)
    xx + 2
   
  EndIf
     
    If KeyboardPushed(#PB_Key_Up)
    xx - 2
  EndIf   
     
   If KeyboardPushed(#PB_Key_Right)
    yy + 3
  EndIf
     
    If KeyboardPushed(#PB_Key_Left)
    yy - 3
  EndIf     
  Title$ + KeyboardInkey()
            ; Display the result
      ;
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(128, 255, 0))
          DrawText(yy, xx, Title$)
        StopDrawing()
      EndIf
   
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf 

Dhoo. Sorry Mate

Posted: Thu Apr 02, 2009 1:04 pm
by PeterGams
I don't explain things very well.
Pressing the down arrow once and releasing it, will cause the text to scroll down about 10pixels smoothly.

bassically if you tap the down arrow key the text will scroll down a set amount of pixels.

Hope that makes it a bit clearer

Posted: Thu Apr 02, 2009 1:11 pm
by PeterGams
Instead of pressing a key and the value of 3 gets added/subtracted to the position of the text every time.

I would like to press a key and it will add from 0 to 10 to the text position for example.

Whenever the down arrow key is tapped the text will scroll down smoothly 10 pixels.

Sorry guys I am hopeless at explaining this stuff.

Posted: Thu Apr 02, 2009 1:49 pm
by Ollivier
And that? Mh?

Code: Select all

xx = 140:yy = 120
  If InitSprite() And InitKeyboard() And OpenScreen(800,600,16,"")
  Title$ = "XXXXXXXXXXXXXX"
    Repeat
      FlipBuffers()
      ClearScreen(RGB(0, 0, 0))
     
      ExamineKeyboard()
 
     
  If KeyboardPushed(#PB_Key_Down)
      CountDown = 10
      vy = 2
  EndIf
  If KeyboardPushed(#PB_Key_Up)
      CountDown = 10
      vy = - 2
  EndIf   
     
   If KeyboardPushed(#PB_Key_Right)
      CountDown = 10
      vx = 2
  EndIf
     
    If KeyboardPushed(#PB_Key_Left)
      CountDown = 10
      vx = - 2
  EndIf   
  
  If CountDown
      CountDown - 1
      xx + vx
      yy + vy
  Else
      vx = 0
      vy = 0
  EndIf
    
  Title$ + KeyboardInkey()
            ; Display the result
      ;
      If StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(RGB(128, 255, 0))
          DrawText(xx, yy, Title$)
        StopDrawing()
      EndIf
   
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf 

Posted: Thu Apr 02, 2009 9:46 pm
by PeterGams
Thanks Oliver...

That's what I was after.

Easy for you not so much for me.

Thanks Mate.

Go MAME !

Posted: Fri Apr 03, 2009 8:14 pm
by Ollivier
In the future, you'll have to use:
- Delay(1) statement >> shares CPU ressources
- Sprites >> accelerate displaying speed
- Macro >> allows you to copy same code lines in different places in the code

This code uses less than 4% CPU ressources (these codes above uses 100%).

Code: Select all

Macro Draw()
   If StartDrawing(SpriteOutput(#StringTestSpr))
      DrawingMode(0)
      Box(0, 0, SpriteWidth(#StringTestSpr), SpriteHeight(#StringTestSpr), RGB(0, 0, 0) )
      DrawText(8, 8, Title$, RGB(128, 255, 0), RGB(0, 0, 0) )
      StopDrawing()
   EndIf
   DisplaySprite(#StringTestSpr, xx, yy)
   Delay(1)
   FlipBuffers()
EndMacro

   #StringTestSpr = 1
   xx = 140:yy = 120
   If InitSprite()
      If InitKeyboard()
         If OpenScreen(800,600,16,"")
            CreateSprite(#StringTestSpr, 256, 32)
            Title$ = "XXXXXXXXXXXXXX"
            ClearScreen(RGB(0, 0, 0))
            Draw()
            Repeat
               Delay(1)
               If ExamineKeyboard()
                  CharKey$ = KeyboardInkey()
                  If CharKey$
                     Title$ + CharKey$
                     Draw()
                  EndIf
                  If KeyboardPushed(#PB_Key_Back)
                     If BackKey = 0
                        BackKeyStart = ElapsedMilliseconds()
                     EndIf
                     If (BackKey = 0) Or ((ElapsedMilliseconds() - BackKeyStart) > 600)
                        If Title$
                           Title$ = Left(Title$, Len(Title$) - 1)
                        EndIf
                        Draw()
                     EndIf
                     BackKey + 1
                  Else   
                     BackKey = 0
                  EndIf
                  If KeyboardPushed(#PB_Key_Down)
                     CountDown = 10
                     vy = 2
                  EndIf
                  If KeyboardPushed(#PB_Key_Up)
                     CountDown = 10
                     vy = - 2
                  EndIf   
                  If KeyboardPushed(#PB_Key_Right)
                     CountDown = 10
                     vx = 2
                  EndIf
                  If KeyboardPushed(#PB_Key_Left)
                     CountDown = 10
                     vx = - 2
                  EndIf   
                  If KeyboardPushed(#PB_Key_Escape)
                     Break
                  EndIf 
               EndIf
               If CountDown
                  xx + vx
                  yy + vy
                  CountDown - 1
                  If CountDown <= 0
                     vx = 0
                     vy = 0   
                  EndIf
                  Draw()
               EndIf
            ForEver
         EndIf
      EndIf
  EndIf