Pseudo 3d road beginnings

Rob Hewitt

Member
Joined
Aug 30, 2017
I'm still learning the language so while I've been researching I found Lou's Pseudo 3d Page. Lots of good info there and I also found an example in a BASIC dialect (Can't remember which one!). So I converted it! I then added some gradients and a fade in for the road. *EDIT And cursor key movement and acceleration. It's not the best conversion as all of the code is called in OnRender() but it works!

Code:
' *****************************************************
' **** Converted to Cerberus-X by Coagulus in 2017 ****
' *****************************************************

'Road Demonstration Program by Louis Gorenfeld 2010

'This program is intended to show concepts described at Lou's Pseudo 3d Page
'http://www.gorenfeld.net/lou/pseudo
' * ABOVE ADDRESS IS NOW http://www.extentofthejam.com/pseudo/  *

'It defaults to generating 80 frames during which the road curves right,
'uncurves, and repeats.

'I've left much detail off of the road.  You can draw them in around line 70! (*Now in drawroad()*)
'Happy coding! :)

Strict

Import mojo

Global RoadLines:Int = 99
Global ScrollSpeed:Float = 0
Global RoadY:Int = -1        'arbitrary
Global ResX:Int = 320
Global ResY:Int = 240
Global PlrLine:Int = 8       'What line is the player sprite on?
Global ScreenLine:Int

Global GrassColor:Int
Global RoadColor:Int

Global TexOffset:Float = 100
Global SegY:Float = RoadLines
Global DX:Float = 0
Global DDX:Float = .02    ' This controls the steepness of the curve
Global X:Float 'AS SINGLE
Global HalfWidth:Float 'AS SINGLE
Global NextStretch:String = "Straight"

Global _scaleRatio:Float
Global _maxX:Int
Global xoff:Int

Global WidthStep:Int = 1

Global ZMap:Float[100]
Global linex:Float[100]

Class road Extends App

    Method OnCreate:Int()
        Local resX:Float = Float( DeviceWidth() )
        Local resY:Float = Float( DeviceHeight() )
        Local loop:Int
          _scaleRatio = resY / 240
        _maxX = resX / _scaleRatio
        xoff = ((resX - (320 * _scaleRatio)) / 2) /_scaleRatio ' so that the game screen remains centred
        SetUpdateRate(0)
        TexOffset = 100
        SegY = RoadLines
        DX = 0
        DDX = .03    ' This controls the steepness of the curve
        initzmap()
        Return 0
    End Method
      
    Method OnUpdate:Int()
  
        Return 0
    End Method
      
    Method OnRender:Int()
        Cls 0,0,0
        PushMatrix()
        Scale(_scaleRatio,_scaleRatio)
        drawroad()
        PopMatrix()
        Return 0
    End Method
      
    Method initzmap:Void()
        For Local A:Int = 1 To RoadLines
            ZMap[A] = RoadY / (A - (ResY / 2.0))
        Next
  
        ' Normalize ZMap so the line with the player on it is scale=1 (or would be
        ' If we had a player sprite :))
        Local b:Float = 1.0 / ZMap[PlrLine]
        b = b * 100   'in percents because QBasic's MOD is lame
        For Local A:Int = 1 To RoadLines
           ZMap[A] = ZMap[A] * b
        Next
    End Method
      
    Method drawroad:Void()
        SetColor 0,255,255
      
        For Local A:Int = 1 To ResY - RoadLines
            Local CL:Int = A * 2
            If CL>255 Then CL=255
            SetColor 0,255-CL,255-CL
            DrawLine  0,A,ResX-1,A
        Next
      
        X = ResX / 2
        DX = 0
        HalfWidth = 120
        ScreenLine = ResY - 1
  
        For Local A:Int = 1 To RoadLines
            If (ZMap[A] + TexOffset) Mod 100 > 50
                GrassColor = 10
                RoadColor = 7
            Else
                GrassColor = 2
                RoadColor = 8
            End If
                  
            SetAlpha (Float(RoadLines-A)/Float(RoadLines))*4
            'draw the road with stripe
            If RoadColor=7 Then SetColor 128,128,128 Else SetColor 64,64,64
            DrawLine linex[A]+(X-HalfWidth),ScreenLine,linex[A]+X+HalfWidth,ScreenLine
            'draw the grass with stripe
            If GrassColor=10 Then SetColor 0,255,0 Else SetColor 0,128,0
            DrawLine 0, ScreenLine,    linex[A]+(X - HalfWidth), ScreenLine
            DrawLine linex[A]+(X + HalfWidth), ScreenLine, ResX - 1, ScreenLine
          
            HalfWidth = HalfWidth - WidthStep
            ScreenLine = ScreenLine - 1
          
            If NextStretch = "Straight" Then
                    If A > SegY Then
                           DX = DX + (DDX)
                    End If
            Elseif  NextStretch = "Curved" Then
                If A < SegY Then
                    DX = DX + (DDX)
                End If
            End If
            X = X + DX
        Next A
      
        SetAlpha 1.0
      
        If KeyDown(KEY_LEFT)
            For Local A:Int = 1 To RoadLines
                linex[A]=linex[A]+((RoadLines-A)/30.0)
            Next
        End If
        If KeyDown(KEY_RIGHT)
            For Local A:Int = 1 To RoadLines
                linex[A]=linex[A]-((RoadLines-A)/30.0)
            Next
        End If
      
        If KeyDown(KEY_UP) Then ScrollSpeed=ScrollSpeed+0.01
        If KeyDown(KEY_DOWN) Then ScrollSpeed=ScrollSpeed-0.01
      
        If ScrollSpeed>10.0 Then ScrollSpeed = 10.0
        If ScrollSpeed<0.0 Then ScrollSpeed = 0.0
      
        TexOffset = TexOffset + ScrollSpeed
        While TexOffset >= 100
            TexOffset = TexOffset - 100
        Wend
        SegY = SegY - ScrollSpeed  ' Decrease SegY by an arbitrary amount.  Adjust to taste.
        While SegY < 0
            SegY = SegY + RoadLines
            If NextStretch = "Curved" Then
                NextStretch = "Straight"
            Elseif NextStretch = "Straight" Then
                NextStretch = "Curved"
            End If
        Wend  
      
    End Method
  
End Class

Function Main:Int()
    New road()
    Return 0
End Function
 
Top Bottom