I have made a simple 2D state change game using Bucky's slick Java tutorials, I modified this game and now want to set collisions on the map so that my player can not go through the house on the map. I think I kind of have a idea of how collisions work:
you make 2 rectangles using the following code:
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
1 for the player and 1 for the obstacle, how would I put this into my code and how would I tell java that the rectangle for the obstacle is different to the player rectangle?
Then after making the 2 rectangles I would set up a if statement saying something like if intersects do this...
Hopefully after this I think it would work. Some more information on the game, it being a state change game it has a few methods, methods like init, render and update (where do I place my rectangles and if statements, in the update method?) also, its a overhead view game kind of like pokemon if that helps. If you require my code please ask, I did not want to put it on now to overcrowd this post.
Edit1:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight, movingBL, movingBR, movingFL, movingFR;
Image worldMap;
boolean quit = false;//gives user to quit the game
int[] duration = {200, 200};//how long frame stays up for
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/world.png");
Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};
Image[] walkBL = {new Image("res/bl.png"), new Image("res/bl.png")};
Image[] walkBR = {new Image("res/br.png"), new Image("res/br.png")};
Image[] walkFL = {new Image("res/fl.png"), new Image("res/fl.png")};
Image[] walkFR = {new Image("res/fr.png"), new Image("res/fr.png")};
movingUp = new Animation(walkUp, duration, false);
movingDown = new Animation(walkDown, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
movingBL = new Animation(walkBL, duration, false);
movingBR = new Animation(walkBR, duration, false);
movingFL = new Animation(walkFL, duration, false);
movingFR = new Animation(walkFR, duration, false);
bucky = movingDown;//facing screen initially on startup
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
bucky.draw(shiftX, shiftY);//makes him appear at center of map
g.drawString("Suraj's X: "+buckyPositionX+"
Suraj's Y: "+buckyPositionY,400,20);//tells us the position
if(quit==true){
g.drawString("Resume(R)", 250, 100);
g.drawString("Main(M)", 250, 150);
g.drawString("Quit Game(Q)", 250, 200);
if(quit==false){
g.clear();//wipe off everything from screen
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
//up
if(input.isKeyDown(Input.KEY_UP)){
bucky = movingUp;//changes the image to his back
buckyPositionY += 10;;//increase the Y coordinates of bucky (move him up)
if(buckyPositionY>162){//if I reach the top
buckyPositionY -= 10;//stops any further movement in that direction
}
}
//down
if(input.isKeyDown(Input.KEY_DOWN)){
bucky = movingDown;
buckyPositionY -= 10;
if(buckyPositionY<-600){
buckyPositionY += 10;//basically change the direction if + make -
}}
//left
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += 10;
if(buckyPositionX>324){
buckyPositionX -= 10;//delta * .1f
}}
//right
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= 10;
if(buckyPositionX<-840){
buckyPositionX += 10;
}}
//2 key combos start here
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBR;
buckyPositionX -= delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBL;
buckyPositionX -= delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFR;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFL;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
}}}
//escape
if(input.isKeyDown(Input.KEY_ESCAPE)){
quit=true;
}
//when the menu is up
if(quit==true){//is the menu on the screen
if(input.isKeyDown(Input.KEY_R)){
quit = false;//resumes the game, makes menu dissapear
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);//takes you to the main menu
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);//quits the game
}
}
}
public int getID(){
return 1;
}
}
This is my Play class, the only other 2 class's I have are the main and the menu, I cant imagine the rectangle methods being made in the main or menu class so the only one left is the Play, but I dont understand how to make 2 different Rectangles (one for the player the other for the house) in the code I have done so far. If you require my main and my menu class please tell me.
Edit 3:
I have tried what you have said and made a Rectanglebase class and put the if sattement you had posted inside there but am getting errors, it is asking me to make a method for getX and getY in my player class also the public in fornt of the constructor also has a error:
public Rectanglebase{}//the public is saying syntax error
I also made a Home and Player class like you had said but am a bit confused on what I need to put in it, I put under the Home class:
return Rectangle(100,100,100,100);
but am getting errors, I am not sure if I did this correct or not.
Also, in the player class for the x,y positions how would I set my float variables from my Play class for my player?
See Question&Answers more detail:
os