Random

Random is your best friend. Well, maybe not your best friend, but a really, really, really good friend. You can do a lot of cool stuff with the Random command in Blitz3D. Okay, enough already, what is Random? The Random command ... well ... makes things random. So far in this book, whenever we’ve wanted to specify an attribute for an object—for example, the color or size or position of a shape—we would actually tell Blitz3D what that attribute should be. Using the Random command, you can have Blitz3D randomly set the attributes for your object.

Why would you want to use random? Let me count the ways. There are literally thousands of different applications for the Random command, but I’ll give you just a few. Typically, the Random command is used when you are dealing with more than one object. Here are a few examples of scenarios where you would use it:

  • Imagine you had a game with 100 different aliens. Rather than having to provide a color to each alien, you could have Blitz3D randomly give them each a color.

  • Let’s say you are creating a series of objects like an asteroid field. You’d probably want each asteroid to be a little different in shape. Rather than having to shape each asteroid individually, you could use the Random command to randomly create different shapes for your asteroid.

  • In a game with different enemies, you want the enemies to be in a different location each time the game is played. Using the Random command, you could have the enemies positioned randomly around the playing field.

So far we’ve been calling it the Random command, but the actual command you use when coding looks something like this:

Rand (-10,50)

The two numbers that you put in parentheses after the Rand command are very important because they allow you to control the range. The number on the left is the lowermost limit for the number, and the number on the right is the uppermost number. In other words, if we were to run this code right now, Blitz3D would randomly choose a number between –10 and 50.

Let’s create some code using the Rand command so that you can get a better understanding as to what it does. In the following example, we will create a sphere and then randomly give it a color. Start by entering the code for the following program that just creates the sphere:

; Random color
;_______________
Graphics3D 640,480
SetBuffer BackBuffer()
; Create camera
camera=CreateCamera()
;Create light
light=CreateLight()
;Create a sphere
sphere=CreateSphere()
PositionEntity sphere, 0,0,6
;The following code makes our program run
While Not KeyDown( 1 )
RenderWorld
Flip
Wend
End

Now let’s add the code to color the sphere. As you should recall, when we color an object we provide a number for the Red, Green, and Blue values. This number is anywhere between 0 and 255 for each color. In this case, we will have Blitz3D randomly select the number for each color by entering the following code in bold:

;Create a sphere
sphere=CreateSphere()
PositionEntity sphere, 0,0,6
EntityColor sphere, Rand(0,255),Rand(0,255),Rand(0,255)

If you run the program now, you’ll see that the sphere has been given a random color. We set the lowest possible number for each color value to be 0 and the highest to be 255 with the code Rand(0,255).

Now let’s try something really interesting. I want you to cut the line you just added and paste it into the game loop. The code for the program should now look like this:

; Random color
;_______________
Graphics3D 640,480
SetBuffer BackBuffer()
; Create camera
camera=CreateCamera()
;Create light
light=CreateLight()
;Create a sphere
sphere=CreateSphere()
PositionEntity sphere, 0,0,6
;The following code makes our program run
While Not KeyDown( 1 )
EntityColor sphere, Rand(0,255),Rand(0,255),Rand(0,255)
RenderWorld
Flip
Wend
End

Go ahead and run the program now. Because we added the command to randomly assign a color to the sphere inside the game loop, the color of the sphere changes with each loop of the game. By the way, you are seeing 30 different color changes per second! Here we have randomly changed the color of an object, but you could have just as easily randomly changed its shape or location or any other attribute.

Note: Rand, Rnd, and SeedRnd

There are actually three different commands you can use for generating a random value. The Rand command will result in an integer value, while the Rnd command will produce a floating-point value. You’ll learn more about floating-point values and integers in the next chapter, but in a nutshell, an integer cannot have decimal places while a floating-point value can. The Rand and Rnd numbers don’t really create random numbers; they choose a random number based on a starting point called a seed value. The seed value is always the same, so if you ran a Rnd or Rand command, you’d get the same number every time you ran the program. The SeedRnd command changes the seed value so that the numbers it generates are truly random.


As I mentioned earlier, using the Rand command comes in very handy when working with multiple objects. In the next example, we will code a program that creates multiple spheres. We are then going to use the Random command to change the shape of the sphere.

The code used to create the multiple spheres is a For...Next statement. I’ll discuss this type of statement further in the next chapter. For now we are just concerned with the Random command that will be used to change the shape of the spheres.

Enter the following code to create the program:

; Random Sphere Shapes
;_______________________
Graphics3D 640,480
SetBuffer BackBuffer()
; Create camera
camera=CreateCamera()
PositionEntity camera, 25,28,-40
;Create light
light=CreateLight()
;Creating Many Spheres
For x = 1 To 10
For y = 1 To 10
sphere=CreateSphere()
PositionEntity sphere, x*5,y*5,6
Next
Next
;The following code makes the program run
While Not KeyDown( 1 )
RenderWorld
Flip
Wend
End

Run the program now, and you should see 100 spheres as in Figure 13.15.

Figure 13.15. We’ve created 100 spheres using a For...Next statement.


Now we will use a Random command to randomly change the shape of each sphere. We are going to add a ScaleEntity command to change the size and shape of the spheres, and we will set the range to be any integer between –3 and 3.

Do this by adding the following code in bold:

;Creating Many Spheres
For x = 1 To 10
For y = 1 To 10
sphere=CreateSphere()
PositionEntity sphere, x*5,y*5,6
ScaleEntity sphere, Rand(-3,3), Rand(-3,3), Rand(-3,3)
Next
Next

When you run the program now, you should have randomly shaped spheres as in Figure 13.16.

Figure 13.16. Using the Rand command, we created randomly shaped spheres.


Note: Now You Try

I want you to try to create a traffic light that is out of order. It should be a cube stretched to look like a rectangular shape. Within the rectangle, there should be three spheres that will act as lights: one red on the top, one yellow in the middle, and one green on the bottom. Make each of the three lights flicker between different shades of their color. Take a look at Figure 13.17 to get a reference for what the shape should look like. When you are finished, compare your code with the following code. You can also open the file called demo13-07.bb to preview the program.

Figure 13.17. Your traffic light should look something like this.



;demo13-07.bb - Out of Order Traffic Light
;_________________________
Graphics3D 640,480
SetBuffer BackBuffer()
; Create camera
camera=CreateCamera()
;Create light
light=CreateLight()
;Create a cube
cube=CreateCube()
ScaleEntity cube, 0.7,1.7,1
PositionEntity cube, 0,0,5
EntityColor cube, 234,234,23
;Create red light
red=CreateSphere()
PositionEntity red, 0,1.2,4.9
ScaleEntity red, 0.5,0.5,0.5
EntityOrder red,-1
;Create yellow light
yellow=CreateSphere()
PositionEntity yellow, 0,0,4.9
ScaleEntity yellow, 0.5,0.5,0.5
EntityOrder yellow,-1
;Create green light
green=CreateSphere()
PositionEntity green, 0,–1.2,4.9
ScaleEntity green, 0.5,0.5,0.5
EntityOrder green,-1
;The following code makes our program run
While Not KeyDown( 1 )
EntityColor red, Rand(177,255),Rand(0,110),Rand(0,87)
EntityColor yellow, Rand(236,255),Rand(193,255),Rand(0,85)
EntityColor green, Rand(0,113),Rand(193,255),Rand(0,120)
RenderWorld
Flip
Wend
End

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.2.87