Tuesday, June 14, 2016

Nibbler game tutorial - Java, libgdx, Universal Tween Engine Part 2

Welcome to part 2 of this tutorial. By the end of today we will have the basic game loop set up. First, double click on "DesktopLauncher.java" and replace the code with this:

package com.mygdx.nibbler.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.nibbler.nibblergame;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "Nibbler";
        config.width = 480;
        config.height = 640;
        new LwjglApplication(new nibblergame(), config);
    }


All this is doing is changing the size of the window when we run the game on Windows. We want the window to be more of a "portrait" than a "landscape."

Now go back to the "core" directory and open "nibblergame.java" Replace all of the code with:

package com.mygdx.nibbler;
package com.mygdx.nibbler;


import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;

public class nibblergame extends Game {

    @Override
    public void create() {
        System.out.println("Game Created");
       
    }
   
}


This creates the game and now we will create a screen for it. Right click on "Nibbler-core" and select New -> Package. Name it com.mygdx.Screen

Add the following code:

package com.mygdx.Screen;
public class GameScreen implements Screen {


Eclipse will show an error and it will say to import and then to add unimplemented functions. Do both. The resulting code should be:

package com.mygdx.Screen;

import com.badlogic.gdx.Screen;

public class GameScreen implements Screen {

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

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
       
    }

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

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

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

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

}

 

Now return back to "nibblergame.java" and add this line:



setScreen(new GameScreen());

Remember to import "GameScreen" and you are done!

No comments:

Post a Comment