dimanche 1 février 2015

Why doesn't the "ball" change colors when intersecting parts of my array? (java)

This code is to a game named brick-breaker. I tried to make the ball change colors when intersecting with the array. I've created an int for the R,G,B values of the custom color, and have these ints be generated on intersect to a number between 1-255



import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class brickbreaker extends Applet implements MouseListener, Runnable, KeyListener
{
Thread main=new Thread(this);
int mouseX, mouseY;
int paddleX = 200, x,y,height,width, ballX = 100, ballY = 400, xChange = 1, yChange = 4, paddleWidth = 150, pageCt = 1, score = 0;
boolean pause = false, gameover=false;
boolean blocks[][] = new boolean [6][10];
int randomR = 255, randomG = 205, randomB = 155;
Random red = new Random();
Random green = new Random();
Random blue = new Random();
Image buffer;
Graphics bufferG;
String input="";
Font font3 = new Font ("Impact",5,50);
Font font4 = new Font ("Impact",5,150);
Font font5 = new Font ("Impact",5,30);
Color randomColor = new Color(randomR,randomG,randomB);
public void init()
{
resize( 1300,700 );
buffer = createImage(this.getWidth(),this.getHeight());
bufferG = buffer.getGraphics();

this.setLayout (null);

this.addKeyListener(this);

this.addMouseListener(this);

this.resize(1400,700);
main.start();



for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
blocks[r][c] = true;
}

public void drawBlocks(Graphics g)
{
for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
if(blocks[r][c])

g.drawRect(100+100*c, 30+30*r, 100, 30);
}

public void run()
{
if (pageCt==1)
{
while(! gameover)
{
if(pause==false)
{
repaint();
ballX = ballX + xChange;
ballY = ballY + yChange;
Rectangle ballRect = new Rectangle (ballX, ballY, 15, 15);
Rectangle paddleRect = new Rectangle (paddleX,650,paddleWidth,10);
Rectangle boundRect = new Rectangle (0,690,1300,10);
for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
if(blocks[r][c])
{
Rectangle blockRect = new Rectangle (100+100*c, 30+30*r, 100, 30);
if(blockRect.intersects(ballRect))
{
blocks[r][c] = false;
yChange *=-1;
score++;
randomR=red.nextInt(255);
randomB=blue.nextInt(255);
randomG=green.nextInt(255);
if (score==60)
pageCt=4;
}
}

if(ballX < 5 || ballX > 1280)
xChange = -1*xChange;
if(ballY < 5 || ballY > 690)
yChange = -1*yChange;
if (ballRect.intersects (paddleRect))
{
yChange = yChange*-1;
//paddleWidth=paddleWidth-3;
}
if (ballRect.intersects (boundRect))
{
pageCt=2;
}
}
repaint();
try
{main.sleep(20);}
catch(Exception e) {}
}
}
}

public void keyPressed(KeyEvent e) // makes applet do something when clicked.
{
int code = e.getKeyCode();
if(code == e.VK_P)
{
if(pause==false)
pause=true;
else
pause=false;
}
if (pause==false)
{
if(paddleX>20)
{
if(code == e.VK_A)
{
paddleX = paddleX-20;
}
}
if(paddleX<1160)
{
if(code == e.VK_D)
{
paddleX = paddleX+20;
}
}
}
}

public void paint(Graphics g) // Outputs on screen
{
if (pageCt==1)
{
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,1300,700);
bufferG.setColor(Color.blue);
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,this.getWidth(),this.getHeight());
bufferG.setColor(randomColor);
bufferG.fillOval(ballX,ballY,15,15);
bufferG.setColor(Color.white);
bufferG.fillRect(paddleX,650,paddleWidth,10);
drawBlocks(bufferG);
bufferG.setFont(font5);
bufferG.drawString("Score : "+score,10,20);
}
if (pageCt==2)
{
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,1300,700);
bufferG.setColor(Color.white);
bufferG.setFont(font4);
bufferG.drawString("You Lose!", 350,400);
}
if (pause==true)
{
bufferG.setColor(Color.white);
bufferG.setFont(font4);
bufferG.drawString("PAUSED",400,350);
}
if (pageCt==0)
{
bufferG.drawString("- Click to begin -",350,350);
}
g.drawImage(buffer,0,0,this);
}

public void update(Graphics g)
{
paint(g);
}

public void mouseClicked (MouseEvent e) {}

public void keyReleased( KeyEvent e){}

public void keyTyped (KeyEvent e){}

public void mousePressed (MouseEvent e)
{
if(pageCt==0)
{
pageCt++;
repaint();
}
}

public void mouseReleased (MouseEvent e) {}

public void mouseEntered (MouseEvent e){}

public void mouseExited (MouseEvent e) {}


}





Aucun commentaire:

Enregistrer un commentaire