my problem is that whenever I try to change an image when a mouse is hovered over it, it doesn't change, and when I do playgame.destroy(); it just shows a white screen behind it, not the other image. Heres my code:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
Image playgame;
Image exitgame;
Image playgame_hover;
Image exitgame_hover;
public String mouse = "No Input Yet!";
public Menu(int state) {
}
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
playgame = new Image("res/playgame.png");
exitgame = new Image("res/exitgame.png");
playgame_hover = new Image("res/playgame_hover.png");
exitgame_hover = new Image("res/exitgame_hover.png");
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawString(mouse, 590, 10);
playgame.draw(100,100);
exitgame.draw(100, 200);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int mousex = Mouse.getX();
int mousey = Mouse.getY();
mouse = "Mouse coordinate x: " + mousex + " y: " + mousey;
// x-min:105 x-max:300 y-min: y-max:300
if(input.isMouseButtonDown(0)) {
if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
sbg.enterState(1);
}
if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
System.exit(0);
}
}
if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
playgame.destroy();
playgame_hover.draw(100, 100);
}
if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
exitgame.destroy();
exitgame_hover.draw(100, 200);
}
}
#Override
public int getID() {
return 0;
}
}
You can only draw things in the render method. You have to save the state changed in the update method and then read those states in the render method.
Related
so I'm trying to write a flappy bird like coin collector game, and when I try to set the coin images coordinates to a random number the screen freezes. Any help?
Heres my player class:
package gamepackage;
import java.util.Random;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;
public class Playing extends BasicGameState{
Image coin;
Image guy;
int coincounter = 0;
float x = 30, y = 90;
int coiny = 20, coinx= 1000;
public Playing(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
guy = new Image("res/PlaneMovie1.gif");
coin = new Image("res/coin.gif");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawString("Coins:" + coincounter, 70, 70);
g.drawImage(coin, coinx, coiny);
g.drawImage(guy, x, y);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
coinx --;
if(input.isKeyDown(Input.KEY_SPACE)){ y -= 1 ; }
if(input.isKeyDown(Input.KEY_SPACE) == false){ y += .5;}
if(y == coiny){ coincounter += 1;coinx = 1000; coiny -= 20;}
if(coinx == -175){coinx = 1000; coiny += 20;}
}
public int getID(){
return 1;
}
}
We are having trouble making our character animate in a java slick2d game. The tutorial I seen shows doing it how I have done it. Right now it only shows the 1st image when going left or right. It never cycles between the 2 images set in walkLeft and walkRight.
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends BasicGameState {
Animation cowboy;
Animation movingLeft;
Animation movingRight;
Image background;
boolean quit = false;
int[] animationDuration = {200,200};
float cowboyPositionX = 0;
float cowboyPositionY = -370;
float shiftX = cowboyPositionX + 200;
float shiftY = cowboyPositionY + 830;
public Game(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
background = new Image("images/background.png");
Image[] walkLeft = { new Image("images/cowboyleft1.png"), new Image("images/cowboyleft2.png") };
Image[] walkRight = { new Image("images/cowboyright1.png"), new Image("images/cowboyright2.png") };
movingLeft = new Animation(walkLeft, animationDuration, false);
movingRight = new Animation(walkRight, animationDuration, false);
cowboy = movingRight;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
background.draw(cowboyPositionX, cowboyPositionY);
cowboy.draw(shiftX, shiftY);
g.drawString("Cowboy's X:" + cowboyPositionX + "\nCowboy's Y: "
+ cowboyPositionY, 400, 20);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_LEFT)) {
cowboy = movingLeft;
cowboyPositionX += horizontalSpeed;
if (cowboyPositionX > 0) {
cowboyPositionX -= delta * horizontalSpeed;
}
}
if (input.isKeyDown(Input.KEY_RIGHT)) {
cowboy = movingRight;
cowboyPositionX -= horizontalSpeed;
if (cowboyPositionX < -2975.0) {
cowboyPositionX += delta * horizontalSpeed;
}
}
public int getID() {
return 1;
}
}
In your update method try inserting cowboy.update(delta); that should work. It worked for the program that I have been working on.
I have this problem, and it keeps on throwing the illegal start of expression, and I checked the brackets, and I think they're fine. Here's the code: Also, it says that theres an error at line 30, which I don't really understand....
public class Menu extends BasicGameState {
Image playNow;
Image exitGame;
public String mouse = "No Input Yet!";
public Menu(int state) {
}
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
playNow = new Image("res/playNow.png");
exitGame = new Image("res/exitGame.png");
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawString(mouse, 590, 10);
g.drawString("Welcome to my Game!", 100, 50);
playNow.draw(100,100);
exitGame.draw(100, 200);
}
//slick counts from the bottom-left of the display, not the top-left, like java does
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int mousex = Mouse.getX();
int mousey = Mouse.getY();
mouse = "Mouse coordinate x: " + mousex + " y: " + mousey;
// x-min:105 x-max:300 y-min: y-max:300
if(input.isMouseButtonDown(0)) {
if(mousex>100 && mousex<300) && (mousey>400 && mousey< 435) { //error
sbg.enterState(1);
}
if(mousex>100 && mousex<300) && (mousey>300 && mousey <335) { //error
System.exit(0);
}
}
}
#Override
public int getID() {
return 0;
}
}
I really need help quickly.
In Java, the condition for an if must be completely surrounded by parentheses. Change
if(mousex>100 && mousex<300) && (mousey>400 && mousey< 435) {
to
if((mousex>100 && mousex<300) && (mousey>400 && mousey< 435)) {
... and the other if condition similarly.
The compiler thought that (mousex>100 && mousex<300) was the entire condition, and && (mousey>400 && mousey< 435) didn't make sense as the body for the condition.
I cannot find an answer in Google, but is there a Mouse Over Area for Slick2D? Google just gives me results of Java's MouseOverArea. I just want to know if there is a MouseOverArea for Slick2D, and how it looks like.
Here is my code:
Game class
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Life - Alpha";
public static int splash = 0;
public static int menu = 1;
public static int loading = 2;
public static int play= 3;
public Game(String gamename) {
super(gamename);
}
public void initStatesList(GameContainer gc) throws SlickException {
this.addState(new SplashScreen (splash));
this.addState(new Menu (menu));
this.addState(new Exit (exit));
this.addState(new Loading (loading));
this.addState(new Play(play));
this.enterState(0);
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
SplashScreen class:
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class SplashScreen extends BasicGameState {
Image splash;
private int elapsedTime;
private final int DELAY = 3000;
public SplashScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
splash = new Image("res/SplashScreen.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(splash, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(1);
}
}
public int getID() {
return 0;
}
}
Menu class:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
public Menu(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Background.png");
g.drawImage(background, 0, 0);
Image logo = new Image("res/Logo.png");
g.drawImage(logo, 275, 50);
Image playButton = new Image("res/Play button.png");
g.drawImage(playButton, 210, 250);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
//I want to put the Slick2D MouseOverArea code here...
//So then when I put the mouse over the playButton, something will display.
}
}
public int getID() {
return 1;
}
}
Loading class:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Loading extends BasicGameState {
private int elapsedTime;
private final int DELAY = 5000;
public Loading(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Back.png");
g.drawImage(background, 0, 0);
Image loading = new Image("res/Loading.png");
g.drawImage(loading, 210, 150);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(3);
}
}
public int getID() {
return 2;
}
}
Play class:
I'm working on it still... but this class doesn't need the MouseOverArea, the Menu class does.
So that was my code above. I just need a MouseOverArea for Slick2D. Google doesn't help. Hope you can.
Also, can you have a TextField in Slick2D? I don't know if I can. I know in normal Java you can, but can you in Slick2D?
If there are any mistakes, don't worry, I can fix them.
Thanks
Isn't the code put in here?
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
//mouseover
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}
okay, so i have my image move straight down along the y axis whenever i click the mouse, my only problem is i don't know how to get it to stop when it hits the bottom of the screen, can someone please help?
import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Control extends BasicGameState {
public static final int ID = 1;
public Methods m = new Methods();
public Point[] point = new Point[(800 * 600)];
int pressedX;
int pressedY;
int num = 0;
String Build = "1.1";
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (Point p : point) {
if (p != null) {
m.drawParticle(p.x, p.y += 1);
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mousePressed(int button, int x, int y) {
pressedX = x;
pressedY = y;
num = num + 1;
point[num] = new Point(pressedX, pressedY);
}
public int getID() {
return ID;
}
}
I'd imagine somewhere you will want to check the x/y pos of the particle before you renderer it and remove it from the array when it's out of bounds...
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (int index = 0; index < point.length; index++) {
Point p = point[index];
if (p != null) {
p.y++;
if (p.y > height) { // You'll need to define height...
point[index] = null; // Or do something else with it??
} else {
m.drawParticle(p.x, p.y);
}
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
You could also do a preemptive check, which would allow you to know what points are at the bottom of the screen...
if (p != null) {
if (p.y >= height) { // You'll need to define height...
// Do something here
} else {
p.y++;
m.drawParticle(p.x, p.y);
}
}