コード例 #1
0
        private void sideScrollerTick(object sender, EventArgs e)
        {
            player.Top += jumpSpeed;
            player.Refresh();

            if (isJumping && jumpForce < 0)
            {
                isJumping = false;
            }

            if (isJumping)
            {
                jumpSpeed  = -12;
                jumpForce -= 1;
            }
            else
            {
                jumpSpeed = 12;
            }

            if (isLeft)
            {
                player.Left -= speed;
            }

            if (isRight)
            {
                player.Left += speed;
            }
            if (isCrouching)
            {
                Size size = new Size(50, 25);
                player.Size = size;
            }
            else
            {
                Size size = new Size(50, 50);
                player.Size = size;
            }

            foreach (Control i in this.Controls)
            {
                if (i is PictureBox && (string)i.Tag == "background")
                {
                    i.Left -= backSpeed;
                }
                if (i is PictureBox && (string)i.Tag == "platform")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        jumpForce  = 8;
                        player.Top = i.Top - player.Height;
                        jumpSpeed  = 0;
                    }
                }

                if (i is PictureBox && (string)i.Tag == "leftEdge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = 0;
                    }
                }

                if (i is PictureBox && (string)i.Tag == "rightEdge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = rightEdge.Left - player.Width;
                    }
                }

                if (i is PictureBox && (string)i.Tag == "enemy")
                {
                    i.Left -= backSpeed;

                    if (player.Bounds.IntersectsWith(i.Bounds) && hasDamaged == false)
                    {
                        hasDamaged = true;
                        scrollerHealth--;
                        i.Left = player.Left - i.Width;
                        switch (scrollerHealth)
                        {
                        case 0:
                            GameOver go = new GameOver();
                            go.Show();
                            Application.OpenForms["MainGame"].Close();
                            this.Close();
                            break;

                        case 1:
                            health2.BackColor = Color.Black;
                            break;

                        case 2:
                            health3.BackColor = Color.Black;
                            break;

                        default:
                            break;
                        }
                    }
                    if (!player.Bounds.IntersectsWith(i.Bounds))
                    {
                        hasDamaged = false;
                    }
                }
                foreach (Control j in this.Controls)
                {
                    if ((j is PictureBox && (string)j.Tag == "background") && (i is PictureBox && (string)i.Tag == "rightFinish"))
                    {
                        if (!(i.Bounds.IntersectsWith(j.Bounds)))
                        {
                            Application.OpenForms["MainGame"].Show();
                            this.Close();
                        }
                    }
                }
            }
        }
コード例 #2
0
        private void mainGameTimer(object sender, EventArgs e)
        {
            player.Top += jumpSpeed;
            player.Refresh();
            if (isJumping && jumpForce < 0)
            {
                isJumping = false;
            }
            if (isJumping)
            {
                jumpSpeed  = -10;
                jumpForce -= 1;
            }
            else
            {
                jumpSpeed = 10;
            }
            if (isLeft)
            {
                player.Left -= speed;
            }
            if (isRight)
            {
                player.Left += speed;
            }
            if (isCrouching)
            {
                Size size = new Size(50, 25);
                player.Size = size;
            }
            else
            {
                Size size = new Size(50, 50);
                player.Size = size;
            }
            if (enemyHealth <= 0)
            {
                WinGame wg = new WinGame();
                wg.Show();
                this.Close();
            }


            foreach (Control i in this.Controls)
            {
                if (i is PictureBox && (string)i.Tag == "platform")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        jumpForce  = 10;
                        player.Top = i.Top - player.Height;
                        jumpSpeed  = 0;
                    }
                }
                if (i is PictureBox && (string)i.Tag == "edge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = 0;
                    }
                }
                if (i is PictureBox && (string)i.Tag == "rightEdge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = rightEdge.Left - player.Width;
                    }
                }
                if (i is PictureBox && (string)i.Tag == "enemy")
                {
                    i.Left -= 2;
                    if (player.Bounds.IntersectsWith(i.Bounds) && hasDamaged == false)
                    {
                        this.Controls.Remove((PictureBox)i);
                        ((PictureBox)i).Dispose();
                        hasDamaged = true;
                        --playerHealth;
                        switch (playerHealth)
                        {
                        case 0:
                            GameOver go = new GameOver();
                            go.Show();
                            this.Close();
                            break;

                        case 1:
                            health2.BackColor = Color.Black;
                            break;

                        case 2:
                            health3.BackColor = Color.Black;
                            break;

                        default:
                            break;
                        }
                    }
                    if (!(player.Bounds.IntersectsWith(i.Bounds)))
                    {
                        hasDamaged = false;
                    }
                }
                if (i is PictureBox && (string)i.Tag == "enemyTop")
                {
                    i.Top += 2;
                    if (player.Bounds.IntersectsWith(i.Bounds) && hasDamaged == false)
                    {
                        this.Controls.Remove((PictureBox)i);
                        ((PictureBox)i).Dispose();
                        hasDamaged = true;
                        --playerHealth;
                        switch (playerHealth)
                        {
                        case 0:
                            GameOver go = new GameOver();
                            go.Show();
                            this.Close();
                            break;

                        case 1:
                            health2.BackColor = Color.Black;
                            break;

                        case 2:
                            health3.BackColor = Color.Black;
                            break;

                        default:
                            break;
                        }
                    }
                    if (!(player.Bounds.IntersectsWith(i.Bounds)))
                    {
                        hasDamaged = false;
                    }
                }
                if (i is PictureBox && (string)i.Tag == "projectile")
                {
                    i.Left += projSpeed;
                    if (((PictureBox)i).Left > 782)
                    {
                        this.Controls.Remove((PictureBox)i);
                        ((PictureBox)i).Dispose();
                    }
                }
                foreach (Control j in this.Controls)
                {
                    if ((j is PictureBox && (string)j.Tag == "projectile") && (i is PictureBox && (string)i.Tag == "enemyHitbox"))
                    {
                        if (i.Bounds.IntersectsWith(j.Bounds))
                        {
                            --enemyHealth;
                            this.Controls.Remove(j);
                            j.Dispose();
                        }
                    }
                }
            }
        }
コード例 #3
0
        private void TopDownTimer_Tick(object sender, EventArgs e)
        {
            if (isLeft)
            {
                player.Left -= playerSpeed;
            }
            if (isRight)
            {
                player.Left += playerSpeed;
            }
            if (enemies.Count <= 0)
            {
                Application.OpenForms["MainGame"].Show();
                this.Close();
            }

            foreach (Control i in this.Controls)
            {
                if (i is PictureBox && (string)i.Tag == "enemy")
                {
                    i.Left += speed;
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        GameOver go = new GameOver();
                        go.Show();
                        Application.OpenForms["MainGame"].Close();
                        this.Close();
                    }
                    if (i.Left == 0)
                    {
                        speed = 1;
                        foreach (Control j in this.Controls)
                        {
                            if (j is PictureBox && (string)j.Tag == "enemy")
                            {
                                j.Top += 10;
                            }
                        }
                    }
                    if (i.Left + i.Width == background.Left + background.Width)
                    {
                        speed = -1;
                        foreach (Control j in this.Controls)
                        {
                            if (j is PictureBox && (string)j.Tag == "enemy")
                            {
                                j.Top += 10;
                            }
                        }
                    }
                }
                if (i is PictureBox && (string)i.Tag == "projectile")
                {
                    i.Top -= projSpeed;
                    if (i.Top > 402)
                    {
                        this.Controls.Remove(i);
                        i.Dispose();
                    }
                }
                if (i is PictureBox && (string)i.Tag == "leftEdge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = 0;
                    }
                }

                if (i is PictureBox && (string)i.Tag == "rightEdge")
                {
                    if (player.Bounds.IntersectsWith(i.Bounds))
                    {
                        player.Left = rightEdge.Left - player.Width;
                    }
                }
                foreach (Control j in this.Controls)
                {
                    if ((j is PictureBox && (string)j.Tag == "projectile") && (i is PictureBox && (string)i.Tag == "enemy"))
                    {
                        if (i.Bounds.IntersectsWith(j.Bounds))
                        {
                            enemies.Remove((PictureBox)i);
                            this.Controls.Remove(i);
                            i.Dispose();
                            this.Controls.Remove(j);
                            j.Dispose();
                        }
                    }
                }
            }
        }