java - Brick Breaker Random Color -


the problem i'm having when run game, instead of staying 1 random color, bricks keep changing color while i'm in game. how fix each brick stay 1 color?

public class game extends joeapplet implements keylistener {     string status;     int ballx = 294; // ball spawn x coordinate     int bally = 640; // ball spawn y coordinate     int batx = 294;      int baty = 654;     int brickx = 32;     int bricky = 50;         double movex = -16;  // x speed of ball     double movey = -16;  //y speed of ball     int count = 0;     int currentlevel=0;     int score=0;      //starts score @ 0     int lives=3;      //lives start @ 3     static boolean right = false;     static boolean left = false;     boolean ballfalldown = false;     boolean bricksover = false;     rectangle ball = new rectangle(ballx, bally, 12, 12); //creates ball     rectangle bat = new rectangle(batx, baty, 100, 12);   //creates bat(paddle)     rectangle[] brick = new rectangle[49];    //creates desired number of bricks     random random = new random();     static final float min_sat = 0.8f;     color color;      public void paint(graphics art)     {         switch(currentlevel)         {         case 0:             menuscreen(art);             break;         case 1:             game(art);             break;         }     }     public void menuscreen(graphics art)     {          setsize(700, 700);          art.setcolor(color.black);          art.fillrect(0, 0, 698, 698);          color ballcolor=new color(0,0,66);          art.setcolor(ballcolor);          art.filloval(ball.x, ball.y, ball.width, ball.height);          color batcolor=new color(0,0,66);          art.setcolor(batcolor);          art.fill3drect(bat.x, bat.y, bat.width, bat.height, true);           art.setcolor(color.green);          art.drawrect(0, 0, 698, 698);          art.setcolor(color.yellow);          font menu = new font("arial", font.bold, 20);          art.setfont(menu);          art.drawstring("brick breaker", 100,400);          art.drawstring("press p play", 100,425);          art.drawstring("press q quit game", 100,450);          (int = 0; < brick.length; i++)           {              if (brick[i] != null)              {                  color mycolor=new color(100,0,0);                  art.setcolor(mycolor);                  art.fill3drect(brick[i].x, brick[i].y, brick[i].width,                  brick[i].height, true);              }           }              art.setcolor(color.yellow);              if (ballfalldown || bricksover)               {                  font f = new font("arial", font.bold, 20);                  art.setfont(f);                  art.drawstring(status, 294, 349);                  ballfalldown = false;                  bricksover = false;              }           }      private  color color;      class brick {         public  color getcolor() { return color; }         public  brick()         {              float hue = random.nextfloat();             float saturation = min_sat + random.nextfloat() * (1f - min_sat);             float brightness = min_sat + random.nextfloat() * (1f - min_sat);             color color = color.gethsbcolor(hue, saturation,              brightness);          }      }     public void game(graphics art)      {          setsize(700, 700);          art.setcolor(color.black);          art.fillrect(0, 0, 698, 698);          color ballcolor=new color(0,0,225);          art.setcolor(ballcolor);          art.filloval(ball.x, ball.y, ball.width, ball.height);          color batcolor=new color(0,0,139);          art.setcolor(batcolor);          art.fill3drect(bat.x, bat.y, bat.width, bat.height, true);           art.setcolor(color.green);          art.drawrect(0, 0, 698, 698);          (int = 0; < brick.length; i++)           {              if (brick[i] != null)              {                   art.setcolor(brick[i].getcolor());                  art.fill3drect(brick[i].x, brick[i].y, brick[i].width,                  brick[i].height, true);              }           }               if (ballfalldown || bricksover)               {                  font f = new font("arial", font.bold, 20);                  art.setfont(f);                  art.drawstring(status, 100,425);                  ballfalldown = false;                  bricksover = false;              }                 (int = 0; < brick.length; i++)              {                     if (brick[i] != null)                      {                         if (brick[i].intersects(ball))                          {                             score=score+10;                             brick[i] = null;                             movey = -movey;                             count++;                         }                        }              }                        if (count == brick.length)                 {                    bricksover = true;                    movex=0;                    movey=0;                    art.setcolor(color.green);                    status = "you beat level!!";                    art.drawstring("press e exit", 100,450);                    art.drawstring("press n next level", 100,475);                    repaint();                }               repaint();              font f = new font("arial", font.bold, 20);              art.setfont(f);              art.setcolor(color.white);              art.drawstring("score:"+score, 600, 684);              ball.x += movex;              ball.y += movey;              if (left == true)               {                  bat.x -= 18;                  right = false;              }              if (right == true)               {                  bat.x += 18;                  left = false;              }              if (bat.x <= 4)              {                  bat.x = 4;              }               else if (bat.x >= 586)              {                  bat.x = 596;              }                  if (ball.intersects(bat))              {                  movey = -movey-.1;              }                 if (ball.x <= 0 || ball.x + ball.height >= 698)              {                  movex = -movex;              }              if (ball.y <= 0)               {                  movey = -movey;              }              font f1 = new font("arial", font.bold, 20);              art.setfont(f1);              art.setcolor(color.white);              art.drawstring("lives:"+ lives, 5, 684);              if (ball.y >= 698 && (bricksover==false) && lives>0)               {                    ballfalldown = true;                    art.setcolor(color.red);                    status = "";                    art.drawstring("", 100,450);                    lives=lives-1;                    ballx = 294;                     bally = 640;                    ball = new rectangle(ballx, bally, 12, 12);                    movex = -16;                    movey = -16;                    repaint();                }               if(lives==0 && ball.y >= 698)              {                  art.setcolor(color.red);                  art.drawstring("you lost!!", 100,425);                  art.drawstring("press e exit", 100,450);              }         }         public void init()      {         addkeylistener(this);         (int = 0; < brick.length; i++)  //creates bricks         {             brick[i] = new rectangle(brickx, bricky, 40, 20);             if (i == 12)        //1st row of bricks             {                 brickx = 32;                 bricky = 84;             }             if (i == 23)    //2nd row of bricks             {                 brickx = 82;                 bricky = 118;             }             if (i == 32)    //3rd row of bricks             {                 brickx = 132;                 bricky = 152;             }             if (i == 39)        //4th row of bricks             {                 brickx = 182;                 bricky = 186;             }             if (i == 44)    //5th row of bricks             {                 brickx = 232;                 bricky = 220;             }             if (i == 47)        //6th row of bricks             {                 brickx = 282;                 bricky = 254;             }             if (i == 48)        //7th row of bricks             {                 brickx = 144;                 bricky = 132;             }             brickx += 50;   //spacing between each brick                 }        }      public void restart()  //if player chooses exit(e) game reset level 1     {           ballx = 294;           bally = 640;          batx = 294;          baty = 654;          brickx = 32;          bricky = 50;             ball = new rectangle(ballx, bally, 12, 12);          bat = new rectangle(batx, baty, 100, 12);          movex = -16;          movey = -16;          ballfalldown = false;          bricksover = false;          count = 0;         status = null;         (int = 0; < brick.length; i++)  //recreates bricks         {             brick[i] = new rectangle(brickx, bricky, 40, 20);             if (i == 12)                     {                 brickx = 32;                 bricky = 84;             }             if (i == 23)                 {                 brickx = 82;                 bricky = 118;             }             if (i == 32)                 {                 brickx = 132;                 bricky = 152;             }             if (i == 39)                     {                 brickx = 182;                 bricky = 186;             }             if (i == 44)                 {                 brickx = 232;                 bricky = 220;             }             if (i == 47)                     {                 brickx = 282;                 bricky = 254;             }             if (i == 48)                     {                 brickx = 144;                 bricky = 132;             }             brickx += 50;                    }              repaint();     }      @override     public void keypressed(keyevent e)  //allows each key desired action     {         int keycode = e.getkeycode();         if (keycode == keyevent.vk_left)          {             left = true;              }          if (keycode == keyevent.vk_right)          {             right = true;         }         if (keycode == e.vk_p && currentlevel == 0)         {             currentlevel = 1;         }                else if (keycode == e.vk_e && currentlevel == 1)         {             currentlevel = 0;             score=0;             lives=3;             restart();         }            else if(keycode == e.vk_q)         {             system.exit(0);         }     }      @override     public void keyreleased(keyevent e)      {         int keycode = e.getkeycode();         if (keycode == keyevent.vk_left)          {             left = false;         }          if (keycode == keyevent.vk_right)         {             right = false;         }     }      @override     public void keytyped(keyevent e)     {      }      public static void main(string[] args)     {            game prog = new game();         prog.init();             } } 

code try give each brick random color:

if (brick[i] != null)              {                  float hue = random.nextfloat();                  float saturation = min_sat + random.nextfloat() * (1f -  min_sat);                  float brightness = min_sat + random.nextfloat() * (1f -  min_sat);                  color color = color.gethsbcolor(hue, saturation,  brightness);                  art.setcolor(color);                  art.fill3drect(brick[i].x, brick[i].y, brick[i].width,                  brick[i].height, true);              }           } 

you generate random color each brick inside game-method, called within paintcomponent. means: each time screen repainted, bricks new color. instead of going way, apply random color each brick, when it's generated , leave way. requires introducing color variable in brick-class. simplest solution generate color directly in constructor:

private color color;  public brick(){     ...      float hue = random.nextfloat();     float saturation = min_sat + random.nextfloat() * (1f - min_sat);     float brightness = min_sat + random.nextfloat() * (1f - min_sat);     color = color.gethsbcolor(hue, saturation,       ... } 

as use color stored in brick in game-method render each brick:

for (int = 0; < brick.length; i++)       {          if (brick[i] != null)          {              art.setcolor(brick[i].getcolor());              art.fill3drect(brick[i].x, brick[i].y, brick[i].width, brick[i].height, true);          }       } 

and general advice:
please refactor code , implement advice gave code on last question asked. going further down path you're on, you'll wind code unreadable , can't debugged. know shouldn't introduce anecdotes here, when started coding, wrote snake in pretty same spaghetti-code style you're using now. in end had 1 small mistake wanted fix, changed 1 line of code. result: entire code broke, , wasn't able fix point, because code unusable. moral of story: in spaghettic-code fixing 1 bug introduces 100 new bugs. write proper code, , live happily.

here's fixed code:

public class game extends joeapplet implements keylistener {     string status;     int ballx = 294; // ball spawn x coordinate     int bally = 640; // ball spawn y coordinate     int batx = 294;     int baty = 654;     int brickx = 32;     int bricky = 50;     double movex = -16;  // x speed of ball     double movey = -16;  //y speed of ball     int count = 0;     int currentlevel=0;     int score=0;      //starts score @ 0     int lives=3;      //lives start @ 3     static boolean right = false;     static boolean left = false;     boolean ballfalldown = false;     boolean bricksover = false;     rectangle ball = new rectangle(ballx, bally, 12, 12); //creates ball     rectangle bat = new rectangle(batx, baty, 100, 12);   //creates bat(paddle)     brick[] brick = new brick[49];    //creates desired number of bricks     random random = new random();     static final float min_sat = 0.8f;      public void paint(graphics art)     {         switch(currentlevel)         {             case 0:                 menuscreen(art);                 break;             case 1:                 game(art);                 break;         }     }     public void menuscreen(graphics art)     {         setsize(700, 700);         art.setcolor(color.black);         art.fillrect(0, 0, 698, 698);         color ballcolor=new color(0,0,66);         art.setcolor(ballcolor);         art.filloval(ball.x, ball.y, ball.width, ball.height);         color batcolor=new color(0,0,66);         art.setcolor(batcolor);         art.fill3drect(bat.x, bat.y, bat.width, bat.height, true);         art.setcolor(color.green);         art.drawrect(0, 0, 698, 698);         art.setcolor(color.yellow);         font menu = new font("arial", font.bold, 20);         art.setfont(menu);         art.drawstring("brick breaker", 100,400);         art.drawstring("press p play", 100,425);         art.drawstring("press q quit game", 100,450);         (int = 0; < brick.length; i++)         {             if (brick[i] != null)             {                 art.setcolor(brick[i].getcolor());                 art.fill3drect(brick[i].x, brick[i].y, brick[i].width,                         brick[i].height, true);             }          }         art.setcolor(color.yellow);         if (ballfalldown || bricksover)         {             font f = new font("arial", font.bold, 20);             art.setfont(f);             art.drawstring(status, 294, 349);             ballfalldown = false;             bricksover = false;         }     }     private  color color;      class brick         extends rectangle     {         private color color;          public  color getcolor() { return color; }         public  brick(int x, int y, int width, int height)         {             super(x, y, width, height);              float hue = random.nextfloat();             float saturation = min_sat + random.nextfloat() * (1f - min_sat);             float brightness = min_sat + random.nextfloat() * (1f - min_sat);             color = color.gethsbcolor(hue, saturation,                     brightness);         }     }     public void game(graphics art)     {         setsize(700, 700);         art.setcolor(color.black);         art.fillrect(0, 0, 698, 698);         color ballcolor=new color(0,0,225);         art.setcolor(ballcolor);         art.filloval(ball.x, ball.y, ball.width, ball.height);         color batcolor=new color(0,0,139);         art.setcolor(batcolor);         art.fill3drect(bat.x, bat.y, bat.width, bat.height, true);         art.setcolor(color.green);         art.drawrect(0, 0, 698, 698);         (int = 0; < brick.length; i++)         {             if (brick[i] != null)             {                  art.setcolor(brick[i].getcolor());                 art.fill3drect(brick[i].x, brick[i].y, brick[i].width,                         brick[i].height, true);             }          }          if (ballfalldown || bricksover)         {             font f = new font("arial", font.bold, 20);             art.setfont(f);             art.drawstring(status, 100,425);             ballfalldown = false;             bricksover = false;         }          (int = 0; < brick.length; i++)         {             if (brick[i] != null)             {                 if (brick[i].intersects(ball))                 {                     score=score+10;                     brick[i] = null;                     movey = -movey;                     count++;                 }             }         }           if (count == brick.length)         {             bricksover = true;             movex=0;             movey=0;             art.setcolor(color.green);             status = "you beat level!!";             art.drawstring("press e exit", 100,450);             art.drawstring("press n next level", 100,475);             repaint();         }          repaint();         font f = new font("arial", font.bold, 20);         art.setfont(f);         art.setcolor(color.white);         art.drawstring("score:"+score, 600, 684);         ball.x += movex;         ball.y += movey;         if (left == true)         {             bat.x -= 18;             right = false;         }         if (right == true)         {             bat.x += 18;             left = false;         }         if (bat.x <= 4)         {             bat.x = 4;         }         else if (bat.x >= 586)         {             bat.x = 596;         }         if (ball.intersects(bat))         {             movey = -movey-.1;         }         if (ball.x <= 0 || ball.x + ball.height >= 698)         {             movex = -movex;         }         if (ball.y <= 0)         {             movey = -movey;         }         font f1 = new font("arial", font.bold, 20);         art.setfont(f1);         art.setcolor(color.white);         art.drawstring("lives:"+ lives, 5, 684);         if (ball.y >= 698 && (bricksover==false) && lives>0)         {             ballfalldown = true;             art.setcolor(color.red);             status = "";             art.drawstring("", 100,450);             lives=lives-1;             ballx = 294;             bally = 640;             ball = new rectangle(ballx, bally, 12, 12);             movex = -16;             movey = -16;             repaint();         }         if(lives==0 && ball.y >= 698)         {             art.setcolor(color.red);             art.drawstring("you lost!!", 100,425);             art.drawstring("press e exit", 100,450);         }     }      public void init()     {         addkeylistener(this);         (int = 0; < brick.length; i++)  //creates bricks         {             brick[i] = new brick(brickx, bricky, 40, 20);             if (i == 12)        //1st row of bricks             {                 brickx = 32;                 bricky = 84;             }             if (i == 23)    //2nd row of bricks             {                 brickx = 82;                 bricky = 118;             }             if (i == 32)    //3rd row of bricks             {                 brickx = 132;                 bricky = 152;             }             if (i == 39)        //4th row of bricks             {                 brickx = 182;                 bricky = 186;             }             if (i == 44)    //5th row of bricks             {                 brickx = 232;                 bricky = 220;             }             if (i == 47)        //6th row of bricks             {                 brickx = 282;                 bricky = 254;             }             if (i == 48)        //7th row of bricks             {                 brickx = 144;                 bricky = 132;             }             brickx += 50;   //spacing between each brick                 }     }      public void restart()  //if player chooses exit(e) game reset level 1     {          ballx = 294;         bally = 640;         batx = 294;         baty = 654;         brickx = 32;         bricky = 50;         ball = new rectangle(ballx, bally, 12, 12);         bat = new rectangle(batx, baty, 100, 12);         movex = -16;         movey = -16;         ballfalldown = false;         bricksover = false;         count = 0;         status = null;         (int = 0; < brick.length; i++)  //recreates bricks         {             brick[i] = new brick(brickx, bricky, 40, 20);             if (i == 12)             {                 brickx = 32;                 bricky = 84;             }             if (i == 23)             {                 brickx = 82;                 bricky = 118;             }             if (i == 32)             {                 brickx = 132;                 bricky = 152;             }             if (i == 39)             {                 brickx = 182;                 bricky = 186;             }             if (i == 44)             {                 brickx = 232;                 bricky = 220;             }             if (i == 47)             {                 brickx = 282;                 bricky = 254;             }             if (i == 48)             {                 brickx = 144;                 bricky = 132;             }             brickx += 50;         }         repaint();     }      @override     public void keypressed(keyevent e)  //allows each key desired action     {         int keycode = e.getkeycode();         if (keycode == keyevent.vk_left)         {             left = true;         }          if (keycode == keyevent.vk_right)         {             right = true;         }         if (keycode == e.vk_p && currentlevel == 0)         {             currentlevel = 1;         }         else if (keycode == e.vk_e && currentlevel == 1)         {             currentlevel = 0;             score=0;             lives=3;             restart();         }         else if(keycode == e.vk_q)         {             system.exit(0);         }     }      @override     public void keyreleased(keyevent e)     {         int keycode = e.getkeycode();         if (keycode == keyevent.vk_left)         {             left = false;         }          if (keycode == keyevent.vk_right)         {             right = false;         }     }      @override     public void keytyped(keyevent e)     {      }      public static void main(string[] args)     {         game prog = new game();         prog.init();     } } 

pretty hard fix stuff already.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -