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.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
 Copyright I/O International, 2013 Visit us at: A Feature Within from Sales Rep User Friendly Maintenance – with Zip Code.
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.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
C++ fundamentals.
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.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
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.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
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.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
OOD Case Study (For parallel treatment, see Chapter 2 of the text)
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
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.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
By the end of this session you should be able to...
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 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.
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.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Select (drop-down list) Inputs. Insert/Form/List Menu.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
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.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
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.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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.
Lives and Scoring Games Programming in Scratch. Games Programming in Scratch L2 Lives and Scoring Learning Objectives Define a variable Understand the.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
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.
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.
Lecture 5 of Computer Science II
Using and Creating Sprites
Creating a Multi-Player Game
Game Loop Update & Draw.
Presentation transcript:

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 to find out which keys have been pressed on the keyboard  See how custom type has been created in XNA to represent key presses and how to use these in your programs  Start to create a message board program  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays to find out which keys have been pressed on the keyboard  See how custom type has been created in XNA to represent key presses and how to use these in your programs  Start to create a message board program Chapter 9.1: Using the Keyboard in XNA2

Game Idea: “Message Board”  We know how to create good looking text  We can also make animated backgrounds and display a clock  We can combine all these abilities to create a message board  First we need to read the keyboard  We know how to create good looking text  We can also make animated backgrounds and display a clock  We can combine all these abilities to create a message board  First we need to read the keyboard Chapter 9.1: Using the Keyboard in XNA3

Reading the Keyboard  We have already used the keyboard in our XNA programs  We used it to make a keyboard version of “Color Nerve”  If the R key is pressed the red intensity value will be increased  We have already used the keyboard in our XNA programs  We used it to make a keyboard version of “Color Nerve”  If the R key is pressed the red intensity value will be increased Chapter 9.1: Using the Keyboard in XNA4 if (pad1.Buttons.B == ButtonState.Pressed || keys.IsKeyDown(Keys.R)) { redIntensity++; }

Levels and Edges  This kind of input is managed by level  Every time we test the key we will find that it is down  If we just tested for keys in this way, we would have an “auto repeat” that worked 60 times a second  This kind of input is managed by level  Every time we test the key we will find that it is down  If we just tested for keys in this way, we would have an “auto repeat” that worked 60 times a second Chapter 9.1: Using the Keyboard in XNA5 if (pad1.Buttons.B == ButtonState.Pressed || keys.IsKeyDown(Keys.R)) { redIntensity++; }

Detecting that a Key Has Been Pressed  To detect a key being pressed we have to do what we did before with the Button Bash game  We have to compare the current state of the key with the previous state  A changes in the two states will mean that a key has been pressed or released  To detect a key being pressed we have to do what we did before with the Button Bash game  We have to compare the current state of the key with the previous state  A changes in the two states will mean that a key has been pressed or released Chapter 9.1: Using the Keyboard in XNA6 if (keyState.IsKeyDown(Keys.R) && oldKeyState.IsKeyUp(Keys.R)) { // if we get here the key R has just been pressed } if (keyState.IsKeyDown(Keys.R) && oldKeyState.IsKeyUp(Keys.R)) { // if we get here the key R has just been pressed }

Detecting All the Keys  To detect all the keys on a keyboard we would have to repeat this process for every single key  This is possible, but would be very tedious  Fortunately XNA provides an easier way of finding out what keys have been pressed, using arrays  To detect all the keys on a keyboard we would have to repeat this process for every single key  This is possible, but would be very tedious  Fortunately XNA provides an easier way of finding out what keys have been pressed, using arrays Chapter 9.1: Using the Keyboard in XNA7

Finding Out the State of the Keyboard  To get hold of the state of the keyboard we can use the GetState method  This returns a value of type KeyboardState which describes the state of the keyboard at the time of the call  To get hold of the state of the keyboard we can use the GetState method  This returns a value of type KeyboardState which describes the state of the keyboard at the time of the call Chapter 9.1: Using the Keyboard in XNA8 KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys(); KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys();

Discovering Which Keys Are Currently Pressed  The GetPressedKeys method can be called on a KeyboardState value  The method returns an array which holds a list of keys that are pressed down  In the code above, the array pressedKeys is set to the value returned by GetPressedKeys  The GetPressedKeys method can be called on a KeyboardState value  The method returns an array which holds a list of keys that are pressed down  In the code above, the array pressedKeys is set to the value returned by GetPressedKeys Chapter 9.1: Using the Keyboard in XNA9 KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys(); KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys();

Creating a String of Key Names  This for loop assembles a list of the keys that are presently pressed and puts their descriptions into a string variable called messageString  The string can be displayed by the Draw method  It does this by working through the array of pressed keys  This for loop assembles a list of the keys that are presently pressed and puts their descriptions into a string variable called messageString  The string can be displayed by the Draw method  It does this by working through the array of pressed keys Chapter 9.1: Using the Keyboard in XNA10 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }

Creating an Empty String  The messageString will hold a string of text that describes each of the keys that has been pressed  It must be cleared to an empty string at the start  This assignment sets messageString to an empty string  The messageString will hold a string of text that describes each of the keys that has been pressed  It must be cleared to an empty string at the start  This assignment sets messageString to an empty string Chapter 9.1: Using the Keyboard in XNA11 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }

Creating a for Loop to Work Down the Array  The for loop needs to know the size of the pressedKeys array  All arrays provide a Length property which returns an integer giving the number of elements in the array  This is used to control the for loop  The for loop needs to know the size of the pressedKeys array  All arrays provide a Length property which returns an integer giving the number of elements in the array  This is used to control the for loop Chapter 9.1: Using the Keyboard in XNA12 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }

Assembling the Message String  We have used the + operator between numbers to perform addition  When + is used between strings it actually puts one string on the end of the other  This statement adds a key description onto the end of the string, along with a separator space  We have used the + operator between numbers to perform addition  When + is used between strings it actually puts one string on the end of the other  This statement adds a key description onto the end of the string, along with a separator space Chapter 9.1: Using the Keyboard in XNA13 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }

Getting the Key Description  You can ask any object in a program to provide a string description of itself  You do this by calling the ToString method on that object  We are asking each key to describe itself  You can ask any object in a program to provide a string description of itself  You do this by calling the ToString method on that object  We are asking each key to describe itself Chapter 9.1: Using the Keyboard in XNA14 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }

Displaying the Message  The Draw method just displays the string  messageString and messageVector are variables in the game world  The Draw method just displays the string  messageString and messageVector are variables in the game world Chapter 9.1: Using the Keyboard in XNA15 protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(font, messageString, messageVector, Color.White); spriteBatch.End(); base.Draw(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(font, messageString, messageVector, Color.White); spriteBatch.End(); base.Draw(gameTime); }

1. Key Viewer Chapter 9.1: Using the Keyboard in XNA16  This program displays a list of keys that are pressed  Note that each key is uniquely identified, including the left and right shift keys  This program displays a list of keys that are pressed  Note that each key is uniquely identified, including the left and right shift keys

Representing Keys in XNA  The designers of XNA needed something to represent keys on the keyboard  Some of the keys were printable characters, others were control keys such as shift and escape  They wanted users of the keyboard to be able to work with any key value that could be produced  To solve this problem they created an enumerated type called Keys  The designers of XNA needed something to represent keys on the keyboard  Some of the keys were printable characters, others were control keys such as shift and escape  They wanted users of the keyboard to be able to work with any key value that could be produced  To solve this problem they created an enumerated type called Keys Chapter 9.1: Using the Keyboard in XNA17

Enumerated Types and the Keys Type  The word “enumerate” means to count  In C# an enumerated type is one that can hold a particular number of values  In the case of the keyboard, the XNA designers wanted to hold values for every available key, but no more than that  They therefore created an enumerated type called Keys  The word “enumerate” means to count  In C# an enumerated type is one that can hold a particular number of values  In the case of the keyboard, the XNA designers wanted to hold values for every available key, but no more than that  They therefore created an enumerated type called Keys Chapter 9.1: Using the Keyboard in XNA18

Why Make an Enumerated Type?  There is no need to use an enumerated type  The XNA designers could just have assigned some values for the keys:  0 means left shift  1 means right shift  2 means the A key  However programmers could then use meaningless values, which would do strange things  An attempt to use key number 1001 might break the program  There is no need to use an enumerated type  The XNA designers could just have assigned some values for the keys:  0 means left shift  1 means right shift  2 means the A key  However programmers could then use meaningless values, which would do strange things  An attempt to use key number 1001 might break the program Chapter 9.1: Using the Keyboard in XNA19

Creating Your Own Enumerated Types  Later in the course you will create your own enumerated types  You can regard these as variable types where you get to choose the values which are allowed  They are used a lot in situations where items must be in one of a number of different states:  Bank account: open, suspended, closed  Video game: attract mode, playing, showing high score  Person: happy, sad, angry, upset  Later in the course you will create your own enumerated types  You can regard these as variable types where you get to choose the values which are allowed  They are used a lot in situations where items must be in one of a number of different states:  Bank account: open, suspended, closed  Video game: attract mode, playing, showing high score  Person: happy, sad, angry, upset Chapter 9.1: Using the Keyboard in XNA20

Enumerated Types and Intellisense  Since Microsoft Visual Studio can find out what values an enumerated type can take, it can show you these automatically  This is an example of the way that the development tool can work with the language that you are using  Since Microsoft Visual Studio can find out what values an enumerated type can take, it can show you these automatically  This is an example of the way that the development tool can work with the language that you are using Chapter 9.1: Using the Keyboard in XNA21

Summary  The keyboard is used in a very similar way to the gamepad, with a GetState method returning an object that describes the state of the keyboard  Your code can ask for the state of an individual key, or it can ask for an array that provides a list of all the keys that are pressed down  To detect a key being pressed an XNA game must use edge detection  An enumerated type has a specific set of values  The keyboard is used in a very similar way to the gamepad, with a GetState method returning an object that describes the state of the keyboard  Your code can ask for the state of an individual key, or it can ask for an array that provides a list of all the keys that are pressed down  To detect a key being pressed an XNA game must use edge detection  An enumerated type has a specific set of values Chapter 9.1: Using the Keyboard in XNA22

True/False Revision Quiz  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values.  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA23

True/False Revision Quiz  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values.  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA24

True/False Revision Quiz  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values.  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA25

True/False Revision Quiz  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values.  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA26

True/False Revision Quiz  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values.  An XNA program uses a GetState method to read the status of a keyboard.  A keyboard can only detect a single key press at a time.  The Length property can be used to determine how many elements an array contains.  An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA27