Search code examples
javaclassopengllwjgl2d-games

Class organization issue when it comes to creating a player pong block


I have a slight predicament with some class organization (something that I've always been bad at). I have a pong game that I'm building in Java using the LWJGL library. I have three classes as of now. I have a Startup class, a class that everything starts in (this displays the window), an InputHandler (class that handles input from the user), and a Player class. I want the Player move methods, moveUp() and moveDown() to go into the player class. Those methods are handled by the InputHandler class which takes in Up arrow and down arrow input and moves the player block based on input. One problem. Where do I write in the player block? Would I write it in startup, so it can display it in the screen? Would I write it into the Player class? Or would I even write it into the InputHandler class so that it can be controlled through Keyboard movement? I'll post all three classes below. They're all pretty short. Please feel free to point out any other flaws that I may not have picked up on. I'm fairly new to Java and incredibly new to LWJGL.

Startup class:

public class Startup {

    // set up display
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(600, 400)); // these numbers pending
            Display.setTitle("Evan's Pong!");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        while (!Display.isCloseRequested()) {
            // render OpenGL here
            handler.handleInput();  
            Display.update();
            Display.sync(60);
        }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 600, 4, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);


        Display.destroy();
    }


    // Let's start this beyotch up!
    InputHandler handler = new InputHandler();

    public static void main(String[] args) {
        new Startup().start();
    }
}

InputHandler:

public class InputHandler {

    /*
     * create a new player object. This is for the player's paddle that will
     * move up and down to block the ball
     */
    Player player = new Player();

    // testing input for now!
    public void handleInput() {
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
            // System.out.println("going up.");
            player.moveUp();
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
            // System.out.println("going down");
            player.moveDown();
        }
    }
}

Player class:

public class Player {

    private int moveSpeed = 10; // mph

    public void moveUp() {
        // move up
    }

    public void moveDown() {
        // move down
    }
}

Solution

  • There is no one right answer to this question, but what I would do is this:

    1. Lose the InputHandler.
    2. Make a method in player that is called every frame that checks for input and changes an internal position variable.
    public class Player {
        private int moveSpeed = 10; // mph
        private int position = 128; // Or whatever would be centered.
    
        public void update() {
            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                position += moveSpeed;
            } else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                position -= moveSpeed;
            }
        }
    }
    

    On an unrelated note, you need to move your OpenGl setup code (GL11.glMatrixMode(GL11.GL_PROJECTION); etc.) up from below your main loop. That stuff needs to be called before any other rendering is done.