Adding Text Variables

As you can see by the example in the last section, having text without some sort of variable just isn’t that effective. In that example, we have the word “Score,” but more importantly, what we don’t have is the actual score. This can be added by expanding our Text command. Think of the Text command as an equation; if you want to add something, just use the + sign.

Now we’ll continue on with the example we used in the last section, so if for some reason you thought it would be a good idea to skip that section and jump to here, think again. Go back and add the “Score” line to the demo13-06.bb program.

Before we add the code necessary to display the score on the screen, we have to determine how we are going to keep score in this game. For this example, we’ll say that every time the sphere touches the cone, a point will be earned. You learned in Chapter 11, “Collisions,” that the CountCollisions command will count the number of times an object collides with another within each frame. We’ll use this to act as our score. To actually add the score variable to the text onscreen, we simply use a + sign. Let’s add the following code in bold to our program:

; The following code makes the program run
While Not KeyDown(ESC_KEY)
x#=0
y#=0
z#=0
If KeyDown(LEFT_KEY)=True Then x#=-0.1
If KeyDown(RIGHT_KEY)=True Then x#=0.1
If KeyDown(DOWN_KEY)=True Then y#=-0.1
If KeyDown(UP_KEY)=True Then y#=0.1
If CountCollisions (sphere) Then score#=score#+1
MoveEntity sphere,x#,y#,z#
UpdateWorld
RenderWorld
Text 10, 20, "Score: " + score#, False, False
Flip
Wend
End

Run the program now, and using the arrow keys, move the sphere into the cone. As soon as the sphere touches the cone, the score will start registering, as in Figure 13.11.

Figure 13.11. When the sphere touches the cone, the score will appear on the screen.


We added only two little snippets of code to create the score. Let’s see how we did it:

If CountCollisions (sphere) Then score#=score#+1—Here we are saying that if a collision occurs between the sphere and the cone, then the variable we created called score# will become score# + 1. In other words, the score starts out at 0. If a collision occurs, then the score becomes 0 + 1, which would make the score 1. If another collision occurs, the score (which is now at 1) becomes 1+1, in other words, 2. This continues on, and we see the score getting higher and higher as long as the two objects are colliding.

Text 10, 20, "Score: " + score#, False, False—Here we added the score variable to the existing text by using a + sign before adding the variable. You can add a variety of variables to your text strings simply by adding a + sign.

Let’s now add some other variables to our text string. The only other variables we have in the program we are using are the x and y locations of the sphere, so let’s add a line of text that displays the position of the sphere at all times. Add the following code to the program:

; The following code makes the program run
While Not KeyDown(ESC_KEY)
x#=0
y#=0
z#=0
If KeyDown (LEFT_KEY)=True Then x#=-0.1
If KeyDown (RIGHT_KEY)=True Then x#=0.1
If KeyDown (DOWN_KEY)=True Then y#=-0.1
If KeyDown (UP_KEY)=True Then y#=0.1
If CountCollisions (sphere) Then score#=score#+1
MoveEntity sphere, x#, y#, z#
UpdateWorld
RenderWorld
Text 10, 20, "Score: "+ score#, False, False
Text 10, 60, "Sphere x-coordinate: " + EntityX(sphere) + " Sphere y-coordinate: "
+ EntityY(sphere), False, False
Flip
Wend
End

Run the program now, and move the sphere around using the arrow keys. Your screen should look similar to Figure 13.12.

Figure 13.12. The code you added will display the x and y coordinates of the sphere.


Let’s take a closer look at the code we entered to see how we accomplished this:

Text 10, 60,—This specifies that the text needs to be placed 10 units from the left of the screen and 60 units down.

"Sphere x-coordinate: "—As you learned earlier, we need to add text in between quotation marks.

+ EntityX(sphere)—By adding a + sign, we are able to add additional variables or text. In this case, we used the variable EntityX(sphere), which displays the x coordinate of the sphere.

+ " Sphere y-coordinate: "—Notice the spaces that were left between the ends of the text and the quotation marks, allowing space for the numbers.

+ EntityY(sphere)—This produces the y coordinate of the sphere.

, False,False—We turned off horizontal and vertical centering. We actually didn’t need this part of the code at all, since these are the defaults.

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

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