Professional Documents
Culture Documents
AJP Microproject
AJP Microproject
Under Subject:
Advanced Java Programming(22517)
Submitted by:
Gangawane Subodh[37]
Class:-CM5I Div:-A
Certificate
S.K.B.P. POLYTECHNIC,KOPARGAON
Prof.R.S.Patil Prof.G.N.Jorvekar
Project Objectives:-
This Pong Game Java Project game
has been designed using Java Applet. It has single
player as well as double player option. It provides users
a new experience that is different from the usual pong
games available. It is a freeware game so users need
not pay any amount to play it. As we all know, a pong
game consists of two pads on both sides and a ball, and
the player has to make sure that he hits the ball with
the pad without missing it to win the game and score
more. This Pong Game Java Project introduces some
additional features that takes the difficulty to the next
level by introducing more random bricks here and
there on the game board and there are multiple balls
as well. The player can control the number of balls and
the number of bricks present in a level. There is a
background score as well in the game.
Proposed System:-
This Pong Game Java Project
system has a single user and multi-user option
(maximum two users). Each level has multiple blocks to
create obstructions and multiple balls to take the
difficulty to the next level. One can change the number
of blocks and the number of balls to be present in each
level. The game has intriguing background music. This
game is different from the usual pong games that are
available online and is much more interesting to play.
Moreover, it is freeware and can be played by anyone
and everyone alike.
Software Requirement:-
. JVM
Technology used:-
. JAVA
Hardware Requirement:-
.Hard Disk-2 GB
. RAM-512 MB
.Processor-Dual Core or Above.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// this class is used to implement all the logic for the game
class Game extends JPanel implements ActionListener
{
boolean leftup; //used to check if the left paddle is moving
up
boolean leftdown; //used to check if the left paddle is
moving down
boolean rightup; //used to check if the right paddle is
moving up
boolean rightdown; //used to check if the right paddle is
moving down
int flag=0; //used to check if the ball has hit the paddle
int win=0; //used to check if the game has been won
Game(int x,int y,int width,int height)
{
setBorder(BorderFactory.createMatteBorder(2,2,2,2,
Color.green));
addKeyListener(new KeyMover());
setBackground(Color.black); //sets the background color
of the board
setFocusable(true);
setVisible(true);
setBounds(x,y,width,height); //sets the bounds of the
board
initGame();
}
leftx=20;
lefty=300;
rightx=1070;
righty=300;
inGame=true;
delay=50;
ballleft=false;
ballright=true;
ballup=true;
balldown=false;
win=0;
ballx=50;
bally=150;
doPaint(g);
ballx+=10;
bally-=10;
}
return;
}
if(ballright && balldown)
{ // if the ball is moving right and down
if(bally==600)
{ //if the ball is at the bottom of the board, make it
move up
ballup=true;
balldown=false;
ballx+=10;
bally-=10;
}
else
{ // if the ball is not at the border of the board, move it
diagonally
ballx+=10;
bally+=10;
}
return;
}
if(ballleft && ballup)
{ // if the ball is moving left and up
if(bally==0)
{ //if the ball is at the top of the board, make it move
down
ballup=false;
balldown=true;
ballx-=10;
bally+=10;
}
else
{ // if the ball is not at the border of the board, move it
diagonally
ballx-=10;
bally-=10;
}
return;
}
if(ballleft && balldown)
{
if(bally==600)
{ //if the ball is at the bottom of the board, make it
move up
ballup=true;
balldown=false;
ballx-=10;
bally-=10;
}
else
{ // if the ball is not at the border of the board, move it
diagonally
ballx-=10;
bally+=10;
}
return;
}
}
if(ballx==leftx)
{ //if the ball is at the left paddle, make it move right
if(bally>=lefty && bally<=(lefty+100))
{ //if the ball is at the left paddle, make it move right
ballright=true;
ballleft=false;
}
}
}
// this method is used to paint the components on the
board
public void doPaint(Graphics g)
{
if(inGame)
{ //if the game is in progress, paint the board
g.setColor(Color.white);
g.setFont(new Font("MV Boli", Font.BOLD, 30));
g.drawString("Use P and L", 850,50);
g.setColor(Color.white);
g.setFont(new Font("MV Boli", Font.BOLD, 30));
g.drawString("Use Q and A", 50,50);
g.setColor(Color.DARK_GRAY);
g.drawLine(550, 0, 550, 600);
g.setColor(Color.red);
g.fillOval(ballx, bally, 10, 10);
g.setColor(Color.yellow);
g.fillRect(leftx, lefty, 10, 100);
g.setColor(Color.magenta);
g.fillRect(rightx, righty, 10, 100);
}
else
{ //if the game is not in progress, paint the winner
gameOver(g);
}
Toolkit.getDefaultToolkit().sync();
}
int width=this.getWidth();
int height=this.getHeight();
g.setColor(Color.white);
g.setFont(small);
g.setColor(Color.orange);
g.setFont(new Font("MV Boli", Font.BOLD, 40));
g.drawString("Press Enter to Restart", (width -
metr.stringWidth(msg))/2 - 120, height/2 + 50);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (inGame)
{
checkWins();
move();
}
repaint();
@Override
public void keyPressed(KeyEvent e) {
@Override
public void keyReleased(KeyEvent e) {
if ((key == KeyEvent.VK_Q) ) {
leftup=false;
}
if ((key == KeyEvent.VK_A) ) {
leftdown=false;
}
if ((key == KeyEvent.VK_P) ) {
rightup=false;
}
if ((key == KeyEvent.VK_L)) {
rightdown=false;
}
}
}
this.height=height;
this.width=width;
this.x=x;
this.y=y;
setBounds(x,y,width,height);
setVisible(true);
setLayout(null);
b1=new Game(0,0,width,height);
cont.add(b1);
setLocationRelativeTo(null);
setResizable(false);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Pong {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Frame e1 = new Frame(0,0,1100,600);
e1.setBounds(200,150,1100,638);
e1.setVisible(true);
}
});
}
Output:-
Conclusion:-
This was a Ping Pong Game using applet and
swing using in advanced java.