Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and.

Similar presentations


Presentation on theme: "CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and."— Presentation transcript:

1 CHAPTER 6 Multiplayer XNA Game Studio 4.0

2 Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and debug a complex program. Write one of the only 16-player games for the Xbox in the world.

3 Creating the Button-Bash Game This project needs to be able to display text. Button-Bash Game Data track of the number of times the button has been pressed. // Game World int count; The int variable in C# can go over 2,000,000,000.

4 Starting the Button-Bash Game The game is started by the player pressing the Start button on the gamepad to zero the counter. The program handles this in the Update method protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.Start == ButtonState.Pressed) { count = 0; } base.Update(gameTime); }

5 Displaying the Button-Bash Count Value Display the current number of presses on the screen for the player to see protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); string countString = count.ToString(); Vector2 countVector = new Vector2(50, 400); SpriteBatch.Begin(); spriteBatch.DrawString(font, countString, countVector, Color.Red); spriteBatch.End(); base.Draw(gameTime); }

6 Counting Button Presses Update method if (pad1.Buttons.B == ButtonState.Pressed) { count++; } This won’t work – Why? Update method is called 60 times a second. If you hold down the button, you find that each time Update is called, the value of count gets one bigger, so the score goes up at a rate of 60 times a second.

7 Detecting Changes in the Button Position The Update method is being called at regular intervals. At some point, the B button is pressed. This means that when Update is called the first time in the illustration, it detects that B is up, and the second time it is called, it detects that B has been pressed.

8 Detecting Changes in the Button Position The Update method needs to know the state of the button the last time Update was called. It can then test to see if the button state has changed since it was called the last time. You can declare a GamePadState variable to hold this value and create an Update method

9 GamePadState oldpad1; // holds the previous state of the gamepad protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.Start == ButtonState.Pressed) { count = 0; oldpad1 = pad1; } if (oldpad1.Buttons.B == ButtonState.Released && pad1.Buttons.B == ButtonState.Pressed) { count++; } oldpad1 = pad1; //store the current pad state base.Update(gameTime); }

10 Level and Edge Detectors The code in the previous section is an edge detector in that it detects a change from one state to another. Use to detect when a game player selects an option or presses a switch.

11 CONSTRUCTING THE COMPLETE GAME XNA Game Studio 4.0 Learn Programming Now

12 Update Method // Gamepad 1 GamePadState pad1;//hold gamepad state GamePadState oldpad1; //hold gamepad state int acount1, int bcount1, int xcount1, int ycount1; Vector2 apos1 = new Vector2(150, 250); Vector2 bpos1 = new Vector2(200, 200); Vector2 xpos1 = new Vector2(100, 200); Vector2 ypos1 = new Vector2(150, 150); pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.IsConnected) { if (pad1.Buttons.Start == ButtonState.Pressed) { acount1 = 0; bcount1 = 0; xcount1 = 0; ycount1 = 0; // repeat for the other three gamepads } if (oldpad1.Buttons.A == ButtonState.Released && pad1.Buttons.A == ButtonState.Pressed) { acount1++; } if (oldpad1.Buttons.B == ButtonState.Released && pad1.Buttons.B == ButtonState.Pressed) { bcount1++; } if (oldpad1.Buttons.X == ButtonState.Released && pad1.Buttons.X == ButtonState.Pressed) { xcount1++; } if (oldpad1.Buttons.Y == ButtonState.Released && pad1.Buttons.Y == ButtonState.Pressed) { ycount1++; } oldpad1 = pad1; }

13 Draw method spriteBatch.Begin(); if (pad1.IsConnected) { spriteBatch.DrawString(font, acount1.ToString(), apos1, Color.Green); spriteBatch.DrawString(font, bcount1.ToString(), bpos1, Color.Red); spriteBatch.DrawString(font, xcount1.ToString(), xpos1, Color.Blue); spriteBatch.DrawString(font, ycount1.ToString(), ypos1, Color.Yellow); } spriteBatch.End();

14 Adding Test Code To test all of the gamepads if there is only one, copy the state of gamepad1 into the other gamepads during the Update method. pad2 = GamePad.GetState(PlayerIndex.Two); // test code – copy the value of pad1 into pad2 pad2 = pad1; if (pad2.IsConnected) { // code for gamepad 2 }

15 CONDITIONAL COMPILATION XNA Game Studio 4.0 Learn Programming Now

16 Conditional Compilation Conditional compilation lets you ask the compiler to ignore parts of a program. This provides a way that you can mark statements of program code that are to be ignored and that do not become part of the program when it is built, but that you can keep around for testing when you later update your code. The part at the very front of the compiler that reads in the C# file is called the preprocessor. The C# compiler preprocessor can also be told to do things to the source that it sees.

17 Conditional Compilation Commands to the preprocessor have a # at the start of the line and are called directives. #if test // test code - copy the value of pad1 into pad4 pad4 = pad1; #endif What the previous statements say to the preprocessor is, “If the test symbol has been defined, pass on the following statements to the rest of the compiler; otherwise, ignore them.” The statements to be passed on are between the #if and the #endif directives.

18 Conditional Compilation If you want to switch these lines on, you simply need to define the test symbol at the top of the source file as follows: #define test If the test symbol has been defined, all the test statements are compiled into the main program. Deleting the #define directive keeps the designated statements from being compiled.


Download ppt "CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and."

Similar presentations


Ads by Google