Question

Im working on a code for a game and what i need help on is that I put objects on the map and am trying to make it so that the Player can not walk through the object.n The problem is that the x boundary works but the y does not and i am not sure how to procede.

 public Boolean checkPathClear(int x, int y){
     xPPos=x;
     yPPos=y;
     int tX1=350-50-xPos,tX2=450-xPos,tY1=200,tY2=300;
     if ((xPPos>=tX1)&&(xPPos<=tX2)&&(yPPos>=tY1)&&(yPPos<tY2))
       return(false);
     else
       return(true);
   }
   public void paintComponent(Graphics g){    
     g.drawImage(imgTurret,350-xPos,200,450-xPos,300,0,0,126,110, this);

     g.drawImage(imgPlayer,xPPos,yPPos,xPPos+50,yPPos+50,xPFace,yPFace,xPFace+50,yPFace+50, this);
       }
 }
 //xPPos is 200 at the start.
 //yPPos is 200 at the start.
 //xPos is 0 at the start.
 //xPFace, yPFace is just the direction the player is facing so it doesnt matter

I edited the code so people do not need to look through everything :)

I'd appreciate if someone could help me :D

EDIT: For anyone who wants to see my aboard which im implementing the code on. as you move right the image display moves left so it looks like the screen is moving right. Arena Board

Was it helpful?

Solution

Your image is drawn with dimensions:

imageX: 350-xPos,
imageY: 200,
width: 450-xPos,
height: 300

So:

return !(imageX <= x && x' < imageX + width
    && imageY <= y && y' < imageY + height);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top