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

Slides:



Advertisements
Similar presentations
Sprite-visual object (actor on the stage) Scripts- Tells actors (sprites) what to do.
Advertisements

How to check close-to-the-line arrows F E D E R A T I O N I T N E R N A T I O N A L E D E T I R A L ' A R C a b TOP VIEW FRONT VIEW a b c d e c d e.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Derived from Kirill Muzykov’s Rocket Mouse Tutorial WakeUpAndCode.com.
Lecture 5: Floodfill. Floodfill Overview First assume that there are no walls Give each cell a distance from the goal. The goal has distance 0. Repeat.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Input.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
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.
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
CSE 380 – Computer Game Programming AI & Collision Strategy Erin Catto’s Box2D.
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett.
AI & 2D Development COSC 315 Fall 2014 Bridget M. Blodgett.
VIDEO GAME PROGRAMMING Video Game Programming Snowball Wars INSTRUCTOR TEACHER’S ASSISTANT.
CSE 380 – Computer Game Programming Sprite Animation
VIDEO GAME PROGRAMMING Video Game Programming Junior – DigiPutt INSTRUCTOR TEACHER’S ASSISTANT.
VIDEO GAME PROGRAMMING Video Game Programming Level One – Breakout INSTRUCTOR Big Dan Teague TEACHER’S ASSISTANT Delmar O'Donnell.
Developing the Game User Interface (UI) Lesson 5.
Art 315 Lecture 5 Dr. J. Parker AB 606. Last time … We wrote our first program. We used a tool called GameMaker. The program we wrote causes a ball to.
1 Useful Tools for Making Video Games Part V An overview of.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
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.
Object Oriented Design COSC 315 Fall 2014 Bridget M. Blodgett.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
Microsoft® Small Basic Collision Detection Estimated time to complete this lesson: 1 hour.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Chapter2: Drawing a Window. Review: Introducing MFC.
Guide to Programming with Python Week 15 Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
3D Cameras COSC 315 Fall 2014 Bridget M. Blodgett.
More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.
11 General Game Programming Approach. The program is event-driven The program is event-driven –Messages = events –So as all windows system (for example.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Today we are learning to: Understand how actions and events control our game. Completing the catch the clown game – making a room – adding music Gather.
Game Project 1 Homage to Pong. Project Rules: The primary project is Pong, the design and development of which will be discussed in detail here. If you.
5 Event Handling Interactive Programming Suggested Reading Interaction: Events and Event Handling, Supplemental Text for CPSC 203 Distributed this term.
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.
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.
Construct 2 Game Development for Kids Platformer Tutorial: Part 1 Shahed Chowdhuri.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
3D Cameras and Enemies COSC 315 Fall 2014 Bridget M. Blodgett.
Build your own 2D Game Engine and Create Great Web Games using HTML5, JavaScript, and WebGL. Sung, Pavleas, Arnez, and Pace, Chapter 6 Examples 1,
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.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Sprites (Images) and Sounds
Sound and more Animations
MORE Genre Specific Physics
Catapult 2016.
Unity 2D: Step by Step, Part 4
Keyboard Input.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Catapult 2014 Session 10.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Week 6: Time and triggers!
Animation Translation.
Presentation transcript:

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

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

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

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;

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

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; }

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;

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

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); }

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;

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); }