Beispiel #1
0
 //本方坦克移动
 public void myTankMove(bool upPress, bool downPress, bool leftPress, bool rightPress, Tank playerTank, List <Wall> wallList, List <Stell> stellList)
 {
     if (upPress)
     {
         playerTank.Direction = 0;
         Rectangle rec = new Rectangle(playerTank.X, playerTank.Y - 2, playerTank.Width, playerTank.Height);
         if (wallTankHit(rec, wallList) || stellTankHit(rec, stellList))
         {
         }
         else
         {
             playerTank.Y -= 2;
         }
         playerTank.refreshRectangle(playerTank.X, playerTank.Y);
     }
     else if (downPress)
     {
         playerTank.Direction = 1;
         Rectangle rec = new Rectangle(playerTank.X, playerTank.Y + 2, playerTank.Width, playerTank.Height);
         if (wallTankHit(rec, wallList) || stellTankHit(rec, stellList))
         {
         }
         else
         {
             playerTank.Y += 2;
         }
         playerTank.refreshRectangle(playerTank.X, playerTank.Y);
     }
     else if (leftPress)
     {
         playerTank.Direction = 2;
         Rectangle rec = new Rectangle(playerTank.X - 2, playerTank.Y, playerTank.Width, playerTank.Height);
         if (wallTankHit(rec, wallList) || stellTankHit(rec, stellList))
         {
         }
         else
         {
             playerTank.X -= 2;
         }
         playerTank.refreshRectangle(playerTank.X, playerTank.Y);
     }
     else if (rightPress)
     {
         playerTank.Direction = 3;
         Rectangle rec = new Rectangle(playerTank.X + 2, playerTank.Y, playerTank.Width, playerTank.Height);
         if (wallTankHit(rec, wallList) || stellTankHit(rec, stellList))
         {
         }
         else
         {
             playerTank.X += 2;
         }
         playerTank.refreshRectangle(playerTank.X, playerTank.Y);
     }
 }
Beispiel #2
0
        //子弹移动
        private void bulletMove(object tanks)
        {
            Tank tank = (Tank)tanks;

            while (tank.IsOver)
            {
                Bullet bullet = new Bullet(tank.X, tank.Y);
                switch (tank.Direction)
                {
                case 0:
                    bullet.Y -= 10;
                    bullet.X += 10;
                    break;

                case 1:
                    bullet.Y += 10;
                    bullet.X += 10;
                    break;

                case 2:
                    bullet.X -= 10;
                    bullet.Y += 10;
                    Rectangle rec = new Rectangle(bullet.X, bullet.Y, 20, 12);
                    bullet.Rectangle = rec;
                    break;

                case 3:
                    bullet.X += 10;
                    bullet.Y += 10;
                    Rectangle recs = new Rectangle(bullet.X, bullet.Y, 20, 12);
                    bullet.Rectangle = recs;
                    break;

                default:
                    break;
                }
                bullet.Direction = tank.Direction;
                bullet.refreshRectangle(bullet.X, bullet.Y);
                if (tank.Type == 1)
                {
                    enemyBulletList.Add(bullet);
                }
                else
                {
                    playerBulletList.Add(bullet);
                }
                switch (tank.Direction)
                {
                case 0:
                    while (!bullet.IsOver && bullet.Y > 0)
                    {
                        if (enemyBulletStellHit(bullet.Rectangle, stellList))
                        {
                            break;
                        }
                        bullet.Y -= 4;
                        //不好提炼,子弹根据方向绘制,每个方向矩形区域不同
                        bullet.refreshRectangle(bullet.X, bullet.Y);
                        bulletRefreshAndHit(bullet, tank);
                        Thread.Sleep(10);      //线程休眠10毫秒
                    }
                    break;

                case 1:
                    while (!bullet.IsOver && bullet.Y < 580)
                    {
                        if (enemyBulletStellHit(bullet.Rectangle, stellList))
                        {
                            break;
                        }
                        bullet.Y += 4;
                        bullet.refreshRectangle(bullet.X, bullet.Y);
                        bulletRefreshAndHit(bullet, tank);
                        Thread.Sleep(10);      //线程休眠10毫秒
                    }
                    break;

                case 2:
                    while (!bullet.IsOver && bullet.X > 0)
                    {
                        if (enemyBulletStellHit(bullet.Rectangle, stellList))
                        {
                            break;
                        }
                        bullet.X -= 4;
                        bullet.refreshRectangle(bullet.X, bullet.Y);
                        bulletRefreshAndHit(bullet, tank);
                        Thread.Sleep(10);      //线程休眠10毫秒
                    }
                    break;

                case 3:
                    while (!bullet.IsOver && bullet.X < 800)
                    {
                        if (enemyBulletStellHit(bullet.Rectangle, stellList))
                        {
                            break;
                        }
                        bullet.X += 4;
                        bullet.refreshRectangle(bullet.X, bullet.Y);
                        bulletRefreshAndHit(bullet, tank);
                        Thread.Sleep(10);      //线程休眠10毫秒
                    }
                    break;

                default:
                    break;
                }
                if (tank.Type == 1)
                {
                    try
                    {
                        enemyBulletList.Remove(bullet);
                    }catch (Exception e) { }
                }
                else
                {
                    try
                    {
                        playerBulletList.Remove(bullet);
                    }catch (Exception e) { }
                }
                if (tank.Type == 0)
                {
                    break;
                }
                Thread.Sleep(10);
            }
        }