Newbie: Scrolling background
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
Newbie: Scrolling background
Hi,
I've searched this forum for half an hour now but I can't find what I need. Maybe it's to simple to mention...
How can I code a scrolling backgound for a space game? Say, I have a tilable bitmap with a starry backgound. Now my spacecraft should remain at center screen an when I move the backgound should scroll in the opposite direction (horizontaly, vertically and diagonaly). So how can I make a bitmap virtually "endless"? For the beginning let's imagine a simple sidescroller...
Do you have any code examples?
I've searched this forum for half an hour now but I can't find what I need. Maybe it's to simple to mention...
How can I code a scrolling backgound for a space game? Say, I have a tilable bitmap with a starry backgound. Now my spacecraft should remain at center screen an when I move the backgound should scroll in the opposite direction (horizontaly, vertically and diagonaly). So how can I make a bitmap virtually "endless"? For the beginning let's imagine a simple sidescroller...
Do you have any code examples?
- Psychophanta
- Always Here

- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
one easy way is to use the same sprites over and over.
e.g. on a 800x600 screen you can use 400x300 sprites, then you'll need at least 3x3 of them.
but a far easier way for a starfield is to use an array that hold the stars and to "swap" it at the end of the screen.
for the stars themselves you can use sprites or plot, just as you like.
...got to install PB first to code a small example, this is a new machine here, didn't install it yet...
e.g. on a 800x600 screen you can use 400x300 sprites, then you'll need at least 3x3 of them.
but a far easier way for a starfield is to use an array that hold the stars and to "swap" it at the end of the screen.
for the stars themselves you can use sprites or plot, just as you like.
...got to install PB first to code a small example, this is a new machine here, didn't install it yet...
oh... and have a nice day.
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
Even if I have 3x3 sprites of this kind, how do I wrap them around when the player flies beyond the end of that 3x3 array?one easy way is to use the same sprites over and over.
e.g. on a 800x600 screen you can use 400x300 sprites, then you'll need at least 3x3 of them.
looking forward couriously...but a far easier way for a starfield is to use an array that hold the stars and to "swap" it at the end of the screen.
for the stars themselves you can use sprites or plot, just as you like.
...got to install PB first to code a small example, this is a new machine here, didn't install it yet...
- Fluid Byte
- Addict

- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
You mean something like this?
[edit]included movement[/edit]
Code: Select all
InitSprite() : InitKeyboard()
OpenWindow(0,0,0,640,480,"Seamless BG",#WS_OVERLAPPEDWINDOW | 1)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0,160,160)
hdc = StartDrawing(SpriteOutput(0))
For i=0 To 159
CVL = ((i * 180) / 159) + 35
Box(0,i,160,1,RGB(255-CVL,CVL,255-CVL))
Next
SetRect_(qrc.RECT,0,0,160,160)
DrawEdge_(hdc,qrc,#EDGE_RAISED,#BF_RECT)
StopDrawing()
SetFrameRate(60)
Repeat
EventID = WindowEvent()
ClearScreen(0)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Right)
SPDX.f - 2 : If SPDX = -160 : SPDX = 0 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Left)
SPDX.f + 2 : If SPDX = 160 : SPDX = 0 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Up)
SPDY.f - 2 : If SPDY = -160 : SPDY = 0 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Down)
SPDY.f + 2 : If SPDY = 160 : SPDY = 0 : EndIf
EndIf
For X = -1 To 4
For Y = -1 To 3
DisplaySprite(0,X * 160 - SPDX,Y * 160 + SPDY)
Next
Next
FlipBuffers()
Until EventID = #WM_CLOSE Or KeyboardPushed(1)Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
@fluid
quite ok, simple example for wrapping of identical sprites.
wrapping of different ones is a more complicated....
but the API isn't really necessary.
a outlined box would have done it.
not a good idea to bluff a beginner with such api calls.
PS: and also better use the PB-Constants....
quite ok, simple example for wrapping of identical sprites.
wrapping of different ones is a more complicated....
but the API isn't really necessary.
a outlined box would have done it.
not a good idea to bluff a beginner with such api calls.
PS: and also better use the PB-Constants....
oh... and have a nice day.
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
the content of the sprite is absolutely meaningless...
and btw, the API there was only used to put the edges on the sprites.
I used the skeleton of Fluid's code to build one with a single star-sprite
half the size of the screen, displayed 3x3 times.
I think it's not SO ungly that it is repetetive.
> is NOT tiled on the visual part of the screen
that is really a BAD idea, because sprites should never be bigger than the screen.
if you need it to be less repetetive, use a lot of different sprites,
but always smaller than the screen and tiled.
after all, if you just want Dots (or very small stars) in the background, the plot-array sure is a better idea...
I will show up with it soon...
and btw, the API there was only used to put the edges on the sprites.
I used the skeleton of Fluid's code to build one with a single star-sprite
half the size of the screen, displayed 3x3 times.
I think it's not SO ungly that it is repetetive.
Code: Select all
InitSprite() : InitKeyboard()
OpenWindow(0,0,0,640,480,"Scrolling Stars")
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0,320,240) ;creating a 320x240 sprite
StartDrawing(SpriteOutput(0))
For n=0 To 499 ; filling it with 500 dots at
col = Random(255) ; randomly gray-value
Plot( Random(319), Random(239), RGB(col,col,col))
Next
StopDrawing()
SetFrameRate(60)
Repeat
EventID = WindowEvent()
ClearScreen($000000)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Right)
BGX +2 : If BGX > 319 : BGX = 0 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Left)
BGX -2 : If BGX < 0 : BGX = 319 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Up)
BGY -2 : If BGY < 0 : BGY = 239 : EndIf
EndIf
If KeyboardPushed(#PB_Key_Down)
BGY +2 : If BGY > 239 : BGY = 0 : EndIf
EndIf
For n=0 To 2
For t=0 To 2
DisplaySprite(0, 320 * n - BGX, 240 * t - BGY)
Next
Next
FlipBuffers()
Until EventID = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)that is really a BAD idea, because sprites should never be bigger than the screen.
if you need it to be less repetetive, use a lot of different sprites,
but always smaller than the screen and tiled.
after all, if you just want Dots (or very small stars) in the background, the plot-array sure is a better idea...
I will show up with it soon...
oh... and have a nice day.
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
uffa... ok.. I made a sketch

the red area is the visible screen.
the blue matrix is the 3x3 "map" of the sprites.
when starting, the upper left four sprites fill the screen, the thirs row / third line is outside, invisible. (pic.1)
moving your "starship" right/down moves the background-stars up/left.
the variables BGX and BGY are increased according to the keys pushed.
the coordinates of the sprites are decreased by BGX/BGY, to shift the sprites up/left. (pic.2)
in the end, the sprites are that far up/left, that the first row / first line is almost invisible, only one pixel will be left. (pic.3)
if you go one more step then, the variables will be reset to 0,
and the movement starts over at pic.1
of course, the swapping effect appears everytime any axis needs it,
only for the demonstration here I explaned both axis at the same time.
the two loops do the following:
first loop For n = 0 To 2 counts the rows, inner loop counts the lines.
the first row starts at x=0, and the sprite is 320pix wide, so the second row starts at x=320.
the the thirs starts at x=640, so we can achieve those values by n * 320
same for the Y-Values for the lines: t * 240
at the end we subtract BGX from the X and BGY from the Y coordinate,
to achieve the soft scrolling as explained with the sketch above.

the red area is the visible screen.
the blue matrix is the 3x3 "map" of the sprites.
when starting, the upper left four sprites fill the screen, the thirs row / third line is outside, invisible. (pic.1)
moving your "starship" right/down moves the background-stars up/left.
the variables BGX and BGY are increased according to the keys pushed.
the coordinates of the sprites are decreased by BGX/BGY, to shift the sprites up/left. (pic.2)
in the end, the sprites are that far up/left, that the first row / first line is almost invisible, only one pixel will be left. (pic.3)
if you go one more step then, the variables will be reset to 0,
and the movement starts over at pic.1
of course, the swapping effect appears everytime any axis needs it,
only for the demonstration here I explaned both axis at the same time.
the two loops do the following:
first loop For n = 0 To 2 counts the rows, inner loop counts the lines.
the first row starts at x=0, and the sprite is 320pix wide, so the second row starts at x=320.
the the thirs starts at x=640, so we can achieve those values by n * 320
same for the Y-Values for the lines: t * 240
at the end we subtract BGX from the X and BGY from the Y coordinate,
to achieve the soft scrolling as explained with the sketch above.
Last edited by Kaeru Gaman on Tue Jul 17, 2007 4:08 pm, edited 1 time in total.
oh... and have a nice day.
- Fluid Byte
- Addict

- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Dude, you should write some offical tutorials. You're good at it! 

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
-
CptGreenwood
- User

- Posts: 11
- Joined: Mon Jul 16, 2007 7:50 am
- Location: Germany
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany