NEWS ARTICLES


    What's News in the PlayBasic world ? - Find out here.




 PlayBasic Tutorial: From Variables To Arrays (Intro To Arrays)

By: Kevin Picone Added: September 21st, 2017

Category: All,Variables,Array,Tutorials,Beginner

PlayBasic Tutorial: From Variables To Arrays (Intro To Arrays) 2017-09-17

    Welcome programmers, in this tutorial we're going to introduce the concept of arrays starting out from variables. So first we build a simple game loop that controls two characters using only variables. The characters are represented on screen as filled circles. After we get up to speed with the variable version we then move onto how we can use parallel integer arrays to store the various properties of the characters. The array version can control as many or as few characters as you like, which is the benefit of Arrays over Variables

Want to learn 2D game programming ?


Video:



Example #1 - Variables Example

PlayBasic Code:
   Xpos1  = 400
   Ypos1  = 300
      
   Xpos2  = 100
   Ypos2  = 100

      
   setfps 20   
      
  do    

    cls rgb(10,20,40)

	; Draw circle 1        
     Circle Xpos1,Ypos1, 32, true
   
     Xpos1 = Xpos1 + 2
     Ypos1 = Ypos1 - 4
  
   	Xpos1 = WrapValue(Xpos1,0,800)	  
    	Ypos1 = WrapValue(Ypos1,0,600)	  
   
   

	; Draw circle 2      
     Circle Xpos2,Ypos2, 64, true
   
     Xpos2 = Xpos2 + 2
     Ypos2 = Ypos2 + 2
    	Xpos2 = WrapValue(Xpos2,0,800)	  
    	Ypos2 = WrapValue(Ypos2,0,600)	  
  
     sync
  loop
  
 

COMMANDS USED: SETFPS | CLS | RGB | CIRCLE | WRAPVALUE | SYNC |


Example #2 - Arrays Example - Using Parallel Arrays To Store Character Properties
PlayBasic Code:
 Setfps 20

   Number_Of_Characters = 50 

   dim Xpos( Number_Of_Characters  )
   dim Ypos( Number_Of_Characters  )
   dim size( Number_Of_Characters  )
   dim Xspeed( Number_Of_Characters )
   dim Yspeed( Number_Of_Characters )
   dim Colour( Number_Of_Characters )
   
   
   for lp = 1 to Number_Of_Characters
 	     Xpos(lp)  = rnd(800)
 		  Ypos(lp)  = rnd(600)
 		  Size(lp)  = rndrange(16,50)
  		 Xspeed(lp) = rndrange(-5, 5)
   	 Yspeed(lp) = rndrange(-5,5)
   	 Colour(lp) = rndrgb()
   next
   
   
  do 
  	
  	   Cls rgb(0,400,20)
  	   
  	   for lp=1 to Number_Of_Characters
  	      
  	      Radius = Size(lp)

  	      circlec xpos(lp),ypos(lp),Radius,true,Colour(lp)
  	      
  	    //  xpos(lp) = Xpos(lp) + Xspeed(lp)
  	    //  ypos(lp) = ypos(lp) + Yspeed(lp)
  	      
  	      xpos(lp) = wrapvalue(xpos(lp) + Xspeed(lp) , -Radius, 800 + Radius)
  	      ypos(lp) = wrapvalue(ypos(lp) + Yspeed(lp) , -Radius, 600 + Radius)
  	        	      
  		next
  	  	
  	   Sync
  	loop

COMMANDS USED: SETFPS | DIM | RND | RNDRANGE | RNDRGB | CLS | RGB | CIRCLEC | WRAPVALUE | SYNC |




Viewing Page [1] of [0]



 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]