Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.

Similar presentations


Presentation on theme: "User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett."— Presentation transcript:

1 User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett

2 More Sprites Add a new skullBall sprite to the program using the same process used for the rings Also rename the variables for the rings animation to add the word rings in front of the variable name Remember to draw the skullball at 100,100 instead of 0,0 so it isn’t drawn on top of the rings

3 Keyboard Input The keyboard is handled through the Keyboard class and has three heavily used methods that are called through the GetState() method –Keys[] GetPressedKeys() –bool IsKeyDown(Keys key) –bool IsKeyUp(Keys key) To get the sprites to move we need to be able to change their current draw position using the arrows as keyboard input

4 Keyboard Input Add two variables to the top of your class Vector2 ringsPosition = Vector2.Zero; const float ringsSpeed = 6; Add the code to move the sprite just before base.Update KeyboardState keyboardState = Keyboard.GetState(); if(keyboardState.IsKeyDown(Keys.Left)) ringsPosition.X -= ringsSpeed; If(keyboardState.IsKeyDown(Keys.Right)) ringsPosition.X += ringsSpeed; If(keyboardState.IsKeyDown(Keys.Up)) ringsPosition.Y -= ringsSpeed; If(keyboardState.IsKeyDown(Keys.Down)) ringsPosition.Y += ringsSpeed;

5 Mouse Input Mouse input works similarly to keyboard input but it returns a MouseState struct instead of an array The other method Mouse class has is void SetPosition(int x, int y) To make a mouse cursor visible in the game set the IsMouseVisible property to true in the Game class

6 Moving with the Mouse To determine if the mouse has moved make a class variable called MouseState prevMouseState; Add the following to Update at the bottom MouseState mouseState = Mouse.GetState(); if(mouseState.X != prevMouseState.X || mouseState.Y != prevMouseState.Y){ ringsPosition = new Vector2(mouseState.X, mouseState.Y) prevMouseState = mouseState; }

7 Limiting Play Right now the pc can move out of the play window To limit it we need to update the position of the sprite and check if it has exceeded the play bounds if(ringsPosition.X < 0) ringsPosition.X = 0; if(ringsPosition.Y < 0) ringsPosition.Y = 0; if(ringsPosition.X > Window.ClinetBounds.Width – ringsFrameSize.X) ringsPosition.X = Window.ClinetBounds.Width – ringsFrameSize.X; if(ringsPosition.Y > Window.ClinetBounds.Height – ringsFrameSize.Y) ringsPosition.Y = Window.ClinetBounds.Height – ringsFrameSize.Y;

8 Collision Detection One of the fastest ways to handle collision detection is through a bounding-box algorithm To begin you need to make the skullball position a variable like the rings Pass the skullPosition variable as the second parameter to the spriteBatch.Draw call

9 Collision Methods In the Game1 class make a new method called collide protected bool Collide(){ Rectangle ringsRect = new Rectangle((int)ringsPosition.X, (int)ringsPosition.Y, ringsFrameSize.X, ringsFrameSize.Y); Rectangle skullRect = new Rectangle((int)skullPosition.X, (int)skullPosition.Y, skullFrameSize.X, skullFrameSize.Y); Return ringsRect.Intersects(skullRect); }

10 Calling Collide Now we can add code to Update the calls Collide and runs when a collision is detected if(Collide()) Exit(); The collisions happen a bit too soon due to the extra whitespace on the sheet Make two new variables – Int ringsCollisionRectOffset = 10; – int skullCollisionRectOffset = 10;

11 Altering Collide Change the collide() code to make use of these variables protected bool Collide(){ Rectangle ringsRect = new Rectangle((int)ringsPosition.X + ringsCollisionRectOffset, (int)ringsPosition.Y + ringsCollisionRectOffset, ringsFrameSize.X – (ringsCollisionRectOffset *2), ringsFrameSize.Y – (ringsCollisionRectOffset * 2)); Rectangle skullRect = new Rectangle((int)skullPosition.X + skullCollisionRectOffset, (int)skullPosition.Y + skullCollisionRectOffset, skullFrameSize.X – (skullCollisionRectOffset * 2), skullFrameSize.Y – (skullCollisionRectOffset * 2)); Return ringsRect.Intersects(skullRect); }


Download ppt "User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett."

Similar presentations


Ads by Google