Пример #1
0
        //таймер (все логика)

        private void timer1_Tick(object sender, EventArgs e)
        {
            ///////////////////////////////////////////////отрисовка нижней панель

            int x = 150, y = 760;

            for (int i = 0; i < setting.lifesPlayer1; i++)
            {
                grMemory.DrawImage(heart, x, y, 25, 25);
                x += 30;
            }
            if (setting.twoPlayer)
            {
                x = 530;
                for (int i = 0; i < setting.lifesPlayer2; i++)
                {
                    grMemory.DrawImage(heart, x, y, 25, 25);
                    x += 30;
                }
            }

            int countDegree = 0;
            int numBuf;

            int[] masNum    = new int[10];
            int   bufPoints = setting.pointsPlayer1;

            x = 255;

            if (bufPoints == 0)
            {
                Numbers.Draw(grMemory, Numbers.ReturnNumPic(0), x, 760);
            }
            else
            {
                while (bufPoints != 0)
                {
                    numBuf = bufPoints % 10;
                    masNum[countDegree] = numBuf;
                    bufPoints          /= 10;
                    countDegree++;
                }
            }

            for (int i = countDegree - 1; i >= 0; i--)
            {
                Numbers.Draw(grMemory, Numbers.ReturnNumPic(masNum[i]), x, 760);
                x += 30;
            }

            if (setting.twoPlayer)
            {
                countDegree = 0;
                bufPoints   = setting.pointsPlayer2;

                x = 633;

                if (bufPoints == 0)
                {
                    Numbers.Draw(grMemory, Numbers.ReturnNumPic(0), x, 760);
                }
                else
                {
                    while (bufPoints != 0)
                    {
                        numBuf = bufPoints % 10;
                        masNum[countDegree] = numBuf;
                        bufPoints          /= 10;
                        countDegree++;
                    }
                }

                for (int i = countDegree - 1; i >= 0; i--)
                {
                    Numbers.Draw(grMemory, Numbers.ReturnNumPic(masNum[i]), x, 760);
                    x += 30;
                }
            }

            /////////////////////////////////////////////////////спавн ботов
            if (timeSpawn == 0 && setting.countEnemies >= 0)
            {
                Random random = new Random();
                bool[] mas    = new bool[4] {
                    false, false, false, false
                };
                int num = random.Next(0, 4);

                for (int i = 0; i < tanks.Count; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (Collisions.check_touch(level.masPointsSpawnEnemy[j, 0], level.masPointsSpawnEnemy[j, 1], tanks[i].x, tanks[i].y))
                        {
                            mas[j] = true;
                        }
                    }
                }

                num = random.Next(0, 4);
                if (mas[0] == true && mas[1] == true && mas[2] == true)
                {
                    timeSpawn = setting.timeSpawn;
                }
                else
                {
                    while (mas[num] == true)
                    {
                        num = random.Next(0, 4);
                    }

                    EnemyTank enemy = new EnemyTank(Peace_of_Tanks.Properties.Resources.enemy11, Peace_of_Tanks.Properties.Resources.enemy12,
                                                    Peace_of_Tanks.Properties.Resources.bulletEnemy, level.masPointsSpawnEnemy[num, 0], level.masPointsSpawnEnemy[num, 1], 2);
                    enemies.Add(enemy);
                    tanks.Add(enemy);
                    MoveEnemy += enemy.Move;

                    timeSpawn = setting.timeSpawn;
                }
            }
            else
            {
                timeSpawn--;
            }

            //////////////////////////////////////////////проверка столкновений
            for (int i = 0; i < bullets.Count; i++) //проверка столкновения пуль и ящиков
            {
                bool bulletDeath = false;
                for (int j = 0; j < boxes.Count; j++)
                {
                    if (!bulletDeath && Collisions.check_touch(this, bullets[i], boxes[j]) == 2)
                    {
                        boxes[j].hitPoint--;
                        if (boxes[j].hitPoint == 0)
                        {
                            Animation anim = new Animation();

                            anim.message.x     = boxes[j].x;
                            anim.message.y     = boxes[j].y;
                            anim.message.type  = 0;
                            anim.message.delay = 10;
                            DelayEvent        += anim.Delay;

                            animations.Add(anim);

                            boxes.RemoveAt(j);
                        }
                        bullets.RemoveAt(i);

                        bulletDeath = true;

                        break;
                    }
                    else if (!bulletDeath && Collisions.check_touch(this, bullets[i], boxes[j]) == 1)
                    {
                        bullets.RemoveAt(i);
                        bulletDeath = true;

                        break;
                    }
                }

                //столкновение пуль с ботами
                for (int j = 0; j < enemies.Count; j++)
                {
                    if (!bulletDeath && Collisions.check_touch(bullets[i], enemies[j]))
                    {
                        if (bullets[i].who == 1)
                        {
                            setting.pointsPlayer1 += setting.pointsForEnemy;
                        }
                        else if (bullets[i].who == 2)
                        {
                            setting.pointsPlayer2 += setting.pointsForEnemy;
                        }

                        if (bullets[i].who != 0)
                        {
                            enemies[j].hitpoints--;
                            bullets.RemoveAt(i);
                            setting.countEnemies--;
                            bulletDeath = true;

                            break;
                        }
                    }
                }

                //столкновение пуль с 1 игроком
                if (!bulletDeath && firstPlayer && Collisions.check_touch(bullets[i], player1))
                {
                    if (bullets[i].who == 2)
                    {
                        setting.pointsPlayer2 += setting.pointsForPlayers;
                    }

                    player1.hitpoints--;
                    bullets.RemoveAt(i);
                    bulletDeath = true;
                }

                //столкновение пуль со 2 игроком
                if (!bulletDeath && secondPlayer && Collisions.check_touch(bullets[i], player2))
                {
                    if (bullets[i].who == 1)
                    {
                        setting.pointsPlayer1 += setting.pointsForPlayers;
                    }

                    player2.hitpoints--;
                    bullets.RemoveAt(i);
                    bulletDeath = true;
                }

                //движение пули
                if (!bulletDeath)
                {
                    bullets[i].Move();
                }
                else
                {
                    i--;
                }
            }

            //задержка отрисовки анимации  (делегат)
            if (DelayEvent != null)
            {
                DelayEvent();
            }

            //движение ботов
            if (MoveEnemy != null)
            {
                for (int i = 0; i < setting.speedEnemy; i++)
                {
                    MoveEnemy(this, boxes, bullets, tanks);
                }
            }
            ;

            /////////////////////////////////////// отрисовка

            for (int i = 0; i < boxes.Count; i++) //отрисовка ящиков
            {
                if (!(boxes[i] is Box_Green))
                {
                    boxes[i].Draw(grMemory);
                }
            }
            for (int i = 0; i < bullets.Count; i++) //отрисовка пуль
            {
                bullets[i].Draw(grMemory);
            }
            for (int i = 0; i < enemies.Count; i++) //отрисовка ботов
            {
                enemies[i].Draw(grMemory);
            }

            if (firstPlayer)
            {
                player1.Draw(grMemory);                                 // отрисовка танков игроков
            }
            if (secondPlayer)
            {
                player2.Draw(grMemory);
            }

            for (int i = 0; i < boxes.Count; i++) // отрисовка лесных ящиков, позже отрисовки танков, чтобы танк мог прятаться в лесу
            {
                if (boxes[i] is Box_Green)
                {
                    boxes[i].Draw(grMemory);
                }
            }

            for (int i = 0; i < animations.Count; i++) // отрисовка анимации
            {
                animations[i].Draw(grMemory);
            }

            ////////////////////////////////////////////////////////////////////////проверка конца игры

            if ((setting.lifesPlayer1 == 0 && !setting.twoPlayer) || ((setting.lifesPlayer2 == 0 && setting.lifesPlayer1 == 0) && setting.twoPlayer) ||
                setting.countEnemies <= 0)
            {
                if (setting.countEnemies <= 0)
                {
                    pauseGameOver -= 1;
                }
                else
                if (gameoverY <= 350)
                {
                    grMemory.DrawImage(gameover, gameoverX, gameoverY += 8);
                }
                else
                {
                    grMemory.DrawImage(gameover, gameoverX, gameoverY);
                    pauseGameOver -= 1;
                }

                if (pauseGameOver == 0)   /////////////// сохранение статистики
                {
                    stats bufStat = new stats();
                    bufStat.difficulty = setting.difficulty;
                    bufStat.hearts     = setting.lifesPlayer1;
                    if (setting.twoPlayer)
                    {
                        bufStat.mode = 2;
                    }
                    else
                    {
                        bufStat.mode = 1;
                    }
                    bufStat.points = setting.pointsPlayer1;

                    setting.stats1 = bufStat;

                    bufStat        = new stats();
                    bufStat.hearts = setting.lifesPlayer2;
                    bufStat.points = setting.pointsPlayer2;

                    setting.stats2 = bufStat;

                    for (int i = 0; i < enemies.Count; i++)
                    {
                        MoveEnemy -= enemies[i].Move;
                    }
                    enemies.Clear();
                    boxes.Clear();
                    tanks.Clear();
                    bullets.Clear();

                    PicturePlayer1_.Visible = picturePlayer2_.Visible = false;

                    MovePlayer1 -= player1.Move;
                    MovePlayer2 -= player2.Move;

                    modeMenu = 4;
                    MenuWorking();

                    return;
                }
            }

            grMain.DrawImage(backgroundMemory, 0, 0);
            grMemory.DrawImage(backgroundMain, 0, 0);

            ////////////////////////////////////////////////////////////// логика

            if (player1.message.pauseSpace > 0)
            {
                player1.message.pauseSpace--;                                                    // пауза между выстрелами у игроков
            }
            if (secondPlayer && player2.message.pauseSpace > 0)
            {
                player2.message.pauseSpace--;       //
            }
            for (int i = 0; i < enemies.Count; i++) //пауза между выстрелами у ботов
            {
                enemies[i].message.pauseSpace--;
            }

            for (int i = 0; i < 5; i++)                                                         // движение танков(внутри проверка на соприкосновения танков)
            {
                if (firstPlayer)
                {
                    MovePlayer1(this, null, player1.message, boxes, tanks);
                }
                if (secondPlayer)
                {
                    MovePlayer2(this, null, player2.message, boxes, tanks);
                }
            }

            {
                if (firstPlayer)                                               //сообщение кто есть в игре (тольк 1 или 2 игрок, оба, или никого)
                {
                    EnemyEventArgs.player1X = player1.x;
                    EnemyEventArgs.player1Y = player1.y;
                    EnemyEventArgs.players  = 1;
                }
                if (secondPlayer)
                {
                    EnemyEventArgs.player2X = player2.x;
                    EnemyEventArgs.player2Y = player2.y;
                    EnemyEventArgs.players  = 2;
                }
                if (firstPlayer && secondPlayer)
                {
                    EnemyEventArgs.player1X = player1.x;
                    EnemyEventArgs.player1Y = player1.y;
                    EnemyEventArgs.player2X = player2.x;
                    EnemyEventArgs.player2Y = player2.y;
                    EnemyEventArgs.players  = 3;
                }
                else if (firstPlayer == false && secondPlayer == false)
                {
                    EnemyEventArgs.players = 0;
                }
            }

            for (int i = 0; i < enemies.Count; i++)                           //уничтожение врагов при хп = 0
            {
                if (enemies[i].hitpoints == 0)
                {
                    Animation anim = new Animation();

                    anim.message.x     = enemies[i].x;
                    anim.message.y     = enemies[i].y;
                    anim.message.type  = 1;
                    anim.message.delay = 10;
                    DelayEvent        += anim.Delay;

                    animations.Add(anim);

                    MoveEnemy -= enemies[i].Move;
                    tanks.RemoveAt(i + 2);
                    enemies.RemoveAt(i);
                    i--;
                }
            }

            if (secondPlayer && player2.hitpoints == 0)  // уничтожение второго игрока
            {
                setting.lifesPlayer2--;

                Animation anim = new Animation();

                anim.message.x     = player2.x;
                anim.message.y     = player2.y;
                anim.message.type  = 1;
                anim.message.delay = 10;
                DelayEvent        += anim.Delay;

                animations.Add(anim);

                Random RanNum = new Random();
                int    num    = RanNum.Next(0, 4);
                if (setting.lifesPlayer2 > 0)
                {
                    player2.x         = level.masPointsSpawnPlayers[num, 0];
                    player2.y         = level.masPointsSpawnPlayers[num, 1];
                    player2.hitpoints = 1;
                }
                else
                {
                    player2.x    = player2.y = -50;
                    secondPlayer = false;
                }
            }
            if (firstPlayer && player1.hitpoints == 0) // уничтожение первого игрока
            {
                setting.lifesPlayer1--;

                Animation anim = new Animation();

                anim.message.x     = player1.x;
                anim.message.y     = player1.y;
                anim.message.type  = 1;
                anim.message.delay = 10;
                DelayEvent        += anim.Delay;

                animations.Add(anim);

                Random RanNum = new Random();
                int    num    = RanNum.Next(0, 4);
                if (setting.lifesPlayer1 > 0)
                {
                    player1.x         = level.masPointsSpawnPlayers[num, 0];
                    player1.y         = level.masPointsSpawnPlayers[num, 1];
                    player1.hitpoints = 1;
                }
                else
                {
                    player1.x   = player1.y = -50;
                    firstPlayer = false;
                }
            }

            for (int i = 0; i < animations.Count; i++)   // уничтожение анимации, если она завершилась (отписка, делегат)
            {
                if (animations[i].message.delay <= 0)
                {
                    DelayEvent -= animations[i].Delay;
                    animations.RemoveAt(i);
                }
            }
        }