1

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

谢谢,马可。

这是 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();
}}
4

1 回答 1

1

我要做的是创建一个具有图像的汽车类。使用此类,您可以使用 int 来保存表示汽车位置的 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());
}

在这种情况下,get width 和 get height 获取用于汽车的图像的宽度和高度,而 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;
}
于 2013-03-02T16:05:47.203 回答