0

I have a ball bouncing off of paddles and walls as desired, i have then added a singular brick via the draw.Rectangle tool and had the ball bounce off of this and then change its colour but could not make it invisible to stop any further collisions. I am using an array for my bricks as i can have many and can turn them true or false after being hit

My issue is that i am trying to get the ball to collide with said array bricks, but cannot for the life of me figure it out even with as much googling as possible. here is the snippet of my code i think 'should' work for the collision

 for (int i = 0; 1 < brickLive.Length; i++)
                if ((y == brickLocation[i, 0]) && (x >= brickLocation[0, i]) && (x <= (brickLocation[0, i] + 60)))
                    yChange = -yChange;

to my understanding this code is saying for the value of i check if ball coords are in the parameters of a bricks location. if it is then change direction. with the current code it runs fine until i start the game (i click the insert button and that enables the bounce button to work)

here is my full code

    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
{
    public partial class Form1 : Form
    {

        private int x, y, x2, y2;
        private int xChange, yChange;

        int bat, batX, batXX, mouseX;

        private Graphics paper;
        private Pen pen, pen2, pen3;

        private Pen brkpen;

        private Random ranNum;

        int brkLength = 60;
        int brkHeight = 20;
    int[,] brickLocation = { { 0, 100 }, { 61, 100 }, { 122, 100 } };
    bool[] brickLive = { true, true, true };


    public Form1()
    {
        InitializeComponent();

        paper = picDisplayBat.CreateGraphics();
        pen = new Pen(Color.Red);
        pen.Width = 10;
        ranNum = new Random();

        paper = picDisplayBat.CreateGraphics();
        pen = new Pen(Color.Blue);
        pen.Width = 3;

        paper = picDisplayBat.CreateGraphics();
        pen2 = new Pen(Color.Red);
        pen.Width = 3;

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

        paper = picDisplayBat.CreateGraphics();
        brkpen = new Pen(Color.Black);
        brkpen.Width = 3;


        //paper = picDisplayBat.CreateGraphics();
        //pen3 = new Pen(Color.Green);
        //pen3.Width = 5;
 }


    private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)                         //DRAWING THE BAT TO MOVE WITH MOUSE
    {
        //paper.Clear(Color.White);
        mouseX = e.X;
    }


    private void btnInsert_Click_1(object sender, EventArgs e)
    {
        {
            btnBounce.Visible = true;
        }
    }

    private void btnBounce_Click_1(object sender, EventArgs e)
    {
        {

            timer1.Interval = 25;
            timer1.Enabled = true;

            x = ranNum.Next(1, picDisplayBat.Height);
            y = ranNum.Next(1, picDisplayBat.Width);
            xChange = ranNum.Next(1, 10); yChange = ranNum.Next(1, 10);


            for (int i = 0; i < brickLive.Length; i++)
            {
                paper.DrawRectangle(brkpen, brickLocation[i, 0], brickLocation[i, 1], brkLength, brkHeight);


            }

        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        {

            x = x + xChange;
            y = y + yChange;
            if (x >= picDisplayBat.Width)
                xChange = -xChange;

            if (y >= picDisplayBat.Height)
                yChange = -yChange;

            if (x <= 0)
                xChange = -xChange;

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

            if ((y > picDisplayBat.Height - 20) && (x >= batX + 10) && (x <= batX + 50))
                yChange = -yChange;

            if ((y < picDisplayBat.Height - 295) && (x >= batX + 10) && (x <= batX + 50))
                yChange = -yChange;

            for (int i = 0; 1 < brickLive.Length; i++)
                if ((y == brickLocation[i, 0]) && (x >= brickLocation[0, i]) && (x <= (brickLocation[0, i] + 60)))
                    yChange = -yChange;



                paper.Clear(Color.White);
            paper.DrawRectangle(pen, mouseX + 10, picDisplayBat.Height - 20, 50, 10);          //bat 1
            paper.DrawEllipse(pen, x, y, 10, 10);                                              //ball
            paper.DrawRectangle(pen2, mouseX + 10, picDisplayBat.Height - 295, 50, 10);        //bat2
            //paper.DrawRectangle(pen3, x2, y2, 60, 10);
            bat = mouseX;
            batX = mouseX;
            batXX = mouseX;

            for (int i = 0; i < brickLive.Length; i++)
            {
                paper.DrawRectangle(brkpen, brickLocation[i, 0], brickLocation[i, 1], brkLength, brkHeight);


            }
        }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {

     Environment.Exit(0);

    }

    private void btnStop_Click(object sender, EventArgs e)
    {


            timer1.Enabled = false;
            paper.Clear(Color.White);

        }

}

}

  • possible duplicate of [Trying to kill bricks made by an array using collision with a bouncing ball. C#](http://stackoverflow.com/questions/27299706/trying-to-kill-bricks-made-by-an-array-using-collision-with-a-bouncing-ball-c-s) – Ňɏssa Pøngjǣrdenlarp Dec 04 '14 at 18:41
  • The one wasnt very good hence its negative points and slating so i made a better one, seen as my edited version saw no help – George Cawdron Dec 04 '14 at 19:00

1 Answers1

0

This looks wrong to me.

if ((y == brickLocation[i, 0]) && (x >= brickLocation[0, i]) && (x <= (brickLocation[0, i] + 60)))

It seems to me you should always have [i, ... for the brick array position, so either [i, 0] for tests against the horizontal component and [i, 1] for the vertical component.

  • It runs in the loop fine (ive done breaks and watched it go through the loop) but once it goes through the loop the fourth time it crashes and says An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Breakout.exe Additional information: Index was outside the bounds of the array. – George Cawdron Dec 04 '14 at 19:02
  • Yes, i tried that and switched it round to as you said but it didnt work the reason i responded with that was to say it only seems to break after i is set to a value above how many components in the array – George Cawdron Dec 04 '14 at 19:14
  • Update, the i++ no longer breaks and ive the signs and i,0 i,1 as you suggested but the collision isnt working. surely the parameters for collision are covered? – George Cawdron Dec 04 '14 at 19:32
  • The main thing i need is how do i get an arrays component coords – George Cawdron Dec 04 '14 at 20:09