コード例 #1
0
        private void gameLoop_Tick(object sender, EventArgs e)
        {
            //update movement of zombies
            foreach (zombie z in zombieList)
            {
                z.Move(zombieSpeed);
            }

            //update movement of peas
            if (peaShot == true)
            {
                foreach (peashooter p in peashooterList)
                {
                    Pea pea1 = new Pea(peaSize, p.x + 45, p.y - 15);
                    peaList.Add(pea1);
                }
                peaShot = false;
            }
            foreach (Pea pe in peaList)
            {
                pe.Shoot(peaSpeed);
            }

            //updating sun label
            sunLabel.Text = sun + " Sun";

            // creating rectangles for plants
            foreach (zombie z in zombieList)
            {
                Rectangle zombieRec = new Rectangle(z.x, z.y, zombieSize, zombieSize + 50);


                //check for collision of plants and zombies
                foreach (peashooter p in peashooterList)
                {
                    Rectangle peashooterRec = new Rectangle(p.x, p.y, plantSize, plantSize);
                    if (zombieRec.IntersectsWith(peashooterRec))
                    {
                        //gameLoop.Enabled = false;
                        //TODO -  peashooterList.Remove();
                    }
                }
                //check for collision of zombies and peas
                foreach (Pea pe in peaList)
                {
                    Rectangle peaRec = new Rectangle(pe.x, pe.y, plantSize, plantSize);
                    if (zombieRec.IntersectsWith(peaRec))
                    {
                        health -= 1;
                        peaList.RemoveAt(0);
                    }
                }

                //check for collision of plants and zombies
                foreach (sunflower s in sunflowerList)
                {
                    Rectangle sunflowerRec = new Rectangle(s.x, s.y, plantSize, plantSize);

                    if (zombieRec.IntersectsWith(sunflowerRec))
                    {
                        //gameLoop.Enabled = false;
                        //TODO - sunflowerList.Remove
                    }
                }
            }

            if (health == 0)
            {
                //TODO zombieList.RemoveAt(0);
            }

            //checking to see if places modes are activated
            if (peashooterPlaceMode == true)
            {
                PeashooterPlace();
            }
            if (sunflowerPlaceMode == true)
            {
                SunflowerPlace();
            }
            if (shovelMode == true)
            {
                Shovel();
            }

            //end game
            foreach (zombie z in zombieList)
            {
                if (z.x < 0)
                {
                    gameLoop.Enabled        = false;
                    sunflowerTimer.Enabled  = false;
                    peashooterTimer.Enabled = false;
                    zombieTimer.Enabled     = false;
                    Thread.Sleep(1000);

                    MainScreen ms = new MainScreen();

                    this.Controls.Add(ms);

                    ms.Location = new Point((this.Width - ms.Width) / 2, (this.Height - ms.Height) / 2);
                }
            }
            Refresh();
        }
コード例 #2
0
        private void gameLoop_Tick(object sender, EventArgs e)
        {
            //update movement of zombies
            foreach (zombie z in zombieList)
            {
                z.Move(zombieSpeed);
            }

            //update movement of peas
            if (peaShot == true && zombieList.Count >= 1)
            {
                foreach (peashooter p in peashooterList)
                {
                    Pea pea1 = new Pea(peaSize, p.x + 45, p.y - 15);
                    peaList.Add(pea1);
                }
                peaShot = false;
            }
            foreach (Pea pe in peaList)
            {
                pe.Shoot(peaSpeed);
            }

            //updating sun label
            sunLabel.Text = sun + " Sun";

            // collisions
            Collisions();

            //checking to see if places modes are activated
            if (peashooterPlaceMode == true)
            {
                PeashooterPlace();
            }
            if (sunflowerPlaceMode == true)
            {
                SunflowerPlace();
            }
            if (shovelMode == true)
            {
                Shovel();
            }

            //end game
            foreach (zombie z in zombieList)
            {
                if (z.x < 0)
                {
                    gameLoop.Enabled        = false;
                    sunflowerTimer.Enabled  = false;
                    peashooterTimer.Enabled = false;
                    zombieTimer.Enabled     = false;
                    Thread.Sleep(1000);

                    peaLabel.Visible          = false;
                    sunflowerLabel.Visible    = false;
                    sunLabel.Visible          = false;
                    noSunLabel.Visible        = false;
                    sunflowerButton.Visible   = false;
                    peaButton.Visible         = false;
                    shovelButton.Visible      = false;
                    placeCancelButton.Visible = false;
                    pauseButton.Visible       = false;
                    PlaceCancel();

                    MainScreen ms = new MainScreen();

                    this.Controls.Add(ms);

                    ms.Location = new Point((this.Width - ms.Width) / 2, (this.Height - ms.Height) / 2);
                }
            }
            Refresh();
        }