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

Simple LibGDX Pong game

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

Problem

I created my first java game in LibGDX and it's working fine but I'm 100% sure a lot of my code can be written shorter than now. Does anyone have tips how I can make this code better?

Like the Gdx.input.getX(), if I run this on Android, it's fine, but when I run this on PC, you just have to hover over your screen to change the players position without clicking. On Android you have to tap first.

Does anyone have any tips how to do this better and make the game playable on pc? So you have to click to move the bar.

Here is a screenshot of my game right now.

Here are my 4 classes with the code I wrote:

MainScreen.java

```
package ..***;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import ..***.Player;
import ..***.Enemy;
import ..***.Ball;

public class MainScreen implements Screen {

//GAMESTATE = 0 ---- MENU
//GAMESTATE = 1 ---- INIT/RESET
//GAMESTATE = 2 ---- START
//GAMESTATE = 3 ---- UPDATE
//GAMESTATE = 4 ---- GAMEOVER
//GAMESTATE = 5 ---- PAUSE
//GAMESTATE = 6 ---- EXIT
//GAMESTATE = 100 ---- WIN

int GAMESTATE = 0;

int timer = 30;
int countdown = 90;

int score=0,lives=5;

private Player player;
private Enemy enemy;
private Ball ball;
private BitmapFont font;
private SpriteBatch batch;
Texture BackGround;
Texture StartScreen;

public static float difficulty;

float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
float screenwidth = width/270;
float screenheight = height/480;

public MainScreen() {
Gdx.app.log("GameScreen", "Attached");
player = new Player();
enemy = new Enemy();
ball = new Ball();

batch = new SpriteBatch();

//Load in Fonts
font = new Bitma

Solution

Scope

You should reduce the scope of variables to the minimum needed. So, if possible make them private.

Something like

public float x,y;

public float getX() {
    return x;
}

public float getY() {
    return y;
}


should be avoided because it removes encapsulation. Sometimes it is ok to expose a variable to the public, but you should usually hide them behind methods. Mixing both is a code smell.

Declaring and initializing of multiple variables should be avoided because readability matters. So instead of

int score=0,lives=5;


it should be

private int score = 0;
private int lives = 5;


By extracting the rendering of the different states to separate methods, your render() method will be easier to maintain and read.

Your variables would be happy to get some space to breathe. So instead of

font.setScale(0.5f*screenwidth,0.5f*screenheight);


it should look like

font.setScale(0.5f * screenwidth, 0.5f * screenheight);


Based on the IDE you are using, there is a keyboard shortcut to format the code by using proper indention and spacing. This will add readability to your code.

Using braces {} for single if statements will make your code less errorprone and is more structured so content which belongs together comes into the focus and can be grapsed at first glance.

If you decide not to use braces you should stick to the choosen style. Right now you are mixing the usage.

Shortening of method names or variable names should be avoided for readability. If you have a method SetDif() it could mean for setting the Difference or Difficulty.

Some construct like

//Player Ball Colission
    if(ball.getX()+8*screenwidth>player.getX()-40*screenwidth&&ball.getX()+8*screenwidthplayer.getY()&&ball.getY()<player.getY()+16*screenheight){
            float zy;
            zy = 3*screenheight;
            ball.setZy(zy);
            difficulty+=0.1;

        }
    }


should be extracted to a separate method like hasBallCollision(Player player) which should return true if both if conditions evaluate to true. This can the be called for the enemy too which will make your code DRY (don't repeat yourself) by removing code duplication.

This would lead to something like

if (hasBallCollision(player)){
    float zy;
    zy = 3 * screenheight;
    ball.setZy(zy);
    difficulty += 0.1;
}


which could be reduced to

if (hasBallCollision(player)){
    ball.setZy(3 * screenheight);
    difficulty += 0.1;
}


You have a lot of magic numbers in your code. You should try to extract them into well named constants.

For the case that the game is won or lost, you should return early from the render() method.

Update

Unfortunately you can't use the hasBallCollision() method with the enemy and the player.

If you don't want to go with Simon André Forsberg suggestion using one class for both, which by the way is a pretty good suggestion, you could add an overloaded method which takes an Enemy as an input parameter.

You should at least simplify the if conditions. If we look closer at the first condition we see that you are adding 8 screenwidth to the left side and substracting 40 screenwidth from the right side

if(ball.getX() + 8 * screenwidth > player.getX() - 40 * screenwidth   
    && ball.getX() + 8 * screenwidth < player.getX() + 40 * screenwidth)


this can be simplified to

if(ball.getX() > player.getX() - 32 * screenwidth   
     && ball.getX() < player.getX() + 32 * screenwidth)


or more readable

int offset = 32 * screenwidth;  

if(ball.getX() > player.getX() - offset   
     && ball.getX() < player.getX() + offset)


For the code you had used for the enemy ball collision the second condition of the second if statement had been

&& ball.getY() + 16 * screenheight < enemy.getY() + 16 * screenheight)


this should be simplified to

&& ball.getY() < enemy.getY()

Code Snippets

public float x,y;

public float getX() {
    return x;
}

public float getY() {
    return y;
}
int score=0,lives=5;
private int score = 0;
private int lives = 5;
font.setScale(0.5f*screenwidth,0.5f*screenheight);
font.setScale(0.5f * screenwidth, 0.5f * screenheight);

Context

StackExchange Code Review Q#78729, answer score: 11

Revisions (0)

No revisions yet.