Presentation is loading. Please wait.

Presentation is loading. Please wait.

XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.

Similar presentations


Presentation on theme: "XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites."— Presentation transcript:

1 XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites

2 KeyboardState keyboardState = Keyboard.GetState( ); if (keyboardState.IsKeyDown(Keys.Left)) spritePosition.X -= 2; if (keyboardState.IsKeyDown(Keys.Right)) spritePosition.X += 2; if (keyboardState.IsKeyDown(Keys.Up)) spritePosition.Y -= 2; if (keyboardState.IsKeyDown(Keys.Down)) spritePosition.Y += 2; Keyboard Control of spritePosition The state of every key on the keyboard is loaded into keyboardState when the Keyboard.GetState( ) method is invoked as shown in the following code sample. This permits the sensing of multiple keys being pressed simultaneously. The functionality of similar code can be evaluated with the demo project whirlygig.zip.

3 Keys Enumeration A list of standard keyboard member names with a description of which key is being pressed is shown here. Member names for special keys on extended keyboards are available from MSDN http://msdn.microsoft.com/en-us /library/microsoft.xna.framework. input.keys.aspx

4 public class whirlygig : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; Texture2D whirlysprite; Rectangle blit = new Rectangle(0, 0, 79, 79); Vector2 blitorigin; Vector2 whirlygigPosition; int maxX, maxY, minX, minY; float scale; int count; SpriteBatch spriteBatch; int msperframe = 40; int delms = 0; public whirlygig() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { Viewport view = graphics.GraphicsDevice.Viewport; whirlygigPosition.X = view.Width / 2; whirlygigPosition.Y = view.Height / 2; blitorigin.X = blit.Width / 2; blitorigin.Y = blit.Height / 2; maxX = view.Width - blit.Width / 2; maxY = view.Height - blit.Height / 2; minX = blit.Width / 2; minY = blit.Height / 2; scale = (float)1.0; base.Initialize(); count = 0; } sets minimum number of milliseconds between frames for the sprite animation (can be <= game loop rate of 60 frames/sec). will hold the accumulated time since last blit in the animation sequence was first displayed. whirlygig.zip

5 protected override void Update(GameTime gameTime) { int rownum, colnum; if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); KeyboardState keyboardState = Keyboard.GetState( ); if (keyboardState.IsKeyDown(Keys.Left)) whirlygigPosition.X -= 2; if (keyboardState.IsKeyDown(Keys.Right)) whirlygigPosition.X += 2; if (keyboardState.IsKeyDown(Keys.Up)) whirlygigPosition.Y -= 2; if (keyboardState.IsKeyDown(Keys.Down)) whirlygigPosition.Y += 2; if (keyboardState.IsKeyDown(Keys.PageUp)) scale += (float)0.05; if (keyboardState.IsKeyDown(Keys.PageDown)) scale -= (float)0.05; if (keyboardState.IsKeyDown(Keys.A)) msperframe += 1; if (msperframe > 100) msperframe = 100; if (keyboardState.IsKeyDown(Keys.Z)) msperframe -= 1; if (msperframe < 15) msperframe = 15; if (whirlygigPosition.X > maxX) whirlygigPosition.X = maxX; if (whirlygigPosition.X < minX) whirlygigPosition.X = minX; if (whirlygigPosition.Y > maxY) whirlygigPosition.Y = maxY; if (whirlygigPosition.Y < minY) whirlygigPosition.Y = minY; if (scale < (float)0.25) scale = (float)0.25; if (scale > (float)1.5) scale = (float)1.5; delms += gameTime.ElapsedGameTime.Milliseconds; if (delms > msperframe) { count += 1; if (count > 47) count = 0; rownum = count / 6; colnum = count % 6; blit.X = colnum * 79 + 1; blit.Y = rownum * 79 + 1; delms = 0; } base.Update(gameTime); } uses A and Z to change the animation rate range is limited to between 15 & 100 ms. accumulates the total time since the time the animation frame count was changed when delms > msperframe the animation frame count is incremented and delms is reset to zero (0)

6 The mouseDemo Project includes a demonstration of how to manage multiple instances of an animated sprite appearing simultaneously. We will create instances of a class called kapow and keep them in a generic list while the animation sequence is being displayed. When a sequence is completed that instance of the class will be removed from the list. List bombs = new List (); GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Vector2 origin = new Vector2(0, 0); Texture2D kablam; SpriteFont font; string bombCount = "0"; Vector2 fontPos = new Vector2(10, 10); MouseState mouse; Vector2 Pos; int delms; int msperframe = 30; int deltxt; int mspertext = 200; bool boom; mouseDemo generic list to hold instances of class kapow contains the spritsheet kablam.bmp a spriteFont available in XNA Game Studio 4.0 will be used to display the generic list count animation milliseconds per frame list count display frame time in milliseconds used to ensure only one kapow instance is generated for each button press

7 Mouse Controls The XNA demo program mouseDemo.zip includes code for getting and using the mouseState as well as a method for loading and accessing a generic list of animated sprites. Let's deal with the mouse controls first. Include the following in global declarations: We can access the state of the mouse in the Update( ) method in the following manner: Other mouse controls are accessed in a similar manner. (Review options in intellisense) MouseState mouse; Vector2 Pos; mouse = Mouse.GetState(); if (mouse.LeftButton.Equals(ButtonState.Pressed)) { Pos.X = mouse.X; Pos.Y = mouse.Y; // do stuff with mouse state here }

8 public class kapow { public Vector2 Pos; public Rectangle Blit = new Rectangle(); public int NumRow; public int NumCol; public int Width; public int Height; public int AnimCount; public int AnimTot; public bool AnimDone; public kapow(int x, int y, int numrow, int numcol, int width, int height) { Blit.Width = width / numcol; Blit.Height = height / numrow; Pos.X = x - Blit.Width / 2; Pos.Y = y - Blit.Height / 2; AnimCount = 0; NumRow = numrow; NumCol = numcol; AnimTot = numrow * numcol; Blit.X = 0; Blit.Y = 0; AnimDone = false; } Class for an Animated Sprite Each animated explosion sequence in our demo will need its own position, blit location, and frame counter. We will create instances of this class each time the left mouse button is pressed when the Update( ) method is called. sprite frames are assumed to be in a two-D array in the sprite sheet arranged in numrow rows and numcol columns for a total of numrow x numcol frames.

9 protected override void Initialize() { base.Initialize(); boom = false; IsMouseVisible = true; delms = 0; deltxt = 0; } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); kablam = Content.Load ("kablam"); font = Content.Load ("SpriteFont1"); } mouseDemo Initialization and Content Loading The sprite sheet kablam.bmp is associated with the project by right-clicking mouseDemoContent and selecting Add - Existing Item. The font SpriteFont1 is chosen by right-clicking mouseDemoContent and selecting Add -> New Item -> Sprite Font

10 Blit.Width = width / numcol; Blit.Height = height / numrow; Pos.X = x - Blit.Width / 2; Pos.Y = y - Blit.Height / 2; AnimCount = 0; NumRow = numrow; NumCol = numcol; AnimTot = numrow * numcol; Blit.X = 0; Blit.Y = 0; AnimDone = false; foreach (kapow pow in bombs) { pow.AnimCount += 1; if (pow.AnimCount >= pow.AnimTot) pow.AnimDone = true; else { pow.Blit.X = (pow.AnimCount % pow.NumCol) * pow.Blit.Width; pow.Blit.Y = (pow.AnimCount / pow.NumCol) * pow.Blit.Height; } numcol 012345 0 1 2 3 4 numrow 880 665 Blitting from the Sprite Sheet kablam.bmp properties defined in Class kapow Example: blit for AnimCount = 15 loop in Update( ) method

11 mouse = Mouse.GetState(); if (mouse.LeftButton.Equals(ButtonState.Pressed) && boom==false || mouse.RightButton.Equals(ButtonState.Pressed)) { Pos.X = mouse.X; Pos.Y = mouse.Y; bombs.Add(new kapow((int)Pos.X, (int)Pos.Y, 5, 6, 880, 665)); boom = true; } if (mouse.LeftButton.Equals(ButtonState.Released)) boom = false; Mouse Controls When the mouse left-button is pressed one instance of kapow is created at the mouse location and added to the generic list bombs. The value of the Boolean boom is set to true and remains true until the left-button is released. This ensures that only one instance of kapow is created for each left-button press. When the mouse right-button is pressed instances of kapow are created and added to the bombs at the game loop rate (60 /sec). Holding down the right-button while moving the mouse produces an effect similar to that shown next.

12

13 delms += gameTime.ElapsedGameTime.Milliseconds; if (delms > msperframe) { foreach (kapow pow in bombs) { pow.AnimCount += 1; if (pow.AnimCount >= pow.AnimTot) pow.AnimDone = true; else { pow.Blit.X = (pow.AnimCount % pow.NumCol) * pow.Blit.Width; pow.Blit.Y = (pow.AnimCount / pow.NumCol) * pow.Blit.Height; } delms = 0; } bombs.RemoveAll(delegate(kapow pow) { return pow.AnimDone; }); Managing Multiple Animated Sprites The generic list bombs may hold many instances of kapow, each displaying a different frame of the animation sequence. Each frame lasts for 30 milliseconds (as set in the demo) and after all frames have been displayed, that instance of kapow is tagged for removal (i.e. Animdone is set to true). The removal of completed instances is done through a delegate bombs.RemoveAll( ) as shown. Attempting to remove items from the list within the foreach loop can result in indexing errors and the removal of the wrong instances of kapow.

14 protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.MidnightBlue); spriteBatch.Begin(); foreach (kapow pow in bombs) spriteBatch.Draw(kablam, pow.Pos, pow.Blit, Color.White, 0, origin, 1, 0, 0); spriteBatch.DrawString(font, bombCount, fontPos, Color.White); spriteBatch.End(); base.Draw(gameTime); } mouseDemo Draw( ) Method In the draw method the currrent frame (blit region) of each kapow sprite in the list bombs is drawn as part of the spriteBatch actions. The number of kapow sprites in bombs is displayed in the upper left-hand corner of the display screen using the DrawString( ) method. deltxt += gameTime.ElapsedGameTime.Milliseconds; if (deltxt > mspertext) { bombCount = Convert.ToString(bombs.Count); deltxt = 0; } The value of bombcount is updated in the Update( ) method at a rate of once every 200 milliseconds. from Update( ) Method

15 graphics.IsFullScreen = true; graphics.ApplyChanges(); mouse = Mouse.GetState(); if (mouse.LeftButton.Equals(ButtonState.Pressed) && mouse.RightButton.Equals(ButtonState.Pressed)) this.Exit(); Going Fullscreen Since we are dealing building XNA applications for Windows, we will want the option to display our games in fullscreen mode. in the Initialize( ) method, include the following: in the Update( ) Method, include some way to exit the game, such as:


Download ppt "XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites."

Similar presentations


Ads by Google