Creating a Multi-Player Game

Slides:



Advertisements
Similar presentations
11 Reaction Timer Game Session 8.1. Session Overview  Find out how an XNA program can measure the passage of time and trigger events at certain points.
Advertisements

Computer Science 1620 Programming & Problem Solving.
Introduction to a Programming Environment
Chapter 1 Program Design
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Bug Session Two. Session description In this session the use of algorithms is reinforced to help pupils plan out what they will need to program on their.
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
COMPUTER PROGRAMMING 2 Timers. Game Idea: Mob Reaction Timer Use a timer variable to keep track of time and a variable for each player to measure the.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Invitation to Computer Science, Java Version, Second Edition.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
11 Getting Player Input Using a Gamepad Session 3.1.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
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.
Intermediate 2 Computing Unit 2 - Software Development.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
11 Computers, C#, XNA, and You Session 1.1. Session Overview  Find out what computers are all about ...and what makes a great programmer  Discover.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
11 Adding Vibration Effects Session Session Overview  Describe how the vibration feature of the gamepad works  Show how an XNA program can control.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
FOP: Multi-Screen Apps
Collision Theory and Logic
Intro CS – Screens and Variables
Collision Theory and Logic
Introduction To Repetition The for loop
Topics Introduction to Repetition Structures
10.3 Details of Recursion.
IF statements.
CS1371 Introduction to Computing for Engineers
The Need for Programming Languages
Topics Introduction to Repetition Structures
Algorithm and Ambiguity
Introduction to Events
Starter Write a program that asks the user if it is raining today.
CS 240 – Lecture 11 Pseudocode.
Programming – Touch Sensors
Computer Programming.
Print slides for students reference
Introduction to Computer Programming
Lesson 15: Processing Arrays
Programming Fundamentals (750113) Ch1. Problem Solving
An Introduction to VEX IQ Programming with Modkit
Java Programming Arrays
HAPPY NEW YEAR! Lesson 7: If-statements unplugged
CSCE 489- Problem Solving Programming Strategies Spring 2018
Computational Thinking for KS3
Programming.
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Topics Introduction to Repetition Structures
Basic Concepts of Algorithm
ECE 352 Digital System Fundamentals
EGR 2131 Unit 12 Synchronous Sequential Circuits
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
ECE 352 Digital System Fundamentals
CMSC 202 Exceptions.
Presentation transcript:

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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 6.1.1 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.

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.

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.)

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

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.

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.

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.

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 6.1.2 Working ButtonBash. This program implements the Button Bash game with the corrected version of the algorithm. Run the program and show that it works.

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.

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.

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/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.

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.

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.

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.