Написал код чтобы узнать столкнулся танк или нет:
Update(){
for(Point p : brickCoords) {//в brickCoords координаты x и y
if((Math.abs(p.x - Player.newX) <= (SCALE + Player.SPRITE_SCALE * Player.scalee) - 10) && (Math.abs(p.y - Player.newY) <= (SCALE + Player.SPRITE_SCALE * Player.scalee) - 10)) { //newX & newY координаты танка
}
}
но как сделать чтобы когда игрок сталкивался с объектом, танк останавливался, я смог только родить идею сделать переменную скорости 0(но это не то), вот код класса игрока:
public class Player extends Entity{
public static final int SPRITE_SCALE = 16;
public static final int SPRITE_ROTATE = 1;
private static float scale;
public static float speedPlayer = 1.90f;
public static float newX;
public static float newY;
public static float scalee;
public enum Rotate{
NORTH(0 * SPRITE_SCALE, 0 * SPRITE_SCALE, 1 * SPRITE_SCALE, 1 * SPRITE_SCALE),
EAST(6 * SPRITE_SCALE, 0 * SPRITE_SCALE, 1 * SPRITE_SCALE, 1 * SPRITE_SCALE),
SOUTH(4 * SPRITE_SCALE, 0 * SPRITE_SCALE, 1 * SPRITE_SCALE, 1 * SPRITE_SCALE),
WEST(2 * SPRITE_SCALE, 0 * SPRITE_SCALE, 1 * SPRITE_SCALE, 1 * SPRITE_SCALE);
private int x, y, w, h;
Rotate(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
protected BufferedImage texture(TextureAtlas atlas) {
return atlas.cut(x, y, w, h);
}
}
private Rotate rotate;
private Map<Rotate, Sprite> spriteMap;
private float speed;
public Player(float x, float y, float scale, float speed, TextureAtlas atlas){
super(EntityType.Player, x, y);
rotate = rotate.NORTH;
spriteMap = new HashMap<Rotate, Sprite>();
this.scale = scale;
this.speed = speed;
for(Rotate r : Rotate.values()) {
SpritePice pice = new SpritePice(r.texture(atlas), SPRITE_ROTATE, SPRITE_SCALE);
Sprite sprite = new Sprite(pice, scale);
spriteMap.put(r, sprite);
}
}
@Override
public void Update(Input input){
scalee = scale;
newX = x;
newY = y;
if(input.getKey(KeyEvent.VK_W)) {
rotate = rotate.NORTH;
newY -= speed;
}else if(input.getKey(KeyEvent.VK_S)) {
rotate = rotate.SOUTH;
newY += speed;
}else if(input.getKey(KeyEvent.VK_A)) {
rotate = rotate.WEST;
newX -= speed;
}else if(input.getKey(KeyEvent.VK_D)) {
rotate = rotate.EAST;
newX += speed;
}
if(newX < 0) {
newX = 0;
}else if(newX >= game.WIDTH - SPRITE_SCALE * scale){
newX = game.WIDTH - SPRITE_SCALE * scale;
}
if(newY < 0) {
newY = 0;
}else if(newY >= game.HEIGHT - SPRITE_SCALE * scale){
newY = game.HEIGHT - SPRITE_SCALE * scale;
}
x = newX;
y = newY;
newX = x;
newY = y;
speed = speedPlayer;
}
@Override
public void render(Graphics2D g){
spriteMap.get(rotate).render(g, x, y);
}
}
если я не скинул какой-то важной инфы напишите, я отредачу