SHARE
TWEET

Platformer.bas

atm959 Jun 19th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. OPTION _EXPLICIT
  2. OPTION BASE 0
  3. DIM windowDisplay&
  4. windowDisplay& = _NEWIMAGE(800, 600, 32) 'Set up the window screen
  5. SCREEN windowDisplay& 'Apply the window screen
  6. _TITLE "Platformer" 'Set the window title
  7.  
  8. DIM stp% 'Global timer
  9.  
  10. DIM level%(64, 64) 'Tile IDs within the level
  11. DIM tiles&(128) 'Tile graphics
  12. DIM scrollX% 'Camera X position
  13. DIM scrollY% 'Camera Y position
  14.  
  15. DIM px% 'Character X position
  16. DIM py% 'Character Y position
  17. DIM gravityForce% 'Amount of gravitational force
  18.  
  19. DIM tx% 'Tile X(for collision)
  20. DIM ty% 'Tile Y(for collision)
  21. DIM tnd% 'Tile ID(for floor collision)
  22.  
  23. DIM tempFile$ 'Used for the tile loading routine
  24. DIM index% 'Used for the tile loading routine
  25.  
  26. DIM ttime%
  27. DIM lastTime%
  28. DIM fps%
  29. DIM fps2%
  30.  
  31. DIM imgData&
  32.  
  33. _PRINTMODE _KEEPBACKGROUND
  34.  
  35. CHDIR "Platformer/Backgrounds"
  36. DIM bg&
  37. bg& = _LOADIMAGE("blueGradient.png")
  38.  
  39. CHDIR "../Character"
  40. DIM character&
  41. character& = _LOADIMAGE("character.png")
  42.  
  43. CHDIR "../Fonts"
  44. DIM font&
  45. font& = _LOADIMAGE("Font1.png")
  46.  
  47. index% = -1 'This is set to -1 because the routine changes it to 0 when it begins
  48.  
  49. DIM x%
  50. DIM y%
  51. DIM level%
  52.  
  53. DIM ex AS DOUBLE
  54.  
  55. RESTORE LevelData
  56. WHILE 1
  57.     FOR x% = 0 TO 7
  58.         FOR y% = 0 TO 7
  59.             READ level%
  60.             IF level% = 255 THEN
  61.                 GOTO endLoad
  62.             END IF
  63.         NEXT
  64.     NEXT
  65. WEND
  66.  
  67. endLoad:
  68.  
  69. CHDIR "../Tiles"
  70. RESTORE tilePaths
  71. DO 'Load all of the tile graphics
  72.     READ tempFile$
  73.     IF tempFile$ <> "0" THEN
  74.         index% = index% + 1
  75.         tiles&(index%) = _LOADIMAGE(tempFile$)
  76.     ELSE
  77.         GOTO Done
  78.     END IF
  79. LOOP
  80. Done:
  81.  
  82. CHDIR "../BGM"
  83. DIM bgMusic&
  84. bgMusic& = _SNDOPEN("NormalTheme.wav") 'Load a music file into the long variable called "bgMusic&"
  85.  
  86. RESTORE LevelData
  87. DIM i%
  88. FOR i% = 0 TO 7
  89.     READ level%(0, i%), level%(1, i%), level%(2, i%), level%(3, i%), level%(4, i%), level%(5, i%), level%(6, i%), level%(7, i%)
  90. NEXT
  91.  
  92. _SNDLOOP bgMusic& 'Loop the music
  93. px% = 135
  94. py% = 50
  95. gravityForce% = 3
  96.  
  97. WHILE 1 'Main game loop
  98.     CLS
  99.     stp% = stp% + 1
  100.     'py% = py% + gravityForce%
  101.  
  102.     scrollX% = 25 * SIN(25 * stp%)
  103.     scrollY% = 25 * COS(25 * stp%)
  104.     px% = stp% MOD 300
  105.  
  106.     GOSUB drawBG
  107.     GOSUB drawLevel
  108.     GOSUB collide
  109.     GOSUB drawCharacter
  110.  
  111.     ttime% = VAL(MID$(TIME$, 7, 2))
  112.     IF ttime% - lastTime% >= 1 THEN
  113.         fps% = fps2%
  114.         fps2% = 0
  115.         lastTime% = ttime%
  116.     ELSE
  117.         fps2% = fps2% + 1
  118.     END IF
  119.  
  120.     PRINT "FPS: "; fps%
  121.     PRINT "scrollX%: "; scrollX%
  122.     PRINT "scrollY%: "; scrollY%
  123.  
  124.     imgData& = _COPYIMAGE(0)
  125.  
  126.     'CLS
  127.  
  128.     FOR i% = 0 TO _HEIGHT(imgData&)
  129.         ex = 8 * SIN((stp% + i%) * 0.05)
  130.         _PUTIMAGE (ex, i%), imgData&, 0, (0, i%)-(_WIDTH(imgData&), i%)
  131.     NEXT
  132.  
  133.     _FREEIMAGE (imgData&)
  134.  
  135.     _DISPLAY 'Update the graphics
  136.     _LIMIT 60 'Limit to 60 FPS
  137. WEND
  138.  
  139. drawBG:
  140. _PUTIMAGE (0, 0), bg&
  141. RETURN
  142.  
  143. drawLevel:
  144. FOR x% = 0 TO 63
  145.     FOR y% = 0 TO 63
  146.         _PUTIMAGE ((x% * 32) + scrollX%, (y% * 32) + scrollY%)-(((x% * 32) + scrollX%) + 31, ((y% * 32) + scrollY%) + 31), tiles&(level%(x%, y%))
  147.     NEXT
  148. NEXT
  149. RETURN
  150.  
  151. collide:
  152. tx% = (px% / 32) MOD 64
  153. ty% = (py% / 32) MOD 64
  154. tnd% = level%(tx%, ty% + 1)
  155. IF tnd% = 2 THEN
  156.     IF py% > (ty% * 32) THEN
  157.         py% = (ty% * 32)
  158.     END IF
  159. END IF
  160. RETURN
  161.  
  162. drawCharacter:
  163. _PUTIMAGE (px% + scrollX%, py% + scrollY%)-((px% + 32) + scrollX%, (py% + 32) + scrollY%), character&
  164. RETURN
  165.  
  166. tilePaths: 'The paths for the tile graphics
  167. DATA "air.png"
  168. DATA "Grass/grassFloorLeftLedge.png"
  169. DATA "Grass/grassFloor.png"
  170. DATA "Grass/grassFloorRightLedge.png"
  171. DATA "Grass/grassLeftLedge.png"
  172. DATA "Grass/grassInside.png"
  173. DATA "Grass/grassRightLedge.png"
  174. DATA "Grass/grassCeilLeftLedge.png"
  175. DATA "Grass/grassCeil.png"
  176. DATA "Grass/grassCeilRightLedge.png"
  177. DATA "Grass/grassCornerTL.png"
  178. DATA "Grass/grassCornerTR.png"
  179. DATA "Grass/grassCornerBL.png"
  180. DATA "Grass/grassCornerBR.png"
  181. DATA "Grass/grassRampUpLeft.png"
  182. DATA "Stone/stoneFloor.png"
  183. DATA "0"
  184.  
  185. LevelData: 'Test level data
  186. DATA 0,0,0,0,0,0,0,0
  187. DATA 0,0,0,0,0,0,0,14
  188. DATA 0,0,0,0,0,0,14,10
  189. DATA 0,0,0,1,2,2,10,0
  190. DATA 0,0,0,4,0,0,0,0
  191. DATA 0,0,0,4,0,0,0,0
  192. DATA 0,0,0,4,0,0,0,0
  193. DATA 2,2,2,10,0,0,0,0
  194. DATA 255
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top