Partage
  • Partager sur Facebook
  • Partager sur Twitter

[LibGDX] Afficher une texture créée dans un objet

    28 août 2015 à 1:12:37

    Bonjour !

    J'ai commencé à programmer un Tetris pour essayer de me familiariser un minimum avec la LibGDX en Java. Juste la version desktop. Et je rencontre quelques difficultés (sûrement tout à fait débiles) pour afficher une texture que j'ai instanciée dans l'instance d'un objet Tetromino (et dans le doute, dans les morceaux du Tetromino)

    Je me retrouve avec tout qui se déroule bien sauf la fonction batch.draw() qui n'affiche évidemment rien. Pourtant j'ai fait des logs improvisés qui me permettent de voir que le programme passe bien dans la condition.

    Voici le code :

    Tetris.java :

    package org.orionss.tetris;
    
    import java.util.ArrayList;
    
    import org.orionss.tetris.tetrominos.Block;
    
    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input;
    import com.badlogic.gdx.InputProcessor;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.math.Vector2;
    
    public class Tetris extends ApplicationAdapter implements InputProcessor {
    	SpriteBatch batch;
    	Texture background;
    	Texture blue;
    	Texture red;
    	Texture green;
    	Vector2 spawnPoint;
    	Vector2 gameZoneDim;
    	Tetromino block;
    	public boolean play;
    	public boolean gamebegined;
    	boolean tetrominoInGame;
    	public ArrayList<Tetromino> tetrominos;
    	public Tetromino currentTetromino;
    	
    	@Override
    	public void create () {
    		batch = new SpriteBatch();
    		play = false;
    		gamebegined = false;
    		tetrominoInGame = false;
    		tetrominos = new ArrayList<Tetromino>();
    		background = new Texture("bg.png");
    		gameZoneDim = new Vector2(600, 1200);
    		spawnPoint = new Vector2(1300, 100);
    		
    		Gdx.input.setInputProcessor(this);
    	}
    
    	@Override
    	public void render () {
    		Gdx.gl.glClearColor(1, 0, 0, 1);
    		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    		batch.begin();
    		if(play)
    		{
    			if(!gamebegined && !tetrominoInGame)
    			{
    				currentTetromino = new Block(spawnPoint);
    				tetrominos.add(currentTetromino);
    				gamebegined = true;
    				tetrominoInGame = true;
    			}
    			renderTetromino();
    			System.out.println("Je passe dans la condition");
    		}
    		batch.draw(background, 0, 0);
    		batch.end();
    	}
    	
    	private void renderTetromino()
    	{
    		for(int i = 0; i < tetrominos.size(); i++)
    		{
    			System.out.println(tetrominos.size());
    			for(int j = 0; j < tetrominos.get(i).parts.size(); j++)
    			{
    				batch.draw(tetrominos.get(i).parts.get(j).getTexture(), tetrominos.get(i).getPos().x + (tetrominos.get(i).parts.get(j).getPos().x * 50), tetrominos.get(i).getPos().y + (tetrominos.get(i).parts.get(j).getPos().y * 50));
    			}
    		}
    	}
    
    	@Override
    	public boolean keyDown(int keycode) {
    		if(keycode == Input.Keys.ENTER)
       		{
       			this.play = true;
       			System.out.print("game is true");
       		}
       		else if(keycode == Input.Keys.DOWN)
       		{
       			this.currentTetromino.setPos(new Vector2(currentTetromino.getPos().x, currentTetromino.getPos().y + 50));
       		}
       		else if(keycode == Input.Keys.UP)
       		{
       			this.currentTetromino.setPos(new Vector2(currentTetromino.getPos().x, currentTetromino.getPos().y - 50));
       		}
       		else if(keycode == Input.Keys.LEFT)
       		{
       			this.currentTetromino.setPos(new Vector2(currentTetromino.getPos().x - 50, currentTetromino.getPos().y));
       		}
       		else if(keycode == Input.Keys.RIGHT)
       		{
       			this.currentTetromino.setPos(new Vector2(currentTetromino.getPos().x + 50, currentTetromino.getPos().y));
       		}
          return true;
    	}
    
    	@Override
    	public boolean keyUp(int keycode) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean keyTyped(char character) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean touchDragged(int screenX, int screenY, int pointer) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean mouseMoved(int screenX, int screenY) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean scrolled(int amount) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    }
    

    Tetromino.java :

    package org.orionss.tetris;
    
    import java.util.ArrayList;
    
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.math.Vector2;
    
    public class Tetromino {
    	public ArrayList<Part> parts = new ArrayList<Part>();
    	private Vector2 pos;
    	private Texture texture;
    	
    	public Tetromino(Vector2 pos)
    	{
    		this.pos = pos;
    	}
    	
    	public void addPart(Part part)
    	{
    		parts.add(part);
    	}
    	
    	public void setPos(Vector2 pos)
    	{
    		this.pos = pos;
    	}
    	public Vector2 getPos()
    	{
    		return this.pos;
    	}
    	public Texture getTexture()
    	{
    		return this.texture;
    	}
    	public Texture setTexture(Texture text)
    	{
    		return this.texture = text;
    	}
    	
    }
    

    Et Part.java

    package org.orionss.tetris;
    
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.math.Vector2;
    
    public class Part {
    	private Vector2 pos;
    	private Texture text;
    	
    	public Part(Vector2 position, Texture texture)
    	{
    		this.pos = position;
    		this.text = texture;
    	}
    	
    	public Vector2 getPos()
    	{
    		return this.pos;
    	}
    	
    	public Texture getTexture()
    	{
    		return this.text;
    	}
    }
    

     Block.java :

    package org.orionss.tetris.tetrominos;
    
    import org.orionss.tetris.Part;
    import org.orionss.tetris.Tetromino;
    
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.math.Vector2;
    
    public class Block extends Tetromino{
    	private Vector2 pos;
    	public Block(Vector2 pos) {
    		super(pos);
    		this.setTexture(new Texture("blue.png"));
    		this.pos = pos;
    		this.addPart(new Part(new Vector2(0,0), this.getTexture()));
    		this.addPart(new Part(new Vector2(1,0), this.getTexture()));
    		this.addPart(new Part(new Vector2(0,1), this.getTexture()));
    		this.addPart(new Part(new Vector2(1,1), this.getTexture()));
    	}
    
    }
    



    Pourriez-vous m'aider s'il vous plaît ?



    -
    Edité par Orionss 28 août 2015 à 11:45:45

    • Partager sur Facebook
    • Partager sur Twitter
    Développeur indépendant à la poursuite de connaissances.
      30 août 2015 à 21:28:04

      Salut, si tu n'as aucune erreur vérifie la position à laquelle tu dessines tes textures. Tes blocs sont peut être dessinés mais en dehors de l'écran ce qui fait que tu ne les vois pas. Cordialement,
      • Partager sur Facebook
      • Partager sur Twitter

      [LibGDX] Afficher une texture créée dans un objet

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown