<< Chapter < Page Chapter >> Page >

Get a reference to the Input object

Listing 1 begins by getting a saving a reference to the Input object that is associated with the GameContainer object. All user input can then be obtained by calling methods on the reference to the Input object.

Test for right or left movement

Then Listing 1 uses a logical inclusive or operator to determine if either the right arrow key or the right mouse button (or both) is currently in the pressed (or down) state. In other words, is the user holding the right arrow key or the right mouse button down?

If the test returns true, the value of bugX is increased. This will cause the visual manifestation of the sprite to move to the rightlater when the render method is executed.

Then Listing 1 performs a similar test on the left arrow key and the left mouse button , decreasing the value of bugX if either test returns true.

Test for up or down movement

Listing 2 performs similar tests on the up arrow key and the down arrow key for the purpose of increasing or decreasing the value of bugY . If the value of bugY is changed, this will cause the sprite to move up or down later when the render method is executed.

Listing 2 . Test for up or down movement.
//Control vertical bug position by pressing the arrow // keys. Vertical bug position cannot be controlled by// pressing mouse buttons. if(input.isKeyDown(Input.KEY_UP)){bugY -= yStep; }//end ifif(input.isKeyDown(Input.KEY_DOWN)){bugY += yStep; }//end if

No up button or down button

There is no up button and no down button on the mouse, so in this program it is not possible to move the sprite up or down by pressingmouse buttons. There are ways that such a thing could be accomplished (such as holding down a keyboard key and pressing a mouse button) , but they were not considered important for the purpose of this module.

Test for collisions with the edges

The code in Listing 3 is similar to, but simpler than the corresponding code in the earlier program named Slick0150a .

In this case, if the sprite collides with an edge, it simply stops moving instead of bouncing off the edge as was the case in the earlier program.

Listing 3 . Test for collisions with the edges.
//Test for collisions with the sides of the game // window and stop moving the bug when a collision// occurs. if(bugX + bugWidth>= backgroundWidth){ //Set the position to the right edge less the width// of the sprite. bugX = backgroundWidth - bugWidth;}//end if//Continue testing for collisions with the edges. if(bugX<= 0){ bugX = 0;}//end ifif(bugY + bugHeight>= backgroundHeight){ bugY = backgroundHeight - bugHeight;}//end ifif(bugY<= 0){ bugY = 0;}//end if

Get and save mouse coordinates

Listing 4 calls the getMouseX and getMouseY methods to get and save the coordinates of the mouse pointer when the mouse pointer is inside the game window. These values will bedisplayed later when the render method is executed as shown in Figure 1 .

Listing 4 . Get and save mouse coordinates.
//Get and save the X and Y coordinates of the mouse // pointer.mouseX = input.getMouseX(); mouseY = input.getMouseY();}//end update

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask