// An Elapsed time ticker based on SetTimer, private void ATimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { foreach (SpaceObject r in Rocks.ToList()) { if (r != null) { r.XCoordinate += r.Velocity * Math.Cos(Math.PI * r.OriginalAngle / 180); r.YCoordinate += r.Velocity * Math.Sin(Math.PI * r.OriginalAngle / 180); r.Theta += 3 * Math.Cos(Math.PI * r.OriginalAngle / 180); if (r.XCoordinate > WINDOWWIDTH - 50) { r.XCoordinate = -50; } else if (r.XCoordinate < -50) { r.XCoordinate = WINDOWWIDTH - 50; } if (r.YCoordinate > WINDOWHEIGHT - 50) { r.YCoordinate = -50; } else if (r.YCoordinate < -50) { r.YCoordinate = WINDOWHEIGHT - 50; } } } // Player1 Score increases for every mili second your alive Player1Ship.Score++; //// Move player 1 if (accel) { if (Player1Ship.Velocity <= 3 * VELOCITY) { Player1Ship.Velocity += .1; } } else { if (Player1Ship.Velocity <= 0) { Player1Ship.Velocity = 0; } else { Player1Ship.Velocity -= .08; } } if (left) { Player1Ship.Theta -= 3; } if (right) { Player1Ship.Theta += 3; } if (shoot) // add a bullet { Application.Current.Dispatcher.Invoke(new Action(() => { var bullet = new SpaceObject('B', Player1Ship.XCoordinate + 8, Player1Ship.YCoordinate + 8, 10, 4 * VELOCITY, Player1Ship.Theta); listOfSpaceObjects.Add(bullet); })); } Player1Ship.XCoordinate += Player1Ship.Velocity * Math.Cos((Math.PI * Player1Ship.Theta / 180)); Player1Ship.YCoordinate += Player1Ship.Velocity * Math.Sin((Math.PI * Player1Ship.Theta / 180)); if (Player1Ship.XCoordinate > WINDOWWIDTH - 50) { Player1Ship.XCoordinate = -50; } else if (Player1Ship.XCoordinate < -50) { Player1Ship.XCoordinate = WINDOWWIDTH - 50; } if (Player1Ship.YCoordinate > WINDOWHEIGHT - 50) { Player1Ship.YCoordinate = -50; } else if (Player1Ship.YCoordinate < -50) { Player1Ship.YCoordinate = WINDOWHEIGHT - 50; } // Score increases for every millisecodn you're alive Player2Ship.Score++; // Move player 2 Ship if (enemyAccel) { if (Player2Ship.Velocity <= 3 * VELOCITY) { Player2Ship.Velocity += .1; } } else { if (Player2Ship.Velocity <= 0) { Player2Ship.Velocity = 0; } else { Player2Ship.Velocity -= .08; } } if (enemyLeft) { Player2Ship.Theta -= 3; } if (enemyRight) { Player2Ship.Theta += 3; } if (enemyShoot) // add a enemy bullet { Application.Current.Dispatcher.Invoke(new Action(() => { var bullet = new SpaceObject('F', Player2Ship.XCoordinate + 8, Player2Ship.YCoordinate + 8, 10, 4 * VELOCITY, Player2Ship.Theta); listOfSpaceObjects.Add(bullet); })); } Player2Ship.XCoordinate += Player2Ship.Velocity * Math.Cos((Math.PI * Player2Ship.Theta / 180)); Player2Ship.YCoordinate += Player2Ship.Velocity * Math.Sin((Math.PI * Player2Ship.Theta / 180)); if (Player2Ship.XCoordinate > WINDOWWIDTH - 50) { Player2Ship.XCoordinate = -50; } else if (Player2Ship.XCoordinate < -50) { Player2Ship.XCoordinate = WINDOWWIDTH - 50; } if (Player2Ship.YCoordinate > WINDOWHEIGHT - 50) { Player2Ship.YCoordinate = -50; } else if (Player2Ship.YCoordinate < -50) { Player2Ship.YCoordinate = WINDOWHEIGHT - 50; } //// Move the player1Bullets for (int i = 0; i < Bullets.Count(); i++) { var b = Bullets.Skip(i).First(); if (b != null) { b.XCoordinate += b.Velocity * Math.Cos(Math.PI * b.Theta / 180); b.YCoordinate += b.Velocity * Math.Sin(Math.PI * b.Theta / 180); if (b.XCoordinate > WINDOWWIDTH - 50 || b.XCoordinate < -50 || b.YCoordinate > WINDOWHEIGHT - 50 || b.YCoordinate < -50) { RemoveSpaceObject(b); } } } // Move enemy bullets for (int i = 0; i < EnemyBullets.Count(); i++) { var b = EnemyBullets.Skip(i).First(); if (b != null) { b.XCoordinate += b.Velocity * Math.Cos(Math.PI * b.Theta / 180); b.YCoordinate += b.Velocity * Math.Sin(Math.PI * b.Theta / 180); if (b.XCoordinate > WINDOWWIDTH - 50 || b.XCoordinate < -50 || b.YCoordinate > WINDOWHEIGHT - 50 || b.YCoordinate < -50) { RemoveSpaceObject(b); } } } // Check for collisions. If bullet hits rock, remove/split rock if rock hits ship, reset ship foreach (SpaceObject rock in Rocks.ToList()) { if (rock != null) { foreach (SpaceObject b in Bullets.ToList()) { //var b = Bullets.Skip(j).First(); if (b != null) { if (((b.XCoordinate > rock.XCoordinate && b.XCoordinate < rock.XCoordinate + rock.Height) || (b.XCoordinate + 5 > rock.XCoordinate && b.XCoordinate + 5 < rock.XCoordinate + rock.Height)) && ((b.YCoordinate > rock.YCoordinate && b.YCoordinate < rock.YCoordinate + rock.Height) || (b.YCoordinate + 5 > rock.YCoordinate && b.YCoordinate + 5 < rock.YCoordinate + rock.Height))) { Player1Ship.Score += Math.Pow((rock.NumberOfHits + 1), 2) * 100; RemoveSpaceObject(rock); RemoveSpaceObject(b); } } } // Chcek against Player2 Bullets foreach (SpaceObject b in EnemyBullets.ToList()) //for (int j = 0; j < EnemyBullets.Count(); j++) { //var b = EnemyBullets.Skip(j).First(); if (b != null) { if (((b.XCoordinate > rock.XCoordinate && b.XCoordinate < rock.XCoordinate + rock.Height) || (b.XCoordinate + 5 > rock.XCoordinate && b.XCoordinate + 5 < rock.XCoordinate + rock.Height)) && ((b.YCoordinate > rock.YCoordinate && b.YCoordinate < rock.YCoordinate + rock.Height) || (b.YCoordinate + 5 > rock.YCoordinate && b.YCoordinate + 5 < rock.YCoordinate + rock.Height))) { Player2Ship.Score += Math.Pow((rock.NumberOfHits + 1), 2) * 100; RemoveSpaceObject(rock); RemoveSpaceObject(b); } } } if (((Player1Ship.XCoordinate > rock.XCoordinate && Player1Ship.XCoordinate < rock.XCoordinate + rock.Height) || (Player1Ship.XCoordinate + Player1Ship.Height > rock.XCoordinate && Player1Ship.XCoordinate + Player1Ship.Height < rock.XCoordinate + rock.Height)) && ((Player1Ship.YCoordinate > rock.YCoordinate && Player1Ship.YCoordinate < rock.YCoordinate + rock.Height) || (Player1Ship.YCoordinate + Player1Ship.Height > rock.YCoordinate && Player1Ship.YCoordinate + Player1Ship.Height < rock.YCoordinate + rock.Height))) { ShipHit(); } if (((Player2Ship.XCoordinate > rock.XCoordinate && Player2Ship.XCoordinate < rock.XCoordinate + rock.Height) || (Player2Ship.XCoordinate + Player2Ship.Height > rock.XCoordinate && Player2Ship.XCoordinate + Player2Ship.Height < rock.XCoordinate + rock.Height)) && ((Player2Ship.YCoordinate > rock.YCoordinate && Player2Ship.YCoordinate < rock.YCoordinate + rock.Height) || (Player2Ship.YCoordinate + Player2Ship.Height > rock.YCoordinate && Player1Ship.YCoordinate + Player1Ship.Height < rock.YCoordinate + rock.Height))) { EnemyShipHit(); } } } foreach (SpaceObject b in Bullets) { if (((b.XCoordinate > Player2Ship.XCoordinate && b.XCoordinate < Player2Ship.XCoordinate + Player2Ship.Height) || (b.XCoordinate + 5 > Player2Ship.XCoordinate && b.XCoordinate + 5 < Player2Ship.XCoordinate + Player2Ship.Height)) && ((b.YCoordinate > Player2Ship.YCoordinate && b.YCoordinate < Player2Ship.YCoordinate + Player2Ship.Height) || (b.YCoordinate + 5 > Player2Ship.YCoordinate && b.YCoordinate + 5 < Player2Ship.YCoordinate + Player2Ship.Height))) { RemoveSpaceObject(b); Player1Ship.Score += 500; EnemyShipHit(); } } foreach (SpaceObject b in EnemyBullets) { if (((b.XCoordinate > Player1Ship.XCoordinate && b.XCoordinate < Player1Ship.XCoordinate + Player1Ship.Height) || (b.XCoordinate + 5 > Player1Ship.XCoordinate && b.XCoordinate + 5 < Player1Ship.XCoordinate + Player1Ship.Height)) && ((b.YCoordinate > Player1Ship.YCoordinate && b.YCoordinate < Player1Ship.YCoordinate + Player1Ship.Height) || (b.YCoordinate + 5 > Player1Ship.YCoordinate && b.YCoordinate + 5 < Player1Ship.YCoordinate + Player1Ship.Height))) { RemoveSpaceObject(b); Player2Ship.Score += 5000; ShipHit(); } } } catch (System.InvalidOperationException) { return; } // Checking for bullets hitting ships and ships hitting ships. }
public void Iteration() { iteration++; /// Fired by the form timer. This will Move and process all of the space objects. ship.Move(); // Move along the shots. // Delete any old ones. // Bullet Loop for (int i = bullets.Count - 1; i >= 0; i--) { Bullets bullet = bullets[i]; bullet.Move(); if (bullet.Iterations > bullet.life) { bullets.Remove(bullet); BullettsFired--; } // Check if we have hit an asteroid. for (int a = asteroids.Count - 1; a >= 0; a--) { Asteroid asteroid = asteroids[a]; if (asteroid.Area.Contains(bullet.point)) { // Add to score. PlayerScore += asteroid.Score; form2.playerScore.Text = PlayerScore.ToString(); // We have a hit. Remove this asteroid and create more smaller ones if appropriate. int createQty = 1; if (asteroid.Size == Asteroid.Sizes.Large) { createQty = random.Next(1, Level); for (int x = 0; x < createQty; x++) { Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Medium, asteroid.Colour); asteroids.Add(newAsteroid); } } if (asteroid.Size == Asteroid.Sizes.Medium) { createQty = random.Next(1, Level * 2); for (int x = 0; x < createQty; x++) { Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Small, asteroid.Colour); asteroids.Add(newAsteroid); } } if (asteroid.Size == Asteroid.Sizes.Small) { createQty = random.Next(1, Level * 3); for (int x = 0; x < createQty; x++) { Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Tiny, asteroid.Colour); asteroids.Add(newAsteroid); } } asteroids.Remove(asteroid); bullets.Remove(bullet); BullettsFired--; } } // Phase 2 Check if we have hit the enemy ship if (enemyShip1 != null && enemyShip1.Area.Contains(bullet.point) && bullet.Enemy != true) { // Add to score. PlayerScore += 50; form2.playerScore.Text = PlayerScore.ToString(); // Remove the object. enemyShip1 = null; soundPlayerGlass.Play(); EnemyShipActive = false; } // Phase 2 - See if the enemy ship has hit us. if (bullet.Enemy == true && ship.Area.Contains(bullet.point)) { // Oh dear we have been hit. Call the NewShip routine. NewShip(); ShowExplosion = 20; // Number of times to show explosion ExplosionPoint = bullet.point; break; } // See is all asteroids have gone. Need to start the next level. if (asteroids.Count <= 0) { Level++; NewLevel(Level); return; } } // End Bullet list loop. // Move the Asteroids. foreach (Asteroid asteroid in asteroids) { asteroid.Move(); // See if this has hit our ship. if (asteroid.Area.IntersectsWith(ship.Area)) { // Oh dear we have been hit. Call the NewShip routine. NewShip(); ShowExplosion = 20; // Number of times to show explosion ExplosionPoint = asteroid.point; break; } } // Phase 2 - Randomly start the enemy ship at the right hand side. if (EnemyShipActive == false) { int randy = random.Next(0, 500); if (randy == 250) { EnemyShipActive = true; int startPosX = FormSizeX - 2; int startPosY = random.Next(1, FormSizeY); enemyShip1 = new EnemyShip(startPosX, startPosY, this); enemyShip1.Angle = 270.0F; } } if (EnemyShipActive == true) { enemyShip1.Move(); if (iteration % 10 == 0) // Fire every ten iterations. { enemyShip1.Shoot(); } } // Fire the form paint event to paint the graphics. form1.Invalidate(); }