Exemplo n.º 1
0
        public Level5(int score, int lives, Splash splash) : base(score, lives, splash)
        {
            Text += ": Level 5";

            SplashHold = splash;

            // Platforms
            Platform plat1 = new Platform(10, 38 * JS, 950, 10, "platform");

            Controls.Add(plat1);
            Platform edge5 = new Platform(500, 30 * JS, 12, 120, "edge");

            Controls.Add(edge5);
            Platform edge6 = new Platform(200, 30 * JS, 12, 120, "edge");

            Controls.Add(edge6);
            Platform edge7 = new Platform(800, 30 * JS, 12, 120, "edge");

            Controls.Add(edge7);
            Platform plat2 = new Platform(10, 30 * JS, 950, 10, "platform");

            Controls.Add(plat2);
            Platform plat3 = new Platform(10, 22 * JS, 950, 10, "platform");

            Controls.Add(plat3);
            Platform edge3 = new Platform(666, 22 * JS, 12, 120, "edge");

            Controls.Add(edge3);
            Platform edge4 = new Platform(333, 22 * JS, 12, 120, "edge");

            Controls.Add(edge4);
            Platform plat4 = new Platform(10, 14 * JS, 950, 10, "platform");

            Controls.Add(plat4);
            Platform edge1 = new Platform(666, 6 * JS, 12, 120, "edge");

            Controls.Add(edge1);
            Platform edge2 = new Platform(333, 6 * JS, 12, 120, "edge");

            Controls.Add(edge2);
            Platform plat5 = new Platform(10, 6 * JS, 950, 10, "platform");

            Controls.Add(plat5);
            Platform edge8 = new Platform(500, 14 * JS, 12, 120, "edge");

            Controls.Add(edge8);
            Platform edge9 = new Platform(200, 14 * JS, 12, 120, "edge");

            Controls.Add(edge9);
            Platform edge10 = new Platform(800, 14 * JS, 12, 120, "edge");

            Controls.Add(edge10);

            // Barrels
            Barrel barrel1 = new Barrel(710, 650, true);

            Controls.Add(barrel1);
            Barrel barrel2 = new Barrel(730, 650, true);

            Controls.Add(barrel2);
            Barrel barrel3 = new Barrel(750, 650, true);

            Controls.Add(barrel3);

            // Ghosts
            Ghost ghost1 = new Ghost(500, 500, true);

            Controls.Add(ghost1);
            Ghost ghost2 = new Ghost(500, 500, false);

            Controls.Add(ghost2);
            Ghost ghost3 = new Ghost(800, 300, true);

            Controls.Add(ghost3);
            Ghost ghost4 = new Ghost(800, 300, false);

            Controls.Add(ghost4);
            Ghost ghost5 = new Ghost(200, 300, false);

            Controls.Add(ghost5);

            // Turtles
            Turtle turtle1 = new Turtle(20, 554, false);

            Controls.Add(turtle1);
            Turtle turtle2 = new Turtle(820, 554, false);

            Controls.Add(turtle2);
            Turtle turtle3 = new Turtle(20, 194, false);

            Controls.Add(turtle3);
            Turtle turtle4 = new Turtle(820, 194, false);

            Controls.Add(turtle4);

            // Exit
            Entity exit = new Entity(500, 61, "exit");

            Controls.Add(exit);

            // Coins
            Entity coin1 = new Entity(900, 180, "coin");

            Controls.Add(coin1);
            Entity coin2 = new Entity(880, 180, "coin");

            Controls.Add(coin2);
            Entity coin3 = new Entity(860, 180, "coin");

            Controls.Add(coin3);
            Entity coin4 = new Entity(840, 180, "coin");

            Controls.Add(coin4);
            Entity coin5 = new Entity(820, 180, "coin");

            Controls.Add(coin5);
            Entity coin6 = new Entity(800, 180, "coin");

            Controls.Add(coin6);
            Entity coin7 = new Entity(780, 180, "coin");

            Controls.Add(coin7);
            Entity coin8 = new Entity(760, 180, "coin");

            Controls.Add(coin8);
            Entity coin9 = new Entity(900, 160, "coin");

            Controls.Add(coin9);
            Entity coin10 = new Entity(880, 160, "coin");

            Controls.Add(coin10);
            Entity coin11 = new Entity(860, 160, "coin");

            Controls.Add(coin11);
            Entity coin12 = new Entity(840, 160, "coin");

            Controls.Add(coin12);
            Entity coin13 = new Entity(820, 160, "coin");

            Controls.Add(coin13);
            Entity coin14 = new Entity(800, 160, "coin");

            Controls.Add(coin14);
            Entity coin15 = new Entity(780, 160, "coin");

            Controls.Add(coin15);
            Entity coin16 = new Entity(760, 160, "coin");

            Controls.Add(coin16);
            Entity coin17 = new Entity(150, 160, "coin");

            Controls.Add(coin17);
            Entity coin18 = new Entity(130, 160, "coin");

            Controls.Add(coin18);
            Entity coin19 = new Entity(130, 180, "coin");

            Controls.Add(coin19);
            Entity coin20 = new Entity(150, 180, "coin");

            Controls.Add(coin20);
        }
        // Game timer that determines player and enemy movement and collisions
        private void Timer_Tick(object sender, System.EventArgs e)
        {
            // If player is not on a platform, apply jumpspeed
            if (!onPlatform)
            {
                player.Top += jumpSpeed;
            }

            // Prevents infinite jump
            if (jumping && force < 0)
            {
                jumping = false;
            }

            // Left and right movements
            if (goLeft)
            {
                player.Left -= 6;
            }
            else if (goRight)
            {
                player.Left += 6;
            }

            // When jumping, apply negative jumpspeed and force
            if (jumping)
            {
                jumpSpeed = -1 * JS;
                force    -= 1;
            }
            // Else, apply positive jumpspeed
            else
            {
                jumpSpeed = JS;
            }

            // Collision Checks
            foreach (Control x in this.Controls)
            {
                // Intersection Checks
                if (player.Bounds.IntersectsWith(x.Bounds))
                {
                    // Player cannot go through floor
                    if (x.Tag.Equals("floor") && !jumping)
                    {
                        force           = 8;
                        player.Top      = x.Top - player.Height;
                        onPlatform      = true;
                        currentPlatform = (Platform)x;

                        // When player lands on floor, switch sprite from jumping to
                        // appropriate image
                        if (goLeft)
                        {
                            player.Image = Image.FromFile("run_left.gif");
                        }
                        else if (goRight)
                        {
                            player.Image = Image.FromFile("run_right.gif");
                        }
                        else
                        {
                            if (lookLeft)
                            {
                                player.Image = Image.FromFile("stand_left.png");
                            }
                            else
                            {
                                player.Image = Image.FromFile("stand_right.png");
                            }
                        }
                    }
                    // Player cannot go through edges
                    if (x.Tag.Equals("edge"))
                    {
                        if (goLeft)
                        {
                            player.Left = x.Left + player.Width;
                        }
                        if (goRight)
                        {
                            player.Left = x.Left - player.Width;
                        }
                    }

                    // Player collects a coin
                    if (x.Tag.Equals("coin"))
                    {
                        UpdateScore(10);
                        x.Dispose();
                        sfxPlayer = new System.Media.SoundPlayer("Coin.wav");
                        sfxPlayer.Play();
                    }
                    // Player collects a heart
                    if (x.Tag.Equals("heart"))
                    {
                        UpdateLives(1);
                        x.Dispose();
                        sfxPlayer = new System.Media.SoundPlayer("getItem.wav");
                        sfxPlayer.Play();
                    }
                    // Player reaches the exit
                    if (x.Tag.Equals("exit"))
                    {
                        UpdateScore(50);
                        timer.Stop();
                        Next();
                    }
                } // End of Intersection Checks

                // Player landing on platform check
                if (x.Tag.Equals("platform") && player.Right > x.Left && player.Left < x.Right &&
                    (player.Bottom <= x.Top && player.Bottom >= x.Top - 20) && !jumping)
                {
                    force           = 8;
                    player.Top      = x.Top - player.Height;
                    onPlatform      = true;
                    currentPlatform = (Platform)x;

                    // When player lands on platform, switch sprite from jumping to
                    // appropriate image
                    if (goLeft)
                    {
                        player.Image = Image.FromFile("run_left.gif");
                    }
                    else if (goRight)
                    {
                        player.Image = Image.FromFile("run_right.gif");
                    }
                    else
                    {
                        if (lookLeft)
                        {
                            player.Image = Image.FromFile("stand_left.png");
                        }
                        else
                        {
                            player.Image = Image.FromFile("stand_right.png");
                        }
                    }
                }

                // Player landing on a turtle of barrel, or getting hit by them
                if ((x.Tag.Equals("turtle") || x.Tag.Equals("barrel")))
                {
                    // If player land on a turtle or barrel, kill it and add to score
                    if (player.Right > x.Left && player.Left < x.Right &&
                        (player.Bottom < x.Top && player.Bottom >= x.Top - 20) && !jumping)
                    {
                        if (x.Tag.Equals("turtle"))
                        {
                            UpdateScore(15);
                            sfxPlayer = new System.Media.SoundPlayer("turtledie.wav");
                            sfxPlayer.Play();
                        }
                        else
                        {
                            UpdateScore(20);
                        }
                        x.Dispose();
                    }

                    // Else player is attacked by turtle or barrel, reset to start and lose a life
                    else if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        player.Left  = 60;
                        player.Top   = 600;
                        player.Image = Image.FromFile("death.png");
                        onPlatform   = false;
                        UpdateScore(-10);
                        UpdateLives(-1);
                        if (GetLives() == 0)
                        {
                            End();
                        }
                    }
                }

                // Player is attacked by a ghost
                if (x.Tag.Equals("ghost") && player.Bounds.IntersectsWith(x.Bounds))
                {
                    player.Left  = 60;
                    player.Top   = 600;
                    player.Image = Image.FromFile("death.png");
                    onPlatform   = false;
                    UpdateScore(-10);
                    UpdateLives(-1);
                    if (GetLives() == 0)
                    {
                        End();
                    }
                }

                // Update Enemy movements
                if (x is Enemy)
                {
                    Enemy temp = (Enemy)x;
                    temp.Movement();
                }

                // Barrel Gravity
                if (x.Tag.Equals("barrel"))
                {
                    Barrel temp = (Barrel)x;
                    foreach (Control y in this.Controls)
                    {
                        // If barrel is not on a platform or another barrel
                        if (!temp.OnPlatform && !(y is Barrel))
                        {
                            // If barrel intersects with a platform or floo,
                            // set it as the platform that it is on.
                            if ((y.Tag.Equals("platform") || y.Tag.Equals("floor")) &&
                                temp.Bounds.IntersectsWith(y.Bounds))
                            {
                                Platform temp2 = (Platform)y;
                                temp.Platform = temp2;
                            }
                        }

                        // Reverse the barrel direction if it hits an edge
                        if (y.Tag.Equals("edge") && temp.Bounds.IntersectsWith(y.Bounds))
                        {
                            if (temp.GoingLeft)
                            {
                                temp.GoingLeft = false;
                            }
                            else
                            {
                                temp.GoingLeft = true;
                            }
                        }
                    }
                } // End of Barrel Gravity
            }     // End of Collision Checks

            // If a player is on a platform and moves off of the platform, they begin to fall
            if (currentPlatform != null)
            {
                // Leave from the left
                if (goLeft && player.Right < currentPlatform.Left)
                {
                    onPlatform      = false;
                    currentPlatform = null;
                }

                // Leave from the right
                if (goRight && player.Left > currentPlatform.Right)
                {
                    onPlatform      = false;
                    currentPlatform = null;
                }
            }
        }