Side Scrolling

New to FreeBASIC? Post your questions here.
Post Reply
Powerworks
Posts: 25
Joined: Oct 30, 2005 22:30
Location: Alabama
Contact:

Side Scrolling

Post by Powerworks »

Anyone have a sample code or tutorial for side scrolling, or scrolling in general for FreeBasic?

There are a lot for QB but they use DEF SEG nad POKE.

Thanks!
SSC
Posts: 319
Joined: May 29, 2005 4:47
Location: Around
Contact:

Post by SSC »

Download rel's pixel scroller, in relib. It was made for QB but can be used with freebasic gfx lib with very little changes. It's in the examples > rpg folder rpg.bas. I still use it for my FB scrolling needs =D

http://rel.betterwebber.com/junk.php?id=22
Ryan
Posts: 695
Joined: Jun 10, 2005 2:13
Location: Louisville, KY
Contact:

Post by Ryan »

Lachie's already ported that in the past. ;-)

http://www.freebasic.net/forum/viewtopi ... light=rels

Check this thread out, too:

http://www.freebasic.net/forum/viewtopic.php?t=691
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

If you use oldscool graphics, you can do a very simple scroll like this...

I commented the code snippet that does the scrolling. All you hardcore coders - Does this method have any drawbacks a n00b like me don't know about?

Code: Select all

''      Real-time gravity simulator

Option Explicit 

Randomize Timer

Const Pi = 3.1415926535897932, _ 
Gravity = 6.673e-11 , _ 
ResX = 1024, _ 
ResY = 768

dim as integer _
i, i2, R, G, B, Xmus, Ymus, _
P1, P2 = 1, _
MidX = 256, _ 
MidY = 192

Dim As single _ 
Distance, _ 
MinDist, _
Gforce

Type PlanetType 
    X As Single 
    Y As Single 
    XVec As Single 
    YVec As Single 
    Velocity As Single 
    Mass As Single 
    Radius As Single 
    Orbit_Radius As Single
    Angle as single 
    R As Single 
    G As Single 
    B As Single 
End Type 

Dim Body(200) As PlanetType 

'' Sun
With Body(0)
    .X = MidX 
    .Y = MidY 
    .Mass = 6e13
    .Radius = 13
End With

'' Comet
With Body(1)
    .Orbit_Radius = 600 
    .Angle = Rnd*(Pi*2)
    .X = MidX-.Orbit_Radius*Sin(.Angle) 
    .Y = MidY-.Orbit_Radius*Cos(.Angle) 
    .Mass = 1e10
    .Radius = 2.5
    .XVec = 0.4*sqr(Gravity*(Body(0).Mass)/(.Orbit_Radius))*Cos(.Angle)
    .YVec = 0.4*sqr(Gravity*(Body(0).Mass)/(.Orbit_Radius))*Sin(-.Angle) 
    .R = 255
    .G = 255
    .B = 255
End With 

''  Planet
With Body(2)
    .Orbit_Radius = 250 
    .Angle = Rnd*(Pi*2)
    .X = MidX-.Orbit_Radius*Sin(.Angle) 
    .Y = MidY-.Orbit_Radius*Cos(.Angle) 
    .Mass = 5e10
    .Radius = 5
    .XVec = 0.85*sqr(Gravity*(Body(0).Mass)/(.Orbit_Radius))*Cos(.Angle)
    .YVec = 0.85*sqr(Gravity*(Body(0).Mass)/(.Orbit_Radius))*Sin(-.Angle) 
    .R = 32 
    .G = 196
    .B = 255
End With 

'' Asteroid belt
For i = 3 To Ubound(Body) 
    With Body(i) 
        .Angle = Rnd*(Pi*2)
        .Orbit_Radius = 350+Rnd*100
        .Mass = 1e8+rnd*10e8
        .X = MidX-.Orbit_Radius*Sin(.Angle) 
        .Y = MidY-.Orbit_Radius*Cos(.Angle) 
        .XVec = sqr((Gravity*Body(0).Mass)/.Orbit_Radius)*Cos(.Angle)
        .YVec = sqr((Gravity*Body(0).Mass)/.Orbit_Radius)*Sin(-.Angle)
        If Int(Rnd*20) = 0 Then 
            .XVec =- .XVec 
            .YVec =- .YVec 
        End If
        .Radius = .mass/5e8
        .R = 64+Rnd*191
        .G = .R-48+Rnd*48
        .B = .G-48+Rnd*48
    End With 
Next 

screen 20, 16, 2, 1

Do 
    
    SCREENSET P2, P1 
    SWAP P2, P1 
    CLS 
    
    Getmouse Xmus, Ymus
    
    ''  Mouse scroll next four lines! 
    ''  Just copy in the MidX and MidY var in any piece of code 
    ''  that decides graphics X-Y placement on screen. 
    ''  See last 10-12 lineshow I did it. Veeery simple...
    if Xmus < 30 then MidX = MidX + abs((1/(Xmus+1))*15)
    if Xmus > 993 then MidX = MidX - abs((1/(1023-Xmus+1))*15)
    if Ymus < 30 then MidY = MidY + abs((1/(Ymus+1))*15)
    if Ymus > 737 then MidY = MidY - abs((1/(767-Ymus+1))*15)

    For i = 0 To Ubound(Body) 
        For i2 = 0 To Ubound(Body) 
            If i2 <> i Then 
            Distance = sqr((Body(i).Y-Body(i2).Y)^2+(Body(i).X-Body(i2).X)^2)
            MinDist = Body(i).Radius+Body(i2).Radius
                If Distance < MinDist then Distance = MinDist
            Gforce = Gravity*((Body(i).Mass*Body(i2).Mass)/Distance^2)
            Body(i).Xvec -= ((Body(i).X-Body(i2).X)/Distance)*(Gforce/Body(i).Mass)
            Body(i).Yvec -= ((Body(i).Y-Body(i2).Y)/Distance)*(Gforce/Body(i).Mass)
            End If
        Next
    Next
    
    'Body(0).X += Body(0).XVec  ''uncomment to make the sun react on gravitiational influence
    'Body(0).Y += Body(0).YVec
    Circle (Body(0).X+MidX, Body(0).Y+MidY), 13, &hff6600,,,,f
    Circle (Body(0).X+MidX, Body(0).Y+MidY), 12, &hffee77,,,,f
    Circle (Body(0).X+MidX, Body(0).Y+MidY), 10, &hffffff,,,,f
    
    For i = 1 To Ubound(Body) 
        Body(i).X += Body(i).XVec
        Body(i).Y += Body(i).YVec
        R = Body(i).R 
        G = Body(i).G 
        B = Body(i).B 
        Circle(Body(i).X+MidX, Body(i).Y+MidY), Body(i).Radius, Rgb(R, G, B),,,,F 
    Next
    
Loop Until Inkey$=CHR$(27)

end
Best regards,
h4tt3n
Post Reply