Hi everyone, it's lavago and I need help.
I've been trying to get it so that when I click on the entity "crow", it gets rid of it.
I'm using
Libraries:
Slick2d and
MarteEngineMy code is:
Crow.java
Code:
package gamepackage;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import it.randomtower.engine.ME;
import it.randomtower.engine.entity.*;
public class Crow extends Entity {
public static String CROW = "crow";
private final String corn = (Corn.CORN);
private int frameCount = 0;
private int frame = 0;
private int nextFrame = 300;
private int health;
public int corncount;
public Crow(float x, float y) throws SlickException {
super(x, y);
Image img = new Image("Resources/crow1.png");
setHitBox(0, 0, img.getWidth(), img.getHeight());
setGraphic(img);
addType(CROW);
health = 90;
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
super.update(gc, delta);
Input input = gc.getInput();
int posX = Mouse.getX();
int posY = Mouse.getY();
Image crow = new Image("Resources/crow1.png");
Image crow2 = new Image("Resources/crow2.png");
Entity corncollide = collide(corn,x,y);
if(corncollide != null) {
corncount -= 1;
ME.world.remove(this);
}
//animation
frameCount += delta;
if(frameCount >= nextFrame) {
frameCount -= nextFrame;
frame++;
if (frame % 2 == 0) {
setGraphic(crow2);
} else {
setGraphic(crow);
}
}
if(posX == this.x && posY == this.y) {
if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
health -= 45;
if(health <= 0) {
this.destroy();
}
}
}
//Remove image if it's out of the screen.
y += (.1 * delta);
if(y > ME.world.getHeight()) {
this.destroy();
}
}
public void render(GameContainer gc, Graphics g) throws SlickException {
super.render(gc, g);
}
@Override
public void collisionResponse(Entity other) {
ME.world.remove(this);
}
}
Corn.java
Code:
package gamepackage;
import org.newdawn.slick.*;
import org.newdawn.slick.state.StateBasedGame;
import it.randomtower.engine.ME;
import it.randomtower.engine.entity.*;
public class Corn extends Entity {
public static final String CORN = "corn";
private final String crows = (Crow.CROW);
public Corn(float x, float y) throws SlickException {
super(x, y);
Image img = new Image("Resources/Corn.png");
setHitBox(0, 0, img.getWidth(), img.getHeight());
setGraphic(img);
addType(CORN);
}
public void init(GameContainer gc, StateBasedGame game) throws SlickException {
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
super.update(gc, delta);
Entity crowcollide = collide(crows,x,y);
if(crowcollide != null) {
ME.world.remove(this);
}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
super.render(gc, g);
}
}
EasyWorld.java
Code:
package gamepackage;
import it.randomtower.engine.*;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class EasyWorld extends World {
Image Background;
public int corncount = 3;
public EasyWorld(int id, GameContainer gc) {
super(id, gc);
}
@Override
public void init(GameContainer gc, StateBasedGame game) throws SlickException {
super.init(gc, game);
Background = new Image("Resources/Background.png");
//The Corn
Corn corn = new Corn(50,500);
add(corn);
Corn corn2 = new Corn(350,500);
add(corn2);
Corn corn3 = new Corn(650,500);
add(corn3);
//The Crows
Crow crow1row1 = new Crow(50,100);
add(crow1row1);
Crow crow2row1 = new Crow(350,100);
add(crow2row1);
Crow crow3row1 = new Crow(650,100);
add(crow3row1);
}
@Override
public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
super.update(gc, game, delta);
if(corncount <= 0) {
gc.exit();
}
}
@Override
public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException {
Background.draw();
super.render(gc, game, g);
}
public int getID(){
return 0;
}
}
Game.java
Code:
package gamepackage;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static int GAME_STATE = 0;
public static int EASY = 1;
public Game(String title) {
super(title);
}
@Override
public void initStatesList(GameContainer gc) throws SlickException {
//UnComment This If You Want To Use The Resource Manager.
/*
* try {
ResourceManager.loadResources("Resources/resources.xml");
} catch (IOException e) {
e.printStackTrace();
}
*/
addState(new EasyWorld(GAME_STATE, gc));
enterState(GAME_STATE);
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer (new Game("Hit Those Crows"));
app.setDisplayMode(800, 600, false);
app.setTargetFrameRate(60);
app.start();
}
}