So i’ve made the game this far. i’ve tried to follow the MVC.
GUI class(makes only the interface)
public class GUI extends JFrame{
Controller controller;
JPanel mainPanel;
public GUI(){
init();
}
public void init(){
this.setTitle("Pong"); //Setting title
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //Close operation
mainPanel = new JPanel(); //Creating main panel
this.setFocusable(true);
this.setContentPane(mainPanel);
this.setSize(300,300); //Assigning size
this.setVisible(true); //Frame is visible
//Movement
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
controller.movePlayer(e.getKeyChar());
}
public void keyReleased(KeyEvent e){
controller.movePlayer(e.getKeyChar());
}
});
}
public void registerController(Controller controller){ //Registers controller
this.controller = controller;
}
}
Controller class(this class is the “logic”)
public class Controller {
GUI gui;
Player player;
Ball ball;
public Controller(Player player, Ball ball, GUI gui){
this.gui = gui;
this.player = player;
this.ball = ball;
}
public void movePlayer(char key){
double x = player.getX();
double velX;
if(key == 'a'){
player.setVelX(2);
velX = player.getVelX();
player.setX(x += velX);
}else if(key == 'd'){
player.setVelX(-2);
velX = player.getVelX();
player.setX(x += velX);
}
System.out.println(player.getX());
}
public void moveBall(double delta){
double x = ball.getX();
double y = ball.getY();
double velX;
double velY;
ball.setVelX(1 + Math.random() * 3);
ball.setVelY(1 + Math.random() * 3);
velX = ball.getVelX();
velY = ball.getVelY();
x += velX * delta;
y += velY * delta;
System.out.println(ball.getX()+","+ ball.getY());
}
}
Ball class
public class Ball{
private double x;
private double y;
private double velX;
private double velY;
private boolean running = true;
public Ball(){ //Constructor
this.setVelX(0.05);
this.setVelY(0.05);
this.setX(0);
this.setY(0);
}
//Setters and Getters
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
public void setVelX(double velX){
this.velX = velX;
}
public void setVelY(double velY){
this.velY = velY;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public double getVelX(){
return this.velX;
}
public double getVelY(){
return this.velY;
}
}
Player class
public class Player {
private double x;
private double y;
private double velX;
public Player(){
this.setX(0);
this.setY(1);
}
//Setters and getters
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
public double getX(){
return this.x;
}
public void setVelX(double velX){
this.velX = velX;
}
public double getVelX(){
return this.velX;
}
}
and Finally Main class(the gameloop is just a test, not sure if it even works :D)
public static void main(String[] args){
Player player = new Player();
Ball ball = new Ball();
GUI gui = new GUI();
Controller controller = new Controller(player, ball, gui);
boolean gameRunning = true;
gui.registerController(controller);
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while (gameRunning){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
controller.movePlayer('a');
updates++;
delta--;
}
//render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println(updates + " Ticks, FPS " + frames);
updates = 0;
frames = 0;
}
}
}
}
So is my gameloop now in the right place
and is there anything else that i should consider/change. I know how the gameloop works, the problem is that i dont know how to use it in my code 