JoeStrout

Raster Road for Mini Micro

Feb 13th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. rh = 2 // raster height, in real-screen pixels
  2. // create a couple of 1-pixel-high road images,
  3. // with and without lane stripes
  4. w = 600 // full width of road sprite
  5. gfx.line 0, 0, w, 0, "#666666"
  6. gfx.line 0, 0, 20, 0, color.white
  7. gfx.line w-20, 0, w, 0, color.white
  8. roadImg = []
  9. roadImg.push gfx.getImage(0, 0, w, 1)
  10. gfx.line w*0.33-10, 0, w*0.33+10, 0, "#EEEEEE"
  11. gfx.line w*0.66-10, 0, w*0.66+10, 0, "#EEEEEE"
  12. roadImg.push gfx.getImage(0, 0, w, 1)
  13.  
  14. // And same for grass. But for this one, we'll just use
  15. // a 4x4 pixel image and stretch it.
  16. gfx.fillRect 0, 0, 4, rh, color.green
  17. grassImg = gfx.getImage(0, 0, 4, rh)
  18.  
  19. drawBackground = function()
  20. img = http.get("https://i.pinimg.com/originals/e8/70/1c/e8701c982aaf5a6f9a93aa17a2cffd90.jpg")
  21. globals.bgWidth = img.width/3
  22. gfx.clear color.blue, img.width, 640
  23. gfx.drawImage img, 0, 320-120
  24. end function
  25. clear
  26. drawBackground
  27.  
  28. // now create a sprite for each row of the screen
  29. sprDisp = display(4)
  30. sprDisp.clear
  31. grass = []
  32. road = []
  33. rasterZ = [] // distance (0-1) of each raster line from the camera
  34. rasterScale = [] // scale of sprites at each raster line
  35. rasterFog = [] // amount of fog to apply at each raster line
  36. setup = function()
  37. maxi = 320/rh
  38. for i in range(0, maxi)
  39. camHeight = 10
  40. z = camHeight / (320/rh+5 - i)
  41. rasterZ.push z
  42. rasterScale.push (1/z) / (1/rasterZ[0])
  43.  
  44. if i > maxi/2 then
  45. rasterFog.push (i-maxi/2) / (maxi/2)
  46. else
  47. rasterFog.push 0
  48. end if
  49.  
  50. // grass
  51. sp = new Sprite
  52. sp.image = grassImg
  53. sp.scale = [1024 / grassImg.width, 1]
  54. sp.x = 480
  55. sp.y = i*rh
  56. grass.push sp
  57. sprDisp.sprites.push sp
  58.  
  59. // road
  60. sp = new Sprite
  61. sp.image = roadImg[0]
  62. sp.scale = rh
  63. sp.x = 480
  64. sp.y = i*rh
  65. sp.scale = [1.5 * rasterScale[-1], rh]
  66. sprDisp.sprites.push sp
  67. road.push sp
  68.  
  69. end for
  70. end function
  71. setup
  72.  
  73. // Update the road and grass sprites along our raster lines.
  74. tintColors = [color.white, "#DDDDDD"] // light/dark tint
  75. fogColor = "#EEEEEE" // fog in the distance
  76. tintLen = 0.1 // how far (in Z) our tint stripes go
  77. stripeLen = 0.06 // how far (in Z) the lane stripes go
  78. updateRoadDisplay = function(zOffset)
  79. x = 480 // X position of the road, on screen
  80. dx = 0 // change in x per raster line
  81. for i in road.indexes
  82. // calculate total Z along the road
  83. z = zOffset + rasterZ[i]
  84.  
  85. // update road position
  86. road[i].x = x
  87. x = x + dx
  88. dx = dx + segmentCurveRates[i > nextSegY]
  89.  
  90. // apply tint and fog
  91. c = tintColors[z % (tintLen*2) > tintLen]
  92. if rasterFog[i] then
  93. road[i].tint = color.lerp(c, fogColor, rasterFog[i])
  94. else
  95. road[i].tint = c
  96. end if
  97. grass[i].tint = road[i].tint
  98.  
  99. // update sprite (for lane stripes)
  100. strip = z % (stripeLen*2) > stripeLen
  101. road[i].image = roadImg[strip]
  102. end for
  103. end function
  104.  
  105. // Scroll the background left/right as we go around curves.
  106. updateBackground = function()
  107. globals.heading = heading + segmentCurveRates[0] * 100
  108. x = heading % bgWidth
  109. if x < 0 then x = x + bgWidth
  110. gfx.scrollX = x
  111. end function
  112.  
  113. // Frames per second (fps) counter
  114. fpsStart = time
  115. fpsFrameCount = 0
  116. updateFpsCounter = function()
  117. now = time
  118. globals.fpsFrameCount = fpsFrameCount + 1
  119. if now - fpsStart > 1 then
  120. fps = fpsFrameCount / (now - fpsStart)
  121. text.row = 25; text.column = 59
  122. print "FPS: " + round(fps) + " "
  123. globals.fpsStart = now
  124. globals.fpsFrameCount = 0
  125. end if
  126. end function
  127.  
  128. // define the track as a series of segments;
  129. // for each one, store the curve (+1 = hard right, -1 = hard left)
  130. trackSegments = [0, 0.5, -0.5, 0, 1, 1, 0, 0, 0, -0.5, -1, -1, -0.5]
  131. curveRate = 0.04
  132. segmentCurveRates = [0,0]
  133. heading = 0
  134.  
  135. // start the next track segment, updating all relevant
  136. // globals (especially segmentCurveRates, which determines
  137. // the current and next curvature of the road)
  138. startTrackSegment = function(n)
  139. globals.nextSegY = rasterZ.len - 1 // position of next road segment on screen
  140. globals.curSeg = n
  141. nextSeg = (curSeg + 1 + trackSegments.len) % trackSegments.len
  142. segmentCurveRates[0] = trackSegments[curSeg] * curveRate
  143. segmentCurveRates[1] = trackSegments[nextSeg] * curveRate
  144. text.row = 25; print "Track Segment: " + n + " "
  145. end function
  146. startTrackSegment 0
  147.  
  148. startNextTrackSegment = function()
  149. startTrackSegment (curSeg + 1 + trackSegments.len) % trackSegments.len
  150. end function
  151.  
  152. // Initial state
  153. bottomTint = 0
  154. stripLeft = 10
  155. totalDistAlongTrack = 0
  156. speed = 0.01
  157.  
  158. // Main update function
  159. update = function()
  160. updateRoadDisplay totalDistAlongTrack
  161. updateBackground
  162. globals.totalDistAlongTrack = totalDistAlongTrack + speed
  163. globals.nextSegY = nextSegY - 200/rh * speed
  164. if nextSegY < 0 then startNextTrackSegment
  165. updateFpsCounter
  166. end function
  167.  
  168. // Main loop
  169. while true
  170. update
  171. yield
  172. end while
  173.  
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment