วันอังคารที่ 13 มิถุนายน พ.ศ. 2560

ตามรอย Super Mario Bros ฉบับ แปลมั่ว สำหรับตอนที่ 4(เรื่อง ข้อความ+เพิ่มภาษาไทย)

   การอธิบายหรือแปลอาจจะมั่วไปเยอะแต่ก็เพราะอยากให้เป็นจุดเริ่มต้นของคนที่อยากทำเกมได้มีแนวทางศึกษา
ครั้งแก้ไขไฟล์ใหม่เกือบทั้งหมดน่าจะมาจากการที่ใช้ HUD ช่วยจัดการข้อความ
ก่อนอื่น font ภาษาไทยและอังกฤษเตรียมไว้ก่อนมีขั้นตอนดังนี้
1.ไปที่เว็บ bmfont แล้วโหลดโปรแกรมมาติดตั้งให้เรียบร้อย
2. เปิดโปรแกรมไปที่ option->fontsetting ตรงfontให้เลือกที่เราถูกใจ แล้วกดตกลง
3.เลือก latin กับ thai เพื่อให้ใช้ภาษาไทยได้ด้วย
4.ไปที่ option -> export option แล้วเลือก textures เป็น png

5.ไปที่ option -> save bitmap จะได้ font ใหม่กับไฟล์ 1 ไฟล์ copy ไปใส่ใน folder งานเราที่ c_Super_Mario\core\assets ทั้ง 2 ไฟล์
เริ่มแก้ code และสร้างไฟล์ใหม่ 1 อัน

c_Super_Mari.java
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class c_Super_Mario extends Game {

    public int V_width = 800;//กำหนดความกว้าง
    public int V_height = 480;//กำหนดความสูง
    SpriteBatch batch;
    //Texture img;

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

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

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

PlayScreen.java
package com.mygdx.game;

import com.badlogic.gdx.Game;
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;

/**
 *
 * @author kitty
 */
public class PlayScreen implements Screen {

    c_Super_Mario game;
    Texture texture;
    OrthographicCamera gamecam;
    Viewport gameport;
    Hud hud;

    public PlayScreen(c_Super_Mario game) {
        this.game = game;
        texture = new Texture("badlogic.jpg");
        gamecam = new OrthographicCamera();
        //gameport = new StretchViewport(800,480,gamecam);
        //gameport = new ScreenViewport(gamecam);
        gameport = new FitViewport(game.V_width,game.V_height,gamecam);//set ค่าตามที่ได้กำหนดเอาไว้
        hud = new Hud(game.batch); //ให้ class ทำหน้าที่สร้างภาพด้วย
    }

    @Override
    public void show() {
    }

    @Override
    public void render(float f) {
        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();*/
       game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
       hud.stage.draw();
    }

    @Override
    public void resize(int w, int h) {
           gameport.update(w, h);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void dispose() {
    }

}

Hud.java
package com.mygdx.game;

import com.badlogic.gdx.Gdx;
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;

/**
 *
 * @author kitty
 */
public class Hud {

    Stage stage;
    Viewport viewport;

    int worldTimer; //เก็บค่าต่างๆ
    float timeCount;
    int score;

    Label countdownLabel;
    Label scoreLabel;
    Label timeLabel;
    Label levelLabel;
    Label worldLabel;
    Label marioLabel;
    
    BitmapFont customFont;//เตรียมใช้ font ที่สร้าง

    Hud(SpriteBatch sb) {
        worldTimer = 300;
        timeCount = 0;
        score = 0;
        viewport = new FitViewport(new c_Super_Mario().V_width, new c_Super_Mario().V_height, new OrthographicCamera());
        stage = new Stage(viewport, sb);

        Table table = new Table();
        table.top();
        table.setFillParent(true);
        
        customFont = new BitmapFont(Gdx.files.internal("thait.fnt")); //load font ที่สร้าง

        countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        scoreLabel = new Label(String.format("%06d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        timeLabel = new Label("เวลาtt", new Label.LabelStyle(customFont, Color.WHITE));//ตรงนี้มีภาษาไทยจึงต้องใช้ font ที่สร้าง
        levelLabel = new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        worldLabel = new Label("world", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        marioLabel = new Label("mario", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

        table.add(marioLabel).expandX().padTop(10);
        table.add(worldLabel).expandX().padTop(10);
        table.add(timeLabel).expandX().padTop(10);
        table.row();
        table.add(scoreLabel).expandX();
        table.add(levelLabel).expandX();
        table.add(countdownLabel).expandX();

        stage.addActor(table);
    }
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น

โปรแกรม ปิดโปรแกรมอื่น และดูโปรแกรมที่ทำงาน

โปรแกรมนี้ใช้ภาษา python 2.7 (ไม่เคยเขียนเพราะปกติไม่ชอบภาษา script แต่ต้องใช้งานบางอย่าง) import subprocess import re import os white_l...