I try to create a mere 2D game in Java and with my researches I found out that the best motor should be Slick.
I used the softaware Tiled to create the map and try to run my Java program to see the result and after many problem of compression I already solved, it turned out that the map is simply false (the tiles are not placed well).
I tried to change see the .tmx code in a text editor but I found nothing to change.
Here's my Java code I run in Eclipse :
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.*;
public class Lecon_1 extends BasicGame {
GameContainer container;
TiledMap map;
public Lecon_1() {
super("Lesson 1 :: WindowGame");
}
public static void main(String[] args) throws SlickException {
new AppGameContainer(new Lecon_1(), 321, 321, false).start();
}
#Override
public void init(GameContainer container) throws SlickException {
this.container = container;
this.map = new TiledMap("src/map/Essai1.tmx");
}
#Override
public void render(GameContainer container, Graphics g) throws SlickException {
this.map.render(0, 0);
}
#Override
public void update(GameContainer container, int delta) throws SlickException {
}
#Override
public void keyReleased(int key, char c) {
if (Input.KEY_ESCAPE == key) {
container.exit();
}
}
}
The Map I created:
The Map I get after running the code:
I tried many things and search in a lot of websites but nobody seems to have this problem. It may be a problem from the Slick classes but I don't know which one and don't know why. Does anyone have an idea?
Thanks in advance,
Alburkerk
Related
The problem is that my application size is not consistent. Sometimes application size is same as I defined in my code, but most of the time it's just a small piece on my screen. I'm using kubuntu and Intelij. I think that maybe It's a problem with my system settings or Intelij. So maybe someone had a similar experience and can give some advices how to solve this problem. My application code bellow and pictures of my problem.
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.Input;
import com.almasb.fxgl.input.UserAction;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import java.util.Map;
public class main extends GameApplication {
#Override
protected void initSettings(GameSettings settings) {
settings.setWidth(600);
settings.setHeight(600);
settings.setTitle("Basic Game App");
settings.setVersion("0.1");
settings.setFullScreenAllowed(true);
}
#Override
protected void initInput() {
Input input = FXGL.getInput();
input.addAction(new UserAction("Move Right") {
#Override
protected void onAction() {
player.translateX(5); // move right 5 pixels
FXGL.getGameState().increment("pixelsMoved", +5);
}
}, KeyCode.D);
input.addAction(new UserAction("Move Left") {
#Override
protected void onAction() {
player.translateX(-5); // move left 5 pixels
FXGL.getGameState().increment("pixelsMoved", +5);
}
}, KeyCode.A);
input.addAction(new UserAction("Move Down") {
#Override
protected void onAction() {
player.translateY(5); // move down 5 pixels
FXGL.getGameState().increment("pixelsMoved", +5);
}
}, KeyCode.S);
}
#Override
protected void initGameVars(Map<String, Object> vars) {
vars.put("pixelsMoved", 0);
}
private Entity player;
#Override
protected void initGame() {
player = FXGL.entityBuilder()
.at(0, 0)
.view(new Rectangle(25, 25, Color.BLUE))
.buildAndAttach();
}
public static void main(String[] args) {
launch(args);
}
}
i'm new to Slick2d and to this page too, i tried asking the slick forums, but there aren't many people so i couldn't get an answer
Anyway, i've been trying to create a platformer engine in Slick2d java game library, and i was thinking of using the contains and intersects methods of the Shape class.
The thing is, if I want to check if a shape contains any object of any kind (or any object from a specific class), Is there a way to do that? all tutorials i've found explain how to test collision with one single shape, but what if i want to check for any object?
package juegoconslick;
import java.util.ArrayList;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
public class JuegoConSlick extends BasicGame {
Jugador player;
Input entrada;
Shape hitbox;
bloque bloq;
ArrayList<bloque> bloques = new ArrayList<>();
public JuegoConSlick(){
super("Mi prueba");
}
public static void main(String[] args) {
AppGameContainer juegito;
try{
juegito = new AppGameContainer(new JuegoConSlick());
juegito.setDisplayMode(630,400,false);
juegito.setMaximumLogicUpdateInterval(60);
juegito.setMaximumLogicUpdateInterval(50);
juegito.setAlwaysRender(true);
juegito.setVSync(true);
juegito.start();
}catch(Exception ex){
System.exit(0);
}
}
#Override
public void init(GameContainer gc) throws SlickException {
player = new Jugador();
hitbox = new Rectangle(player.X, player.Y, 10, 10);
bloq = new bloque(50,50,50,50);
}
#Override
public void update(GameContainer gc, int i) throws SlickException {
intersecta();
Input entrad = gc.getInput();
if(entrad.isKeyDown(Input.KEY_RIGHT) && player.X<600){
player.X+=3;
player.sprite.update(1);
hitbox.setX(player.X);
}
if(entrad.isKeyDown(Input.KEY_LEFT) && player.X>0){
player.X-=3;
player.sprite.update(1);
hitbox.setX(player.X);
}
if(entrad.isKeyDown(Input.KEY_UP) && player.Y>0){
player.Y-=3;
player.sprite.update(1);
hitbox.setY(player.Y);
}
if(entrad.isKeyDown(Input.KEY_DOWN) && player.Y<370){
player.Y+=3;
player.sprite.update(1);
hitbox.setY(player.Y);
}
}
#Override
public void render(GameContainer gc, Graphics grphcs) throws SlickException {
grphcs.draw(bloq.bloque);
grphcs.draw(hitbox);
}
public void intersecta(){
try{
if(hitbox.contains(null)){//i tried checking if it didnt contain any object,
}else{
System.exit(0);
}
}catch(Exception ex){
}
}
}
EDIT: i think i have found a solution, though im not sure if its the most efficient.
basiaclly, i'll save all objects of the same class in an ArrayList:
ArrayList<bloque> bloques = new ArrayList<>();
bloques.add(new bloque(50,50,100,100));
bloques.add(new bloque(100,100,100,100));
Then, what im going to do is check the entire arraylist each time i call the intersects method:
public boolean intersecta(){
boolean devuelve=false;
for(int i=0; i<bloques.size(); i++){
if(hitbox.intersects(bloques.get(i).bloque)){
devuelve=true;
}
}
return devuelve;
}
then im going to use the value i get from intersects to decide whether the player can move or not
public void update(GameContainer gc, int i) throws SlickException {
Input entrad = gc.getInput();
if(entrad.isKeyDown(Input.KEY_RIGHT) && player.X<600 && intersecta()==false){
player.X+=3;
player.sprite.update(1);
hitbox.setX(player.X);
}
and so on with the other keys....
so im not sure if its the best solution, but as far as i have seen it seems to be working. I hope this results useful for others.
I recommend to create a new Class.
This class Needs to inherit an Slick2d Shape, so for instance:
public class ShapeWithReference extends Rectangle{}
This class can have a list or a single object reference:
public class ShapeWithReference Rectangle{
Object reference = new String("hello"); //This is dirty and can be replaced by any object.
}
Since your new class is a Slick2D shape you can now find out whether it is contained by another shape:
Rectangle rec = new Rectangle();
if(rec.contains(ShapeWithReference)){
if(ShapeWithReference.reference instanceof String){
System.out.println((String)ShapeWithReference.reference);
}
}
Contains checks whether the shape really contains another shape. So they are not just intersecting. So that's the first part you check, the second one is whether the reference Attribute of the Shape is for e.g. a String. So if our first rectangle contains our new class and the reference of this class is type of a String this String is supposed to be written in to the console.
Hope this helps or gives you a hint.
EDIT:
public class SlickGame extends BasicGame {
static AppGameContainer appgc;
Rectangle rec;
ShapeWithReference swr;
public SlickGame(String title) {
super(title);
}
#Override
public void render(GameContainer container, Graphics g) throws SlickException {
}
#Override
public void init(GameContainer container) throws SlickException {
rec = new Rectangle(0, 0, 64, 64);
swr = new ShapeWithReference(0, 0, 1, 1);
}
#Override
public void update(GameContainer container, int delta) throws SlickException {
if(rec.intersects(swr)){
if(swr.reference instanceof String){
System.out.println((String)swr.reference);
}
else if(swr.reference instanceof Integer){
System.out.println((Integer)swr.reference);
}
else if(swr.reference instanceof Shape){
Shape temp = (Shape)swr.reference;
System.out.println(temp.getWidth());
}
}
}
public static void main(String[] args) {
try {
appgc = new AppGameContainer(new SlickGame("title"));
appgc.setDisplayMode(800, 600, false);
appgc.setTargetFrameRate(60);
appgc.setFullscreen(false);
appgc.start();
} catch (SlickException ex) {
ex.printStackTrace();
}
}
}
This is the code you should be able to copy & paste directly.
It does work for me, I'm not sure if I got your problem right but after the instanceof Operator you can just add the Class Name and check for it, I added some examples with an Integer and a Shape.
This is the 2nd class:
public class ShapeWithReference extends Rectangle {
private static final long serialVersionUID = 1L;
public ShapeWithReference(float x, float y, float width, float height) {
super(x, y, width, height);
}
Object reference = new String("hello");
}
Does this work for you?
I am a noob learning basic game programming in Slick 2D Using Eclipse and java
I am following a tutorial at https://www.youtube.com/watch?annotation_id=annotation_871076&feature=iv&src_vid=NoksHLldlcM&v=oWm5JY6IlUo and when i hit run it does not work. I get this error
Exception in thread "main" java.lang.RuntimeException: Resource not found: testdata/alphamap.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.Image.<init>(Image.java:170)
at org.newdawn.slick.Image.<init>(Image.java:158)
at org.newdawn.slick.Image.<init>(Image.java:136)
at org.newdawn.slick.tests.AlphaMapTest.init(AlphaMapTest.java:33)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at org.newdawn.slick.tests.AlphaMapTest.main(AlphaMapTest.java:79)
This is what I am attempting
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class Main extends BasicGame{
private static final String Slick2D = null;
public Main(String title) {
super(title);
// TODO Auto-generated constructor stub
}
//this is where execution starts
public static void man(String args[]) throws SlickException {
AppGameContainer app = new AppGameContainer(new Main("First Slick2D"));
app.setDisplayMode(800, 600, false);
app.start();
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException {
// draw all the graphics
g.fillOval(200, 200, 100, 300)
g.fillRect(300, 200, 100, 200)
g.fillRoundRect(500, 200, 100, 50, 30)
g.drawLine(0, 0, 800, 600)
g.drawString(Welcome to Slick2D, 400, 0)
}
#Override
public void init(GameContainer arg0) throws SlickException {
// load all fonts, graphics, sounds, etc.
}
#Override
public void update(GameContainer arg0, int arg1) throws SlickException {
// game logic (AI, user input)
}
}
it's simply can't find image. check your scr folder for testdata folder and check if it contains alphamap.png (or you can delete some code where you getting and setting this picture)
Slick expects images to be in a folder called images. I'm not sure why, but it does.
Your direcetories should look like this:
\src\Main.java
\images\alphamap.png
If that doesn't work, make sure your directories are properly named and are within the project folder, not the src folder.
Example:
\MyProject\src\Main.java is your source code
\MyProject\images\alphamap.png
Alright the deal is I am trying to use the drawPie method to create my pie chart in an applet. After attempting google searches I find multiple tutorials that explain part of the process but not all of it. While trying to knit together partial information I am not getting the results I want.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JComponent;
public class JLW_PieApplet extends JApplet {
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = new PieValue(35, Color.red);
slices[1] = new PieValue(33, Color.green);
slices[2] = new PieValue(20, Color.pink);
slices[3] = new PieValue(12, Color.blue);
}
public void paint(Graphics g) {
drawPie((Graphics2D)g, getBounds(), slices);
}
}
}
Ther isn't such method in Swing called drawPie. Without the contents of this method, we have no idea of how to help you
Try having a read through 2D Graphics and have a look at Ellipse2D in particular
The other problem I can see is you don't call super.paint(g) in your paint method. This is VERY, VERY, VERY important
You have a PieData component within your applet but you never add it, so you need to add it in init and bring in drawPie from your link above:
public class JLW_PieApplet extends JApplet {
public void init() {
add(new PieData());
}
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = ...
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawPie((Graphics2D) g, getBounds(), slices);
}
public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
...
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.