Note that there are some explanatory texts on larger screens.

plurals
  1. POClass organization issue when it comes to creating a player pong block
    text
    copied!<p>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, <code>moveUp()</code> and <code>moveDown()</code> to go into the player class. Those methods are handled by the <code>InputHandler</code> 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. </p> <p>Startup class: </p> <pre><code>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(); } } </code></pre> <p>InputHandler:</p> <pre><code>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(); } } } </code></pre> <p>Player class: </p> <pre><code>public class Player { private int moveSpeed = 10; // mph public void moveUp() { // move up } public void moveDown() { // move down } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload