【发布时间】:2020-05-31 09:09:09
【问题描述】:
我知道这个问题可能会被删除,但我还是要问它。
我一直在尝试使用 Libgdx 用 Java 制作草稿游戏;我也一直在使用 Stage2D。
我不知道如何构建项目,因为我计划为棋盘、游戏、玩家、棋子和 GUI 设置单独的类。另外,我编写了一些代码来绘制板,但是它似乎不适用于 Stage2D。
我仍然想自己解决这个问题,因为我想从这个项目中学习。所以,请不要付出太多。
但是,我不知道如何画板或开始这个项目。
我在网上找到的所有资源和教程都非常不清楚。我真的不知道该怎么办。
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;
}
}
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;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MyGdxGame extends ApplicationAdapter {
Stage stage;
private GUI gui;
private ShapeRenderer shapeRenderer;
private OrthographicCamera camera;
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;
@Override
public void create() {
ScreenViewport viewport = new ScreenViewport();
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);
shapeRenderer = new ShapeRenderer();
camera = new OrthographicCamera();
gui = new GUI(shapeRenderer, camera);
Soldier piece1 = new Soldier(new Player(), new Location(400, 400));
stage.addActor(piece1);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i % 2) == 0) {
if ((j % 2) == 0) {
shapeRenderer.setColor(r_brown, g_brown, b_brown, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
} else {
shapeRenderer.setColor(r_cream, g_cream, b_cream, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
}
} else {
if ((j % 2) == 0) {
shapeRenderer.setColor(r_cream, g_cream, b_cream, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
} else {
shapeRenderer.setColor(r_brown, g_brown, b_brown, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
}
}
}
}
shapeRenderer.end();
camera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
/*
Piece[][] pieces = new Piece[8][8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Piece currentPiece = pieces[i][j];
currentPiece.draw();
}
}
*/
}
@Override
public void dispose() {
shapeRenderer.dispose();
}
}
【问题讨论】:
-
您还可以启动并运行最基本的教程,并让它在屏幕上绘制一些东西。然后对其进行微小的更改,如果出现问题,您可以随时恢复。您可能还会发现学习和使用 'git' 很有帮助 - 如果您学习和使用 git,您可以更轻松地恢复到之前工作的代码版本,特别是如果您只在代码工作时提交。