突围游戏(C#) - 蝙蝠碰撞问题

时间:2014-12-10 23:37:11

标签: c# windows visual-studio-2013

我目前正在用C#创建一个Breakout游戏,我遇到了蝙蝠碰撞的问题。当我用蝙蝠击球时,球被蝙蝠抓住并且没有被正确击中。很难解释,但我想知道是否有人能在我的代码中发现可能导致此问题的问题。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BreakOut_Game
{
    public partial class Form1 : Form
    {
        Graphics paper;
        Pen pen;
        Rectangle ball, bat, brick1, brick2, brick3, brick4, brick5, brick6, brick7, brick8, brick9, brick10;
        Random randomNumber;
        private int ballX, ballY, batX, batY, xChange, yChange;
        public int lives;
        public int score;


        public Form1()
        {
            InitializeComponent();

            paper = picMainGame.CreateGraphics();

            pen = new Pen(Color.Black);
            pen.Width = 2;


            picMainGame.MouseMove += new System.Windows.Forms.MouseEventHandler(picDraw_MouseMove);

            randomNumber = new Random();
            score = 0;

            lblLives.Text = ("Lives: " + lives);
            lblScore.Text = ("Score: " + score);

        }
        private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            {
                batX = e.X;
                batY = e.Y;
            }
        }

        private void drawBat()
        {

            bat = new Rectangle(batX + 10, picMainGame.Height - 30, 40, 12);
            paper.DrawRectangle(pen, bat);
            paper.FillRectangle(Brushes.Black, bat);
        }



        public void drawBall()
        {
            pen = new Pen(Color.Black);
            ball = new Rectangle(ballX, ballY, 8, 8);
            paper.DrawEllipse(pen, ball);
            paper.FillEllipse(Brushes.Black, ball);
        }

        public void moveBall()
        {

            ballX = ballX + xChange;
            ballY = ballY + yChange;

            if ((ballX >= picMainGame.Width) || (ballX <= 0))
                xChange = -xChange;

            if (ballY <= 0)
                yChange = -yChange;

            if ((ballX >= (batX - 25)) && (ballX <= (batX + 25)) && (ballY >= (picMainGame.Height - 30)))

            if (ballY >= picMainGame.Height)
      {
          lives = lives - 1;
          lblLives.Text = Convert.ToString(lives);
          timer1.Enabled = false;

          btnLaunch.Enabled = true;

      }
 }

        public void createBricks()
        {
            brick1 = new Rectangle(25, 3, 50, 10);
            brick2 = new Rectangle(76, 3, 50, 10);
            brick3 = new Rectangle(127, 3, 50, 10);
            brick4 = new Rectangle(178, 3, 50, 10);
            brick5 = new Rectangle(229, 3, 50, 10);
            brick6 = new Rectangle(280, 3, 50, 10);
            brick7 = new Rectangle(331, 3, 50, 10);
            brick8 = new Rectangle(382, 3, 50, 10);
            brick9 = new Rectangle(433, 3, 50, 10);
            brick10 = new Rectangle(484, 3, 50, 10);

        }

        public void drawBricks()
        {
            paper.DrawRectangle(Pens.Black, brick1);
            paper.FillRectangle(Brushes.Black, brick1);
            paper.DrawRectangle(Pens.White, brick2);
            paper.FillRectangle(Brushes.White, brick2);
            paper.DrawRectangle(Pens.Red, brick3);
            paper.FillRectangle(Brushes.Red, brick3);
            paper.DrawRectangle(Pens.Blue, brick4);
            paper.FillRectangle(Brushes.Blue, brick4);
            paper.DrawRectangle(Pens.Green, brick5);
            paper.FillRectangle(Brushes.Green, brick5);
            paper.DrawRectangle(Pens.Yellow, brick6);
            paper.FillRectangle(Brushes.Yellow, brick6);
            paper.DrawRectangle(Pens.Black, brick7);
            paper.FillRectangle(Brushes.Black, brick7);
            paper.DrawRectangle(Pens.Green, brick8);
            paper.FillRectangle(Brushes.Green, brick8);
            paper.DrawRectangle(Pens.Red, brick9);
            paper.FillRectangle(Brushes.Red, brick9);
            paper.DrawRectangle(Pens.Blue, brick10);
            paper.FillRectangle(Brushes.Blue, brick10);

        }

        private void checkBatCollision()
        {
            if (ball.IntersectsWith(bat))
            {
                xChange = -xChange;
                yChange = -yChange;
            }
        }


      public void checkCollision()
    {

        if (ball.IntersectsWith(brick1))
        {
            brick1 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;
        }

        if (ball.IntersectsWith(brick2))
        {
            brick2 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick3))
        {
            brick3 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick4))
        {
            brick4 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick5))
        {
            brick5 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick6))
        {
            brick6 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick7))
        {
            brick7 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick8))
        {
            brick8 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick9))
        {
            brick9 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }

        if (ball.IntersectsWith(brick10))
        {
            brick10 = new Rectangle(0, 0, 0, 0);
            score = score + 10;
            yChange = -yChange;     
        }


       }


        private void picMainGame_Click(object sender, EventArgs e)
        {

        }

        private void btnCredit_Click(object sender, EventArgs e)
        {
            if (lives == 0)
            {
                lives = lives + 5;
                lblLives.Text = Convert.ToString(lives);

                btnCredit.Enabled = false;
            }
        }

        private void btnDisplayBricks_Click(object sender, EventArgs e)
        {
            if (lives == 0)
            {
                MessageBox.Show("Insert Credit to play!");
            }
            else
            {

                createBricks();
                drawBricks();
                btnLaunch.Enabled = true;

            }
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            ballX = 100;
            ballY = 150;

            xChange = -7;
            yChange = -7;


            timer1.Enabled = true;

        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false)
                timer1.Enabled = true;

            else
            {
                timer1.Enabled = false;

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            paper.Clear(Color.Silver);
            drawBat();
            drawBall();
            drawBricks();
            moveBall();
            checkBatCollision();
            checkCollision();
            btnLaunch.Enabled = false;
            btnPause.Enabled = true;
            btnDisplayBricks.Enabled = false;
        }

        private void picMainGame_Click_1(object sender, EventArgs e)
        {

        }
    }

}

1 个答案:

答案 0 :(得分:0)

这么多东西。

在内存中表示球和砖是否更容易,然后每隔一段时间绘制其状态的表示?

通过您当前的实现,您还可以通过将精灵(球)保持为单独的图像并在需要的位置移动它来提高性能。

在一个平原上绘制所有内容只有在精灵/球的数量非常大的情况下才有用。

还有其他各种问题也可归因于您的错误,例如元素的大小与屏幕上的大小,因此表示有助于实现分辨率缩放。

希望有帮助