Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pong Game in Java

843789May 24 2010 — edited May 25 2010
Hi, my friend and I are working on our final project for the end of the year in Java. We really haven't worked outside of the topics in regular AP Computer Science A, so we need a little help. We want to create a basic Pong game with basic functionality. This is what we have so far.

Ball Class.*

import java.awt.*;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

public class Ball
{
private int x;
private int y;
private final int height = 20;
private final int width = 20;
private int xdir = 2;
private int ydir = 0;

public Ball (int xVal, int yVal)
{
x = xVal;
y = yVal;
}

public void paintComponent(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(x,y,width,height);
}

public void move()
{
x +=xdir;
y +=ydir;

if(x > 570 ){
setXDir(-8);
setYDir(randomNum());

}
if(x < 0 ){
setXDir(8);
setYDir(randomNum());
}


if(y < 2){
setYDir(8);
setXDir(randomNum());
}

if(y > 520){
setYDir(-8);
setXDir(randomNum());

}
}

public int randomNum()
{
double r = Math.random();
int myNumber = (int)(r*2f);
int num = 0;
if(myNumber==0)num = -8;
if(myNumber==1)num = 8;
return num;
}


public void resetState()
{
x =400;
y = 300;
xdir = 0;
ydir = 0;
}

public void setYDir(int y)
{
ydir = y;
}

public void setXDir(int x)
{
xdir = x;
}

public int getYDir()
{
return ydir;
}

public int getXDir()
{
return xdir;
}
}


GamePaddle class._

import java.awt.*;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

public class GamePaddle
{
private int x;
private int y;
private final int height = 110;
private final int width = 15;

public GamePaddle (int xVal, int yVal)
{
x = xVal;
y = yVal;
}

public void moveRight()
{
if (x > 500)
{
x += 0;
}

else x+=40;
}

public void moveLeft()
{
if (x < 15)
{
x -= 0;
}

else
x-=40;
}

public void stop()
{
x = x;
y = y;
}

public Rectangle getRect()
{
return new Rectangle(x, y,width, height);
}

public void paintComponent(Graphics block)
{
block.setColor(Color.YELLOW);
block.fillRect(x,y,width,height);
}
}

Pong Class*

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

public class Pong extends JFrame
{
private JPanel jContentPane = null;
// This is the panel of the game class
private PongGame panel = null;

private PongGame getPanel()
{
if (panel == null)
{
// This creates a new game panel
panel = new PongGame();
}
return panel;
}

/**
* This is the default constructor
*/

public Pong()
{
initialize();

// Listens for the keyboard buttons and controls
this.addKeyListener(new KeyAdapter()
{

//The button that is pressed
@Override
public void keyPressed(KeyEvent evt)
{
formKeyPressed(evt);
}

// the button that is released
@Override
public void keyReleased(KeyEvent evt)
{
formKeyReleased(evt);
}
});
}

public void formKeyPressed(KeyEvent evt)
{
panel.keyPressed(evt);
}

public void formKeyReleased(KeyEvent evt)
{
panel.keyReleased(evt);
}


// Sends which keys were pressed and release to the game class
//This creates the frame and its dimensions

private void initialize()
{
this.setResizable(false);
this.setSize(300, 300);
this.setContentPane(getJContentPane());
this.setTitle("Pong");
}

//intialize jContentPane
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getPanel(), BorderLayout.CENTER);
}
return jContentPane;
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Pong thisClass = new Pong();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
}

PongGame Class_

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.Graphics.*;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


public class PongGame extends JPanel
{
private Ball ball;
private GamePaddle Player1;
private GamePaddle Player2;
private int score1;
private int score2;

public PongGame()
{
Ball ball = new Ball(200, 300);
GamePaddle Player1 = new GamePaddle(100,10);
GamePaddle Player2 = new GamePaddle(10,220);
ball.paintComponent(Graphics g);
}

public void keyPressed(KeyEvent evt)
{
if(evt.getKeyChar() == KeyEvent.VK_A)
Player1.moveLeft();
else if(evt.getKeyChar() == KeyEvent.VK_D)
Player1.moveRight();
else if(evt.getKeyChar() == KeyEvent.VK_LEFT)
Player2.moveLeft();
else if(evt.getKeyChar() == KeyEvent.VK_RIGHT)
Player2.moveRight();
}

public void keyReleased(KeyEvent evt)
{
if(evt.getKeyChar() == KeyEvent.VK_A)
Player1.stop();
else if(evt.getKeyChar() == KeyEvent.VK_D)
Player1.stop();
else if(evt.getKeyChar() == KeyEvent.VK_LEFT)
Player2.stop();
else if(evt.getKeyChar() == KeyEvent.VK_RIGHT)
Player2.stop();
}
}




Right now, we are struggling with creating the images of the ball and gamepaddles in the panel. Any ideas what we are doing wrong or what we are missing? Any suggestions would be appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 22 2010
Added on May 24 2010
4 comments
451 views