Hey guys. I can’t express my appreciation for your help enough. I’m working on my first ever Java game. I’m using libgdx. I’ve got the paddle and ball set up and moving around. I know i’m doing some of this stuff incorrectly (ie: using rect.collide to check on collision of ball and paddle) but I plan on redoing all of it. Just getting my foot in the door and seeing something working right now.
I want to encapsulate the ball and the paddle classes with different java class files. I’m a little confused on how to do this with libgdx. I’ve found some tutorials, but haven’t been able to discern this.
What do I need to do to set the class up? i’m not sure which classes I need to import or where to do the actual drawing of the paddle and ball. Do I need a create(), render(), etc. in the Ball class? I’m confused basically, on how to separate the code from the main Game class. I’ve attached my code so far, below. It’s a bit messy, I apologize for that. That is the reason for me asking this question, so I can clean it up a bit.
package com.psillicoder.brickbreaker;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
//import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
public class Game implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture ballImage;
private Texture paddleImage;
private int ballSpeedX;
private int ballSpeedY;
private int paddleSpeed;
//private TextureRegion region;
//rectangles
Rectangle ballRect;
private Rectangle paddleRect;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
ballSpeedX = 1;
ballSpeedY = 1;
paddleSpeed = 2;
paddleImage = new Texture(Gdx.files.internal("data/paddle.PNG"));
ballImage = new Texture(Gdx.files.internal("data/ball.png"));
//region = new TextureRegion(paddleImage,0,0,64,16);
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
//paddle
paddleRect = new Rectangle();
paddleRect.x = 400/2 - 64/2;
paddleRect.y = 20;
paddleRect.width = 64;
paddleRect.height = 16;
ballRect = new Rectangle();
ballRect.x = 400/2;
ballRect.y = 600/2;
ballRect.width = 16;
ballRect.height = 16;
}
@Override
public void dispose() {
batch.dispose();
//ballImage.dispose();
paddleImage.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//camera.update();
CheckKeys();
moveBall();
//batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(ballImage,ballRect.x,ballRect.y);
batch.draw(paddleImage, paddleRect.x, paddleRect.y);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
public void moveBall() {
ballRect.x= ballRect.x + ballSpeedX;
ballRect.y= ballRect.y + ballSpeedY;
if (ballRect.x >= 400-16){ballSpeedX=-ballSpeedX;}
if (ballRect.x <= 0){ballSpeedX=-ballSpeedX;}
if (ballRect.y >= 600-16){ballSpeedY=-ballSpeedY;}
if (ballRect.y <= 0){ballSpeedY=-ballSpeedY;}
if (ballRect.overlaps(paddleRect)){ballSpeedY = -ballSpeedY;}
ballRect.x= ballRect.x + ballSpeedX;
ballRect.y= ballRect.y + ballSpeedY;
}
public void CheckKeys(){
boolean isLeftPressed;
boolean isRightPressed;
if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT))
{
isLeftPressed=true;
}else{isLeftPressed=false;}
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT))
{
isRightPressed=true;
}else{isRightPressed=false;}
if (isLeftPressed==true && paddleRect.x >=0){paddleRect.x=paddleRect.x-paddleSpeed;}
if (isRightPressed==true && paddleRect.x <= 400 - 64){paddleRect.x=paddleRect.x+paddleSpeed;}
System.out.println(isLeftPressed);
System.out.println("Paddle X: " + paddleRect.x + " Paddle Y: " + paddleRect.y );
}
}
Thanks! You guys are awesome!
Hopefully I won’t have such elementary problems in the future.