HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

WIP platformer game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wipgameplatformer

Problem

Initial note: You do not need to have knowledge of LWJGL or OpenGL to review this code. The only parts of the code that require LWJGL knowledge are the Game class, and the rest, although it may contain some LWJGL/OpenGL/Slick-util code, it is rather self explanatory for anybody who's worked in Java before.

So I've built this simple game in Java using LWJGL. It's a relatively simple platformer, and I would like some opinions on my code so that I can correct it. Feel free to leave any constructive criticism or thoughts that you may have. Any and all opinions are appreciated. This is my first game, so it's bound to be a bit rough. This is also still early in development. I'll post all the classes that I've written code for so far.

Game class:

```
import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;

import com.hasherr.platformer.entity.Player;

public class Game { // starts game & manages display/init OpenGL

public Game() {
initDisplay(800, 500, "Komo"); // Temporary game name - KOMO
}

// initiate necessary classes
Player player = new Player(); // user-representation
InputHandler inputHandler = new InputHandler(player);
SpriteHandler spriteHandler = new SpriteHandler();
TextureHandler textureHandler = new TextureHandler();

private void initDisplay(int width, int height, String title) {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(title);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
grabAllTextures();
while (!Display.isCloseRequested()) {
Display.update();
Display.sync(60); // 60 FPS
inputHandler.handleInput();
initGL(width, height);
spriteHandler.drawSprite(play

Solution

General:

Dont use } else { or } catch(...) { because it breaks the brace rule and it's not that readable.

Do code a Vector class because it groups your coordinates together so the code gets more easily readable and managable.

SpriteHandler Class:

glBegin(GL_QUADS);
    glTexCoord2f(0, 1);
    glVertex2f(x, y);


Don't format the code this way because you don't open a new brace level.

TextureHandler Class:

} catch (IOException e) {
        e.printStackTrace();
    }


don't eat important error/fault information, propagate it further downwards with a exception or a null result which is checked.

Don't call the class Handler because a Handler handles player input or a fault.

Player Class:

if (moveSpeed  -MAX_MOVE_SPEED) {
        moveSpeed += force;
        xVelocity = scaleX * moveSpeed;
    } else if (moveSpeed >= MAX_MOVE_SPEED) {
        moveSpeed = MAX_MOVE_SPEED;
        xVelocity = scaleX * moveSpeed;
    } else if (moveSpeed <= -MAX_MOVE_SPEED) {
        moveSpeed = -MAX_MOVE_SPEED;
        xVelocity = scaleX * moveSpeed;
    }


one word: redudandency

You should try to avoid it as best as possible, and on top of that there is no else part, so a better code is

if (abs(moveSpeed) < MAX_MOVE_SPEED) {
        moveSpeed += force;
    }
    else {
        moveSpeed = clamp(moveSpeed, -MAX_MOVE_SPEED, MAX_MOVE_SPEED);
    }

    xVelocity = scaleX * moveSpeed;

Code Snippets

glBegin(GL_QUADS);
    glTexCoord2f(0, 1);
    glVertex2f(x, y);
} catch (IOException e) {
        e.printStackTrace();
    }
if (moveSpeed < MAX_MOVE_SPEED && moveSpeed > -MAX_MOVE_SPEED) {
        moveSpeed += force;
        xVelocity = scaleX * moveSpeed;
    } else if (moveSpeed >= MAX_MOVE_SPEED) {
        moveSpeed = MAX_MOVE_SPEED;
        xVelocity = scaleX * moveSpeed;
    } else if (moveSpeed <= -MAX_MOVE_SPEED) {
        moveSpeed = -MAX_MOVE_SPEED;
        xVelocity = scaleX * moveSpeed;
    }
if (abs(moveSpeed) < MAX_MOVE_SPEED) {
        moveSpeed += force;
    }
    else {
        moveSpeed = clamp(moveSpeed, -MAX_MOVE_SPEED, MAX_MOVE_SPEED);
    }

    xVelocity = scaleX * moveSpeed;

Context

StackExchange Code Review Q#29073, answer score: 3

Revisions (0)

No revisions yet.