Reputation: 67
Ok, so i have a car that you can drive around in my game. It's basically an Image that drives around on a background, a and d turns it, w and s drives back and forth. But i want to make some borders on the screen edges so it doesnt just keep driving out into nothing, but to do that i think i have to get the x and y location of the Image. Does anyone know how to get this/know another way of making borders in Slick2D?
Thanks, Marco.
This is the GameplayState code:
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));
}
}
}
}
Car code:
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();
}}
Upvotes: 1
Views: 1713
Reputation: 399
What I would do is create a Car class that has an Image. With this class, you can have ints that hold the x and y values representing the car's location, as well as the image of the car. Then, you just set the values or x and y and rotate the image like you are doing in your example, except for the Car object. You may do this already, I am not sure though without more code than just the input handler. To draw it, do something like this in your render:
drawImage(car.getImage(), car.getX(), car.getY());
As far as borders, assuming you just don't want your Car to go off screen, you can do something like this:
// 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());
}
In this context, get width and get height are getting the width and height of the image you use for the car, and SCREEN_HEIGHT and SCREEN_WIDTH are constants that hold the width and height of the screen. You may have to fine tune a bit depending on how your game works.
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;
}
Upvotes: 1