PlayBASIC Tutorial: From Arrays To Types (Intro To Types) - (2018-06-12)

Started by kevin, June 11, 2018, 01:28:30 PM

Previous topic - Next topic

kevin

PlayBASIC Tutorial:  From Arrays To Types  (Intro To Types) - (2018-06-12)


This tutorial picks up where the previous variables to arrays tutorial left off,  in that it takes the array code, demos that code then we set about converting the parallel array approach shown in the previous tutorial and we build a structure (TYPE) to hold each characters properties.   Once the type has been defined that includes all the required properties, we then define a typed array that will house the collection of characters.    Later in the video take a look at using typed lists also.   So if your struggling with types this could be a good place to start.

Go here (variables to array tutorial  for the previous  video & source code.


To get the source code created in this video  go here:

Video:


http://playbasic.com
http://underwaredesign.com


Example #1 - Converting the Parallel Arrays To Typed Array

PlayBASIC Code: [Select]
    Setfps 20

Number_Of_Characters = 50

type tCharacter
Xpos#
Ypos#
Xspeed#
Yspeed#
Colour
Size
endtype


dim Characters(Number_Of_Characters) as tCharacter


for lp = 1 to Number_Of_Characters

; Allocate a newe tCharacter and place it's handle
; into the Character(lp) array / container at this position
Characters(lp) = new tCharacter

Characters(lp).Xpos = rnd(800)
Characters(lp).Ypos = rnd(600)
Characters(lp).Size = rndrange(16,50)
Characters(lp).Xspeed = rndrange(-5, 5)
Characters(lp).Yspeed = rndrange(-5,5)
Characters(lp).Colour = rndrgb()

next


//-------------------------------------------------------
//---[ MAIN LOOP ]---------------------------------------
//-------------------------------------------------------

do

Cls rgb(0,400,20)

for lp = 1 to Number_Of_Characters
Radius = Characters(lp).Size
x#=Characters(lp).Xpos
y#=Characters(lp).Ypos


circlec X#,Y#,Radius,true,Characters(lp).Colour

x# = wrapvalue(x# + Characters(lp).Xspeed , -Radius, 800 + Radius)
y# = wrapvalue(y# + Characters(lp).Yspeed , -Radius, 600 + Radius)

Characters(lp).xpos= x#
Characters(lp).ypos= y#
next

Sync
loop






Example #2 - Converting Typed Array To Typed List

PlayBASIC Code: [Select]
   Setfps 20

Number_Of_Characters = 50

type tCharacter
Xpos#
Ypos#
Xspeed#
Yspeed#
Colour
Size
endtype


dim Characters as tCharacter list


for lp = 1 to Number_Of_Characters

Characters = new tCharacter

Characters.Xpos = rnd(800)
Characters.Ypos = rnd(600)
Characters.Size = rndrange(16,50)
Characters.Xspeed = rndrange(-5, 5)
Characters.Yspeed = rndrange(-5,5)
Characters.Colour = rndrgb()

next


//-------------------------------------------------------
//---[ MAIN LOOP ]---------------------------------------
//-------------------------------------------------------

do

Cls rgb(0,400,20)

for each Characters()

Radius = Characters.Size
x#=Characters.Xpos
y#=Characters.Ypos


circlec X#,Y#,Radius,true,Characters.Colour

x# = wrapvalue(x# + Characters.Xspeed , -Radius, 800 + Radius)
y# = wrapvalue(y# + Characters.Yspeed , -Radius, 600 + Radius)

Characters.xpos= x#
Characters.ypos= y#
next

Sync
loop










baggey

Absolutly first class!

Love working through code like this. It's like being an apprentice again.

Sort of have an idea now as to why things are layed out this way now. Rather than remembering numbers to elements in an array. I can go straight to it with a name.

Kind Regards Bagggey
Jesus was only famous because of his dad