News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Random colours

Started by Buÿs, September 28, 2005, 04:20:37 AM

Previous topic - Next topic

Buÿs

Hi to all

It feels good if you get something to work like you want to, but if you'r inexperienced like I am it takes you so much longer to eventualy get it to work, and then there is the problem of getting it to work in other situations.

I found a way of using a random colour selection from certain colours that I want to use to draw shapes and so forth..

It looks like this:
-----------------------------------------------------------------------------------------------
; Create a number of colours to chose from
   
                            Colour0=RGB(0,0,255)
                Colour1=RGB(0,255,0)
                Colour2=RGB(255,0,0)
                Colour3=RGB(255,255,0)
               
   ; Make an array of boxes
   
    Size=50
    Ypos=0

    For ylp=0 To 10
       Xpos=0
          For Xlp=0 To 16
         
   ; Randomize your selected colours
For c =0 To 3
   
       Box_Colour=Rnd©

      If Box_Colour=0 Then Box_Colour=Colour0 : Continue
      If Box_Colour=1 Then Box_Colour=Colour1 : Continue
      If Box_Colour=2 Then Box_Colour=Colour2 : Continue
      If Box_Colour=3 Then Box_Colour=Colour3 : Continue
      
Next c
          BoxC Xpos,Ypos,Xpos+Size-5,Ypos+size-5,1,Box_Colour
       

          xpos=xpos+size
         Next
       ypos=ypos+size
    Next                
               
Sync
WaitKey


-----------------------------------------------------------------------------------------------

Is this the correct way of doing it?
How do I get it to work in a loop function (like in a "repeat" "until" function), without letting the colours change continuously and getting one colour at a time to be displayed?

empty

There are a few optimisations possible. :)

We start with the first section

Colour0=RGB(0,0,255)
Colour1=RGB(0,255,0)
Colour2=RGB(255,0,0)
Colour3=RGB(255,255,0)

This is a perfect opportunity to use arrays.

Dim Colour(3)
Colour(0) = RGB(0,0,255)
Colour(1) = RGB(0,255,0)
Colour(2) = RGB(255,0,0)
Colour(3) = RGB(255,255,0)


Now for the randomize section:

; Randomize your selected colours
For c =0 To 3

Box_Colour=Rnd( c )

If Box_Colour=0 Then Box_Colour=Colour0 : Continue
If Box_Colour=1 Then Box_Colour=Colour1 : Continue
If Box_Colour=2 Then Box_Colour=Colour2 : Continue
If Box_Colour=3 Then Box_Colour=Colour3 : Continue

Next c

This section would iterate 4 times. With each iteration it would generate a random number and then assign it to Box_Colour and overwrite the assignment of the previous loop. So there's no point in using a loop here.

Box_Colour = rnd(3)  ; create a random number between 0 and 3
Box_Colour = Colour(Box_Colour) ; assign the corresponding value from our array

Of course we can ommit the Box_Colour variable completely and use this line instead:

BoxC Xpos, Ypos, Xpos+Size-5, Ypos+size-5, 1, Colour(rnd(3))

That way we use the rnd function and use the result as our array index.


Here's the complete code:

Dim Colour(3)

Colour(0)=RGB(0,0,255)
Colour(1)=RGB(0,255,0)
Colour(2)=RGB(255,0,0)
Colour(3)=RGB(255,255,0)

; Make an array of boxes

Size=50
Ypos=0

For ylp=0 To 10
 Xpos=0
 For Xlp=0 To 16
   BoxC Xpos,Ypos,Xpos+Size-5,Ypos+size-5,1,Colour(Rnd(3))
 xpos=xpos+size
 Next
 ypos=ypos+size
Next

Sync
WaitKey


Hope it's understandable. :)

kevin

perhaps your after something  like this ?




; Create a number of colours to chose from
Dim Colours(3)

Colours(0)=RGB(0,0,255)
Colours(1)=RGB(0,255,0)
Colours(2)=RGB(255,0,0)
Colours(3)=RGB(255,255,0)

; Define some variables to hold the Width/Height of the colour table
ColourTableWidth=10
ColourTableHeight=16

; create an array to hold the Grid colours
Dim ColourTable(ColourTableWidth,ColourTableHeight)


; Init the Colours in the colour Table array
For ylp=0 To ColourTableHeight
For Xlp=0 To ColourTableWidth
 ColourTable(Xlp,ylp)=Colours(rnd(3))
next xlp
next ylp



; Start of a repeat until looop
Repeat
; Clear the Screen to Black
Cls 0


; Draw the Colour Table tiles
Size=50
Ypos=0
For ylp=0 To ColourTableHeight
 Xpos=0
 For Xlp=0 To ColourTableWidth
      Box_Colour=ColourTable(Xlp,ylp)
  BoxC Xpos,Ypos,Xpos+Size-5,Ypos+size-5,1,Box_Colour
  xpos=xpos+size
 Next
ypos=ypos+size
Next


; Draw circle at the mouses coords
Circle MouseX(),mousey(),10,0


 ; Show the Screen

   Sync
; repeat until the user hits the SpaceKey
Until SpaceKey()=true


print "Done"

Sync
WaitKey