Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating a Multi-Player Game

Similar presentations


Presentation on theme: "Creating a Multi-Player Game"— Presentation transcript:

1 Creating a Multi-Player Game
Session 6.1 Creating a Multi-Player Game On completion of this lesson, students will be able to: Introduce the use of debugging to detect and remedy problems in algorithm design. Appreciate the difference between level-triggered inputs and event-triggered inputs. Understand how the Draw and Update behaviors of XNA make it necessary for games to maintain state if they are to detect events. Appreciate the importance of algorithms in program design and the way that errors can be introduced into programs if the algorithms are faulty.

2 Chapter 6.1: Creating a Multi Player Game
Session Overview Show how we get started creating a playable game Find out what an algorithm is and how to test and improve a broken one Find out how a game program can detect changes in the state of buttons on the gamepad Create a program that can count events to create a simple “Button Bashing” game This session introduces slightly more complex game behavior, building on the abilities that the students now have. It also shows how very simple game software can create marketable games (everyone will have played a game where the object is to press a button or waggle a joystick as rapidly as possible). The game that is introduced is very simple in concept, but requires the game to detect changes in state rather than just poll values. An initial algorithm is introduced which is found to be insufficient, and investigation is used to derive a correct approach. To do this in the context of XNA, a program needs to be able to detect changes in state of a signal, rather than just detect levels. How this is done is explored in the context of making a game state aware.

3 Chapter 6.1: Creating a Multi Player Game
Creating a Game We now know how XNA games are structured and how to add C# code into methods in an XNA game to control game play We can read user input using the gamepad We can display text messages on the screen under the control of our program Now we are going to create our first “proper” game There are a number of commercial games based on this game play idea Make the point that we now know enough to be able to create our own versions of some classic games. The game we are going to make first is based on a classic game play mechanic, which involves counting how many times the player can press a button in a given interval.

4 Game Idea: “Button Bash”
Chapter 6.1: Creating a Multi Player Game Game Idea: “Button Bash” The idea of this game is to see how many times the player can press a button in ten seconds There were a great many “sport simulation” games that worked like this Many games have this kind of game play as a “mini game” today Ask the class if they have ever played one of these “button bashing” games. Ask if they are any good at them? Tell them that we are going to create our own version of the game and see how such a game works internally.

5 Chapter 6.1: Creating a Multi Player Game
Game Design The game must increase a counter each time one of the buttons on a gamepad is pressed and released This means that it will need a counter variable that holds the current count value Each time the Update method detects a button press, the count value will be increased The Draw method will draw the current count value The final version of the game will have a counter for each button on the gamepad, so the game can support multi-player action This sets out the requirements for the game. From a behavior point of view, the game must contain a variable that is used to count the button presses. This will be updated by the Update method and displayed by the display method.

6 Getting Started with a Single-Player Version
Chapter 6.1: Creating a Multi Player Game Getting Started with a Single-Player Version We are going to start by making a single-player version of the game We can expand this to support multiple players per gamepad and even multiple gamepads The game will eventually support up to 16 players Starting with a single player keeps things simple This is an important programming point. Rather than start with the most complex situation, start with the simplest and add complication later. Once we have made the game work for one player we can easily expand it to work with the additional ones.

7 Game World for Our Button Bash Test
Chapter 6.1: Creating a Multi Player Game Game World for Our Button Bash Test // Game World SpriteFont font; int count; The Game World just contains two items: The variable count holds the number of presses The variable font is used to draw text The count variable is of type int and can hold values over 4,000,000,000 This should be sufficient for most players This is the Game World that we will be using. The font variable is something we have seen before. Make the point that this will be set up in the LoadContent method (ask the class to tell you where if you like). The count variable will count the number of presses that the player has performed on the required button. It is of type int, giving 32 bits of storage. Ask the class if this counts as overkill. It probably does (it is unlikely that even Superman could press the button this many times) but make the point that we are only storing one of these values, and that there is really plenty of memory available. Generally speaking, unless there are really good reasons, it is advisable to use int as this can be transferred most efficiently between processor and memory.

8 Clearing the Count at the Start of the game
Chapter 6.1: Creating a Multi Player Game Clearing the Count at the Start of the game GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.Start == ButtonState.Pressed) { count = 0; } At the start of the game the value in count needs to be set to 0 We can use the Start button on the gamepad When the Start button is pressed the value in count is set to 0 To start the game the count must be set to 0. This code will use the Start button on gamepad 1 to clear the counter back to 0. This is nothing special, it should make perfect sense to the class. We have seen very similar things when updating color intensity value, but this method just sets the value of count to 0.

9 Displaying the Count in the Draw Method
Chapter 6.1: Creating a Multi Player Game Displaying the Count in the Draw Method string countString = count.ToString(); Vector2 countVector = new Vector2(50, 400); spriteBatch.Begin(); spriteBatch.DrawString(font, countString, countVector, Color.Red); spriteBatch.End(); The Draw method is very similar to the one that drew the time in our clock, however this time it is drawing the contents of the count variable. Make the point that we have seen things like ToString before (ask where?). The ToString method is very similar to the string methods provided by the values of the DateTime type, which provide a number of string methods that describe the time value they hold. The Draw method contains the code that draws the current count value on the screen It uses the ToString method provided by the count variable to get a string version of the count

10 Counting Button Presses in the Update Method
Chapter 6.1: Creating a Multi Player Game Counting Button Presses in the Update Method if (pad1.Buttons.B == ButtonState.Pressed) { count++; } The Update method is where the state of the game should be updated If the button has been pressed the counter should be increased by one This code looks like it might do the trick Make the point that our game is adhering to the proper XNA design guidelines. We have a Game World of data that is used by the game to manage game state. The Draw method uses this Game World data to draw the Game World for the player to see. The Update method performs the required updating of the Game World.

11 Chapter 6.1: Creating a Multi Player Game
1. Broken Button Bash The code that has been discussed looks like it should work But it does not work properly Instructor should do the following: Note: You will need a gamepad for this demonstration. Start Microsoft Visual Studio 2008. Open the XNA Game Solution called ButtonBash in the demo directory Broken ButtonBash. This program implements the Button Bash game with the code that has previously been discussed. Show each of the elements and get the class to agree that they are all in the right places. Run the program and press button B on the gamepad. Note that the value counts up rapidly until the button is released. Ask the class what is going on. Press the Start button on the gamepad and show that it seems to work perfectly, clearing the value to 0. Tell the class that you know that if you hold down the B button for 5 seconds the number would count up to 300 Ask them how you know that. (The Update method is called 60 times a second. If the B button is held down this means that the count value will be increased by 1 each time Update is called). This means that the number will go up by 60 every second, or 300 every five seconds.

12 Chapter 6.1: Creating a Multi Player Game
Finding the Fault We know that the Update method is called 60 times a second Each time the method is called it will test the B button on the gamepad and increase the counter if the button is down This is not what we want We want the counter to increase when the button changes state from up to down, and not just because the button is held down There is a fine distinction here, once everyone understands how this works they will have a good grasp of how the Update method really works. Remind the class that we found that the counter was increasing at a rate of 60 Hz, which is the speed the Update method is being called. But we don’t want to increase the counter just because the button is being held down, we want to detect that edge.

13 Chapter 6.1: Creating a Multi Player Game
Signals and Edges The counter should not increase when the button is held down It must increase when the button is pressed The program must detect this edge All keyboards work like this Make the point that keyboards work in the way we want our game to work. They don’t detect that a key is held down, they detect the event of the key being pressed. Try to avoid a discussion about auto repeat, this happens when the key has been held down for a particular time. (Actually we can discuss how to make a gamepad button auto-repeat later on if you wish, but for now you are trying to get a distinction between a level-triggered signal and one where the program is detecting an edge.)

14 Chapter 6.1: Creating a Multi Player Game
Detecting an Edge Talk the class through the sequence above. Make the point that time is running left to right. The first time Update is called the button is up. The second time Update is called the button is pressed. Ask the class how the program could be made to “notice” this. With a bit of luck someone will suggest that the program be made to “remember” the previous state of the gamepad so that the change can be detected. The second time that Update is called the button state has changed from up to down The game must detect this change

15 Game World for Our Button Bash Test
Chapter 6.1: Creating a Multi Player Game Game World for Our Button Bash Test // Game World SpriteFont font; int count; GamePadState oldpad1; The Game World now contains an extra variable This records the previous state of the gamepad It allows a game to compare the current state of a key with the previous state Code in the Update method can test for a change in the state of the button Make the point that a program can only be made to remember things if we write the code to do the remembering. This means that to remember the state of the gamepad the program needs a variable that will hold this state. We can add a new variable to hold the state of the gamepad last time that Update was called. The variable is called oldpad1.

16 An Edge Detection Algorithm
Chapter 6.1: Creating a Multi Player Game An Edge Detection Algorithm An algorithm is a way of solving a problem The edge detection algorithm is: "If the previous state of the button is up and the current state of the button is down the counter must be increased" We need to convert this algorithm into C# and add it to the Update method Take the class through this orally. Make sure that everyone understands what is going on.

17 Chapter 6.1: Creating a Multi Player Game
Correct Counting Code if ( oldpad1.Buttons.B == ButtonState.Released && pad1.Buttons.B == ButtonState.Pressed ) { count++; } oldpad1 = pad1; // remember the state for next time This code implements the algorithm It will increase the value of count only if the button has changed state from Released to Pressed Note that it also records the old state for the next call of Update Two important things to emphasize here: The test in the condition is a direct translation of the English description of the algorithm we saw on the previous slide. The program must store the state of the gamepad for next time Update is called. Ask the class what would happen if we forgot to do this. (The game would count the first button press but not detect any others.) Remind the class of the behavior of the && operator, and how this is used to combine two conditions and return true if both are true.

18 Chapter 6.1: Creating a Multi Player Game
2. Working Button Bash This version of the program uses the improved button detector It counts button presses correctly Instructor should do the following: Note: You will need a gamepad for this demonstration. Start Visual Studio 2008. Open the XNA Game Solution called ButtonBash in the demo directory Working ButtonBash. This program implements the Button Bash game with the corrected version of the algorithm. Run the program and show that it works.

19 Chapter 6.1: Creating a Multi Player Game
Faults and Algorithms The first version of the game didn’t work because the algorithm was faulty Our understanding of how to solve the problem was not correct This is the worst kind of programming error Often a program will work fine until it is given values that it wasn’t designed to work with: The age 1,000,000 in a pension program The price of -50 in a discount calculator We have not introduced any new programming constructions in this section, but we have underpinned knowledge about the way that the XNA game works. We have also introduced a new level of error. Initially we were concerned with compilation errors that stop the program running. Then we moved on to runtime errors, where mistakes in the code make the program behave incorrectly. This third level is an algorithmic error, where our understanding of what is needed to solve the problem is incorrect. Make the point that the worst programming errors are where the algorithm is faulty. Often this does not mean that the program fails, but that with certain inputs it goes wrong. For example, a discount program will work fine until someone puts in a negative price (or discount). When a game goes wrong this is not usually a huge problem (although it will affect game sales). However, computer programs do lots of very important things today, and it is important that they work correctly.

20 Chapter 6.1: Creating a Multi Player Game
Summary The first idea you have to implement a game might not be one that works A description of a solution to a problem is called an algorithm Some inputs to a program will be levels (high or low) and others will be events (change in state) For an XNA game to detect changes in state it must contain Game World data to retain the previous state of the game Make it clear that the job of programming is not just writing programs. This process needs to be placed in the context of general problem solving and producing solutions of good quality. This is our first introduction to algorithms as a higher-level abstraction when creating solutions. Make the point that we now know about two types of signal, level and event (or edge). Ask the class to suggest level- and edge-triggered events in a game: (Driving game: accelerator is level and the gear change is edge. Shooting game: movement buttons are level, but gun is edge.) Remind the class that the only way to detect an edge is if the program can compare the current state with the previous one.

21 True/False Revision Quiz
Chapter 6.1: Creating a Multi Player Game True/False Revision Quiz An algorithm must be written in C#. To detect the level of a signal, a program must retain the previous signal value. An XNA program can only detect edges on a single gamepad button. The Update method in an XNA game is called 60 times a second.

22 True/False Revision Quiz
Chapter 6.1: Creating a Multi Player Game True/False Revision Quiz An algorithm must be written in C#. To detect the level of a signal, a program must retain the previous signal value. An XNA program can only detect edges on a single gamepad button. The Update method in an XNA game is called 60 times a second. False. You can express an algorithm in any language you like. Initially it would probably be written in English.

23 True/False Revision Quiz
Chapter 6.1: Creating a Multi Player Game True/False Revision Quiz An algorithm must be written in C#. To detect the level of a signal, a program must retain the previous signal value. An XNA program can only detect edges on a single gamepad button. The Update method in an XNA game is called 60 times a second. False. The level of a signal can be read directly. To detect an edge, the game must retain the previous signal value so that it can compare it with the current one.

24 True/False Revision Quiz
Chapter 6.1: Creating a Multi Player Game True/False Revision Quiz An algorithm must be written in C# To detect the level of a signal, a program must retain the previous signal value An XNA program can only detect edges on a single gamepad button. The Update method in an XNA game is called 60 times a second. False. The program can compare the state of as many buttons as required.

25 True/False Revision Quiz
Chapter 6.1: Creating a Multi Player Game True/False Revision Quiz An algorithm must be written in C#. To detect the level of a signal, a program must retain the previous signal value. An XNA program can only detect edges on a single gamepad button. The Update method in an XNA game is called 60 times a second. True. If we want to detect changes in state the program must remember the previous state and then compare the previous value with the new one.


Download ppt "Creating a Multi-Player Game"

Similar presentations


Ads by Google