Slick ausprobiert, Problem

oOJavaNeulingOo

Bekanntes Mitglied
Guten Morgen!

Ich habe mal die Library Slick ausprobiert, da Konsolenzeugs langweilig wurde.
Nun habe ich ein Problem.. Mein Code sollte eigentlich ein Rechteck erstellen - Wird aber nicht!

Spiel_Main:
Java:
package game;

import java.util.ArrayList;

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;

public class Spiel_Main extends BasicGame {

	public static final int
	HEIGHT = 300,
	WIDTH = 400;
	
	public Spiel_Main() {
		super("Spiel");
	}

	public ArrayList<Entity> entitys = new ArrayList<Entity>();
	EntityPlayer player;
	
	@Override
	public void render(GameContainer gc, Graphics arg1) throws SlickException {
		//player.setLocation(new Location(20,20));
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		player = new EntityPlayer(100, 100, 100, 100);
		entitys.add(player);
	}

	@Override
	public void update(GameContainer gc, int arg1) throws SlickException {
		Input input = gc.getInput();
	}
	
	
	public static void main(String[] args) throws SlickException{
		AppGameContainer spiel = new AppGameContainer(new Spiel_Main());
		spiel.setDisplayMode(WIDTH, HEIGHT, false);
		spiel.setVSync(true);
		spiel.setShowFPS(true);
		spiel.start();
	}

}
Location:
Java:
package game;

public class Location {
	
	private double x,y;
	public Location(double x, double y){
		setX(x);setY(y);
	}
	
	public double getX(){
		return this.x;
	}
	public double getY(){
		return this.y;
	}
	public void setX(double i){
		this.x = i;
	}
	public void setY(double i){
		this.y = i;
	}

}
Entity:
Java:
package game;

public class Entity {
	
	private int width, height, speed;
	Location loc;
	
	public Entity(){
		
	}
	
	public int getWidth(){
		return width;
	}
	public int getHeight(){
		return height;
	}
	public int getSpeed(){
		return speed;
	}
	public Location getLocation(){
		return this.loc;
	}
	public void setWidth(int i){
		this.width = i;
	}
	public void setHeight(int i){
		this.height = i;
	}
	public void setSpeed(int i){
		this.speed = i;
	}
	public void setLocation(Location l){
		this.loc = l;
	}

}

EntityPlayer:
Java:
package game;

import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;

public class EntityPlayer extends Entity{
	
	private Rectangle p_o;
	
	public EntityPlayer(int w, int h, int x, int y){
		
		//Variabeln des Konstruktors
		Location l = new Location(x, y);
		
		//Maße, Position und mehr setzen
		setHeight(h);
		setWidth(w);
		
		//Spieler initialisieren
		p_o = new Rectangle(Math.round(l.getX()), Math.round(l.getY()), getWidth(), getHeight());
		System.out.println("Spieler initialisiert..");
		
		setLocation(l);
		
	}
	
	public boolean collide(Shape s){
		return this.p_o.intersects(s);
	}
	
	@Override
	public void setLocation(Location l){
		this.loc = l;
		this.p_o.setLocation(Math.round(l.getX()), Math.round(l.getY()));
	}

}


Was geht da nicht? Und was kann man bereits in diesem Stand der Dinge verbessern (im code)?

Die Installation von Slick habe ich per Tutorial gemacht, aber den Rest dann einfach ohne - Ich habe das Gefühl, dass ich rein gar nichts lerne wenn ich nur auf ein Tutorial schaue.
 
Zuletzt bearbeitet:
Moin,

kannst du mir mal erzählen welche Lib das ist ?
ich benutze : Slick in Scala und habe noch nichts von Slick in Java gehört.

Dein code soll ein reteck erstellen?
Dann nimm doch paintComponents und zeichne es mit dem Graphics Objekt.

Oder willst du einen Spieler haben der rumlaufen kann auf einem Spielfeld?
Um das zu realisieren würde ich dir vorschlagen, ein JLabel zu verwenden und dass immer neu zu positionieren. Das kann ein ImageIcon verwenden und zack fertig, noch bischen KeyListener und paar Frame, Panel Bounds definieren und du hast ein Spieler auf einem Spielfeld!!!

Dein Code ist völlig überladen zu mindest für den Forum Post --- kann nicht genau erkennen, in welchen der Klassen du jetzt was ändern möchtest 😉

grüße spin
 
Vielen Dank für die Antwort!

Ich benutze diese Library hier: Slick2D | Open Source 2D Java Game Library

Das erstellen des Rechteckes habe ich nun hinbekommen. Jetzt habe ich aber gleich 2Probleme:
1:
- Statt um zu "laufen" immer einfach eins zu addieren, wollte ich eine move-Methode erstellen. Leider funktioniert sie nicht richtig...
In der Main, render:
Java:
Input input = gc.getInput();
		if(input.isKeyDown(Input.KEY_DOWN)){
			player.move(Direction.DOWN);
		}
		if(input.isKeyDown(Input.KEY_UP)){
			player.move(Direction.UP);
		}
		if(input.isKeyDown(Input.KEY_LEFT)){
			player.move(Direction.LEFT);
		}
		if(input.isKeyDown(Input.KEY_RIGHT)){
			player.move(Direction.RIGHT);
		}

Dann in der Klasse Entity:
Java:
	public Entity move(Location loc){
		this.setLocation(new Location(loc.getX()*speed, loc.getY()*speed));
		return this;
	}
(temporär - schließlich sollen die bewegungen ja "sanft" betätigt werden

EntityPlayer:
Java:
	@Override
	public Entity setLocation(Location l){
		this.loc = l;
		this.p_o.setLocation(Math.round(l.getX()), Math.round(l.getY()));
		return this;
	}
	@Override
	public Entity move(Location l){
		Location old = getLocation();
		Location loc = new Location(old.getX()*l.getX()*getSpeed(), old.getY()*l.getY()*getSpeed());
		this.setLocation(loc);
		return this;
	}
Leider geht es nicht so ganz. Statt sich zu Bewegen, springt das Viereck einmal irgendwo an die Ecke, und dann geht nichts mehr...

2:
- Wie kann ich dem Viereck 'nen SKin verpassen? Also dass es bei Kollisionserkennung und so weiter ein Viereck bleibt, aber eben anders aussieht..
 
Zuletzt bearbeitet:
Das mit dem Bewegen ist glaube ich halbwegs gelöst!

Wäre nett wenn jemand sagen kann ob dieser Code benutzbar ist oder nicht 🙂:
Java:
public Entity move(Location l){
		for(int i = 0; i < getSpeed(); i++){
			Location loc = getLocation().add(l.getX(), l.getY());
			this.setLocation(loc);
		}
		return this;
	}


Texturen + Shape ist geschafft!
 
Zuletzt bearbeitet:
So wie ich es jetzt richtig verstehe, übernimmt dein Framework schon einiges. Du übergibst also eine Richtung und bewegst dich von der aktuellen Position zur nächsten, in dem du den Abstand von Speed gehst.

Java:
public Entity move(Location l){
        for(int i = 0; i < getSpeed(); i++){
            Location loc = getLocation().add(l.getX(), l.getY());
            this.setLocation(loc);
        }
        return this;
    }

Java:
getLocation().add(l.getX(), l.getY())

Deine .add() funktion addiert also immer +1 ????
 
Das mit dem gehen ist gelöst ^^ Naja, nicht wirklich - Meine add Funktion addiert die angegeben Parameter von der Location, und die Location wird von einer Klasse namens "Direction" übergeben - so ist UP zum beispiel new Location(0, 1); und so weiter.
Java:
public Location add(double x, double y){
		setX(getX() + x);
		setY(getY() + y);
		return this;
	}

Jetzt habe ich aber ein neues Problem. Habe bis jetzt ungefähr alles einigermaßen hingekriegt. Jetzt wollte ich mit kollidierungen anfangen; Also habe ich "Münzen" spawnen lassen, jedoch ufnktioniert mein Code nicht..

Java:
public boolean collide(Shape s){
		return this.p_o.intersects(s);
	}
	public Entity move(Location l, String s){
		setTexture(s);
		for(int i = 0; i < getSpeed(); i++){
			Location locc = getLocation().add(l.getX(), l.getY());
			for(Entity e : Spiel_Main.items){
				if(collide(e.getShape())){
					if(e.isSolid()){
						return this;
					}else{
						checkCollide(e);
					}
				}
			}
			this.setLocation(locc);
		}
		
		return this;
	}
	
	private void checkCollide(Entity e){
		if(e instanceof EntityMuenze){
			e.remove();
			setPoints(getPoints() + 1);
		}
	}
Der Spieler wird einfach nur langsamer, wenn ich durch ne Münze gehe. Und bleibt langsam.
Die Entity-Klasse:
Java:
package game;

import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;

public class Entity {
	
	private String texture = "";
	private Shape s = new Rectangle(1, 1, 80, 80);
	private int width, height, speed;
	Location loc;
	private boolean isBreakable, isSolid;
	
	public Entity(){
		width=0;height=0;speed=1;loc=new Location(0,0);isBreakable=false;isSolid=true;
	}
	
	public int getWidth(){
		return width;
	}
	public int getHeight(){
		return height;
	}
	public int getSpeed(){
		return speed;
	}
	public Location getLocation(){
		return this.loc;
	}
	public Shape getShape(){
		return s;
	}
	public boolean isBreakable(){
		return isBreakable;
	}
	public boolean isSolid(){
		return isSolid;
	}
	public Entity setWidth(int i){
		this.width = i;
		return this;
	}
	public Entity setHeight(int i){
		this.height = i;
		return this;
	}
	public Entity setSpeed(int i){
		this.speed = i;
		return this;
	}
	public Entity setLocation(Location l){
		this.s.setLocation(Math.round(l.getX()), Math.round(l.getY()));
		this.loc = l;
		return this;
	}
	public Entity setBreakable(boolean b){
		this.isBreakable = b;
		return this;
	}
	public Entity setSolid(boolean b){
		this.isSolid = b;
		return this;
	}
	public String getTexture(){
		return texture;
	}
	public Entity setTexture(String s){
		this.texture = s;
		return this;
	}
	

	
	
	public Entity move(Location l){
		for(int i = 0; i < getSpeed(); i++){
			Location locc = getLocation().add(l.getX(), l.getY());
			this.setLocation(locc);
		}
		return this;
	}
	
	
	
	public void remove(){
		Spiel_Main.items.remove(this);
	}

}
 
Okay; Geht auch! Bleiben zwar noch viele Probleme, aber gut.. Vor allem würde ich gerne wissen wie man einen EventHandler erstellen kann, und dass der spieler nicht aus dem rand kann 😀
 

Zurück
Oben