//Now here are the actual hit tests //Bullets hit invaders private bool BulletInvaderHitTest(Bullet thisBullet, Invader thisInvader) { //two rectangles are defined: one around the bullet, one around the invader Rect recBullet = new Rect(Canvas.GetLeft(thisBullet), Canvas.GetTop(thisBullet), thisBullet.ActualWidth, thisBullet.ActualHeight); Rect recInvader = new Rect(Canvas.GetLeft(thisInvader), Canvas.GetTop(thisInvader), thisInvader.ActualWidth, thisInvader.ActualHeight); //The intersectsWith method is called on recBullet with the argument recInvader which returns true if the rectangles overlap. //This result is returned to the condition statement calling the method and it is similar to a function in visual basic. return recBullet.IntersectsWith(recInvader); }
//bullets hit shield elements private bool BulletShieldElementHitTest(Bullet thisBullet, shieldElement thisShieldElement) { Rect recBullet = new Rect(Canvas.GetLeft(thisBullet), Canvas.GetTop(thisBullet), thisBullet.ActualWidth, thisBullet.ActualHeight); Rect recShieldElement = new Rect(Canvas.GetLeft(thisShieldElement), Canvas.GetTop(thisShieldElement), 10, 20); return recBullet.IntersectsWith(recShieldElement); }
//here is the fire bullet method private void fireBullet() { //instanciate a new bullet user control Bullet newBullet = new Bullet(); //put it on the screen andd add refernece to the bullets list cnvSpace.Children.Add(newBullet); Bullets.Add(newBullet); //set its position to that of the centre of the gunner Canvas.SetLeft(newBullet, Canvas.GetLeft(imgGunner) + imgGunner.ActualWidth*.5); Canvas.SetTop(newBullet, Canvas.GetTop(imgGunner)); }