Exemplo n.º 1
0
        private void gameLoop_Tick(object sender, EventArgs e)
        {
            //update location of all boxes (drop down screen)
            foreach (Box b in leftBoxes)
            {
                b.leftMove();
            }

            foreach (Box b in rightBoxes)
            {
                b.rightMove();
            }

            //add new box if it is time

            newBoxCounter++;

            if (newBoxCounter == 60)
            {
                //creating all of the boxes on the screen and adding them to lists.
                Box b1 = new Box(-40, 100, boxSize, boxSpeed3);
                Box b2 = new Box(-40, 130, boxSize, boxSpeed2);
                Box b3 = new Box(-40, 300, boxSize, boxSpeed1);
                Box b4 = new Box(1000, 200, boxSize, boxSpeed3);
                Box b5 = new Box(1000, 250, boxSize, boxSpeed2);
                Box b6 = new Box(1000, 350, boxSize, boxSpeed1);

                leftBoxes.Add(b1);
                leftBoxes.Add(b2);
                leftBoxes.Add(b3);
                rightBoxes.Add(b4);
                rightBoxes.Add(b5);
                rightBoxes.Add(b6);
                newBoxCounter = 0;
            }
            //Removing boxes after they leave the screen
            foreach (Box b in leftBoxes)
            {
                if (leftBoxes[0].x > this.Width - 20)
                {
                    leftBoxes.RemoveAt(0);
                    break;
                }
            }

            foreach (Box b in rightBoxes)
            {
                if (rightBoxes[0].x < -20)
                {
                    rightBoxes.RemoveAt(0);
                    break;
                }
            }

            //hero collison with the sides of the screen
            if (hero.x < -40)
            {
                hero.x = 900;
            }
            if (hero.x > 900)
            {
                hero.x = -40;
            }

            //move our hero
            if (leftArrowDown == true)
            {
                hero.Move("left");
                leftArrowDown = false;
            }

            if (leftArrowUp == true)
            {
                leftArrowDown = true;
            }

            if (rightArrowDown == true)
            {
                hero.Move("right");
                rightArrowDown = false;
            }

            if (rightArrowUp == true)
            {
                rightArrowDown = true;
            }

            if (upArrowDown == true)
            {
                hero.Move("up");
                upArrowDown = false;
                score1++;
            }

            if (upArrowUp == true)
            {
                upArrowUp = true;
            }

            if (downArrowDown == true)
            {
                hero.Move("down");
                downArrowDown = false;
                score1        = score1 - 2;
            }

            if (downArrowUp == true)
            {
                downArrowUp = true;
            }

            //Check for collision between hero and boxes
            foreach (Box b in leftBoxes)
            {
                if (hero.Collision(b))
                {
                    squashPlayer.Play();

                    gameLoop.Enabled = false;
                    Form f = this.FindForm();
                    f.Controls.Remove(this);

                    GameoverScreen gos = new GameoverScreen();
                    f.Controls.Add(gos);
                }
            }

            foreach (Box b in rightBoxes)
            {
                if (hero.Collision(b))
                {
                    squashPlayer.Play();

                    gameLoop.Enabled = false;
                    Form f = this.FindForm();
                    f.Controls.Remove(this);

                    GameoverScreen gos = new GameoverScreen();
                    f.Controls.Add(gos);
                }
            }

            //check top wall
            if (hero.wallCollision(topWall))
            {
                hero.Move("down");
            }
            //check bottom wall
            if (hero.wallCollision(bottomWall))
            {
                hero.Move("up");
            }
            //check victory wall
            if (hero.wallCollision(victoryWall))
            {
                Thread.Sleep(1500);

                gameLoop.Enabled = false;
                Form f = this.FindForm();
                f.Controls.Remove(this);

                victoryScreen vs = new victoryScreen();
                f.Controls.Add(vs);
            }

            Refresh();
        }