Sphere5


Now things are becoming interesting. We are inching our way to our first full-blown game, but first we have to crawl a little more. In this program we will do a lot:

  • Add background music.

  • Add a sound when a collision occurs.

  • Display a text message on the screen.

  • Display x distance between two objects.

  • Add the and operator to our if statement.

  • Perform basic mathematical operations.

Please see Figure 4.6 for a screenshot of the program.

click to expand
Figure 4.6: Sphere5 screenshot. Sounds and music have been added, and distances are computed and displayed.

Adding Background Music and Sounds

Adding background music is a simple, yet very important task. To better understand what we are saying, try turning the sound down during a game ”you will probably find that the experience will not seem anywhere near as rich.

You can start by making a new project, or you can just make changes to your previous project. Once you have a project started, you can import the music file from one of the games in the Jamagic electronic manual.

To import the music, you must first right-click Musics a few lines below Sources on the Jamagic screen, and select Import Music Files. A window will appear with a place for you to enter information. Use the arrow near the top to scroll your way to Program Files/Jamagic/Examples/Advanced. Once there, double click the demo game Fish, and then click Musics. Now double-click melody , which will be the tune played during our game.

Tip  

Alternatively, you could have loaded one of the many terrific MIDI music files from the Jamagic CD-ROM (applies only to the full-capability version of Jamagic ), which we will explore in a later project.

Here are the commands involved in playing music in the background; as in the past, the explanations follow the commands:

 omusic = New Music("melody"); 

This line tells Jamagic that a new piece of music will be added to the program, and that is name is melody . Please note that the music must be imported into the program before you run it, as just described.

 omusic.SetLoop(ON,3); 

The melody tune (which the program now recognizes as omusic from the previous statement) lasts only a few minutes, so if we want music to play for longer periods, we must loop the music. The command just mentioned tells Jamagic to loop omusic three times. If you want to make sure that music plays for the entire game, just use a huge number instead of the number three.

 omusic.Play(); 

This statement starts the music playing. It will not start until it sees this command.

 omusic.pause(); 

This statement pauses the music, and it can be restarted again with the following:

 omusic.resume(); 

Sounds are very similar to music, but they are generally much shorter in duration. However, we can make sounds come on and turn off using the same commands as those we used with music.

In this case, the name of the sound is catch and you will have to import it into the project, just as you did with the music:

 osound = New Sound("catch"); osound.SetLoop(ON,1); osound.Play(); 

Like the music file imported earlier, the sound will be imported to our project from one of the Advanced Example Games in the Jamagic electronic manual. To import a sound, right click Sounds (a few lines beneath Sources) and follow the same procedure used to import the music into the game. The sound we want is in the advanced game called Rolling Ball 3D. When you get into the Rolling Ball 3D directory, click Sounds instead of Musics, and then double-click catch .

Note  

You may edit sounds using the Jamagic sound editor, which will be covered in a later section.

Displaying Messages on the Screen

Displaying messages on the screen is very useful, especially when simulating vehicles because the user might need to know information like the amount of fuel remaining, the speed of travel, the heading being followed, and so on. We will now spend some time discussing the commands we will be using to display such messages. As always, the command will be given first, followed by an explanation.

 mytext = New StaticText(CurWindow," ",100,10,150,20); 

This line tells Jamagic to make a new text called mytext and to place it in the current window ( CurWindow is a statement that Jamagic recognizes). The set of quotes can hold a message like Get me out of here! ; if it did, that message would be printed on the screen. However, because we will need to display a message that is constantly changing, we put the quotes in and leaving them blank; by doing so, we are telling the computer that something will be written later.

The numbers 100 and 10 indicate the x, y location of the text. It s easy to get confused here because a different coordinate system is being used for locating text on the screen than was used to move the objects in the 3D world as discussed earlier. The screen coordinate system is a little strange : in this scenario, the upper-left corner of the screen is the origin (0,0). if you move to the right from this corner, you are increasing the positive values of x, and when you move down from the origin, you are increasing the positive values of y (see Figure 4.7). Your screen size is automatically set at 640 pixels wide by 480 pixels in height by Jamagic. We will learn how to adjust this later.

click to expand
Figure 4.7: Screen coordinate system.

The numbers 150 and 20 specify the width and height of the message. You must specify a height and width exactly the same size or larger than the size of the actual message or the message will be cut off.

One of the messages we want to display is the x distance from the static sphere to the moving sphere. By the x distance, we mean the difference between the two spheres x coordinates. This computation will be useful in the next game when we ll want to know how far away our sphere is from the landing spot, which will help us determine when we need to apply our thrusters to slow down.

Calculating Distance between Objects

We will place the static sphere at (“500,0,0) and the moving sphere ( mysphere ) at (0,0,0) (this means that their centers are at those locations). Each sphere is 100 units in diameter.

We can easily find the distance between the centers of the spheres by saying

 x distance = (x coordinate of static sphere)  (x coordinate of mysphere). 

Notice that under some situations (like when the static sphere is to the left of the moving sphere) the value of the distance would be negative. We would prefer for distance to always be positive. To make it positive, we can use the absolute value function (which most programming languages including Jamagic have) to make any negative numbers positive. This can be done by using the following equation:

 distance = abs((x coordinate of static sphere)  (x coordinate of mysphere)). 

As you have probably figured out, using abs() takes the absolute value of whatever is inside the parentheses.

If you think about it, you will agree that in order to determine how close the spheres are to each other, we prefer to know the distance from the outer edge of one sphere to the outer edge of the other, not the distance from center to center. We will cover this momentarily.

Consider the following Jamagic command:

 mysphere.GetX; 

This gives us the x coordinate of the object we call mysphere . We know that the static sphere is always at x = “500, so we can calculate the distance (which we will give the name dist ) from center to center by using the following:

 dist = abs(500  (mysphere.GetX)), 

However, recall that we want the closest distance from outer edge to outer edge. Since each edge is 50 units from the center (recall that each sphere is 100 units in diameter), the sphere edges will be 100 units closer than the formula above would indicate, so we need to subtract 100 from our formula above to get

 x dist = ABS(500  (mysphere.GetX))  100. 

This is the equation that we will use to find the distance between the static sphere and the moving sphere called mysphere .

Before we move on, we must note some important things about this equation:

  • Every left parenthesis has a matching right parenthesis to go with it. This will be true of all equations!

  • It s important to get the parentheses right so that the absolute value gets taken of precisely the part that you need.

Hierarchy of Operations and Basic Calculations

Before we continue, we need to stop for one minute and think about the order in which calculations are done in an equation. Jamagic will do everything in parentheses first, then it will do the multiplication and division, and finally it will do the addition and subtraction.

For example, if we had written

 dist = 100 + abs(500  (mysphere.GetX)), 

we would have achieved the same result as the previous equation since the quantities in the parentheses would have been calculated first, followed by the addition/subtraction. The order in which calculations are performed is called the hierarchy of operations , and it is very important to keep it in mind when you are writing equations. For example, the two equations below give different results (incidentally, the * sign indicates multiplication, while / denotes division):

 x = (3 * 3) + 2 / 1 = 11 x = 3 * (3+2 / 1) = 15 

Refer back to the hierarchy of operations just described if you don t understand the results. Which is correct? It depends on what you, the programmer, want!

Tip  

Please consult the language reference in the Jamagic electronic manual for more information on performing mathematical calculations.

Once we calculate the distance between the spheres, we can display it by putting the following lines inside our game loop:

 dist = abs(500 (mysphere.GetX))  100; mytext.SetText("Distance: " + dist); 

The SetText command tells Jamagic to print Distance: in mytext followed by the value calculated for the distance, which we called dist . It s very important to realize that we could have written anything inside the quotation marks that surround Distance: (such as The distance is: , Here s the distance, I hope you like it: , and so on) as long as the text you made in the New StaticText is long enough to hold it (see the section Displaying Messages on the Screen earlier in this chapter).

Another thing to note is that we left a space after the colon and before the end of the quotation marks. We did this so that there would be a space between the word Distance and the number dist .

Note  

The commands above must be placed inside the game loop or else dist will not be updated continuously as the sphere moves around the screen.

We are also starting to get somewhat creative in this program. Now when a collision occurs, we want a message to appear in a second text message. Our function, docollision , now looks like this:

 Function docollision {  mysphere.Hide;  mysphere.ReplaceMaterial(mat2);  omusic.Pause();  osound.Play();  mytext2.SetText("Press right arrow key to uncollide,  then 'f4' to see ball and then 'r' to hear music");  //last two lines on one line } 

You can probably figure out what is happening here: when two objects collide, mysphere will be hidden from view and its appearance will change ”that s nothing new. The music will stop, and the sound we imported will be played. In addition, instructions will be given to the user, informing him what to do next. By pressing the left arrow key, he will cause mysphere to move away from the static ball so that the collision is no longer occurring. Then, when he presses the F4 key, the ball will reappear, and when he presses the letter R on the keyboard, the music will come on again. The following lines of code are the ones that make this happen:

 if(Keyboard.IsKeyDown(Keyboard.F4))  {   mysphere.Show;   } 

No problems with this one, we hope. The next one bears some explaining.

if-and Statements

The && characters in the following code indicate a very powerful capability of the if statement:

 if(Keyboard.IsKeyDown('R')&& omusic.IsPaused())  {  omusic.Resume();  mytext2.SetText(" ");   } 

The first line of this code means that if the letter r is pressed down AND the music is paused , then the instructions that follow should be performed. With this explanation, you can probably figure out what is going on in the rest of this code segment now. If the letter R is pressed down and the music is NOT playing, then the music will resume and the text in mytext2 will change to nothing.

After this explanation, you should now be ready to type in Sphere5 s code and run it. You will see the distance displayed constantly on the screen, and you will see it change when you use the right or left arrow keys. Why does distance change only with the right and left keys? This is because we are displaying the distance between the two spheres in the x direction ”the right and left keys are the only ones that move the sphere along the x axis.

Sphere5 Code

 // // Sphere5: 5th Spherelander game. Will add music as well     // as // collision sounds to previous program. // Music will be added from fish game.catch sound from // rolling ball 3D game. // Also added are double conditional statements. // // Also added text message on the screen with distance to // static ball displayed. // Message changes upon collision. // Create a new world with a new camera. oworld = New World(); ocamera = New Camera(oworld); // Music omusic = New Music("melody"); omusic.SetLoop(ON,3); omusic.Play(); //Text message mytext = New StaticText(CurWindow," ",100,10,150,20); mytext2 = New StaticText(CurWindow," ",100,40,450,20); // Sounds osound = New Sound("catch"); osound.SetLoop(ON,1); // Fog commands ocamera.EnableFog(ON); ocamera.SetFogColor(GetRGB(30,0,0)); ocamera.SetFogMaxZPosition(5000); ocamera.SetFogMinZPosition(3000); // Create a sphere with Gouraud shading. mysphere = oworld.CreateSphere(100,100,100,10,10); mysphere.SetPosition(0,0,0); omaterial = New Material     (oworld,GetRGB(50,150,200),"lightblue");//one line mysphere.SetGouraud(ON); mysphere.ReplaceMaterial(omaterial); //Create a 2nd sphere with flat solid color. sphere2 = oworld.CreateSphere(100,100,100,10,10); sphere2.SetPosition(500,0,0); mat2 = New Material(oworld,GetRGB(0,200,0),"green"); mat2.SetFlat(ON); sphere2.ReplaceMaterial(mat2); sphere2.SetStatic(); //Create a 3rd sphere with flat mesh. ball3 = oworld.CreateSphere(100,50,50,10,5); ball3.SetPosition(500,500,1000); mat3 = New Material     (oworld,GetRGB(50,150,200),"lightblue");//one line ball3.ReplaceMaterial(mat3); ball3.SetStatic(); // Camera ocamera.SetPosition(0,0,3000); oworld.Optimize(ocamera); //Set collision detection on. oworld.OnCollide = docollision; mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_STOP); //Game loop While(1) { if(Keyboard.IsKeyDown(Keyboard.UP))  {    mysphere.Move(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.DOWN))  {    mysphere.Move(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.RIGHT))  {    mysphere.MoveRight(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.LEFT))  {    mysphere.MoveLeft(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.SHIFT))  {    mysphere.MoveDown(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.CAPSLOCK))  {    mysphere.MoveUp(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.F4))  {  mysphere.Show;  } if(Keyboard.IsKeyDown('R')&& omusic.IsPaused())  {  omusic.Resume();  mytext2.SetText(" ");  } dist = Abs(500 -(mysphere.GetX)) - 100; mytext.SetText("Distance: " + dist); } Function docollision {    mysphere.Hide;    mysphere.ReplaceMaterial(mat2);    omusic.Pause();     osound.Play();     mytext2.SetText("Press right arrow key to uncollide,    then 'f4' to see ball and then 'r' to hear music");    // Last  lines should fit all on one line! } 



Elementary Game Programming and Simulators Using Jamagic
Elementary Game Programming & Simulations Using Jamagic (Charles River Media Game Development)
ISBN: 1584502614
EAN: 2147483647
Year: 2002
Pages: 105
Authors: Sergio Perez

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net