So I am trying to get a simple GUI setup for my teams game we are starting and I'm trying to make a drop down inventory using "I" as the hot key for it and it drops fine but I can't seem to figure out a way without using a different key to make it retract or in essence "un" draw the image'
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws
SlickException {
mapHud.draw(440, 1);
hotBar.draw(160, 454);
if(inv){
inventory.draw(-40, 1);
}
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
if(input.isKeyPressed(Input.KEY_I)){
inv = true;
}
if(input.isKeyPressed(Input.KEY_ESCAPE)) {
sbg.enterState(0);
}
}
Try something like this:
if(input.isKeyPressed(Input.KEY_I)){
inv = !inv;
}
Related
I am making a game in Slick2D for Java, but when I tried to display an Image(the Slick2D type) it gives me this error:
java.lang.NullPointerException
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384)
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433)
at luke_r.games.java.broadway.states.M.render(M.java:21)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at luke_r.games.java.broadway.C.main(C.java:21)
Here is the for the M class (excluding imports and class declaration, they are unneeded):
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
PL.x = 100;
PL.y = 100;
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawImage(I.title, 0, 0); //Line 21, the error
PL.render(g);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{}
#Override
public int getID()
{
return 0;
}
The problem is that I.title == null. Set it to a correct value.
In init function set the url to the image into I.title, i mean => I.title = new Image("-your URL-");
Slick2D works in this form: first of all he is init all params in method init , then until somthing happend that change the current state "he" render => update => render => update =>render => update ... You can set how much the update will occurs in the SetUpClass by using SetTargetFrameRate method.
I've implemented some pathfinding in my 2d game, but I don't know how to move my 'Zombie' character.
I'm using slick2d and everything gets put on the screen in the render method, however I want to update the zombies movement in the Zombie class.
Here is the method in the zombie class:
public void findPrey(){
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
for(PathCoordinates p: pf.calculatePath()){
if(p.getXPosition() > xPosition){
moveRight(2);
}if(p.getXPosition() < xPosition){
moveLeft(2);
}if(p.getYPosition() > yPosition){
moveUp(2);
}if(p.getYPosition() < yPosition){
moveDown(2);
}
}
}
So I want to loop through the List of x and y coordinates and move the zombie.
However nothings happens- the zombie doesnt move at all.
In my 'world' class I have the render method, which loops through the zombies and renders them, the code has been edited slightly:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
for (Zombie z : zombies) {
if (!z.isDead()) {
z.render(gc, sbg, g);
z.findPrey();
}
}
}
So, basically my question is how - do I make my zombie character move?! Thanks for any help or guidance
You should first move the zombie, then render it.
There's another problem: you allow to move the zombie all the way it should with one findPrey() (using updatePosition() in code below) method call.
Zombie class:
public void updatePosition() {
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
PathCoordinates p = pf.calculatePath()[0];
if(p.getXPosition() > xPosition) {
moveRight(2);
} if(p.getXPosition() < xPosition) {
moveLeft(2);
} if(p.getYPosition() > yPosition) {
moveUp(2);
} if(p.getYPosition() < yPosition) {
moveDown(2);
}
}
World class:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
for(Zombie z : zombies) {
if(!(z.isDead())) {
z.updatePosition();
z.render(gc, sbg, g);
}
}
}
I've Googled a lot and could not find the answer to this. I know how to load an image, I need to load images that aren't 256 by 256 or a direct power of 2.
Like how could I load an image that's 128 by 384 or something like that.
What I'm using now:
Load an image:
public static Texture cow = loadTexture("res/cow.png");
private static Texture loadTexture(String file){
try {
return TextureLoader.getTexture("JPG", new FileInputStream(new File(file)));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Now to draw it on a 3D poly face:
txt.bind();
GL11.glBegin(GL11.GL_QUADS);
{
//GL11.glColor3d(lightLevel, lightLevel, lightLevel);
GL11.glColor3d(l, l, l);
GL11.glTexCoord2f(0,0); GL11.glVertex3f(x1,y1, z1);
GL11.glTexCoord2f(1,0); GL11.glVertex3f(x1+(x2-x1), y1,z1);
GL11.glTexCoord2f(1,1); GL11.glVertex3f(x1+(x2-x1), y1+(y2-y1), z1+(z2-z1));
GL11.glTexCoord2f(0,1); GL11.glVertex3f(x1,y1+(y2-y1), z1+(z2-z1));
}
GL11.glEnd();
Now that works perfectly, I just need to load images that aren't a power of 2.
you could use slick2d, it is way easier to use and works with lwjgl! All you have to do is:
Image title = null;
public static void main(String[] args) {
}
#Override
public void init(GameContainer Gc, StateBasedGame Sbg)
throws SlickException {
/**
* Images
*/
title = new Image("gfx/main_menu/title/new_title.png");
}
#Override
public void render(GameContainer Gc, StateBasedGame Sbg, Graphics G)
throws SlickException {
/**
* Background
*/
G.setColor(Color.white);
G.fillRect(0, 0, w*s, h*s);
/**
* Images
*/
title.draw(titleY*s,titleX*s);
}
I'm playing around with Slick2D and I'm running into a problem when trying to move a simple rectangle with the cursor keys.
Functionality wise, the code works. However, if you keep one of the cursor keys pressed, every now and then there is a little hick-up in the movement wen looking at the screen.
Can anyone suggest a code improvement to make the rectangle move smoothly?
Here is the test code I used:
public class SlickGame extends BasicGame {
// ==================================================================================
// Fields
// ==================================================================================
private Rectangle mPlayer;
// ==================================================================================
// Constructor
// ==================================================================================
public SlickGame() {
super("SlickGame");
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public void init(GameContainer pGameContainer) throws SlickException {
// create the player centered on the screen
mPlayer = new Rectangle(pGameContainer.getWidth()/2 - 20, pGameContainer.getHeight()/2 - 20, 40, 40);
}
#Override
public void update(GameContainer pGameContainer, int pDelta) throws SlickException {
Input input = pGameContainer.getInput();
int speed = 200;
float distance = speed * ((float)pDelta/1000);
if (input.isKeyDown(Input.KEY_LEFT)) {
mPlayer.setX(mPlayer.getX() - distance);
}
if (input.isKeyDown(Input.KEY_RIGHT)) {
mPlayer.setX(mPlayer.getX() + distance);
}
if (input.isKeyDown(Input.KEY_UP)) {
mPlayer.setY(mPlayer.getY() - distance);
}
if (input.isKeyDown(Input.KEY_DOWN)) {
mPlayer.setY(mPlayer.getY() + distance);
}
}
#Override
public void render(GameContainer pGameContainer, Graphics pGraphics) throws SlickException {
pGraphics.fill(mPlayer);
}
// ==================================================================================
// Methods
// ==================================================================================
public static void main(String[] args) {
try {
// create the game's container app
AppGameContainer container = new AppGameContainer(new SlickGame());
// adjust the resolution and disable fullscreen
container.setDisplayMode(800, 600, false);
// specify desired FPS
container.setTargetFrameRate(60);
// start the game
container.start();
}
catch (SlickException e ) {
e.printStackTrace();
}
}
}
Thanks, naughty_hacker. Adding container.setVSync(true); did the trick.
Every couple seconds, the window in which my game is playing will briefly disappear and then reappear. I'm on Windows 7 with the latest version of Slick (a game library for Java). Here's the code I'm using:
package Main;
import org.newdawn.slick.*;
public class Main extends BasicGame{
public Main() {
super("Flashing window issue");
}
#Override
public void init(GameContainer gc) throws SlickException {
}
#Override
public void update(GameContainer gc, int delta) throws SlickException {
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException {
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer(new Main());
app.setDisplayMode(800, 600, false);
app.start();
}
}
How can I fix this issue?
Progress so far:
Update: No solution found yet, but playing the game in fullscreen mode eliminates the flicker. Perhaps this will lead to a solution...
Update 2: Monitering the task manager shows that while the game is flickering its status in the task manager is 'Not Responding'.
Update 3: It seems to only happen when the mouse leaves the game area (regardless of whether the game window loses focus).
Update 4 - Current workaround:
app.setMouseGrabbed(true); // force the mouse to stay in the game area
then in update(...):
// exit when escape is pressed:
if (gc.getInput().isKeyDown(Input.KEY_ESCAPE)) {
gc.exit();
}
I'm not familiar with slick2d but does it have a concept of double buffering? that would be something you would want to turn on if you get flickering.