Hey so i have to make this game, basically a ball (bug) needs to change directions once it hits other objects, (rectangles and objects) Im using getBounds.intersects to see if it hits the objects or not, but it doesn't work if the ball goes from right to left. Basically any object it hits going from right to left it just goes through which causes the game to get stuck eventually, but any other directions work just fine.
here is my code
package week6Homework;
import java.awt.Color;
import java.awt.Frame;
import java.util.Random;
import acm.graphics.GOval;
import acm.graphics.GRect;
import acm.graphics.GRectangle;
import acm.program.*;
public class BugBotTry1984756212 extends GraphicsProgram
{
//constants for screen specs
final int WIN_HEIGHT = 900;
final int WIN_WIDTH = 1900;
//booleans for turns
boolean turn;
boolean left;
boolean right;
boolean down;
boolean up;
//creating bugbot itself using external class
BugBot bug = new BugBot();
//creating arrays for ovals and rectangles
GOval[] ovals = new GOval[4];
GRect[] rects = new GRect[4];
//this is a string array that will bre sued to figure out what if the bugbot
//needs to go up/down or left/right
//its hard to explain what this does but you will figure it out later in the code
String[] horizontalTurn = new String [2];
String[] verticalTurn = new String[2];
public void init()
{
//setting name of the applet
Frame title = (Frame)this.getParent().getParent();
this.setTitle("BugBotField");
//set screen specs
setSize(WIN_WIDTH, WIN_HEIGHT);
}//init
public void run()
{
//using methods to create the rectangles and ovals and bugbot
createOvals();
createRects();
add(bug, 100, 450);
//asigning words to the horizontal string array
horizontalTurn[0] = "down";
horizontalTurn[1] = "up";
verticalTurn[0] = "right";
verticalTurn[1] = "left";
//assigning values to boolean variables to see where the bug should move
//basivally if the right = true the bugbot will be moving right, and if down is true
//and the rest of them are false the bugbot will be moving down.
right = false;
left = true;
down = false;
up = false;
//main while loop that checks everything nad does everything
while (true)
{
//pause so the game doesn't end within one second after start.
pause(10);
if (right == true)
{
bug.move(5, 0);
}
else if (left == true)
{
bug.move(-5, 0);
}
else if (down == true)
{
bug.move(0, 5);
}
else if (up == true)
{
bug.move(0, -5);
}
//get X and Y coordinates for the bugbot so it wont go out of boundaries.
int bugX = (int)bug.getX();
int bugY = (int)bug.getY();
//for loop to check colossion detection.
//basically for every time the while loop runs (which is every .1 seconds because of our pause)
//it will run this for loop 4 times (because thats how many objects we have in our arrays
// and we are using array.length to see how many times the for loop needs to run
for (int i = 0; i < rects.length; i++)
{
//integer that gets bounds of all rectangles in the array.
GRectangle rectangleBounds = rects[i].getBounds();
//making 3 of those since if the bug hits the last red circle we need
//the game to stop and not turn directions.
// i could do it differently but didn't feel like redoing bunch of stuff so here.
GRectangle ovalBounds1 = ovals[0].getBounds();
GRectangle ovalBounds2 = ovals[1].getBounds();
GRectangle ovalBounds3 = ovals[2].getBounds();
GRectangle ovalBounds = ovals[i].getBounds();
//check for collision with other objects
if (bug.getBounds().intersects(rectangleBounds) || bug.getBounds().intersects(ovalBounds))
{
//check for collision if direction is "right"
if (right == true && left == false && down == false && up == false)
{
for (int x = 3; x >= 0; x--)
{
pause (50);
bug.move(-2, 0);
}
String direction = getRandom(horizontalTurn);
right = false;
left = false;
down = false;
up = false;
if (direction == "up")
{
up = true;
}
else if (direction == "down")
{
down = true;
}
}
//check for collision if direction is "left"
else if (left == true && right == false && right == false && left == false)
{
for (int x = 3; x >= 0; x--)
{
pause (50);
bug.move(2, 0);
}
String direction = getRandom(horizontalTurn);
right = false;
left = false;
down = false;
up = false;
if (direction == "up")
{
up = true;
}
else if (direction == "down")
{
down = true;
}
}
//check for collision if direction "down"
else if (down = true && up == false && right == false && left == false)
{
for (int x = 3; x >= 0; x--)
{
pause (50);
bug.move(0, -2);
}
String direction = getRandom(verticalTurn);
right = false;
left = false;
down = false;
up = false;
if (direction == "right")
{
right = true;
}
else if (direction == "left")
{
left = true;
}
}
//check for collision if direction "up"
else if (up == true && down == false && right == false && left == false)
{
for (int x = 3; x >= 0; x--)
{
pause (50);
bug.move(0, 2);
}
String direction = getRandom(verticalTurn);
right = false;
left = false;
down = false;
up = false;
if (direction == "right")
{
right = true;
}
else if (direction == "left")
{
left = true;
}
}
}//if for collision with objects
else if (bugX >= WIN_WIDTH)
{
right = false;
left = true;
down = false;
up = false;
}
else if (bugX <= 0)
{
right = true;
left = false;
down = false;
up = false;
}
else if (bugY >= WIN_HEIGHT)
{
down = false;
up = true;
}
else if (bugY <= 0)
{
up = false;
down = true;
}
}
}
}
public void createOvals()
{
GOval orange = new GOval(400,100,200,200);
GOval green = new GOval(100,550,300,250);
GOval red = new GOval(1500,450,125,125);
GOval blue = new GOval(1650,150,200,200);
ovals [0] = orange;
ovals [1] = green;
ovals [2] = blue;
ovals [3] = red;
ovals[0].setFilled(true);
ovals[0].setColor(Color.ORANGE);
add(ovals[0]);
ovals[1].setFilled(true);
ovals[1].setColor(Color.GREEN);
add(ovals[1]);
ovals[2].setFilled(true);
ovals[2].setColor(Color.BLUE);
add(ovals[2]);
ovals[3].setFilled(true);
ovals[3].setColor(Color.RED);
add(ovals[3]);
}//creating ovals method
public void createRects()
{
GRect green = new GRect(1000,0,250,250);
GRect green2 = new GRect(650,400,250,100);
GRect RedR = new GRect(1300,700,300,250);
GRect BlueR = new GRect(450,750,250,250);
rects [0] = green;
rects [1] = green2;
rects [2] = RedR;
rects [3] = BlueR;
rects[0].setFilled(true);
rects[0].setColor(Color.GREEN);
add(rects[0]);
rects[1].setFilled(true);
rects[1].setColor(Color.GREEN);
add(rects[1]);
rects[2].setFilled(true);
rects[2].setColor(Color.RED);
add(rects[2]);
rects[3].setFilled(true);
rects[3].setColor(Color.BLUE);
add(rects[3]);
}//create rectangles method
public static String getRandom(String[] array)
{
int rnd = new Random().nextInt(array.length);
return array[rnd];
}//getRandom
}
please help, thank you.
P.S. my BugBot bug is an external class i created, now sure if it matters what i have in there. But if it does ill add it later on.
Since all other directions are working fine, the problem can't be with the intersects function.
This if statement will never be true, since you're comparing left to both true and false.
//check for collision if direction is "left"
else if (left == true && right == false && right == false && left == false)
I think you want
else if (left == true && right == false && up == false && down == false)