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.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

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.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Games and Simulations O-O Programming in Java The Walker School
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
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.
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.
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.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Developing the Game User Interface (UI) Lesson 5.
1 Useful Tools for Making Video Games Part V An overview of.
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.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU.
Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency.
CHAPTER 10 Using C# Methods to Solve Problem XNA Game Studio 4.0.
CS PC/CONSOLE GAME PROGRAMMING/DEVELOPMENT CHAPTER 2: DRAWING, DATA AND CONTROLS.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
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.
Variables and Inheritance Part 1
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Rob Miles. How does an XNA game program work? Programs tell computers what to do A program is written in a programming language – C# is a programming.
11 Getting Player Input Using a Gamepad Session 3.1.
CIS 3.5 Lecture 2.2 More programming with "Processing"
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
Aaron Yuen 1 Outline 9.1Storyboarding & Charting Method for Game Design 9.2Game State in Game Development 9.3Game State Management 9.4GUI Assets for Gameplay.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
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.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
XNA ● Proprietary Microsoft framework ● C#. Interface.
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.
Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.
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.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.
11 Adding Vibration Effects Session Session Overview  Describe how the vibration feature of the gamepad works  Show how an XNA program can control.
XNA Sound Effects. float volume, pitch, pan; SoundEffect boing1; SoundEffect boing2; boing1 = Content.Load ("boing1"); boing2 = Content.Load ("boing2");
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
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.
Chapter 4 - Finishing the Crab Game
Lecture 4 Using SpriteFonts
Using and Creating Sprites
PYGAME.
Creating a Multi-Player Game
Microsoft Access Illustrated
Starter Write a program that asks the user if it is raining today.
Introduction to the C Language
Collision Detection.
Side Scrolling Game Development.
2.3. Architectural Design I
MTA-4201 Game Programming Chapter 8 : Scrolling Background & Font
Collisions with Static Objects
Presentation transcript:

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 debug a complex program. Write one of the only 16-player games for the Xbox in the world.

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.

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); }

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); }

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.

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.

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

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); }

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.

CONSTRUCTING THE COMPLETE GAME XNA Game Studio 4.0 Learn Programming Now

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; }

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();

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 }

CONDITIONAL COMPILATION XNA Game Studio 4.0 Learn Programming Now

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.

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.

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.