Draw Text and move with arrow keys

Just starting out? Need help? Post your questions and find answers here.
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Draw Text and move with arrow keys

Post 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..
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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 may look like a mule, but I'm not a complete ass.
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

I have the text moving but.

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I don't understand... works okay here!
I may look like a mule, but I'm not a complete ass.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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 
Last edited by Ollivier on Thu Apr 02, 2009 1:05 pm, edited 1 time in total.
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Dhoo. Sorry Mate

Post 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
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Post 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.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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 
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Post by PeterGams »

Thanks Oliver...

That's what I was after.

Easy for you not so much for me.

Thanks Mate.

Go MAME !
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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 
Post Reply