So i'm creating a frogger game and i've just about got everything but I'm running into one small problem, The collision system I have is very inconsistent, I've been trying to find out whats what but I can't quite figure out what it is. I want it to stop right when it hits the rectangles. any help would be appericated.
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
public class frogger
{
static double frogX = .5;
static double frogY = .02;
static double frogW = .02;
static double frogH = .02;
static double halfWidth = .05;
static double halfHeight = .02;
static double speeds[] = {.02, .03, .035 , .040, .043, .046, .060, .070};
static Rectangle2D.Double[] rectangles = {new Rectangle2D.Double(.45 , .12 , .1 , .04), new Rectangle2D.Double(.45 , .27 , .1 , .04), new Rectangle2D.Double(.45 , .42 , .1 , .04),
new Rectangle2D.Double(.45 , .57 , .1 , .04), new Rectangle2D.Double(.45 , .72 , .1 , .04), new Rectangle2D.Double(.45 , .87 , .1 , .04), new Rectangle2D.Double(.45 , .92 , .1 , .04)};
public static void drawscene()
{
StdDraw.clear();
StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.filledRectangle(frogX, frogY, frogW, frogH);
StdDraw.setPenColor(StdDraw.RED);
for(int i = 0; i < rectangles.length; i++)
{
StdDraw.filledRectangle(rectangles[i].x + halfWidth, rectangles[i].y - halfHeight, halfWidth, halfHeight);
}
StdDraw.show(1000/24);
}
public static void updateDirection()
{
for(int i = 0; i < rectangles.length; i++){
rectangles[i].x += speeds[i];
}
for(int i = 0; i < rectangles.length; i++){
if(rectangles[i].x + halfWidth >= .98 || rectangles[i].x + halfWidth <= .02){
speeds[i] *= -1;
}
}
if (StdDraw.isKeyPressed(KeyEvent.VK_UP))
{
frogY += .01;
}
else if(StdDraw.isKeyPressed(KeyEvent.VK_DOWN))
{
frogY -= .01;
}
else if(StdDraw.isKeyPressed(KeyEvent.VK_LEFT))
{
frogX -= .01;
}
else if(StdDraw.isKeyPressed(KeyEvent.VK_RIGHT))
{
frogX += .01;
}
}
public static void main(String[] args)
{
outerloop:
while(true)
{
frogger.drawscene();
frogger.updateDirection();
if (frogX >= 1 )
{
break;
}
if (frogX <= 0)
{
break;
}
if(frogY >= 1)
{
break;
}
Rectangle2D.Double frog = new Rectangle2D.Double(frogX - frogW, frogY + frogH, frogW * 2, frogH * 2);
for(int i = 0; i < rectangles.length; i++){
if(frog.intersects(rectangles[i])){
break outerloop;
}
}
}
// put whatever you want to display at the end of the game here
}
}