さて、私のゲームで運転できる車があります。これは基本的に、背景の上を走り回るイメージです。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();
}}