<< Chapter < Page Chapter >> Page >

A 3D display

As mentioned earlier, this game is fully three dimensional. The prey fish and the predator are free to swim in any direction in 3D space. The tails on theprey fish and the predator appear longest when they are swimming parallel to the x-y plane. As they change their angle relative to the x-y plane, the tailsappear to become shorter or longer and in some cases, they appear not to have a tail at all. The fish that appear to have no tails are either swimming directlytoward the viewer or swimming directly away from the viewer.

This can best be observed by clicking the Stop button just as the fish scatter during an attack and freezing the state of the fish in the 3D world asshown in Figure 1 . If you examine the image at that point, you are likely to see some fish with shorter tails than other fish. This is evident by some of theprey fish near the center of Figure 1 that appear to have no tails at all.

Enough talk, let's see some code

A complete listing of this program is provided in Listing 31 near the end of the module.

Portions of this program are very similar to programs that I have explained in earlier modules in this series. I won't repeat those explanations here.Rather, I will concentrate mostly on the code that is new and different in this module.

Abbreviated constructor for the GUI class

Listing 1 shows an abbreviated constructor for the GUI class. (Much of the code in the constructor has been explained before, and was deleted from Listing1 for brevity.)

Listing 1 . Abbreviated constructor for the GUI class in GM01test08.
GUI(){//constructor//Code deleted for brevity //Register this object as an action listener on all// three buttons. startButton.addActionListener(this);attackButton.addActionListener(this); stopButton.addActionListener(this);//Make the drawing color RED at startup.g2D.setColor(Color.RED); }//end constructor

If you examine Listing 31 , you will see that the GUI class implements the ActionListener interface. Therefore, an object of the GUI class can be registered as an action listener on a button. Listing 1 registers the object of the GUI class as a listener on all three of the buttons shown in Figure 6 .

Beginning of the actionPerformed method

The actionPerformed method that begins in Listing 2 is called whenever the player clicks any of the three buttons shown in Figure 6 .

Listing 2 . Beginning of the actionPerformed method in GM01test08.
public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("Start")&&!animate){ //Service the Start button.//Get several user input values.numberPoints = Integer.parseInt( numberPointsField.getText());if(drawPointsBox.getState()){ drawPoints = true;}else{ drawPoints = false;}//end elseif(drawVectorsBox.getState()){ drawVectors = true;}else{ drawVectors = false;}//end else //Initialize some working variables used in the// animation process. animate = true;baseTime = new Date().getTime(); killCount = 0;//Enable the Attack button.attackButton.setEnabled(true);//Initialize the text in the timer field. timer.setText("0 / 0");//Cause the run method belonging to the animation// thread object to be executed. new Animate().start();}//end if

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask