private void movement_Tick(object sender, EventArgs e) { // laser movement for (int i = 0; i < laser_list.Count; i++) { Shot s = new Shot(); s.graphic = laser_list[i].graphic; s.x = laser_list[i].x; s.y = laser_list[i].y - laser_speed; laser_list.RemoveAt(i); laser_list.Insert(i, s); } // asteroid movement for (int i = 0; i < asteroid_list.Count; i++) { Asteroid a = new Asteroid(); a.graphic = asteroid_list[i].graphic; a.x = asteroid_list[i].x; a.y = asteroid_list[i].y + asteroid_list[i].speed; a.speed = asteroid_list[i].speed; a.type = asteroid_list[i].type; a.hp = asteroid_list[i].hp; a.size = asteroid_list[i].size; asteroid_list.RemoveAt(i); asteroid_list.Insert(i, a); } // detect collisions // asteroid with ground for (int i = 0; i < asteroid_list.Count; i++) { Asteroid cur_asteroid = asteroid_list[i]; // COLLISION WITH SHIP if ((cur_asteroid.y > (800 - cur_asteroid.size - 100)) && (Math.Abs(cur_asteroid.x - spaceship.Location.X) < 50)) { // reduce lives numLives = 0; Debug.WriteLine("GAME OVER"); // remove all lasers and asteroids asteroid_list.Clear(); laser_list.Clear(); // disable timers movement_timer.Stop(); spawn_timer.Stop(); level_timer.Stop(); movement_timer.Enabled = false; spawn_timer.Enabled = false; level_timer.Enabled = false; // display game over message countdown_label.Text = "Game Over"; countdown_label.Show(); // set boolean GameOver = true; lives.Text = "Lives: 0"; } // COLLISION WITH FLOOR if (cur_asteroid.y > (800 - cur_asteroid.size)) { // reduce # lives Debug.WriteLine("LIFE LOST"); numLives--; String lives_text = "Lives: " + numLives.ToString(); lives.Text = lives_text; // remove graphic asteroid_list.RemoveAt(i); // end game if # lives == 0 if (numLives == 0) { Debug.WriteLine("GAME OVER"); // remove all lasers and asteroids asteroid_list.Clear(); laser_list.Clear(); // disable timers movement_timer.Stop(); spawn_timer.Stop(); level_timer.Stop(); movement_timer.Enabled = false; spawn_timer.Enabled = false; level_timer.Enabled = false; // display game over message countdown_label.Text = "Game Over"; countdown_label.Show(); // set boolean GameOver = true; } } // COLLISION WITH LASER for (int j = 0; j < laser_list.Count; j++) { Shot cur_laser = laser_list[j]; if ((Math.Abs(cur_asteroid.y - cur_laser.y) < cur_asteroid.size) && (Math.Abs(cur_asteroid.x - cur_laser.x) < cur_asteroid.size)) { // decrement asteroid HP cur_asteroid.hp = cur_asteroid.hp - 1; Debug.WriteLine(cur_asteroid.hp); // remove laser laser_list.RemoveAt(j); // increase points & redo label points = points + point_increment; String score_text = "Score: " + points.ToString(); score.Text = score_text; // reinsert asteroid if (cur_asteroid.hp != 0) { asteroid_list.RemoveAt(i); asteroid_list.Insert(i, cur_asteroid); } else { asteroid_list.RemoveAt(i); } } } } // redraw objects this.Invalidate(); }
private void Asteroids_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == ' ' && !Shooting && !GameOver) { // shoot Shooting = true; System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.HotPink); System.Drawing.Graphics laser; laser = this.CreateGraphics(); laser.FillRectangle(myBrush, new Rectangle(this.spaceship.Location.X + 23, this.spaceship.Location.Y - 15, laser_size, laser_size)); myBrush.Dispose(); Shot shot = new Shot(); shot.graphic = laser; shot.x = this.spaceship.Location.X + 23; shot.y = this.spaceship.Location.Y - 15; laser_list.Add(shot); } }