a little tutorial STEP by STEP on scrolling in 2D graphic mode - TempodiBasic - 12-01-2024
Hi friends
I want to share these basical knowledges about scrolling background in graphic mode using the powerful _PUTIMAGE.
It is a basical 2D graphic mode 32bit.
I post the following 6 QB64pe code examples that start from a backgroung scrolling to right and leaving a black screen to arrive to a background scrolling in the 4 directions of a bidimensional field in which the user can start/stop the scrolling, change the directions of the scrolling and quit the program.
If you want to see the final result you must run the STEP 6.
If you want follow the tutorial you must go step by step from 1 to 6.
Happy enjoyment with this code.
STEP 1
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 1 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 1" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PrintString (1, 1), "Press Q to quit program" ' it shows instruction for user on the screen
x = x + 1 'it calculates the new position where starting to paint background, it is towards right
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
STEP 2
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 2 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 2" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PrintString (1, 1), "Press Q to quit program, A to restart" ' it shows instruction for user on the screen
x = x + 1 'it calculates the new position where starting to paint background, it is towards right
If k = "A" Then x = 1 ' it corrects x when user restarts
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
STEP 3
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 3 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String
_Title " Demo of background scrolling: step 3" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PutImage (x - w, y), Back, Scr ' it completes the background on the left side
_PrintString (1, 1), "Press Q to quit program" ' it shows instruction for user on the screen
x = x + 1 'it calculates the new position where starting to paint background, it is towards right
If x > w Then x = 1 ' it corrects x when is over
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
STEP 4
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 4 a background scrolling towards right in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, S As Integer
_Title " Demo of background scrolling: step 4" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
S = 1
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PutImage (x - w, y), Back, Scr ' it completes the background on the left side
_PrintString (1, 1), "Press Q to quit program, S to stop/start demo" ' it shows instruction for user on the screen
If k = "S" Then If S > 0 Then S = -1 Else S = 1 ' it starts/stops the scrolling to right
If S > 0 Then x = x + 1 'it calculates the new position where starting to paint background, it is towards right
If x > w Then x = 1 ' it corrects x when is over
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
STEP 5
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 5 a background scrolling towards right /left in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, S As Integer, d As Integer
_Title " Demo of background scrolling: step 5" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
S = 1
d = 1
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a gradient background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 10
Line ((w / 10) * (i - 1), y)-((w / 10) * (i + 1), h), _RGBA32(1 + (i - 1) * (255 / 10), 127, 127, 255), BF
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling towards right of background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PutImage (x - w, y), Back, Scr ' it completes the background on the left side
_PrintString (1, 1), "Press Q to quit program, S to stop/start demo, R to reverse direction" ' it shows instruction for user on the screen
If k = "S" Then If S > 0 Then S = -1 Else S = 1 ' it starts/stops the scrolling to right
If k = "R" Then d = d * -1 ' it changes the direction of scrolling +1 = to right / -1 = to left
If S > 0 Then x = x + d 'it calculates the new position where starting to paint background, it is towards right
If d > 0 And x > w Then x = 1 ' it corrects x when is over w and scrolling is to right
If d < 0 And x < 1 Then x = w ' it corrects x when is under 1 and scrolling is to left
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
STEP 6
Code: (Select All)
Rem Demonstration of scrolling towards right or left of the background
Rem STEP 6 a background scrolling towards right /left & down/up in loop
' Global variables' declaration
Dim Back As Long, Scr As Long, x As Integer, y As Integer, w As Integer, h As Integer
Dim i As Integer, k As String, dx As Integer, dy As Integer, O As Integer, V As Integer, S As Integer
_Title " Demo of background scrolling: step 6" ' title on the titlebar of the application
' variables' initialization
w = 800: h = 600
y = 1
x = 1
Scr = _NewImage(w, h, 32)
Back = _NewImage(w, h, 32)
dx = 1
dy = 1
S = 1
O = 1
V = 1
Randomize Timer ' it starts the random generator of number
Screen Scr ' it creates window of the application
_Dest Back ' it sets the surface as destination of graphic output
' it draws a background by using a FOR loop with graphic instructions (LINE and _RGBA)
For i = 1 To 100
Line ((Rnd * (780) + 10), Rnd * (580) + 10)-(Rnd * (780) + 10, Rnd * (580) + 10), _RGBA32(Rnd * (255), Rnd * (255), Rnd * (255), 255), B
Next i
_Dest 0 ' it sets the surface of destination of graphic output as the window of the application
' it manages the keyboard input of user and the scrolling background
While k <> "Q"
k = UCase$(InKey$) ' it takes the input from keyboard
Cls 'it clears previous screen
_PutImage (x, y), Back, Scr 'it paints screen now
_PutImage (x - w, y), Back, Scr ' it completes the background on the left side
_PutImage (x, y - h), Back, Scr ' it completes the background on the upper side
_PutImage (x - w, y - h), Back, Scr ' it completes the background on the upper side
_PrintString (1, 1), "Press Q to quit program, S to stop/start demo, R to reverse directions" ' it shows instruction for user on the screen
_PrintString (1, 20), " O stop/start horizontal scrolling, V stop/start vertical scrolling"
' it starts/stops the scrolling
If k = "S" Then S = S * -1
If k = "R" Then
' it changes only scrolling activated
If O > 0 Then dx = dx * -1
If V > 0 Then dy = dy * -1
' it changes the direction of scrolling +1 = to right to bottom/ -1 = to left to up
End If
If k = "O" Then O = O * -1 'it sets the status of horizontal scrolling
If k = "V" Then V = V * -1 ' it sets the status of vertical scrolling
If S > 0 And O > 0 Then x = x + dx ' it scrolls only horizontal
If S > 0 And V > 0 Then y = y + dy ' it scrolls only vertical
If dx > 0 And x > w Then x = 1 ' it corrects x when it is over w and scrolling is to right
If dx < 0 And x < 1 Then x = w ' it corrects x when it is under 1 and scrolling is to left
If dy > 0 And y > h Then y = 1 ' it corrects y when it is over h and scrolling is to down
If dy < 0 And y < 1 Then y = h ' it corrects when it is under 1 and scrolling is to up
_Display ' it forces hardware to show image
_Limit 100 ' it reduces the number of cycles of CPU used by this application
Wend
End
Thanks for feedbacks
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - bplus - 12-01-2024
It'd be MUCH better if your backround was HUGE and didn't all fit in the viewing screen like they do in those games that travel the kingdom collecting magic potions, fighting monsters and rescuing damsels in distress or those space games that travel the universe collecting technology, fighting aliens and rescuing planets in jeopardy of alien invasion.
You know like the tutorial Fellippe did.
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - TempodiBasic - 12-01-2024
Hi Bplus
yes the tecnique to use a BIG scenario and a LITTLER camera that shows only a piece of the scenario on the screen is very smart and useful.
Just like the code written by Fellippe in that game online via TCP/IP. A clone of Among Us.
I remember C&C and AOE as famous prototypes. Very smart also the use of the fog to not discover all the map in a look.
Here a oltragious example
Code: (Select All)
Rem demonstration of scrolling of a camera (little view) on a scenario (Total view)
' global variables
Dim Scenario As Long, Camera As Long
Dim d As Integer, x As Integer, y As Integer, Sw As Integer, Sh As Integer, Cw As Integer, Ch As Integer
Dim k As String
' initialization
Sw = 1200
Sh = 800
Cw = 200
Ch = 200
x = 1
y = 1
k = ""
Randomize Timer
Scenario = _NewImage(Sw, Sh, 32)
Camera = _NewImage(Cw, Ch, 32)
Screen Camera
_FullScreen
_Title "A demonstration of scrolling of a camera on a scenario"
' creating the scenario
_Dest Scenario
For a = 1 To 100
Line (Rnd * Sw, Rnd * Sh)-(Rnd * Sw, Rnd * Sh), _RGBA32(Rnd * 255, Rnd * 255, Rnd * 255, 255), B
Next a
_Dest Scr
While k <> "Q"
k = UCase$(InKey$)
If k = "W" Then y = y - 1
If k = "S" Then y = y + 1
If k = "A" Then x = x - 1
If k = "D" Then x = x + 1
If x < 1 Then x = 1
If x > (Sw - Cw) Then x = Sw - Cw
If y < 1 Then y = 1
If y > (Sh - Ch) Then y = Sh - Ch
Cls
_PutImage (1, 1), Scenario, Camera, (x, y)-(Cw + x, Ch + y)
_PrintString (1, 1), "Use WASD to move camera"
_PrintString (1, 180), "Q to quit"
_Display
Wend
End
The tecnique showed in this tutorial is for background like dino run, moon racer, car racing game, or infinite pacman labyrinth or infinite frogger. Over this image of background it must be painted the hero, the obstacles, the enemies etc etc all objects variable for position, for movement and dimensions.
But that is another tutorial.
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - bplus - 12-01-2024
Background image BIGGER than the display screen:
Code: (Select All) _Title "Image bigger than screen" ' b+ 2024-12-30
Randomize Timer
Screen _NewImage(800, 600, 32)
_ScreenMove 100, 20
Dim bg&
bgw = 2400
bgh = 600
bg& = _NewImage(bgw, bgh, 32)
_Dest bg&
For i = 1 To 100
Line (Rnd * bgw, Rnd * bgh)-Step(Rnd * 100 + 50, Rnd * 75 + 25), _RGB32(Rnd * 255, Rnd * 255, Rnd * 255), BF
Next
_Dest 0
d = 1
While _KeyDown(27) = 0
Cls
k$ = UCase$(InKey$)
If k$ = "A" Then d = -1
If k$ = "D" Then d = 1
If d Then
If le + 800 > bgw Then
d = -d
te = bgw - le
_PutImage (0, 0)-(te, bgh), bg&, 0, (bgw - te, 0)-(bgw, bgh)
_PutImage (te, 0)-(800, bgh), bg&, 0, (0, 0)-(800 - te, bgh)
Beep
Else
_PutImage (0, 0)-(800, bgh), bg&, 0, (le, 0)-(le + 800, bgh)
End If
End If
le = le + d
If le < 0 Then le = 0: d = -d: Beep
_Display
_Limit 120
Wend
This is just left and right, A and D, maybe @TempodiBasic can add up and down?
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - SMcNeill - 12-01-2024
I've got a boatload of programs with the background image being bigger than the visible screen, and which scroll up/down. It's a common technique which I use for quickly and easily making a scrollable screen with more information than what you can normally find on a single page.
Make a background image 32000 pixels tall, print/draw all you want on it, and then display your _Height from StartY to StartY + _Height to put the portion of the image on the screen that you want.
Bonus points involved if you include a slider on the side which you can use for navigation as well.
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - TempodiBasic - 12-02-2024
(12-01-2024, 10:53 PM)bplus Wrote: Background image BIGGER than the display screen:
Code: (Select All) _Title "Image bigger than screen" ' b+ 2024-12-30
Randomize Timer
Screen _NewImage(800, 600, 32)
_ScreenMove 100, 20
Dim bg&
bgw = 2400
bgh = 600
bg& = _NewImage(bgw, bgh, 32)
_Dest bg&
For i = 1 To 100
Line (Rnd * bgw, Rnd * bgh)-Step(Rnd * 100 + 50, Rnd * 75 + 25), _RGB32(Rnd * 255, Rnd * 255, Rnd * 255), BF
Next
_Dest 0
d = 1
While _KeyDown(27) = 0
Cls
k$ = UCase$(InKey$)
If k$ = "A" Then d = -1
If k$ = "D" Then d = 1
If d Then
If le + 800 > bgw Then
d = -d
te = bgw - le
_PutImage (0, 0)-(te, bgh), bg&, 0, (bgw - te, 0)-(bgw, bgh)
_PutImage (te, 0)-(800, bgh), bg&, 0, (0, 0)-(800 - te, bgh)
Beep
Else
_PutImage (0, 0)-(800, bgh), bg&, 0, (le, 0)-(le + 800, bgh)
End If
End If
le = le + d
If le < 0 Then le = 0: d = -d: Beep
_Display
_Limit 120
Wend
This is just left and right, A and D, maybe @TempodiBasic can add up and down? @Bplus
Hi man
so you're using filled rectangles into your BIGGER image!
And what? Do you like put the scrolling working in automation, it does not matter about user input!
These concepts are over mine knowledge so I cannot UP/DOWN scrolling.
Buuut if you add this you can add to this scrolling the stop & go feature like the ecological car.
Code: (Select All)
S = 1
while _keydown(27) = 0
...
...
IF k$ = "S" THEN S = S * -1
...
....
...
IF S>0 THEN le = le +d
...
...
wend
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - Petr - 12-02-2024
I use a different solution. If the screen resolution is, for example, 800x600 and the displayed object has coordinates 9000, 320, then it does not meet the conditions and is not displayed. Anything that is in the range 0 - 800 and 0 - 600 meets them and is therefore visible.
Here is an example. Let's say you need to display a higher resolution image on an 800x600 screen. Just use what _PutImage can do by itself and take advantage of the fact that things outside the visible area are not rendered.
In the program, use the left, right, up, down arrows and the mouse wheel to zoom.
Also try inserting any other image instead of _ScreenImage. It will work the same.
Code: (Select All)
Dim As Long MX, MY, LB, RB, Imag
Imag = _ScreenImage
Screen _NewImage(800, 600, 32)
Z = 799
RatioY = _Height / _Width
Do Until k& = 27
k& = _KeyHit
GetMouse MX, MY, LB, RB, MWH
Select Case k&
Case 18432: Y = Y - 10
Case 20480: Y = Y + 10
Case 19200: X = X - 10
Case 19712: X = X + 10
End Select
Z = Z + MWH
Cls
_PutImage (0, 0)-(799, 599), Imag, 0, (X, Y)-(X + Z, Y + Z * RatioY)
_Display
_Limit 30
Loop
Sub GetMouse (MX As Long, MY As Long, LB As Long, RB As Long, MWH As Single)
omwh = MWH
Do While _MouseInput
MWH = MWH + 10 * _MouseWheel
Loop
If omwh - MWH = 0 Then MWH = 0
MX = _MouseX
MY = _MouseY
LB = _MouseButton(1)
RB = _MouseButton(2)
End Sub
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - SMcNeill - 12-02-2024
Just a copy/paste from elsewhere, as this topic has fragmented into a couple of different spots now. It's still the same stuff; just directed to different people at this point.
Since you guys are all having fun with this, here's a Steve Simple version of screen scrolling at work. Just use the mouse and watch what happens!
Code: (Select All)
Screen _NewImage(800, 600, 32) 'the screen that the user is going to see
Background = _NewImage(800 * 3, 600 * 3, 32) 'my background which is 3 times larger than my screen
_Dest Background
_PrintMode _KeepBackground
'Create a background which we can scroll and view via the mouse
For x = 0 To 2
For y = 0 To 2
Line (x * 800, y * 600)-Step(800, 600), _RGB(x * 127.5, y * 127.5, Rnd * 256), BF 'draw each quadrent a different color
For i = 0 To 37
text$ = "QUADRENT" + Str$(y * 3 + x + 1) + ": LINE #" + Str$(i)
_PrintString (x * 800, y * 600 + i * _FontHeight), text$
Next
Next
Next
StartX = 800: StartY = 600
_MouseMove 400, 300 'center the mouse before we start scrolling like crazy
_Delay .2 'give the mouse time to move to the proper position for starting
Do
While _MouseInput: Wend
'If the mouse is near an edge, then change the starting points to reflect the scrolling
If _MouseX < 20 Then StartX = StartX - (20 - _MouseX): If StartX < 0 Then StartX = 0
If _MouseX > 779 Then StartX = StartX + (_MouseX - 779): If StartX > 1600 Then StartX = 1600
If _MouseY < 20 Then StartY = StartY - (20 - _MouseY): If StartY < 0 Then StartY = 0
If _MouseY > 579 Then StartY = StartY + (_MouseY - 579): If StartY > 1200 Then StartY = 1200
_PutImage (0, 0)-Step(800, 600), Background, 0, (StartX, StartY)-Step(800, 600)
_Display
_Limit 60
Loop Until _MouseButton(1) Or _MouseButton(2) 'press a mouse button to end
System
Note: The closer your mouse gets to the edge, the faster the scrolling. Advance your pointer slowly towards the edge and watch as the scroll speed increases and decreases as you approach the edge more.
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - Pete - 12-02-2024
Now let's see a SCREEN 0 example!!!
Actually that's the one thing I haven't yet coded in SCREEN 0, a lateral scroll routine. The IDE uses one, which I presume is written in SCREEN 0.
Hmm, with arrays it's pretty easy...
Use arrow left and right keys to laterally scroll the text on the screen.
Code: (Select All)
ReDim l$(20)
For h = 1 To 20
For i = 1 To Rnd * 50 + 60
j = Int(Rnd * 10 + 48)
l$ = l$ + Chr$(j)
If Int(Rnd * 7) = 1 And Right$(l$, 1) <> " " Then l$ = l$ + " "
Next
l$(h) = l$: l$ = ""
Next
k = 1
Do
For i = 1 To 20
Locate i, 1:
a$ = Space$(_Width)
Mid$(a$, 1) = Mid$(l$(i), k, _Width)
Print a$;
Next
Do
_Limit 60
b$ = InKey$
Select Case b$
Case Chr$(27): End
Case Chr$(0) + "K"
If k > 0 Then k = k - 1: Exit Do
Case Chr$(0) + "M"
k = k + 1: Exit Do
End Select
Loop
Loop
Now if the text is a single variable with line breaks, that would require a slightly different approach.
Code: (Select All)
Width 80, 25
_ScreenMove _Middle
For h = 1 To 20
For i = 1 To Rnd * 50 + 60
j = Int(Rnd * 10 + 48)
a$ = a$ + Chr$(j)
If Int(Rnd * 7) = 1 And Right$(a$, 1) <> " " Then a$ = a$ + " "
Next
a$ = a$ + Chr$(13) + Chr$(10)
Next
Rem a$ = _Clipboard$
Do
seed = 1
For i = 1 To 20
Locate i, 1
a1$ = Mid$(a$, seed, _Width + k)
If k < InStr(a1$ + Chr$(13), Chr$(13)) Then a1$ = Mid$(a1$, k + 1) Else a1$ = ""
If InStr(a1$, Chr$(13)) Then a1$ = Mid$(a1$, 1, InStr(a1$, Chr$(13)) - 1)
x$ = Space$(_Width)
Mid$(x$, 1) = a1$
Print x$;
seed = InStr(seed, a$, Chr$(13)) + 2
Next
Do
_Limit 60
b$ = InKey$
Select Case b$
Case Chr$(27): End
Case Chr$(0) + "K"
If k > 0 Then k = k - 1: Exit Do
Case Chr$(0) + "M"
k = k + 1: Exit Do
End Select
Loop
Loop
Anyway, not goof proofed so if you remove the remark at the _clipboard option just be sure you copy enough text to cover 20 or more lines. This is just what I would start out with a s way of displaying and horizontally scrolling text in SCREEN 0.
Pete
RE: a little tutorial STEP by STEP on scrolling in 2D graphic mode - TempodiBasic - 12-04-2024
(12-02-2024, 07:27 PM)Pete Wrote: Now let's see a SCREEN 0 example!!!
Actually that's the one thing I haven't yet coded in SCREEN 0, a lateral scroll routine. The IDE uses one, which I presume is written in SCREEN 0.
Hmm, with arrays it's pretty easy...
Use arrow left and right keys to laterally scroll the text on the screen.
Code: (Select All)
ReDim l$(20)
For h = 1 To 20
For i = 1 To Rnd * 50 + 60
j = Int(Rnd * 10 + 48)
l$ = l$ + Chr$(j)
If Int(Rnd * 7) = 1 And Right$(l$, 1) <> " " Then l$ = l$ + " "
Next
l$(h) = l$: l$ = ""
Next
k = 1
Do
For i = 1 To 20
Locate i, 1:
a$ = Space$(_Width)
Mid$(a$, 1) = Mid$(l$(i), k, _Width)
Print a$;
Next
Do
_Limit 60
b$ = InKey$
Select Case b$
Case Chr$(27): End
Case Chr$(0) + "K"
If k > 0 Then k = k - 1: Exit Do
Case Chr$(0) + "M"
k = k + 1: Exit Do
End Select
Loop
Loop
Now if the text is a single variable with line breaks, that would require a slightly different approach.
Code: (Select All)
Width 80, 25
_ScreenMove _Middle
For h = 1 To 20
For i = 1 To Rnd * 50 + 60
j = Int(Rnd * 10 + 48)
a$ = a$ + Chr$(j)
If Int(Rnd * 7) = 1 And Right$(a$, 1) <> " " Then a$ = a$ + " "
Next
a$ = a$ + Chr$(13) + Chr$(10)
Next
Rem a$ = _Clipboard$
Do
seed = 1
For i = 1 To 20
Locate i, 1
a1$ = Mid$(a$, seed, _Width + k)
If k < InStr(a1$ + Chr$(13), Chr$(13)) Then a1$ = Mid$(a1$, k + 1) Else a1$ = ""
If InStr(a1$, Chr$(13)) Then a1$ = Mid$(a1$, 1, InStr(a1$, Chr$(13)) - 1)
x$ = Space$(_Width)
Mid$(x$, 1) = a1$
Print x$;
seed = InStr(seed, a$, Chr$(13)) + 2
Next
Do
_Limit 60
b$ = InKey$
Select Case b$
Case Chr$(27): End
Case Chr$(0) + "K"
If k > 0 Then k = k - 1: Exit Do
Case Chr$(0) + "M"
k = k + 1: Exit Do
End Select
Loop
Loop
Anyway, not goof proofed so if you remove the remark at the _clipboard option just be sure you copy enough text to cover 20 or more lines. This is just what I would start out with a s way of displaying and horizontally scrolling text in SCREEN 0.
Pete Hi Pete
your scrolling in SCREEN 0 is very interesting. As I have seen it I remembered when I saw for the first time the Text-windows in Turbo Pascal 6.0 in which you can scroll to right and to bottom beyond the text in the window.
So you bring me to imagine a demo
here it is
Code: (Select All)
Width 80, 25 ' screen 0 only text 80 columns for 25 rows
_ControlChr Off
_Title "Scrolling text in SCREEN 0 with arrow keys"
Dim Row As String, Text(1 To 40) As String, cText(1 To 255, 1 To 40) As Integer, a As Integer, b As Integer, R As Integer, C As Integer
' initialization
' it builds a string of 255 characters
For a = 1 To 255
Row = Row + Chr$(a)
Next a
' it builds 40 rows of 255 characters for row
For a = 1 To 40
Text(a) = Right$(Row, a + 10) + Left$(Row, 255 - a - 10)
For b = 1 To 255
cText(b, a) = (b * a) Mod 7
Next b
Next a
Row = ""
R = 1
C = 1
While Row <> "Q"
Row = UCase$(InKey$)
If Row = Chr$(0) + "H" Then R = R - 1
If Row = Chr$(0) + "P" Then R = R + 1
If Row = Chr$(0) + "K" Then C = C - 1
If Row = Chr$(0) + "M" Then C = C + 1
Cls , 0
For a = 0 To 24
Locate a + 1, 1
If (a + R) < 41 And (a + R) > 0 Then
For b = 1 To 80
If (C + b - 1 < 256) And (C + b - 1 > 0) Then
Color , cText(C + b - 1, a + R)
Print Mid$(Text(a + R), C + b - 1, 1);
Else
Color , 0
Print " ";
End If
Next b
End If
Next
_Display
_Limit 100
Wend
End
I hope you don't mind
|