//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); }
//the method the draw the aliens at the start of the game private void drawAliens() { //first the embedded .png files are loaded into bitmap variables BitmapImage bitInv1a = new BitmapImage(); bitInv1a.BeginInit(); bitInv1a.UriSource = new Uri("inv1a.png", UriKind.Relative); bitInv1a.EndInit(); BitmapImage bitInv1b = new BitmapImage(); bitInv1b.BeginInit(); bitInv1b.UriSource = new Uri("inv1b.png", UriKind.Relative); bitInv1b.EndInit(); BitmapImage bitInv2a = new BitmapImage(); bitInv2a.BeginInit(); bitInv2a.UriSource = new Uri("inv2a.png", UriKind.Relative); bitInv2a.EndInit(); BitmapImage bitInv2b = new BitmapImage(); bitInv2b.BeginInit(); bitInv2b.UriSource = new Uri("inv2b.png", UriKind.Relative); bitInv2b.EndInit(); BitmapImage bitInv3a = new BitmapImage(); bitInv3a.BeginInit(); bitInv3a.UriSource = new Uri("inv3a.png", UriKind.Relative); bitInv3a.EndInit(); BitmapImage bitInv3b = new BitmapImage(); bitInv3b.BeginInit(); bitInv3b.UriSource = new Uri("inv3b.png", UriKind.Relative); bitInv3b.EndInit(); BitmapImage bitBullet = new BitmapImage(); bitBullet.BeginInit(); bitBullet.UriSource = new Uri("bullet.png", UriKind.Relative); bitBullet.EndInit(); //five rows of aliens for (int j = 0; j < 5; j++) { //15 aliens per row for (int i = 0; i < 15; i++) { //instanciates a new user control of the type Invader Invader thisInvader = new Invader(); //using modular division aliens are created in 3 types switch (j % 3) { case (0): thisInvader.image1.Source = bitInv1a; thisInvader.lives = 1; thisInvader.points = 100; thisInvader.flavour = 1; break; case (1): thisInvader.image1.Source = bitInv2a; thisInvader.lives = 2; thisInvader.points = 200; thisInvader.flavour = 2; break; case (2): thisInvader.image1.Source = bitInv3a; thisInvader.lives = 3; thisInvader.points = 300; thisInvader.flavour = 3; break; } //invader is added to the screen cnvSpace.Children.Add(thisInvader); //a reference to the invader is added to the typed list aliens Aliens.Add(thisInvader); //a horizontal gap of 5px between invaders (dimesnion is 30*30) plus an offset of 5px Canvas.SetLeft(thisInvader, i * 35 + 5); //vertical gap if 5px plus 40px offset Canvas.SetTop(thisInvader, j * 35 + 40); //z-index is set up so that game over will appear above the sprite Canvas.SetZIndex(thisInvader, 0); } } }