努力构建libgdx项目

时间:2020-05-31 09:09:09

标签: java libgdx

我知道这个问题可能会被解决,但是我还是要问这个问题。

我一直在尝试使用Libgdx用Java编写草稿游戏;我也一直在使用Stage2D。

我不知道如何构造项目,因为我计划为棋盘,游戏,玩家,棋子和GUI创建单独的类。另外,我已经编写了一些代码来画板,但是,它似乎不适用于Stage2D。

我仍然想自己弄清楚这一点,因为我想从这个项目中学习。因此,请不要放弃太多。

但是,我不知道该如何画板或开始这个项目。

我在网上找到的所有资源和教程都还不清楚。我真的不知道该怎么办。

var data =[
 { 
  name: 'Product 1', 
  type: 'a', 
  updateDetails: { time: '2020-05-28T05:53:31.540Z', userId: 1, officeId: 2 }
 },
 { 
  name: 'Product 2', 
  type: 'a', 
  updateDetails: null
 },
 { 
  name: 'Product 3', 
  type: 'a', 
  updateDetails: { time: '2020-05-27T05:53:31.540Z', userId: 1, officeId: 2 }
 },
 { 
  name: 'Product 4', 
  type: 'a', 
  updateDetails: null
 },
 { 
  name: 'Product 5', 
  type: 'a', 
  updateDetails: { time: '2020-05-20T05:53:31.540Z', userId: 1, officeId: 2 }
 }
]
data.sort((a,b)=>{
  if(a.updateDetails && !b.updateDetails){
    return -1
  }
  return a.name - b.name;

});

console.log(data)
package com.mygdx.game;

public enum Color {
    BLACK, WHITE
}
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class GUI {
    float r_brown = (float) 163 / 255;
    float g_brown = (float) 102 / 255;
    float b_brown = (float) 46 / 255;

    float r_cream = (float) 240 / 255;
    float g_cream = (float) 187 / 255;
    float b_cream = (float) 108 / 255;

    private ShapeRenderer shapeRenderer;
    private OrthographicCamera camera;

    public GUI(ShapeRenderer shapeRenderer, OrthographicCamera camera) {
        this.shapeRenderer = shapeRenderer;
        this.camera = camera;
    }

    public void drawGUI() {
    }
}
package com.mygdx.game;

public enum Type {
    SOLDIER, KING
}
package com.mygdx.game;

public class Player {
    private Color color;

    public Player() {
    }
}
package com.mygdx.game;

public class King extends Piece {

    private Location location;
    private Type type = Type.KING;
    private Player player;

    public King(Player player, Location location) {
        super(player, location);
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
    }

    @Override
    public void move() {
        // TODO Auto-generated method stub
    }

    @Override
    public void capture() {
        // TODO Auto-generated method stub
    }
}
package com.mygdx.game;

public class Location {
    private int x;
    private int y;

    public Location(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
package com.mygdx.game;

import com.badlogic.gdx.scenes.scene2d.Actor;

public abstract class Piece extends Actor {
    private Location location;
    private Type type;
    private Player player;

    public Piece(Player player, Location location) {
        this.player = player;
        this.location = location;
    }

    public abstract void draw();

    public abstract void move();

    public abstract void capture();

    public void setLocation(Location location) {
        this.location = location;
    }

    public Location getLocation() {
        return this.location;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Type getType() {
        return this.type;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public Player getPlayer() {
        return this.player;
    }
}
package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.scenes.scene2d.InputEvent;

public class Soldier extends Actor {
    private Location location;
    private Type type = Type.SOLDIER;
    private Player player;
    Sprite sprite = new Sprite(new Texture(Gdx.files.internal("black_solider.png")));

    public Soldier(Player player, Location location) {
        //super(player, location);
        setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
        setTouchable(Touchable.enabled);

        addListener(new InputListener() {
            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Input.Keys.RIGHT) {
                    MoveByAction mba = new MoveByAction();
                    mba.setAmount(100f, 0f);
                    mba.setDuration(5f);

                    Soldier.this.addAction(mba);
                }
                return true;
            }
        });
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        sprite.draw(batch, parentAlpha);
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    //@Override
    public void move() {
        // TODO Auto-generated method stub
    }

    //@Override
    public void capture() {
        // TODO Auto-generated method stub
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public Location getLocation() {
        return this.location;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Type getType() {
        return this.type;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public Player getPlayer() {
        return this.player;
    }
}

1 个答案:

答案 0 :(得分:1)

一些快速而肤浅的指针:

您似乎面临多个挑战和问题。如果可以的话,一次专注于它们,一一分开并征服它们。

如果您出于学习目的而进行此操作,并且对编程非常陌生,请注意不要将项目范围扩大。为您要在项目中完成的工作设定一个非常适度的目标,一旦完成该目标,就可以添加另一个目标。反复进行。这是非常具有风险承受能力的,因为您从一个相对简单的地方开始,然后从那里开始。这样做的缺点是您可能必须做大量的工作,因为一个合适的早期设计可能不适合以后的工作,并且可能需要重写或重新设计较小或较大的程度。一旦您有了更多的经验,就可以尽早进行规划和寻求设计,这将非常有帮助,尤其是当您知道某些重要方面将在以后变得很重要并且您尽早选择的设计将对这些产生影响时,但是再次,一旦您更有经验。

考虑一下您希望游戏具备的功能,例如AI(状态空间搜索),图形,保存/加载,使用GUI进行移动的玩家等,然后选择一些内容您认为既容易又最愉快。如果您是新手,不要要求它是一个有凝聚力的整体,那么一旦您有了更多的经验,学到了更多的东西,对事物进行了更多思考之后,就可以随时将其作为一个更加雄心勃勃的目标。对您的目标和范围非常谦虚,并获得一些经验并只是学习。