O'Reilly logo

Beginning 3D Game Programming by Tom Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Controlling Player Movement

Speaking of having the player move, you should implement this code as well. Considering that sometimes the player cannot move in a specific direction, you want to first check whether the move the player wants to make is valid and then do the move if necessary. Add the methods in Listing 8.4 to your Level class to handle these cases.

Listing 8.4. Controlling Player Movement
 /// <summary> /// Can the player move horizontally /// </summary> /// <param name="right">Is the player moving right</param> /// <returns>true if the move can be made; false otherwise</returns> private bool CanPlayerMoveHorizontal(bool right) { if ( ((playerIndex % SquareSize) == 0) && (!right)) { return false; } else if ( ((playerIndex % SquareSize) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required