1 / 36

Getting Player Input Using a Gamepad

Session 3.1. Getting Player Input Using a Gamepad. Introduce the gamepad device Show how a C# object can be used to represent the state of a gamepad Find out how to use the properties of the GamePadState object in a program Create a Mood Light that is controlled by the gamepad

graceland
Download Presentation

Getting Player Input Using a Gamepad

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 3.1 Getting Player Input Using a Gamepad

  2. Introduce the gamepad device Show how a C# object can be used to represent the state of a gamepad Find out how to use the properties of the GamePadStateobject in a program Create a Mood Light that is controlled by the gamepad Discover how to use logical operators OR and AND Create our first XNA multiplayer game Chapter 3.1: Getting Player Input Using a Gamepad Session Overview

  3. We are going to make a Mood Light that is controlled by the user They will press the buttons on the gamepad to control the brightness and color of the screen background The user can select any color that they want This can also serve as the basis of a game To do this we need to know how XNA games get input from the player Chapter 3.1: Getting Player Input Using a Gamepad A User-Controlled Mood Light

  4. The gamepad is a very complex device It is connected to the host either by USB or a wireless connection It can be used with a Windows PC or an Xbox You can obtain a wireless adapter for the Windows PC Chapter 3.1: Getting Player Input Using a Gamepad The Xbox Gamepad

  5. The Zune buttons work in the same way as the buttons on a gamepad The same XNA program can work with a gamepad or a Zune However, the Zune doesn’t provide all the buttons that a gamepad does Chapter 3.1: Getting Player Input Using a Gamepad The Zune Buttons

  6. We know that C# programs are made up of objects that bring together data and behaviors • A game has Game World data and draw and update methods • The state of a gamepad can be represented by a C# object called a GamePadState object • This contains the information about the state of the gamepad at a particular instant • The GamePadState object also provides properties you can use to read this status information Chapter 3.1: Getting Player Input Using a Gamepad Software and Objects

  7. A GamePadState object contains information about all the input devices the gamepad has We are going to consider just the buttons and the DPad at the moment When the Red button (B) is pressed we want the redIntensity value of the background color to increase Chapter 3.1: Getting Player Input Using a Gamepad The GamePadState object Buttons DPad GamePadState

  8. The game will use a variable to hold the state of the gamepad It will then test the values of the buttons in this variable so that the gamepad can be used to control the game The variable must be declared like any other variable in the game program It has been given the identifier pad1 Chapter 3.1: Getting Player Input Using a Gamepad Creating a GamePadState Variable GamePadState pad1;

  9. Each time that update is called, the pad1 variable is set with the state of the gamepad for player 1 • Code in the update method will use this variable • Should the pad1 variable be a local variable or a member of the Game1 class? • Member variables are shared between all the methods in a class • Local variables are just used inside the body of a method and discarded when no longer required Chapter 3.1: Getting Player Input Using a Gamepad The pad1 Variable

  10. The variable pad1 should be local to the update method We don’t want to retain the value of gamepad states from previous calls of update No other methods need to use the value of pad1 Deciding which variables should be local and which should be members is part of the program design process Chapter 3.1: Getting Player Input Using a Gamepad Using Local Variables

  11. We can set the value of pad1 when we declare it The XNA Framework provides a class called GamePad that exposes agetStatemethod The getState method is told which gamepad to get the state of We don’t need to know how getState works, just how to call it and what the type of the result is Chapter 3.1: Getting Player Input Using a Gamepad Setting the pad1 Variable GamePadState pad1 = GamePad.GetState (PlayerIndex.One) ;

  12. A property is member of a class that represents some data in the class They can be set up to be read or written, or both The GamePadState properties can only be read Each of the properties can contain several values Chapter 3.1: Getting Player Input Using a Gamepad GamePadState Properties Buttons DPad GamePadState

  13. This condition tests the state of the B (Red) button If the button is Pressed the condition adds 1 to the value of redIntensity If the button is held down successive calls of update will cause the redIntensity to increase The equality operator is used because we are comparing the state of Buttons.B with the value ButtonState.Pressed Chapter 3.1: Getting Player Input Using a Gamepad Game Update Behavior if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;

  14. This is the code that allows a game to control the red intensity using button B on a gamepad • Create a local variable called pad1 that contains the state of the gamepad for player 1 • Get the state of button B out of the Buttons in the pad, and if the button is pressed, increase the value of redIntensity. Chapter 3.1: Getting Player Input Using a Gamepad Game Update Behavior GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;

  15. Chapter 3.1: Getting Player Input Using a Gamepad 1. User-Controlled Red Light • This version of Mood Light uses the code above to control the redIntensityvalue • When the Red button is pressed the screen redIntensity value gets larger

  16. The same code construction can be used to control the green and blue intensity values Note that we don’t need to get a new GamePadState value each time, the program can just read different properties from the same pad1 value Chapter 3.1: Getting Player Input Using a Gamepad Controlling Green and Blue GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++; if (pad1.Buttons.A == ButtonState.Pressed) greenIntensity++; if (pad1.Buttons.X == ButtonState.Pressed) blueIntensity++;

  17. The gamepad has a Yellow button (Y) We can increase the yellow intensity by increasing both red and green when this button is pressed To achieve this, the two update statements must be placed in a block after the condition Chapter 3.1: Getting Player Input Using a Gamepad Controlling Yellow Intensity Using the Y Button if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++; }

  18. Chapter 3.1: Getting Player Input Using a Gamepad 2. User-Controlled Light • This version of Mood Light uses all the gamepad buttons to control the background color

  19. The Zune does not have all of the buttons on the gamepad However it does have a DPad which can be pressed in one of four directions We could make a “Mood Flashlight” by using the DPad to control the colors Then the program would work on the Zune as well Chapter 3.1: Getting Player Input Using a Gamepad Adding Zune Support Using the DPad

  20. The GamePadState type contains a DPad property which gives the states of the Left, Right, Up, and Down positions of the DPad These are used in exactly the same way as the colored buttons on the gamepad If any button state is equal to the value ButtonState.Pressedit means that the direction is selected on the gamepad Chapter 3.1: Getting Player Input Using a Gamepad Controlling Red Using the DPad.Right Value if (pad1.DPad.Right == ButtonState.Pressed) redIntensity++;

  21. We want to make the red light brighter if button B (Red) is pressed or Right is pressed on the DPad We can do this by having two separate if statements However, it would be neater to be able to use a single condition: “If Button B is pressed ORDPad Right is pressed increase the value of redIntensity” C# provides a logical operator to allow this We must use the logical OR operator Chapter 3.1: Getting Player Input Using a Gamepad Combining Tests Using Logical OR

  22. The logical OR operator is two vertical bars - || It works between Boolean values (i.e. true or false) If either of the values on each side are true, the result it gives is true The above code increases redIntensity if either button is pressed Chapter 3.1: Getting Player Input Using a Gamepad The Logical OR operator if (pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed) { redIntensity++; }

  23. Logical OR works between any two conditions that return a Boolean result • We can use it between the values true and false • Although this might result in compiler warnings • Which of the above statements would increase the value ofredIntensity? Chapter 3.1: Getting Player Input Using a Gamepad Logical OR Test if (true || false ) redIntensity++; if (true || true ) redIntensity++; if (false|| false ) redIntensity++;

  24. The OR operator returns true if either of the conditions on each side of the operator is true Therefore it only returns false if both the conditions are false Chapter 3.1: Getting Player Input Using a Gamepad Logical OR Test if (true || false ) redIntensity++; // increases if (true || true ) redIntensity++; // increases if (false || false ) redIntensity++; // doesn’t increase

  25. There is also a logical AND operator • We could use this to make the program reset all the colors to 0 if both triggers are pressed on the gamepad • This makes it harder to reset the colors by mistake • The logical AND condition only works out to true if the conditions on both sides are true • The logical AND operator is the character sequence && Chapter 3.1: Getting Player Input Using a Gamepad Logical AND

  26. The program can test the values of the shoulder buttons on the controller If both of them are pressed the block of code is performed This resets the intensity values to 0 Chapter 3.1: Getting Player Input Using a Gamepad Logical AND Value Reset if (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; }

  27. Chapter 3.1: Getting Player Input Using a Gamepad 3. Fully Functional Light • This version of the Mood Light will work on Zune, Windows PC or Xbox • It also provides the reset behavior with the shoulder buttons

  28. You can use this program as the basis of a game: • Players take turns to press any buttons they like on the gamepad (but they must change the color on the screen) • If the screen flashes from bright to dark (i.e. an intensity value wraps around) during a turn that player is out • The game continues until one player is left to become the winner • Press the shoulder buttons to reset for the next game Chapter 3.1: Getting Player Input Using a Gamepad Game Idea – Color Nerve

  29. The XNA Framework uses the type GamePadStateto represent the state of a gamepad in a game The GamePad class, which is part of XNA, provides a getState method that can be used to get the GamePadStatevalue for the selected player The GamePadStatetype exposes properties that can be read to get the state of the gamepad These states can be combined using logical operators to allow more complicated behaviors to be created in a program Chapter 3.1: Getting Player Input Using a Gamepad Summary

  30. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  31. An XNA program can control the state of the buttons on a gamepad. The GamePadState type holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  32. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  33. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  34. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by||in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  35. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

  36. An XNA program can control the state of the buttons on a gamepad. The GamePadStatetype holds information about the state of a gamepad. Objects can expose information by means of properties. An XNA program can only connect to one gamepad. The Zune has a full set of colored buttons. The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad True/False Revision Quiz

More Related