Sign in to follow this  
louif34

Code Review Pong

Recommended Posts

louif34    111

Hi, I've made a pong and I would like some reviews. Thanks for your time and help gentleman.
 
So this is my interface
 
 

import javax.swing.*;

 

import java.awt.*;

import java.awt.event.*;

 

 

public class Interface extends JFrame{

 

private static final long serialVersionUID = 1L;

private GamePanel panel;

 

public Interface(){
 
  setTitle("Pong");
  setResizable(false);

  setMaximumSize(new Dimension(400,200));
  setMinimumSize(new Dimension(400,200));
  setBounds(400, 200, 400, 200);

  setLayout(new BorderLayout());
  add(getPanel(),BorderLayout.CENTER);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent evt){
      move(evt);
    }

    public void keyReleased(KeyEvent evt){
      stopMove(evt);
    }
  });
}

private GamePanel getPanel() {

  if(panel == null)
    panel = new GamePanel();
  return panel;
}

private void move(KeyEvent evt) {

  panel.move(evt);
}

private void stopMove(KeyEvent evt) {

  panel.stopMove(evt);
}


public static void main(String[] args) {

// TODO Auto-generated method stub

  Interface i = new Interface();
  i.setVisible(true);

}
}

 

and this is my engine :
 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.KeyEvent;

 

import javax.swing.*;

 

 

public class GamePanel extends JPanel implements Runnable{

 

private static final long serialVersionUID = 1L;

 

private int player1_x = 10;
private int player2_x = 380;
private int player1_y = 80;
private int player2_y = 80;

private int player1_score = 0;
private int player2_score = 0;

private int direction1 = 0;
private int direction2 = 0;

private int ball_x = 150;
private int ball_y = 150;

private boolean ballLeft = true;
private boolean ballUp = true;

private boolean game = true;

private final int UP = -5, DOWN = 5, LEFT = -5, RIGHT = 5;

public GamePanel(){
  Thread th = new Thread(this);
  th.start();
}

public void move(KeyEvent evt) {

  switch(evt.getKeyCode()){

    case KeyEvent.VK_UP :
      if(player2_y > 0)
        direction2 = UP;
      else
        direction2 = 0;
      break;

    case KeyEvent.VK_DOWN :
      if(player2_y < 200 - 50)
        direction2 = DOWN;
      else
        direction2 = 0;
      break;

    case KeyEvent.VK_W :
      if(player1_y > 0)
        direction1 = UP;
      else
        direction1 = 0;
      break;

    case KeyEvent.VK_S :
      if(player1_y < 200 - 50)
        direction1= DOWN;
      else
        direction1 = 0;
      break;
  }

player1_y += direction1;
player2_y += direction2;

}

 

public void stopMove(KeyEvent evt) {

  switch(evt.getKeyCode()){

    case KeyEvent.VK_UP :
      direction1 = 0;
      break;

    case KeyEvent.VK_DOWN :
      direction1 = 0;
      break;

    case KeyEvent.VK_W :
      direction2 = 0;
      break;

    case KeyEvent.VK_S :
      direction2 = 0;
      break;
  }
}

 

private void moveBall(){

  if(ballLeft){
    if(ball_x <= 0){
      player2_score++;
      ballLeft = false;
    }
    else if(ball_x == player1_x && ball_y > player1_y && ball_y < player1_y + 30)
      ballLeft = false;
    else
      ball_x += LEFT;
  }
  else{
    if(ball_x >= 400){
      player1_score++;
      ballLeft = true;
    }
    else if(ball_x == player2_x && ball_y > player2_y && ball_y < player2_y + 30)
      ballLeft = true;
    else
      ball_x += RIGHT;
  }

  if(ballUp){
    if(ball_y <= 0){
      ballUp = false;
    }
    else
      ball_y += UP;
  }
  else{
    if(ball_y >= 200 - 25){
      ballUp = true;
    }
    else
      ball_y += DOWN;
  }
}

public void paintComponent(Graphics g){
  super.paintComponent(g);
  g.setColor(Color.BLACK);

  g.fillRect(player1_x, player1_y, 5, 30);
  g.fillRect(player2_x, player2_y, 5, 30);

  g.fillOval(ball_x, ball_y, 8, 8);

  g.drawString("Player 1 : " + player1_score , 30, 10);
  g.drawString("Player 2 : " + player2_score , 200, 10);

  if(player1_score == 5 || player2_score == 5){
    game = false;
    g.drawString("Game Over", 200, 100);
  }
}

@Override

public void run() {

  while(true){
    if(game){
      moveBall();
      repaint();
      try{
        Thread.sleep(60);
      }catch (Exception e){}
    }
  }
}
} 


 

Thanks again for all the comments

Edited by louif34

Share this post


Link to post
Share on other sites
louif34    111

Wow 3000 lines ?!?! 

 

I must say that I know a little C++, in fact I can use it very well, but I have never try to do an interface like this. And I don't think that the engine must be a lot bigger. But you have the .h that take more place :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this