Slick2D获取图像x和y

时间:2013-03-02 14:47:03

标签: java eclipse image slick2d

好的,所以我有一辆车,你可以在我的游戏中开车。它基本上是一个在背景上驱动的图像,a和d转动它,w和s来回驱动。但是我想在屏幕边缘做一些边框,所以它不会只是一无所知,但为了做到这一点,我想我必须得到图像的x和y位置。有谁知道如何获得这个/知道在Slick2D中制作边框的另一种方式?

谢谢,Marco。

这是GameplayState代码:

package myprojects.main;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameplayState extends BasicGameState {

    int stateID = -1;
    Image imgBG = null;
    Image player = null;
    float scale = 1.0f;
    Sound ding = null;
    Sound gamemusic = null;
    int height;
    int width;
    private static int rightSide = 1200;
    private static int topSide = 800;
    float startGameScale = 1;
    float scaleStep = 0.001f;
    int[] duration = { 200, 200 };
    public Car car;
    // Animation car, driveUp, driveDown, driveRight, driveLeft;

    boolean hasWon;

    GameplayState(int stateID) {
        this.stateID = stateID;
    }

    @Override
    public int getID() {
        return stateID;
    }

    public void init(GameContainer gc, StateBasedGame sbg)
            throws SlickException {
        imgBG = new Image("myprojects/main/resources/land.png");
        player = new Image("myprojects/main/resources/playercar.png");
        ding = new Sound("myprojects/main/resources/ding.wav");
        gamemusic = new Sound("myprojects/main/resources/gamemusic.wav");
        car = new Car(300,300);

        /**
         * Image[] carUp = {new Image("myprojects/main/resources/carUp.png"),
         * new Image("myprojects/main/resources/carUp.png")}; Image[] carDown =
         * {new Image("myprojects/main/resources/carDown.png"), new
         * Image("myprojects/main/resources/carDown.png")}; Image[] carRight =
         * {new Image("myprojects/main/resources/carRight.png"), new
         * Image("myprojects/main/resources/carRight.png")}; Image[] carLeft =
         * {new Image("myprojects/main/resources/carLeft.png"), new
         * Image("myprojects/main/resources/carLeft.png")};
         * 
         * driveUp = new Animation(carUp, duration, false); driveDown = new
         * Animation(carDown, duration, false); driveRight = new
         * Animation(carRight, duration, false); driveLeft = new
         * Animation(carLeft, duration, false); player = driveDown;
         **/

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
            throws SlickException {
        imgBG.draw(0, 0);
        gc.setSoundVolume(0.05f);
        gamemusic.loop();
        g.drawString("Pause Game", 1100, 775);
        g.drawImage(car.getImage(), car.getX(), car.getY());

    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta)
            throws SlickException {
        Input input = gc.getInput();
        // Check if outside left border
        if(car.x < 0) {
            car.setX(0);
        }
        // Check if outside top border
        if(car.y < 0) {
            car.setY(0);
        }
        // Check if outside right border
        if(car.x >= gc.getWidth() - car.getWidth()) {
            car.setX(gc.getWidth() - car.getWidth());
        }
        // Check if outside bottom border
        if(car.y >= gc.getHeight() - car.getHeight()) {
            car.setY(gc.getHeight() - car.getHeight());
        }

        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
        boolean hasWon = false;

        if ((mouseX <= rightSide && mouseX >= rightSide - 100)
                && (mouseY <= topSide && mouseY >= topSide - 50)) {
            hasWon = true;
        }
        if (hasWon) {
            if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
                sbg.enterState(StarRacingGame.MAINMENUSTATE);
            } else {

            }
        }

        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            ding.play();
            sbg.enterState(StarRacingGame.MAINMENUSTATE);
        }

        if (input.isKeyDown(Input.KEY_A)) {
            car.rotate(-0.2f * delta);
        }

        if (input.isKeyDown(Input.KEY_D)) {
            car.rotate(0.2f * delta);
        }

        if (input.isKeyDown(Input.KEY_S)) {
            float hip = 0.4f * delta;

            float rotation = car.getRotation();

            car.x -= hip * Math.sin(Math.toRadians(rotation));
            car.y += hip * Math.cos(Math.toRadians(rotation));

        }

        if (input.isKeyDown(Input.KEY_W)) {
            if (input.isKeyDown(Input.KEY_LSHIFT)) {
                float hip = 1.4f * delta;
                float rotation = car.getRotation();

                car.x += hip * Math.sin(Math.toRadians(rotation));
                car.y -= hip * Math.cos(Math.toRadians(rotation));
            } else {
                float hip = 0.4f * delta;
                float rotation = car.getRotation();

                car.x += hip * Math.sin(Math.toRadians(rotation));
                car.y -= hip * Math.cos(Math.toRadians(rotation));
            }
        }

    }

}

汽车密码:

    package myprojects.main;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Car {
public int x;
public int y;
public Image image;

public Car(int x, int y) throws SlickException{
    this.x = x;
    this.y = y;
    image = new Image("myprojects/main/resources/playercar.png");

}

public org.newdawn.slick.Image getImage() {
    return image;
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public void setY(int y) {
    this.y = y;
}

public int getWidth() {
    return 100;
}

public int getHeight() {
    return 100;
}

public int getY() {
    return y;

}

public void rotate(float f) {
    image.rotate(f);
}

public float getRotation() {
    return image.getRotation();
}}

1 个答案:

答案 0 :(得分:1)

我要做的是创建一个具有Image的Car类。使用此类,您可以使用包含表示汽车位置的x和y值以及汽车图像的整数。然后,您只需设置值或x和y并旋转图像,就像您在示例中所做的那样,Car对象除外。你可能已经这样做了,我不确定没有更多的代码而不仅仅是输入处理程序。要绘制它,在渲染中执行类似的操作:

drawImage(car.getImage(), car.getX(), car.getY());

至于边界,假设你不想让你的汽车离开屏幕,你可以这样做:

// Check if outside left border
if(car.getX() < 0) {
    car.setX(0);
}
// Check if outside top border
if(car.getY() < 0) {
    car.setY(0);
}
// Check if outside right border
if(car.getX() >= SCREEN_WIDTH - car.getWidth()) {
    car.setX(SCREEN_WIDTH - car.getWidth());
}
// Check if outside bottom border
if(car.getY() >= SCREEN_HEIGHT - car.getHeight()) {
    car.setY(SCREEN_HEIGHT - car.getHeight());
}

在此上下文中,获取宽度和获取高度获取用于汽车的图像的宽度和高度,SCREEN_HEIGHT和SCREEN_WIDTH是保持屏幕宽度和高度的常量。您可能需要根据游戏的工作方式进行微调。

public Class Car {
private int x;
private int y;
private Image image;

public Car(int x, int y) {
    this.x = x;
    this.y = y;
    image = new Image("location/car.png");
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}