In this project we will cover the following topics:
Adding a menu
Switching case
In this program, we will add a pop-up menu to our game. This menu will allow the program user to select the level of difficulty of the game, give a brief explanation of how the game is played , give some information about the game author, and enable the user to turn the background music off. Figure 4.20 shows the end result.
The pop-up menu we will add will be activated when the user clicks anywhere on the screen, and we need to let the user know this. To make this happen, we will change the writing on the toolbar that creates the window as follows :
// New Main Window owindow = New Window("Spherelander-Please Click on Main Screen for Menu",640,480,Window.STANDARD);//all one line
The menu will consist of a main pop-up menu with the following categories: Quit, Instructions, About the Author, Music Off, and Level of Difficulty. When Level of Difficulty is selected, a smaller pop-up menu will appear with two categories: Easy and Difficult.
The following lines of code create the main menu and add the menu items:
// This line makes a new menu which we call 'mainmenu' mainmenu = New Menu(); //The next line creates an item called 'quitter,' which //will //be inserted in our menu. The item will read 'Quit'. quitter = mainmenu.InsertItem("Quit"); //The next line creates the second menu item, called // 'instruct.' //The item will read 'Instructions.' instruct = mainmenu.InsertItem("Instructions"); //Two more similar items to be inserted into the menu about = mainmenu.InsertItem("About the Author"); xmusic = mainmenu.InsertItem("Music Off"); //The next line will draw a line in-between the "Music Off" // selection and the entry that will come after it. mainmenu.InsertItemSeparator(); difmenu = New Menu(); hard = difmenu.InsertItem("Difficult"); easy = difmenu.InsertItem("Easy"); mainmenu.InsertItem(difmenu,"Select Difficulty"); // When mouse clicked, show menu. //The command 'OnMouseUp' tells Jamagic to perform the //function 'doshowmenu' whenever the mouse is clicked //anywhere // in window owindow (our window). owindow.OnMouseUp = doshowmenu; //When Main Menu item is selected, do Function domenu. The //Command 'OnSelectItem' instructs the computer to perform // 'domenu' if any menu item is clicked. mainmenu.OnSelectItem = domenu; // When submenu (difmenu) item is selected, do function // seldif difmenu.OnSelectItem = seldif; // We need to add music to this game so that we can // shut it off using the menu. Don't forget to import it // into the game before running the program. omusic = New Music("melody"); omusic.SetLoop(ON,3); omusic.Play();
All we need in order to finish are the function domenu (which is called when an item on the main menu is clicked), the function seldif (which is called when an item on the submenu is clicked), and the function doshowmenu (which makes the menu appear when the user clicks the screen).
Now we will introduce the switch case statement, which is used in the functions mentioned above. The switch case statement is a tool that can, in some cases, eliminate the need for an if statement and save some programming time. For example, you may recall that the program user may click the Quit button (called quitter in the code) or on Instructions (called instruct in the code) on the main menu. The switch statement tells the program what to do if either one of these is selected.
Take a look at the function domenu in the following code. You will see there are four different Case s, each of which is one of our possible selections, inside the brackets that are encompassed by Switch . Case quitter tells Jamagic to end the program; Case instruct and Case about tell Ja-magic to write a message on the screen; and Case xmusic tells the program to stop the music. The Break statement follows each case, and each has a colon (:) (rather than a semicolon (;) after it.
Function domenu(mainmenu,pos,id) { Switch(id) { Case quitter: End; Break; Case instruct: mytext8.SetText("You must press the 'Off' button before changing thrusters");//one line Break; Case about: mytext8.SetText("This software was written by S. Perez.");//one line Break; Case xmusic: omusic.Stop; Break; } }
Tip | You may notice that there is a set of parentheses with three items inside them in the code above. These represent information that the function needs to know. Normally, if a variable is declared ( initialized ) in your main program, the function will automatically know the value of the variable. However, if the variable is undeclared, then you must pass the variable to the function via the parentheses. |
The next function is seldif , and its job is to determine what is to be done when one of two cases ( easy or hard ) is selected from the submenu. This function has the same format as the preceding one:
Function seldif(difmenu,pos,id) { Switch(id) { Case easy: fuel = 200.; fuelleft = 200.; Break; Case hard: fuel = 100.; fuelleft = 100.; Break; } }
Finally, here is the function that shows the menu when the user clicks the screen:
Function doshowmenu { mainmenu.OpenPopupMenu(300,200); }
That s all the code that you need to add to your Sphere8 program in order to have a pop-up menu. Please consult the Jamagic documentation under Help if you wish to use a toolbar type of menu.
Here is the entire code for you to type in and run.
// // Menusample Game 8.5 Spherelander game. Add a menu // to 8th game with following choices: //(a) Select level of difficulty based on fuel amount. //(b) Learn how to play the game. //(c) Find out information about the game writer. //(d) Select whether background music is on or off. // New Main Window owindow = New Window("Spherelander - Please Click on Main Screen for Menu",640,480,Window.STANDARD);//one line owindow.Show(); owindow.SetAutoRefresh(OFF); // New World oworld = New World(); // Import and massage the 1st 3D object made with //MilkShape oworld.Load("mtnlow"); oflatland = oworld.GetObject(oworld.GetNObjects()1); oflatland.Scale(10); oflatland.SetPosition(0,0,3000); matflatland =New Material(oworld,GetRGB(100,200,0)); matflatland.SetMapped(ON); oflatland.SetGouraud(ON); oflatland.ReplaceMaterial(matflatland); oflatland.SetStatic(); // 2nd 3D object oworld.Load("plains"); mountain = oworld.GetObject(oworld.GetNObjects()1); mountain.Scale(30); mountain.SetPosition(400,0,0); matmount =New Material(oworld,GetRGB(100,200,0)); matmount.SetFlat(ON); mountain.SetGouraud(ON); mountain.ReplaceMaterial(matflatland); mountain.SetStatic(); //Initialize variables fuel = 200.; fuelleft = 200.; ftime = 0.0; fuelflag = 0; block = FALSE; clickcheck = TRUE; // New Cameras ocamera1 = New Camera(oworld,owindow); ocamera1.SetPosition(500,500,5000); // Instruments mytext6 = New StaticText(owindow,"INSTRUMENTS",130,0,170,20); mytext6.SetBackColor(GetRGB(0,0,0)); mytext6.SetColor(GetRGB(0,255,255)); fnt = New Font("ARIAL",20,Font.BOLD); mytext6.SetFont(fnt); mytext7 = New StaticText(owindow,"Remaining Fuel:",0,150,170,20);//one line mytext7.SetBackColor(GetRGB(0,0,0)); mytext7.SetColor(GetRGB(255,0,0)); mytext8 = New StaticText(owindow," ",100,350,400,100); mytext8.SetBackColor(GetRGB(0,100,230)); mytext8.SetColor(GetRGB(255,255,255)); mytext = New StaticText(owindow," ",0,30,110,20); mytext.SetBackColor(GetRGB(0,0,0)); mytext.SetColor(GetRGB(255,255,255)); mytext2 = New StaticText(owindow," ",0,60,110,20); mytext2.SetBackColor(GetRGB(0,0,0)); mytext2.SetColor(GetRGB(255,255,255)); mytext3 = New StaticText(owindow," ",0,90,110,20); mytext3.SetBackColor(GetRGB(0,0,0)); mytext3.SetColor(GetRGB(255,255,255)); mytext4 = New StaticText(owindow," ",0,120,140,20); mytext4.SetBackColor(GetRGB(0,0,0)); mytext4.SetColor(GetRGB(255,255,255)); mytext5 = New StaticText(owindow," ",0,170,220,20); mytext5.SetBackColor(GetRGB(0,0,0)); mytext5.SetColor(GetRGB(0,0,255)); //Buttons myButton=New Button(owindow,"Ahead",220,30,65,20); myButton.OnClick = Doahead; myButton2=New Button(owindow,"Off",220,60,65,20); myButton2.OnClick = Dostop; myButton3=New Button(owindow,"Back",220,90,65,20); myButton3.OnClick = Doback; myButton4=New Button(owindow,"Right",300,60,65,20); myButton4.OnClick = Doright; myButton5=New Button(owindow,"Left",135,60,65,20); myButton5.OnClick = Doleft; mybutton6 = New Button(owindow,"Thrust Up", 220,120,65,20);//one line mybutton6.OnClick = doup; // Create a sphere with Gouraud shading. mysphere = oworld.CreateSphere(50,50,50,20,20); mysphere.SetPosition(500,150,2000); omaterial = New Material(oworld,GetRGB(50,150,200), "lightblue"); omaterial.SetFlat(ON); mysphere.SetGouraud(ON); mysphere.ReplaceMaterial(omaterial); // Create a plane and rotate it so it's flat. myplane = oworld.CreatePlane(10000,10000); myplane.SetPosition(0,0,0); matp =New Material(oworld,GetRGB(0,100,255)); matp.SetFlat(ON); //myplane.SetGouraud(ON); myplane.ReplaceMaterial(matp); myplane.SetAngle(-Pi/2,0,0); myplane.SetStatic(); // Create a target plane and rotate it so it's flat. myplane2 = oworld.CreatePlane(50,50); myplane2.SetPosition(150,40,2974); matp2 =New Material(oworld,GetRGB(100,200,0)); matp2.SetFlat(ON); myplane2.SetGouraud(ON); myplane2.ReplaceMaterial(matp2); myplane2.SetAngle(-Pi/2,0,0); myplane2.SetStatic(); // Camera ocamera1.Follow(mysphere,2000); oworld.Optimize(ocamera1); //oworld.Optimize(ocamera2); //Set collision detection. oworld.OnCollide = docollision; mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_STOP); // Set gravity on. mysphere.SetGravity(0.002); //********* menu ************************** mainmenu = New Menu(); quitter = mainmenu.InsertItem("Quit"); instruct = mainmenu.InsertItem("Instructions"); about = mainmenu.InsertItem("About the author"); xmusic = mainmenu.InsertItem("Music Off"); mainmenu.InsertItemSeparator(); // Add another item to the main menu, but this one // makes a sub-menu pop-up. difmenu = New Menu(); hard = difmenu.InsertItem("Difficult"); easy = difmenu.InsertItem("Easy"); mainmenu.InsertItem(difmenu,"Select Difficulty"); // When mouse clicked, show menu owindow.OnMouseUp = doshowmenu; //When Main Menu item's selected, do Function mainmenu. mainmenu.OnSelectItem = domenu; // When submenu item's selected, do function seldif. difmenu.OnSelectItem = seldif; // Music omusic = New Music("melody"); omusic.SetLoop(ON,3); omusic.Play(); //Game loop While(1) { // Distance calculations // distx = (mysphere.GetX) + 150; mytext.SetText("X Distance: " + distx); disty = (mysphere.GetY) ; mytext2.SetText("Height: " + disty); distz = (mysphere.GetZ)2974 ; mytext3.SetText("Z Distance: " + distz); totaldist = Sqrt(distx*distx + disty*disty + distz*distz);//one line totalxz = Sqrt(distx*distx + distz*distz); mytext4.SetText("x-z distance "+ totalxz); // Refresh ocamera1.ActivateRender(); owindow.Refresh(); //Fuel calculations if(fuelflag == 1) { time2 = System.GetElapsedTime; fueltime = time2 - ftime; fuelleft = fuel - fueltime/1000.; } mytext7.SetText("Remaining Fuel Time: " + fuelleft); if(fuelleft<0) { // myshpere.Stop(); mytext5.SetText("FUEL GONE!!"); block = TRUE; } //End of game loop follows } Function docollision { if(totalxz <= 50) { mytext5.SetText("Congratulations! You landed on target!");//one line mysphere.Stop; } Else { mytext5.SetText("Back to flight school for you!");//one line mysphere.Stop; } } Function doahead { if (block == FALSE && clickcheck == TRUE) { ftime = System.GetElapsedTime; mysphere.Move(5000,50); fuelflag = 1; clickcheck = FALSE; } } Function doback { if (block == FALSE && clickcheck == TRUE) { mysphere.Move(5000,50); ftime = System.GetElapsedTime; fuelflag = 1; clickcheck = FALSE; } } Function doright { if (block == FALSE && clickcheck == TRUE) { mysphere.MoveRight(5000,50); ftime = System.GetElapsedTime; fuelflag = 1; clickcheck = FALSE; } } Function doleft { if (block == FALSE && clickcheck == TRUE) { mysphere.MoveLeft(5000,50); ftime = System.GetElapsedTime; fuelflag = 1; } } Function dostop { clickcheck = TRUE; mysphere.Stop(); fuelflag = 0; fuel = fuelleft; mysphere.SetGravity(0.002); } Function doup { if (block == FALSE && clickcheck == TRUE) { mysphere.SetGravity(0.002); ftime = System.GetElapsedTime; fuelflag = 1; clickcheck = FALSE; } } Function domenu(mainmenu,pos,id) { Switch(id) { Case quitter: End; Break; Case instruct: mytext8.SetText("You must press the 'OFF' button before changing thrusters");//one line Break; Case about: mytext8.SetText("This software was written by someone whose wife is very upset at him at this time!");// Break; Case xmusic: omusic.Stop; Break; } } Function doshowmenu { mainmenu.OpenPopupMenu(300,200); } Function seldif(difmenu,pos,id) { Switch(id) { Case easy: fuel = 200.; fuelleft = 200.; Break; Case hard: fuel = 100.; fuelleft = 100.; Break; } }