160 likes | 282 Views
Game Rules Implementation. Lecture 5. Revisit . Lab 4 – Other Input Methods Touches Basics Relationship between TouchesBegan , TouchesMoved , and TouchesEnded Getting Touch Point on screen Handling MultiTaps and MultiTouches Touches Actions and Gestures Dragging on UserImage
E N D
Game Rules Implementation Lecture 5
Revisit • Lab 4 – Other Input Methods • Touches Basics • Relationship between TouchesBegan, TouchesMoved, and TouchesEnded • Getting Touch Point on screen • Handling MultiTaps and MultiTouches • Touches Actions and Gestures • Dragging on UserImage • Moving to change orientation of the userImage • Tilting Devices • Accelerometer Reading Basics • Tilting devices to move the userImage
Overview of Lab 5 INPUT PROCESS OUTPUT Part 3 Part 1 Part 2 Collision Detection Game End Condition Tutorial 5
Missing Parts of the Game • Collision Detection • This is necessary to detect whether various animating images collide with each other, so as to determine scores and allow the game to progress • Part 1: Whether the balloons collide with the userImage • Part 2: Whether the balloons collide with the bullets • Game End Condition • This is necessary to determine when the game should end
Common Collision Technique I • Each image object is treated as a circle with a radius on the screen • The radius can be approximated by width/2 or height/2 • If the sum of the radius of two objects is less than the distance between their center positions • Then, the two objects are considered to be collided. • To simplify your work, you can add the following method to the view controller to check whether two objects collide • -(bool) collide : (float) x1 : (float) y1 : (float) r1 : (float) x2 : (float) y2 : (float) r2 { • float centerDistance = sqrt(pow((x1-x2),2) + pow((y1-y2),2)); • return (centerDistance < (r1 + r2)); • }
Collision Detection Technique II Radius of Balloon Distance between userImage and balloon Radius of user Image Collide Not Collide
Part 1: Whether Balloons Collide with UserImage Algorithm • For each target balloon, check whether it collides with userImage • If yes, • Remove it from targetDataArray and targetImageArray • Generate enough targets so that the total number of targets is still DEFAULT_TARGET_NUM_ON_SCREEN
Issue on Forward Traversing the Targets Data or Image Array • Maybe we can consider the following situation: • We have a target data Array of 5 elements • If target collides with the userImage • The corresponding target is removed from the array Original Index 0 1 2 3 4 Index Traverse 0 Original Target Array 1 2 0 Original Index 1 3 4 3 0 1 New Index 2 3 New Target Array Not considered
Backward Traverse • Again, the conditions are the same as before, but we traverse the array from backward direction 1 2 3 4 Index Traverse 4 Original Target Array 3 2 1 0 Original Index 1 3 4 0 1 New Index 2 3 New Target Array Considered 9
Part 2: Whether the balloons collide with the bullets Algorithm I • For each bullet, • For each target, check whether it collides with the target • If yes, • Check if it is the current target • If yes, • Update targetsLeft and targetsShot • Update current target and next target • Remove the bullet and balloon • Generate a new balloon
Part 2: Whether the balloons collide with the bullets Algorithm II • Basically, the algorithm here is very similar to that of Part 1 • Except • Need to consider the traverse on two arrays • Need to update the scores shown on the screen • Need to update the current target window and next target window • Does forward traverse issue occur in this case? Why? • How can we solve it?
Part 3 – Game End Conditions • When you first design the game, you must have designed the game objective • In this game, our objective is to shoot a certain amount of targets shown on the current target window within a certain time limit • We can see from the objective that • Winning Condition – Shoot a certain amount of targets before time end • Losing Condition – time up • When the game end, we should • Stop the timer and reset the accelerometer • Display a message to show whether you win or lose
Display a Pop up window • In iPhone, you can display a pop up window to alert the user with some special event. • This is known as UIAlertView • // First of all, we have to initiate an UIAlertView object with the associated title and message. • // Here, we also set it to show one button only • UIAlertView* av = [[UIAlertViewalloc] initWithTitle:@”Game Ended”messages:@”Times Up !!! Please try again” delegate:selfcancelButtonTitle:@”Finish”otherButtonTitles:nil, nil, ni]; • // The alertView is pop up • [av show]; • // Since it has already been pop up, we can release the memory used to hold the alert view • [av release];
Part ∞: Challenges • Introduce sound effects, such as, balloon “pops” when hit by a bullet • Note that two sound files are provided • Different difficulty levels • Keeping best score • Multiplayer game, etc. • Other interesting game rules?