Ejemplo n.º 1
0
 private void timer1_Tick(object sender, EventArgs e)
 {
     if (_gameState == GameState.Open && cnt < 20 && _listEnemyTank.Count + 1 <= 20 - cnt)
     {
         ttank enemyTank = new ttank(Side.Enemy);
         _listEnemyTank.Add(enemyTank);
         pictureBox1.Invalidate();
     }
 }
Ejemplo n.º 2
0
        private void MenuItemBegin_Click(object sender, EventArgs e)
        {
            cnt            = 0;
            _gameState     = GameState.Open;
            _myBulletCount = 0;
            _listEnemyTank.Clear();
            _listBullet.Clear();
            _myBulletCount = 0;
            pictureBox1.Invalidate();
            _myTank = new ttank(Side.Me);

            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
            timer4.Enabled = true;
        }
Ejemplo n.º 3
0
 private void timer2_Tick(object sender, EventArgs e)
 {
     if (_gameState == GameState.Open)
     {
         Random myRand = new Random(DateTime.Now.Millisecond);
         for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
         {
             int   randd     = myRand.Next(1, 51);
             ttank enemyTank = _listEnemyTank[i];
             if (randd < 25)
             {
                 if (Math.Abs(enemyTank._Positon.X - _myTank._Positon.X) <= 10)
                 {
                     if (enemyTank._Positon.Y < _myTank._Positon.Y)
                     {
                         enemyTank.Move(Direction.Down);
                     }
                     else
                     {
                         enemyTank.Move(Direction.Up);
                     }
                     _listEnemyTank[i].Fire();
                 }
                 else if (Math.Abs(enemyTank._Positon.Y - _myTank._Positon.Y) <= 10)
                 {
                     if (enemyTank._Positon.X < _myTank._Positon.X)
                     {
                         enemyTank.Move(Direction.Right);
                     }
                     else
                     {
                         enemyTank.Move(Direction.Left);
                     }
                     _listEnemyTank[i].Fire();
                 }
                 else
                 {
                     int newDirection = myRand.Next(1, 50);
                     if (newDirection <= 4)
                     {
                         _listEnemyTank[i].Move((Direction)newDirection);
                     }
                     else
                     {
                         _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);
                     }
                 }
             }
             else
             {
                 int newDirection = myRand.Next(1, 50);
                 if (newDirection <= 4)
                 {
                     _listEnemyTank[i].Move((Direction)newDirection);
                 }
                 else
                 {
                     _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);
                 }
             }
             if (Math.Abs(enemyTank._Positon.X - _myTank._Positon.X) <= 20 && Math.Abs(enemyTank._Positon.Y - _myTank._Positon.Y) <= 20)
             {
                 MenuItemEnd_Click(sender, e);
                 MessageBox.Show(this, "对不起,你输了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
         }
         for (int i = 0; i < _listBullet.Count; i++)
         {
             Bullet myBullet = _listBullet[i];
             myBullet.Move();
             if (myBullet._Position.X < 0 || myBullet._Position.X > Screen.PrimaryScreen.Bounds.Width)
             {
                 if (myBullet._Side == Side.Me)
                 {
                     _myBulletCount--;
                 }
                 _listBullet.RemoveAt(i);
             }
             else if (myBullet._Position.Y < 0 || myBullet._Position.Y > Screen.PrimaryScreen.Bounds.Height)
             {
                 if (myBullet._Side == Side.Me)
                 {
                     _myBulletCount--;
                 }
                 _listBullet.RemoveAt(i);
             }
             else
             {
                 if (myBullet._Side == Side.Enemy)
                 {
                     if (Math.Abs(myBullet._Position.X - _myTank._Positon.X) <= 20 && Math.Abs(myBullet._Position.Y - _myTank._Positon.Y) <= 20)
                     {
                         MenuItemEnd_Click(sender, e);
                         MessageBox.Show(this, "对不起,你输了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     }
                 }
                 else
                 {
                     for (int j = 0; j < _listBullet.Count; j++)
                     {
                         Bullet bullet = _listBullet[j];
                         if (bullet._Side == Side.Enemy)
                         {
                             if (Math.Abs(myBullet._Position.X - bullet._Position.X) <= 10 && Math.Abs(myBullet._Position.Y - bullet._Position.Y) <= 10)
                             {
                                 _listBullet.RemoveAt(i);
                                 if (i < j)
                                 {
                                     _listBullet.RemoveAt(j - 1);
                                 }
                                 else
                                 {
                                     _listBullet.RemoveAt(j);
                                     i--;
                                 }
                                 i--;
                                 _myBulletCount--;
                                 break;
                             }
                         }
                     }
                     for (int j = 0; j < _listEnemyTank.Count; j++)
                     {
                         ttank enemyTank = _listEnemyTank[j];
                         if (Math.Abs(myBullet._Position.X - enemyTank._Positon.X) <= 20 && Math.Abs(myBullet._Position.Y - enemyTank._Positon.Y) <= 20)
                         {
                             _listEnemyTank.RemoveAt(j);
                             _listBullet.RemoveAt(i);
                             i--;
                             _myBulletCount--;
                             cnt++;
                             if (cnt == 20)
                             {
                                 MenuItemEnd_Click(sender, e);
                                 MessageBox.Show(this, "恭喜,你赢了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             }
                             break;
                         }
                     }
                 }
             }
         }
         pictureBox1.Invalidate();
     }
 }