c# - Breakout Brick Collision -


i need getting collision detection work bricks in breakout clone. i've spent hours reading answers on different websites seem in other coding languages i'm not familiar , not sure how translate them over.

i've got collision working on walls, paddle, , ball, can't work bricks.i'm not sure if you'll need see code different classes here's bricks, ball, , main form classes. format right haha. or advice appreciated.

brick class:

    class bricktest2     {       public int x {get; set;}       public int y {get; set;}       public int brwidth { get; set; }       public int brheight { get; set; }       public static int rows = 4;       public static int columns = 10;        public image bricktest2img;        list<rectangle> brick = new list<rectangle>();        public rectangle brickrect2;        public rectangle brickrect2       {         { return brickrect2; }       }        public bricktest2()       {         x = 0;         y = 0;         brwidth = 32;         brheight = 12;          bricktest2img = breakout.properties.resources.brickred;         brickrect2 = new rectangle(x, y, brwidth, brheight);        }        public void drawbricktest2(graphics paper)       {          (int c = 0; c < columns; c++)             (int r = 0; r < rows; r++)             {                rectangle brickrect2 = new rectangle(x + c * brwidth, y + r * brheight, brwidth, brheight);                 brick.add(brickrect2);                  paper.drawimage(bricktest2img, brickrect2);                 switch(r)                 {                     case 0:                         bricktest2img = breakout.properties.resources.brickred;                         break;                     case 1:                         bricktest2img = breakout.properties.resources.brickyellow;                         break;                     case 2:                         bricktest2img = breakout.properties.resources.brickgreen;                         break;                     case 3:                         bricktest2img = breakout.properties.resources.brickblue;                         break;                    default:                         break;                 }             }      }    }  } 

ball class:

    public class ball     {       public int x, y, width, height;       public int xspeed, yspeed;       private random randspeed;        private image ballimage;       private rectangle ballrec;        public rectangle ballrec       {         { return ballrec; }       }        public ball()       {         randspeed = new random();         x = 130;         y = 130;         width = 8;         height = 8;          xspeed = randspeed.next(5, 7);         yspeed = randspeed.next(5, 7);          ballimage = breakout.properties.resources.ball2;          ballrec = new rectangle(x, y, width, height);       }        public void drawball(graphics paper)       {         paper.drawimage(ballimage, ballrec);        }        public void moveball()       {         ballrec.x += xspeed;         ballrec.y += yspeed;       }        public void collidewallball()       {         if (ballrec.x < 0 || ballrec.x > 270)         {             xspeed *= -1;         }          if (ballrec.y < 0)         {             yspeed *= -1;         }        }        public void balldead()       {         if (ballrec.y > 260)         {            // yspeed *= -1; testing             settings.gmover = true;         }       }        public void collidepaddleball(rectangle paddlerec)       {         if (paddlerec.intersectswith(ballrec))         {             yspeed *= -1;         }       }         public void collidebrickball(rectangle brickrec2)       {         if (brickrec2.intersectswith(ballrec))         {             // desperate attempt work             if(brickrec2.x == ballrec.x)             {                 yspeed *= -1;             }         }       }     }  } 

main form:

public partial class form1 : form {      // initiallizes class files     graphics paper;     paddle gmpaddle = new paddle();     ball gmball = new ball();     bricktest2 gmbricktest = new bricktest2();      public form1()     {          // initializes         initializecomponent();         gamestart();      }       private void gamestart()     {          // sets new game         new settings();         settings.gmover = false;         lblgameover1.visible = false;         lblgameover2.visible = false;         cursor.hide();       }       private void form1_paint(object sender, painteventargs e)     {         paper = e.graphics;          if (settings.gmover == true)         {             // once game over, stops drawing object , brings game on message             string gameover1 = "game over!";             string gameover2 = "press enter try again";             lblgameover1.text = gameover1;             lblgameover2.text = gameover2;             lblgameover1.visible = true;             lblgameover2.visible = true;         }          if(settings.gmover != true)         {             // draws objects if game not on             gmpaddle.drawpaddle(paper);             gmball.drawball(paper);             gmbricktest.drawbricktest2(paper);          }      }       private void form1_mousemove(object sender, mouseeventargs e)     {         // calls mouse movement paddle         gmpaddle.movepaddle(e.x);         this.invalidate();     }      private void timer1_tick(object sender, eventargs e)     {         //updates collision         gmball.moveball();         gmball.collidewallball();         gmball.collidepaddleball(gmpaddle.paddlerec);         gmball.balldead();         gmball.collidebrickball(gmbricktest.brickrect2);          this.invalidate();     }        private void form1_keypress(object sender, keypresseventargs e)     {         // restarts game, still needs work         gamestart();      }    }  } 

after looking @ code you've posted, suggest verify if use brickrect2 variable correctly.

in class constructor assign brickrect2 = new rectangle(x, y, brwidth, brheight);, it's (0, 0, 32, 12).

then, in drawbricktest2(graphics paper) method don't draw rectangle directly, instead, shift according row , column variables: rectangle brickrect2 = new rectangle(x + c * brwidth, y + r * brheight, brwidth, brheight);, here use local variable named same field in class.

i cannot see code, call collidebrickball(rectangle brickrec2) method, suppose may try use field class, (0, 0, 32, 12).

if case, tere several methods can use fix this:

  • assign appropriate rectangle coordinates brickrect2 field instead of shifting when drawing
  • don't use intersectwith(rectangle rect) method , collision comparison manually, considering coordinates shift

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -