libgdx

And_Rocoman

tmxhsk99 2018. 11. 28. 18:54
package com.rocoman.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.rocoman.game.Screens.PlayScreen;

public class Rocoman extends Game {
public SpriteBatch batch;

@Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}

@Override
public void render () {
super.render();
}

@Override
public void dispose () {
batch.dispose();

}
}

Rocoman.java


처음 있던 상속구조를 Game로 만든다 

SpriteBatch는 모든 이미지를 가져온다 

Screen 은 화면 인데 처음 화면을 만든다 


rander는 화면을 새로 갱신? 그리는거??


dispose는 일이 끝나면 전부 삭제함 


Screen->PlayScreen.java

package com.rocoman.game.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.rocoman.game.Rocoman;

public class PlayScreen implements Screen {
private Rocoman game;
Texture texture;

public PlayScreen(Rocoman game){
this.game = game;
texture = new Texture("badlogic.jpg");
}

@Override
public void show() {

}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(texture,0,0);
game.batch.end();
}

texture는 이미지 

PlayScreen의 생성자는 위의 기본 반복구조가 들어가는것 같다 

game.batch.begin();--> 시작할 때는 항상 넣는 것같음

game.batch.draw(texture,0,0)-> 그리는 것 (이미지, x,y)

game.batch.end(); 끝내는것 

-----------------------------------------------------------

Aspect Ratios & Viewports



package com.rocoman.game.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.rocoman.game.Rocoman;

public class PlayScreen implements Screen {
private Rocoman game;
Texture texture;
private Viewport gamePort;
private OrthographicCamera gamecam;
public PlayScreen(Rocoman game){
this.game = game;
texture = new Texture("badlogic.jpg");
gamecam = new OrthographicCamera();
gamePort = new FitViewport(800,480,gamecam);
}

@Override
public void show() {

}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
game.batch.draw(texture,0,0);
game.batch.end();
}

@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

이렇게 바꾸면 어떤화면크기에든 맞게 다 이미지가 조정된다

 

Creating a HUD

package com.rocoman.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.rocoman.game.Screens.PlayScreen;

public class Rocoman extends Game {
public static final int V_WIDTH = 400;
public static final int V_HEITGHT = 208;
public SpriteBatch batch;

@Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}

@Override
public void render () {
super.render();
}

@Override
public void dispose () {
batch.dispose();

}
}
가로 세로 화면크기를 static final로 지정 
package com.rocoman.game.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.rocoman.game.Rocoman;
import com.rocoman.game.Scenes.Hud;

public class PlayScreen implements Screen {
private Rocoman game;
private Viewport gamePort;
private Hud hud;
private OrthographicCamera gamecam;
public PlayScreen(Rocoman game){
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(Rocoman.V_WIDTH,Rocoman.V_HEITGHT,gamecam);
hud = new Hud(game.batch);
}

@Override
public void show() {

}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}

@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

PlayScreen도 카메라 값을 아까 지정한 값으로 변경 


화면에 띄우는 HUD

HUD란?

게임 플레이 시에 현재 플레이어 본인이나 게임 상황에 대한 정보를 알리는 데 사용된다. HUD에는 플레이어의 현재 체력, 사용 중인 무기나 아이템, 다른 플레이어나 아이템 등의 위치를 나타내는 미니맵, 아군과 적군의 점수 등과 같은 정보들이 포함될 수 있고 매우 다양하다. 이러한 정보는 월드 위에다 그래픽과 텍스트를 그려 전달해 줄 수도 있고 RPG의 경우 UI 수준으로 복잡하게 그려내기도 한다.


Scenes 패키지에 

HUD 클래스를 만듬

package com.rocoman.game.Scenes;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.rocoman.game.Rocoman;

public class Hud {
public Stage stage;
private Viewport viewport;

private Integer worldTimer;
private float timeCount;
private Integer score;

Label countdownLabel;
Label scoreLabel;
Label timeLabel;
Label levelLabel;
Label worldLabel;
Label rocomanLabel;

public Hud (SpriteBatch sb){
worldTimer =300;
timeCount=0;
score =0;

viewport = new FitViewport(Rocoman.V_WIDTH,Rocoman.V_HEITGHT,new OrthographicCamera());
stage = new Stage(viewport,sb);

Table table = new Table();
table.top();
table.setFillParent(true);

countdownLabel= new Label(String.format("%03d",worldTimer),new Label.LabelStyle(new BitmapFont(), Color.WHITE));
scoreLabel= new Label(String.format("%06d",score),new Label.LabelStyle(new BitmapFont(), Color.WHITE));
timeLabel= new Label("TIME",new Label.LabelStyle(new BitmapFont(), Color.WHITE));
levelLabel= new Label("1-1",new Label.LabelStyle(new BitmapFont(), Color.WHITE));
worldLabel= new Label("WORLD",new Label.LabelStyle(new BitmapFont(), Color.WHITE));
rocomanLabel= new Label("ROCOMAN",new Label.LabelStyle(new BitmapFont(), Color.WHITE));

table.add(rocomanLabel).expandX().padTop(1);
table.add(worldLabel).expandX().padTop(1);
table.add(timeLabel).expandX().padTop(1);
table.row();
table.add(scoreLabel).expandX();
table.add(levelLabel).expandX();
table.add(countdownLabel).expandX();
stage.addActor(table);
}
}

위에 띄울 테이블에 라벨을 집어넣는다 줄바꿈은 row로 함 

한후에 table을 

stage.addActor로 집어넣음 

stage 생성시에는 viewport와 spritebach가 필요함 (카메라와 이미지 )


그럼 화면이 이렇게 나타난다 . 


https://github.com/libgdx/libgdx/wiki