CHAPTER 3 Getting Player Input XNA Game Studio 4.0.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead February 12, 2008 Creative Commons Attribution.
Pseudocode.
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.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Getting started with Microsoft.NET Gadgeteer Comberton Village College Gadgeteer Club.
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.
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.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Developing the Game User Interface (UI) Lesson 5.
Word Lesson 2 Basic Editing
1 Useful Tools for Making Video Games Part V An overview of.
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.
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.
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
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.
ResponseCard XR Answering Questions. Polling The host computer must be able to receive your responses, but it can only do so when the polling is open.
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.
Computer Science Binary. Binary Code Remember the power supply that is inside your computer and how it sends electricity to all of the components? That.
11 Getting Player Input Using a Gamepad Session 3.1.
Slide 1 Project 1 Task 2 T&N3311 PJ1 Information & Communications Technology HD in Telecommunications and Networking Task 2 Briefing The Design of a Computer.
Algorithms Writing instructions in the order they should execute.
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.
Robotics NXT-G: variables, file Rotation sensor Lab: Use buttons to hit specific ball. Homework: Postings. Start planning mapping the room.
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.
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 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 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.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
1 Word Lesson 2 Basic Editing Microsoft Office 2010 Introductory Pasewark & Pasewark.
Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.
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.
Lecture 2 Richard Gesick
Learning Objective: to be able to design programs that use sequencing.
Scripts & Functions Scripts and functions are contained in .m-files
Programming – Touch Sensors
Introduction to Problem Solving and Control Statements
ICT Gaming Lesson 3.
Collisions with Static Objects
Presentation transcript:

CHAPTER 3 Getting Player Input XNA Game Studio 4.0

Objectives Find out how Microsoft XNA represents the gamepads and keyboards. Discover the C# language structures that let us get player input. Write some really silly games and scare people with them.

You have learned… You now know the basics of computer game programming. You know that a program is actually a sequence of statements, each of which performs a single action. You have seen that statements are held inside methods, each of which performs a particular task, and that methods are held in classes along with data. The program itself works on data values, which are held in variables of a particular type, and the program can make decisions based on the values that the variables have.

READING A GAMEPAD

Gamepads and Classes The gamepad information is represented in XNA by means of a class called GamePadState. The job of this class is to provide the connection between the program and the physical gamepad that the player is holding. A class contains data (variables that can hold stuff) and methods (code that can do stuff).

Gamepads and Classes When an XNA game starts, the XNA system makes an instance of the Game1 class that it then can ask to Draw and Update. When an instance of a class is created, the instructions for the methods that it contains are loaded into memory and space is set aside for the data variables that the instance holds.

Finding a Gamepad XNA also looks after a lot of other things when a game is running, one of which is the GamePad class connected to all the gamepads. The GamePad class contains a method called GetState, which gets the state of one of the gamepads. When GetState is called, it looks at one of the gamepads, reads its settings, and then sends information back for use in the statement it was called from.

Finding a Gamepad The GetState method is supplied with a parameter that identifies the gamepad to be read. A parameter is a way that a call can give information to a method. In the case of the GetState method, the parameter identifies the gamepad that you want to read.

Testing the Gamepad Status This compares the state of the red button B with the value ButtonState.Pressed. If the two are equal, this means that the button is down, and the Update method must make the red intensity bigger.

Testing the Gamepad Status 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.B == ButtonState.Pressed) redIntensity++; if (pad1.Buttons.X == ButtonState.Pressed) blueIntensity++; if (pad1.Buttons.A == ButtonState.Pressed) greenIntensity++; base.Update(gameTime); }

Testing the Gamepad Status In order to handle the Yellow button, add the following code. if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++; } Note the statements that are enclosed in curly braces This is called a block of code.

USING THE KEYBOARD Computer Programming 2

Using the Keyboard XNA works with keyboards as well as with gamepads. You might be surprised to learn that you can plug a USB keyboard into an Xbox 360 and use it just as you’d use the keyboard on the PC. To work with the keyboard, you can add this code. KeyboardState keys = Keyboard.GetState(); if (keys.IsKeyDown(Keys.R)) redIntensity++; if (keys.IsKeyDown(Keys.B)) blueIntensity++; if (keys.IsKeyDown(Keys.G)) greenIntensity++; if (keys.IsKeyDown(Keys.Y)) { redIntensity++; greenIntensity++; }

Using the Keyboard KeyboardState keys = Keyboard.GetState(); The KeyboardState item is an object that provides methods that the program can use to discover whether a particular key is pressed. if (keys.IsKeyDown(Keys.Y)) Supply the IsKeyDown method with a parameter that identifies the key you are interested in. The method IsKeyDown returns true if the key is down and false if not.

Stopping the Game with the Escape Key Update method contains a test that checks for the Back button on gamepad 1 and calls the Exit method to stop the game when the Back button is pressed. If you are using a keyboard instead of a gamepad you will not be able to press this button to stop the game. You can add a test for the Escape key on the keyboard. This key is a “control” key. It does not actually relate to a printable character, but is designed to signal an action you want the program to take. Other control keys include the Enter key and the Backspace key. You can use the same IsKeyDown method to test for the Escape key.

Using a Gamepad and a Keyboard at the Same Time To use a GamePad and keyboard at the same time, you must test for both. Combine two conditions and then perform some code if either condition is true. Use the OR logical operator It is true if one thing or the other is true, and it is written in the program Write as two vertical bars (||) if (pad1.Buttons.B == ButtonState.Pressed || keys.IsKeyDown(Keys.R)) redIntensity++;

ADDING VIBRATION Computer Programming 2

Controlling the Vibration of a Gamepad The communication between the gamepad and the game works in both directions. Not only can you read buttons on the gamepad, but also you can send commands to the gamepad to turn on the vibration motors. The GamePad class provides a method called SetVibration that lets a program control the vibration motors: GamePad.SetVibration(PlayerIndex.One, 0, 1);

Controlling the Vibration of a Gamepad GamePad.SetVibration(gamePad, leftVibration, rightVibration); The SetVibration method uses three parameters. The first one identifies which gamepad you want to vibrate. The second parameter is a value between 0.0 and 1 that controls the vibration of the left motor. The bigger the number, the more the gamepad vibrates. The third parameter controls the right motor in the same way as the left one. The bigger the number, the more the gamepad vibrates.

Controlling the Vibration of a Gamepad GamePad.SetVibration(gamePad, leftVibration, rightVibration); The left motor is the low-frequency vibration, and the right motor is the high-frequency vibration. Once the method has been called, the gamepad starts to vibrate, and it keeps vibrating until you call the method again to change its setting.

Testing Intensity Values The game needs to decide when to turn on the vibration. To do this, it must test the intensity values and turn on the vibration motor if any of them is getting too large. if ( redIntensity > 220 || greenIntensity > 220 || blueIntensity > 220 ) { GamePad.SetVibration(PlayerIndex.One, 0, 1); //turns on } else { GamePad.SetVibration(PlayerIndex.One, 0, 0); //turns off }

Exit Method If the Back button is pressed, the Exit method is called to stop the game. The Exit method removes the game display and shuts the game down in a tidy fashion. What the program must do is turn off the gamepad motors before Exit is called.

Exit Method Code needing added to the Update Method: if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { //turns off vibration GamePad.SetVibration(PlayerIndex.One, 0, 0); // call Exit Method this.Exit(); }