Post: [Java] Pong :]
04-30-2011, 02:40 PM #1
kiwimoosical
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); So yeah, programming class I had to make pong, simple really, but I felt I should post my source, so here ya go:
    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pong extends JApplet implements KeyListener, Runnable
{
Integer x = 100;
Integer y = 150;
Integer size = 25;
int px = x, py = y;

String xDirection = "right";
String yDirection = "down";

Boolean gameOver = false;

JLabel paddle1;
JLabel paddle2;

Rectangle pr1;
Rectangle pr2;

Rectangle leftBorder = new Rectangle(0,0,2,300);
Rectangle rightBorder = new Rectangle(0,0,2,300);

Thread moveBall;

public void init()
{
setLayout(null);
addKeyListener(this);

moveBall = new Thread(this);
moveBall.start();

pr1 = new Rectangle(0,99,26,102);
pr2 = new Rectangle(474,99,26,102);

ImageIcon p1 = new ImageIcon(getImage(getCodeBase(), "p1.jpg"));
ImageIcon p2 = new ImageIcon(getImage(getCodeBase(), "p2.gif"));

paddle1 = new JLabel(p1);
paddle2 = new JLabel(p2);

add(paddle1);
add(paddle2);

paddle1.setBounds(0,100,25,100);
paddle2.setBounds(475,100,25,100);
}

public void run()
{
try
{
while(!gameOver)
{
repaint();
Thread.sleep(7);
}
}catch(Exception ex){run();}
}

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.WHITE);
g.fillOval(px,py,size,size);
g.setColor(Color.BLACK);
if(xDirection.equals("right"))
x += 1;
else
x -= 1;
if(yDirection.equals("down"))
y += 1;
else
y -= 1;

if(pr1.contains(x,y))
xDirection = "right";
if(pr2.contains(x+25,y))
xDirection = "left";
if(y <= 1 )
yDirection = "down";
if(y >= 276)
yDirection = "up";
if(x == 0 || x == 500)
gameOver = true;
if(!gameOver)
g.fillOval(x,y,size,size);
else
{
if( x == 500 )
g.drawString("Right side wins!",250,150);
if( x == 0 )
g.drawString("Left side wins!",250,150);
}
}

public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
try
{
if(e.getKeyCode() == KeyEvent.VK_W)
{
if(paddle1.getY() > 0)
{
paddle1.setBounds(paddle1.getX(), (paddle1.getY() - 10),25,100);
pr1.setBounds((int)pr1.getX(), (int)pr1.getY() - 10,25,100);
}
}

else if(e.getKeyCode() == KeyEvent.VK_S)
{
if(paddle1.getY() < 200)
{
paddle1.setBounds(paddle1.getX(), (paddle1.getY() + 10),25,100);
pr1.setBounds((int)pr1.getX(), (int)pr1.getY() + 10,25,100);
}
}

if(e.getKeyCode() == KeyEvent.VK_UP)
{
if(paddle2.getY() > 0)
{
paddle2.setBounds(paddle2.getX(), (paddle2.getY() - 10),25,100);
pr2.setBounds((int)pr2.getX(), (int)pr2.getY() - 10,25,100);
}
}

else if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
if(paddle2.getY() < 200)
{
paddle2.setBounds(paddle2.getX(), (paddle2.getY() + 10),25,100);
pr2.setBounds((int)pr2.getX(), (int)pr2.getY() + 10,25,100);
}
}
}catch(Exception ex){}
}
}
(adsbygoogle = window.adsbygoogle || []).push({});
04-30-2011, 03:58 PM #2
Very nice Smile Screen shots?
04-30-2011, 04:00 PM #3
viralhysteria
74261700027
OMG! Java!
Good work Smile
04-30-2011, 04:01 PM #4
Originally posted by deRez View Post
OMG! Java!
Good work Smile


I Love your sig and avatar, Smile
04-30-2011, 04:02 PM #5
Pichu
RIP PICHU.
I'll keep this for future use. I'm learning Java right now, but I'm doing to alone. :/

Any tips?
04-30-2011, 04:07 PM #6
viralhysteria
74261700027
Originally posted by Relevant View Post
I Love your sig and avatar, Smile


Why thank you Smile
04-30-2011, 04:12 PM #7
Pichu
RIP PICHU.
I get this to go on...
You must login or register to view this content.
04-30-2011, 04:13 PM #8
kiwimoosical
Bounty hunter
Originally posted by Relevant View Post
Very nice Smile Screen shots?

You must login or register to view this content.

Originally posted by Papa
I'll keep this for future use. I'm learning Java right now, but I'm doing to alone. :/

Any tips?

You must login or register to view this content.

If you learn good from reading source code I recommend that site :]
04-30-2011, 04:15 PM #9
Pichu
RIP PICHU.
Originally posted by kiwimoosical View Post
You must login or register to view this content.


You must login or register to view this content.

If you learn good from reading source code I recommend that site :]


What I wanted. Happy

I'm actually watching videos on YouTube right now that are helping me get confortable with program first... :/

Any help on what went wrong or how I get them to function right?
04-30-2011, 04:15 PM #10
kiwimoosical
Bounty hunter
You need to put the p1.jpg and p2.gif files in the project folder:
p1: You must login or register to view this content.
p2: You must login or register to view this content.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo