11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.

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

Dan Waters, Academic Relations Manager, Microsoft.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
3.3. G AME I NPUT Handling input within games. In lecture exploration of answers to frequently asked student questions.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Input.
Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead February 12, 2008 Creative Commons Attribution.
Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead January 21, 2009 Creative Commons Attribution.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
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.
CHAPTER 6 Loop Structures.
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.
1 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and Custom Forms.
Chapter Introduction to Computers and Programming 1.
A First Program Using C#
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.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
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.
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.
Microsoft Tech Days 2012 Cheezia: Developing a Windows Phone XNA Game Rodrigo Barretto Software Engineer - MCPD on Windows Phone
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
FIRST GADGETEER PROJECT. Where are you? Making a VS project Parts of a C# program Basics of C# syntax Debugging in VS Questions? 2.
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.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
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.
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.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code.
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.
Chapter 3 I Need a Tour Guide (Introduction to Visual Basic 2010) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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.
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.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
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.
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.
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.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
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.
EGR 2261 Unit 10 Two-dimensional Arrays
Creating a Multi-Player Game
Programming – Touch Sensors
Microsoft Visual Basic 2005 BASICS
Variables and Arithmetic Operations
CIS16 Application Development – Programming with Visual Basic
Developing Games for Windows Phone 7 with XNA Game Studio 4.0
Presentation transcript:

11 Using the Keyboard Session 3.2.1

Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types  Show how an XNA program can use both the keyboard and the gamepad to control game programs  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types  Show how an XNA program can use both the keyboard and the gamepad to control game programs Chapter 3.2.1: Using the Keyboard2

XNA and the Keyboard  The Xbox 360 is not supplied with a keyboard  You can connect a keyboard to any of the USB ports on the Xbox itself  A Windows PC already has a keyboard available  The keyboard is used in exactly the same way in an XNA game program for Xbox or Windows PC  It is not possible to use the keyboard on a Zune device  The Xbox 360 is not supplied with a keyboard  You can connect a keyboard to any of the USB ports on the Xbox itself  A Windows PC already has a keyboard available  The keyboard is used in exactly the same way in an XNA game program for Xbox or Windows PC  It is not possible to use the keyboard on a Zune device Chapter 3.2.1: Using the Keyboard3

Keyboards in XNA Game Programs  The keyboard is used in a very similar way to the gamepad  A game can tell whether or not a particular key is pressed or released  Keys do not generate a character as such, instead a game can get the keyboard status and check the status of particular keys  All the keys on a keyboard can be tested  Shift and control keys can be tested as well  The keyboard is used in a very similar way to the gamepad  A game can tell whether or not a particular key is pressed or released  Keys do not generate a character as such, instead a game can get the keyboard status and check the status of particular keys  All the keys on a keyboard can be tested  Shift and control keys can be tested as well Chapter 3.2.1: Using the Keyboard4

The KeyboardState type  The state of the keyboard is represented in XNA by a variable of type KeyboardState  This type provides methods that can be used to find out if particular keys have been pressed  They are used in a slightly different way to the properties of GamePadState  But the underlying principle is the same  The state of the keyboard is represented in XNA by a variable of type KeyboardState  This type provides methods that can be used to find out if particular keys have been pressed  They are used in a slightly different way to the properties of GamePadState  But the underlying principle is the same Chapter 3.2.1: Using the Keyboard5

Creating a KeyboardState Variable  The game will use a variable to hold the state of the keyboard  It will then test the values of the keys in this variable so that the keyboard 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 Keys  The game will use a variable to hold the state of the keyboard  It will then test the values of the keys in this variable so that the keyboard 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 Keys Chapter 3.2.1: Using the Keyboard6 KeyboardState keys;

Setting the Keys Variable  The Keyboard class is part of XNA and provides a method called getState to read the keyboard state  You do not need to tell getState which keyboard to read as the XNA Framework only supports one keyboard  The getState method delivers a KeyboardState object that holds the state of the keyboard at that instant  The Keyboard class is part of XNA and provides a method called getState to read the keyboard state  You do not need to tell getState which keyboard to read as the XNA Framework only supports one keyboard  The getState method delivers a KeyboardState object that holds the state of the keyboard at that instant Chapter 3.2.1: Using the Keyboard7 KeyboardState keys = Keyboard.GetState();

Testing for a Key Press  The method isKeyDown is told which key is to be tested  The method returns true if the key is pressed  The above code would increase the red intensity if the R key is pressed down  The programmer can identify the key to be tested by using a value from the Keys enumeration  The method isKeyDown is told which key is to be tested  The method returns true if the key is pressed  The above code would increase the red intensity if the R key is pressed down  The programmer can identify the key to be tested by using a value from the Keys enumeration Chapter 3.2.1: Using the Keyboard8 if (keys.IsKeyDown(Keys.R)) redIntensity++;

The Keys enumeration  The designers of XNA have created an enumeration which holds values that represent the keys on the keyboard  An enumeration is a type which can hold a particular set of values  They are created for use in specific situations where you only want a variable to hold particular values  Later you will create your own enumerations to manage the state of a game  The designers of XNA have created an enumeration which holds values that represent the keys on the keyboard  An enumeration is a type which can hold a particular set of values  They are created for use in specific situations where you only want a variable to hold particular values  Later you will create your own enumerations to manage the state of a game Chapter 3.2.1: Using the Keyboard9

Enumerations and Microsoft Visual Studio  A Visual Studio feature called “Intellisense” can suggest values from an enumeration that can be used  This makes it much easier for the programmer to create correct code  A Visual Studio feature called “Intellisense” can suggest values from an enumeration that can be used  This makes it much easier for the programmer to create correct code Chapter 3.2.1: Using the Keyboard10

Program Exit Using the Escape key  The empty project that XNA creates can only be stopped by pressing the Back button  XNA provides a method called exit to stop the game  The program above calls the exit method on the running game when the Escape key is pressed  This allows the program to be keyboard controlled  The empty project that XNA creates can only be stopped by pressing the Back button  XNA provides a method called exit to stop the game  The program above calls the exit method on the running game when the Escape key is pressed  This allows the program to be keyboard controlled Chapter 3.2.1: Using the Keyboard11 if (keys.IsKeyDown(Keys.Escape)) { this.Exit(); } if (keys.IsKeyDown(Keys.Escape)) { this.Exit(); }

Handling the Keyboard and Gamepad  This code can use either gamepad, Zune, or keyboard input to control the red intensity  Keyboard.GetState() method always returns  If there is no keyboard present it returns a KeyboardState value with no keys pressed  This code can use either gamepad, Zune, or keyboard input to control the red intensity  Keyboard.GetState() method always returns  If there is no keyboard present it returns a KeyboardState value with no keys pressed Chapter 3.2.1: Using the Keyboard12 if (keys.IsKeyDown(Keys.R) || pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed) { redIntensity++; } if (keys.IsKeyDown(Keys.R) || pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed) { redIntensity++; }

Advanced Logic  This code sets the intensities to 0 if both Shift keys are pressed or both shoulder buttons are pressed on the gamepad  It combines a number of conditions to do this  This code sets the intensities to 0 if both Shift keys are pressed or both shoulder buttons are pressed on the gamepad  It combines a number of conditions to do this Chapter 3.2.1: Using the Keyboard13 if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed)) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; } if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed)) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; }

Advanced Logic  The two conditions are enclosed in brackets to tell the C# compiler they need to be worked out first and then combined using an OR operator  The brackets work as they would in arithmetic  The two conditions are enclosed in brackets to tell the C# compiler they need to be worked out first and then combined using an OR operator  The brackets work as they would in arithmetic Chapter 3.2.1: Using the Keyboard14 if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) ) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; } if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) ) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; }

Chapter 3.1: Getting Player Input Using a Gamepad15

Summary  The XNA Framework provides a type called KeyboardState to represent the state of the keyboard at a particular instant  The Keyboard class provides getState which returns a KeyboardState value  The getState method is given a value of type Keys to identify the key being tested  A KeyboardState value provides a method called isKeyDown which can check if a particular key is pressed  The XNA Framework provides a type called KeyboardState to represent the state of the keyboard at a particular instant  The Keyboard class provides getState which returns a KeyboardState value  The getState method is given a value of type Keys to identify the key being tested  A KeyboardState value provides a method called isKeyDown which can check if a particular key is pressed Chapter 3.2.1: Using the Keyboard16

True/False Revision Quiz  An XNA program can handle up to four keyboards.  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard.  An XNA program can handle up to four keyboards.  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard. Chapter 3.2.1: Using the Keyboard17

True/False Revision Quiz  An XNA program can handle up to four keyboards.  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard.  An XNA program can handle up to four keyboards.  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard. Chapter 3.2.1: Using the Keyboard18

True/False Revision Quiz  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard.  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard. Chapter 3.2.1: Using the Keyboard19

True/False Revision Quiz  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard.  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard. Chapter 3.2.1: Using the Keyboard20

True/False Revision Quiz  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard.  An XNA program can handle up to four keyboards  The KeyboardState type can hold values that represent the state of a keyboard.  The getState method from the Keyboard class returns the state of the keyboard.  The Keys type has a value for every possible key on the keyboard. Chapter 3.2.1: Using the Keyboard21