Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Breakout Game - Bat Collision Issue

breakout game c# collision

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 KieranWillis

KieranWillis

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 December 2014 - 04:39 PM

I am currently creating a Breakout game in C# and I am having a problem with my bat collision. When I go to hit the ball with the bat, the ball is getting caught on the bat and isn't being hit correctly. Quite hard to explain, but I was wondering if anyone could spot a problem in my code that might be causing this.

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;
        int batX, ballX, ballY, mouseX, mouseY, 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)
        {
            {
                mouseX = e.X;
                mouseY = e.Y;
            }
        }

        private void drawBat()
        {

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

       

        public void drawBall()
        {
            pen = new Pen(Color.Black, 2);
            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)))
                yChange = -yChange;

            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 = 8;
            yChange = 8;


            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)
        {

        }
    }

}



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 11 December 2014 - 08:12 AM

My first guess would be that ball.IntersectsWith(batis returning false, thus not changing y's orientation.

 

Put a breakpoint on that statement, and trace into the function.

 

Second, and unimportant, your btnPause_Click method could be simplified by just changing it to:

 

timer1.Enabled = !timer1.Enabled;

 

If you still can't figure out the problem, zip or rar the project and add as an attachment.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download