aNdy-AL-Cosine

PB Parallax Starfield

Sep 29th, 2018
699
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ------------------------------------------------------------
  2. ;
  3. ;   Parallax Starfield (4 layers)
  4. ;
  5. ;   Code by aNdy/AL/Cosine in 2018
  6. ;
  7. ;   https://andyvaisey.blogspot.com/p/code.html
  8. ;
  9. ; ------------------------------------------------------------
  10.  
  11. ;- INITIALISE ENVIRONMENT ------------------------------------
  12.  
  13. InitKeyboard()
  14. InitSprite()
  15.  
  16. ;- CREATE VARIABLES ------------------------------------------
  17.  
  18. #XRES = 800
  19. #YRES = 600
  20. #LAYER1SPEED = 1
  21. #LAYER2SPEED = 3
  22. #LAYER3SPEED = 5
  23. #LAYER4SPEED = 10
  24.  
  25. ;- PROCEDURES ------------------------------------------------
  26.  
  27. Procedure OpenMainWindow()
  28.  
  29.   OpenWindow(0,0,0,#XRES,#YRES,"Parallax Stars!",#PB_Window_ScreenCentered)
  30.   OpenWindowedScreen(WindowID(0),0,0,#XRES,#YRES)
  31.  
  32. EndProcedure
  33.  
  34. Procedure CreateStarfields()
  35.  
  36.   Global sp_BKGRDSTAR = CreateSprite(#PB_Any,#XRES,#YRES)
  37.   StartDrawing(SpriteOutput(sp_BKGRDSTAR))
  38.   For n=0 To 999
  39.     Plot(Random(#XRES-1),Random(#YRES-1),RGB(100,100,100))
  40.   Next
  41.   StopDrawing()
  42.  
  43.   Global sp_MDGRDSTAR = CreateSprite(#PB_Any,#XRES,#YRES)
  44.   StartDrawing(SpriteOutput(sp_MDGRDSTAR))
  45.   For n=0 To 299
  46.     Plot(Random(#XRES-1),Random(#YRES-1),RGB(150,150,150))
  47.   Next
  48.   StopDrawing()
  49.  
  50.   Global sp_FTGRDSTAR = CreateSprite(#PB_Any,#XRES,#YRES)
  51.   StartDrawing(SpriteOutput(sp_FTGRDSTAR))
  52.   For n=0 To 199
  53.     Plot(Random(#XRES-1),Random(#YRES-1),RGB(200,200,200))
  54.   Next
  55.   StopDrawing()
  56.  
  57.   Global sp_CLGRDSTAR = CreateSprite(#PB_Any,#XRES,#YRES)
  58.   StartDrawing(SpriteOutput(sp_CLGRDSTAR))
  59.   For n=0 To 19
  60.     Plot(Random(#XRES-1),Random(#YRES-1),RGB(255,255,255))
  61.   Next
  62.   StopDrawing()
  63.    
  64. EndProcedure
  65.  
  66. Procedure DrawStarfields()
  67.  
  68.   Shared bgx1, bgx2, bgx3, bgx4
  69.  
  70.   For s1 = 0 To 1
  71.     For s2 = 0 To 1
  72.       DisplayTransparentSprite(sp_BKGRDSTAR,#XRES*s1-bgx1,#YRES*s2)
  73.       DisplayTransparentSprite(sp_MDGRDSTAR,#XRES*s1-bgx2,#YRES*s2)
  74.       DisplayTransparentSprite(sp_FTGRDSTAR,#XRES*s1-bgx3,#YRES*s2)
  75.       DisplayTransparentSprite(sp_CLGRDSTAR,#XRES*s1-bgx4,#YRES*s2)
  76.     Next
  77.   Next
  78.  
  79.   bgx1+#LAYER1SPEED : If bgx1 > #XRES-1 : bgx1=0 : EndIf
  80.   bgx2+#LAYER2SPEED : If bgx2 > #XRES-1 : bgx2=0 : EndIf
  81.   bgx3+#LAYER3SPEED : If bgx3 > #XRES-1 : bgx3=0 : EndIf
  82.   bgx4+#LAYER4SPEED : If bgx4 > #XRES-1 : bgx4=0 : EndIf
  83.  
  84. EndProcedure    
  85.  
  86. ;- BEGIN PROGRAM ----------------------------------------------------      
  87.  
  88. OpenMainWindow()                            
  89. CreateStarfields()
  90. HideWindow(0,#False)
  91.  
  92. ;- MAIN PROGRAM LOOP ------------------------------------------------  
  93.  
  94. Repeat
  95.  
  96.   WaitWindowEvent(1)
  97.   ClearScreen(0)
  98.   DrawStarfields()
  99.   FlipBuffers()
  100.   ExamineKeyboard()
  101.  
  102. Until KeyboardPushed(#PB_Key_Escape)
  103.  
  104. ;- EXIT PROGRAM ----------------------------------------------------    
  105.  
  106. FreeSprite(#PB_All)
  107. CloseWindow(#PB_All)
  108.  
  109. End
RAW Paste Data