private void TimerAnimationTick(object sender, EventArgs e) { int index = -1; //store three x position for opponent in an array int[] arrayPositionOpponent = { 20, 140, 260 }; Random _rand = new Random(); foreach (Lane lane in _listLane) { lane.Move(); if (lane.Y > _pictureBoxRoadRacing.Height) { lane.Y -= _pictureBoxRoadRacing.Height + lane.Height; } } if (_listOpponent.Count < 6) { int randomIndex = _rand.Next(0, 3); int randomAppearance = _rand.Next(100); int randomSpeed = _rand.Next(1, 10); if (randomAppearance < 4) { //opponent appear in three possible x position randomly and at random speed Opponent opponent = new Opponent(arrayPositionOpponent[randomIndex], -100, randomSpeed); if (!CheckOpponentOverlap(opponent)) { _listOpponent.Add(opponent); } } } foreach (Opponent opponent in _listOpponent) { opponent.Move(); if (opponent.Y > _pictureBoxRoadRacing.Height) { index = _listOpponent.IndexOf(opponent); } } if (index != -1) { _listOpponent.RemoveAt(index); } _pictureBoxRoadRacing.Refresh(); }
private bool CheckOpponentOverlap(Opponent opponent) { if (_listOpponent != null) { foreach (Opponent existedOpponent in _listOpponent) { if (opponent.X == existedOpponent.X) { if (opponent.Y + opponent.Height > existedOpponent.Y - 1 && opponent.Y < existedOpponent.Y + existedOpponent.Height + 1) { return(true); } else if (opponent.Speed > existedOpponent.Speed) { return(true); } } } } return(false); }
/// <summary> /// Check whether two opponents overlap with each other /// </summary> /// <param name="opponent"></param> /// <returns></returns> private bool CheckOpponentOverlap(Opponent opponent) { if (_listOpponent != null) { foreach (Opponent existedOpponent in _listOpponent) { //if they have the same x position if (opponent.X == existedOpponent.X) { //if they overlap based on the y postion if (opponent.Y + opponent.Height > existedOpponent.Y - 1 && opponent.Y < existedOpponent.Y + existedOpponent.Height + 1) { return(true); } //if this opponent has faster speed than the existed opponent, they could still overlap later else if (opponent.Speed > existedOpponent.Speed) { return(true); } } } } return(false); }
/// <summary> /// Timer for the animation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TimerAnimationTick(object sender, EventArgs e) { int index = -1; //store three x position for opponent in an array int[] arrayPositionOpponent = { 20, 140, 260 }; //create a random number Random _rand = new Random(); foreach (Lane lane in _listLane) { //move all the lanes lane.Move(); //if the lane is outside of the picture box, move it back to the top if (lane.Y > _pictureBoxRoadRacing.Height) { lane.Y -= _pictureBoxRoadRacing.Height + lane.Height; } } //if the count of the list for opponent is less than 6 if (_listOpponent.Count < 6) { int randomIndex = _rand.Next(0, 3); int randomAppearance = _rand.Next(100); int randomSpeed = _rand.Next(1, 10); //if the random number is less than the difficulty, then an opponent is created if (randomAppearance < _difficulty) { Bitmap bitmap0 = Properties.Resources.opponent_blue; Bitmap bitmap1 = Properties.Resources.opponent_orange; Bitmap bitmap2 = Properties.Resources.opponent_green; Bitmap bitmap = Properties.Resources.opponent_blue; int randomNumber = _rand.Next(0, 3); switch (randomNumber) { case 0: bitmap = bitmap0; break; case 1: bitmap = bitmap1; break; case 2: bitmap = bitmap2; break; } //opponent appear in three possible x position randomly and at random speed and using one of the three bitmaps Opponent opponent = new Opponent(arrayPositionOpponent[randomIndex], -100, randomSpeed, bitmap); //if opponent doesnot overlap with another opponent and there is space for user to pass between opponents if (!CheckOpponentOverlap(opponent) && AllowUserToSurvive(opponent)) { //add the created opponent to the list for opponent _listOpponent.Add(opponent); } } } foreach (Opponent opponent in _listOpponent) { //move every opponent in the list for opponent opponent.Move(); //if user collide with an opponent if (_user.Collide(opponent)) { //stop all the timers timerAnimation.Enabled = false; timerSpeedUp.Enabled = false; //show the label of Game Over labelGameOver.Visible = true; //store the best score if (_bestScore < _score) { _bestScore = _score; } } //if opponent is outside of the picture box, store its index value if (opponent.Y > _pictureBoxRoadRacing.Height) { index = _listOpponent.IndexOf(opponent); } } //remove the opponent from the list if (index != -1) { _listOpponent.RemoveAt(index); } _pictureBoxRoadRacing.Refresh(); }