Esempio n. 1
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            // create New Shoot in game every  sec

            // Create Game Shoot
            Random     rnd = new Random();
            int        pos1, pos2, pos3;
            List <int> L = new List <int>();

            L.Add(prev1);
            L.Add(prev2);
            L.Add(prev3);
            L.Sort();
            while (true)
            {
                pos1 = rnd.Next(0, 8);
                pos2 = rnd.Next(0, 8);
                pos3 = rnd.Next(0, 8);
                List <int> L1 = new List <int>();
                L1.Add(pos1);
                L1.Add(pos2);
                L1.Add(pos3);
                if (pos1 != pos2 && pos2 != pos3 && pos1 != pos3)
                {
                    bool ok = true;
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            if (L[i] == L1[j])
                            {
                                ok = false;
                            }
                        }
                    }
                    if (ok)
                    {
                        break;
                    }
                }
            }
            prev1 = pos1;
            prev2 = pos2;
            prev3 = pos3;
            PictureBox picture = new PictureBox
            {
                Name      = "pictureBox",
                Size      = shootgame.Size,
                BackColor = shootgame.BackColor,
                Location  = new Point(posofshoots[pos1], 58),
                //  BackColor = System.Drawing.Color.Lime,
                Image    = shootgame.Image,
                SizeMode = shootgame.SizeMode,
            };

            this.Controls.Add(picture);
            Shoots.Add(picture);
            int r1 = rnd.Next(1, 4);    // 1 for horz , 2 for left ,3 for right

            // lvl 2 of game
            if (lvl2 == true)
            {
                moveLeft.Add(r1);
            }
            else
            {
                moveLeft.Add(1);
            }
            if (lvl2)
            {
                PictureBox picture1 = new PictureBox
                {
                    Name      = "pictureBox",
                    Size      = shootgame2.Size,
                    BackColor = shootgame2.BackColor,
                    Location  = new Point(posofshoots[pos2], 58),
                    //  BackColor = System.Drawing.Color.Lime,
                    Image    = shootgame2.Image,
                    SizeMode = shootgame2.SizeMode,
                };
                this.Controls.Add(picture1);
                Shoots.Add(picture1);

                r1 = rnd.Next(1, 4);    // 1 for horz , 2 for left ,3 for right
                // lvl 2 of game
                if (lvl2 == true)
                {
                    moveLeft.Add(r1);
                }
                else
                {
                    moveLeft.Add(1);
                }
                PictureBox picture2 = new PictureBox
                {
                    Name      = "pictureBox",
                    Size      = shootgame3.Size,
                    BackColor = shootgame3.BackColor,
                    Location  = new Point(posofshoots[pos3], 58),
                    //  BackColor = System.Drawing.Color.Lime,
                    Image    = shootgame3.Image,
                    SizeMode = shootgame3.SizeMode,
                };
                this.Controls.Add(picture2);
                Shoots.Add(picture2);
                r1 = rnd.Next(1, 4);    // 1 for horz , 2 for left ,3 for right
                // lvl 2 of game
                if (lvl2 == true)
                {
                    moveLeft.Add(r1);
                }
                else
                {
                    moveLeft.Add(1);
                }
            }
            // determine objects that not in form and delete it
            if (Shoots.Count > 0)
            {
                timer2.Enabled = false;
                for (int i = 0; i < Shoots.Count; i++)
                {
                    if (Shoots[i].Top > 640) // not in form
                    {
                        Shoots[i].Dispose();
                        Shoots.RemoveAt(i);
                        i = -1;
                        if (Health.Value > 0)
                        {
                            Health.Value -= 25;
                        }
                    }
                }
                timer2.Enabled = true;
            }
            if (PlayerShoots.Count > 20)
            {
                timer2.Enabled = false;
                for (int i = 0; i < PlayerShoots.Count; i++)
                {
                    if (PlayerShoots[i].Top < 23)
                    {
                        PlayerShoots[i].Dispose();
                        PlayerShoots.RemoveAt(i);
                        i = -1; // to start loop from first after remove
                        // MessageBox.Show(i.ToString());
                    }
                }
                timer2.Enabled = true;
            }
        }
Esempio n. 2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            Scoretxt.Text = "Score : " + score.ToString();
            if (score == 500)
            {
                distmov = 2;
            }
            if (score == 1000)
            {
                distmov = 3;
            }
            if (score == 1500)
            {
                distmov = 4;
            }
            if (score == 2000)
            {
                distmov = 5;
            }
            if (score == 100 && lvl2 == false)
            {
                lvl2 = true;
                Point p = new Point();
                p.X = 278;
                p.Y = 331;
                if (music)
                {
                    playerwinsound.Play();
                }
                lvltwo.Location = p;
                lvltwo.Visible  = true;
                timer1.Enabled  = false;
                timer2.Enabled  = false;
            }
            if (playwithkeyboard)
            {
                if (keyleft)
                {
                    if (player.Left - 10 > 2)
                    {
                        player.Left -= 10;
                    }
                    // keyleft = false;
                }
                else if (keyright)
                {
                    if (player.Left + 10 < 709)
                    {
                        player.Left += 10;
                    }
                    // keyright = false;
                }
                else if (keyup)
                {
                    if (player.Top - 10 > 2)
                    {
                        player.Top -= 10;
                    }
                    //keyup = false;
                }
                else if (keydown)
                {
                    if (player.Top + 10 < 540)
                    {
                        player.Top += 10;
                    }
                    //keydown = false;
                }
            }
            else
            {
                if (inside && Cursor.Position.X - (player.Width + 10) * 2 > 2 && Cursor.Position.X - (player.Width + 10) * 2 < 709)
                {
                    player.Left = Cursor.Position.X - (player.Width + 10) * 2;
                }
                //if (Cursor.Position.X - Left - (player.Width / 2) >= 2 && Cursor.Position.X - Left - (player.Width / 2) < 709)
                //{
                //    if (Cursor.Position.Y - Top - 31 - (player.Height / 2) > 2 && Cursor.Position.Y - Top - 31 - (player.Height / 2) <540)
                //    player.Location = new Point(Cursor.Position.X - Left - (player.Width / 2), Cursor.Position.Y - Top - 31 - (player.Height / 2));

                //}
            }

            // move Shoots
            for (int i = 0; i < Shoots.Count; i++)
            {
                Shoots[i].Top += distmov;
                if (lvl2)                 // move diagonal in lvl2
                {
                    if (moveLeft[i] == 2) // move left
                    {
                        Shoots[i].Left -= distmov;
                        if (Shoots[i].Left <= -20)
                        {
                            moveLeft[i] = 3; // move right
                        }
                    }
                    else if (moveLeft[i] == 3) // move right
                    {
                        Shoots[i].Left += 1;
                        if (Shoots[i].Left >= 768)
                        {
                            moveLeft[i] = 2; // move left
                        }
                    }
                }
            }
            for (int i = 0; i < PlayerShoots.Count; i++)
            {
                PlayerShoots[i].Top -= 5;
            }

            // Create New Shoot
            if (keyspace || movdown)
            {
                // playershootsound.Play();

                Point p = new Point();
                p = player.Location;
                PictureBox picture = new PictureBox
                {
                    Name      = "pictureBox",
                    Size      = shootplayer.Size,
                    BackColor = shootplayer.BackColor,
                    Location  = new Point(p.X + 45, p.Y),
                    //  BackColor = System.Drawing.Color.Lime,
                    Image    = shootplayer.Image,
                    SizeMode = shootplayer.SizeMode,
                };
                keyspace = false;
                movdown  = false;
                this.Controls.Add(picture);
                PlayerShoots.Add(picture);
            }
            // Remove Shoots that hit
            for (int i = 0; i < PlayerShoots.Count; i++)
            {
                for (int j = 0; j < Shoots.Count; j++)
                {
                    if (Shoots[j].Bounds.IntersectsWith(PlayerShoots[i].Bounds))
                    {
                        score += 10;
                        PlayerShoots[i].Visible = false;
                        Shoots[j].Visible       = false;
                        this.Controls.Remove(Shoots[j]);
                        this.Controls.Remove(PlayerShoots[i]);
                        Shoots[j].Dispose();
                        PlayerShoots[i].Dispose();
                        PlayerShoots.RemoveAt(i);
                        Shoots.RemoveAt(j);
                        moveLeft.RemoveAt(j);
                        break;
                    }
                }
            }
            for (int j = 0; j < Shoots.Count; j++)
            {
                if (Shoots[j].Bounds.IntersectsWith(player.Bounds))
                {
                    Shoots[j].Visible = false;
                    this.Controls.Remove(Shoots[j]);
                    Shoots[j].Dispose();
                    Shoots.RemoveAt(j);
                    moveLeft.RemoveAt(j);
                    if (Health.Value > 0)
                    {
                        Health.Value -= 25;
                    }
                    if (Health.Value == 0)
                    {
                        if (music)
                        {
                            gameover.Play();
                        }
                        timer1.Enabled = false;
                        timer2.Enabled = false;
                        Point p = new Point();
                        p.X = 278;
                        p.Y = 331;
                        gameover1.Location = p;
                        gameover1.Visible  = true;
                    }
                    break;
                }
            }
            if (timer1.Enabled && Health.Value == 0)
            {
                if (music)
                {
                    gameover.Play();
                }
                timer1.Enabled = false;
                timer2.Enabled = false;
                Point p = new Point();
                p.X = 278;
                p.Y = 331;
                gameover1.Location = p;
                gameover1.Visible  = true;
            }
        }