Draw Text and move with arrow keys
Draw Text and move with arrow keys
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..
So near but yet so far..
I have the text moving but.
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...
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
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" ':
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.
Dhoo. Sorry Mate
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
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
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.
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.
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
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%).
- 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