All pastes #483037 Raw Edit

Josh's Pong

public text v1 · immutable
·published 2007-05-11 15:18 UTC
rendered paste body
import java.applet.*;
import java.awt.*;
public class Pong extends Applet implements Runnable {
    private Image dbImage;
    private Graphics dbGraphics;
    private Thread t;
    private int ballX = 250;
    private int ballY = 150;
    private int xModifier = 1;
    private int yModifier = 1;
    private PongPaddle[] paddles = new PongPaddle[2];
    private int[] scores = {0,0};
    private final int radius = 15; 
 
    public void init() {
        setBackground(Color.BLACK);
        setSize(500, 300);
        t = new Thread(this);
        t.start();
        
        // Initialize the two paddles
        paddles[0] = new PongPaddle(20, 100, 1, createImage(10, 50));
        paddles[1] = new PongPaddle(getSize().width - 40, 100, 2, createImage(10, 50));
        
        resetBall();
    }
    
    public void update(Graphics g) {
        if (dbImage == null) {
            dbImage = createImage(getSize().width, getSize().height);
            dbGraphics = dbImage.getGraphics();
        }
        paint(dbGraphics);
        g.drawImage(dbImage, 0, 0, this);
    }
    
    public void run() {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        while(true) {
            // Move the ball
            ballX += xModifier;
            ballY += yModifier;

            // Check for a collision with the wall
            if ((ballY > getSize().height - radius) || ballY < radius)
                yModifier *= -1;

            if (ballX > getSize().width - radius) {
                // player 1 (right) scored
                resetBall();                
            } else if (ballX < 0) {
                // player 2 (left) scored
                //System.out.println(ballX);
                resetBall();
            }
            delay(10);
            repaint();
        }
    }
    
    public void resetBall() {
        ballX = (int) getSize().width / 2;
        ballY = (int) getSize().height / 2;
        randomizeDirection();
    }
    
    public void randomizeDirection() {
        int randNum = (int) (Math.random() * 3) - 1;
        //System.out.println(randNum);
        if (randNum > 0) {
            xModifier = -1;
        } else {
            xModifier = 1;
        }
        
        randNum = (int) (Math.random() * 3) - 1;
        if (randNum > 0) {
            yModifier = -1;
        } else {
            yModifier = 1;
        }
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getSize().width, getSize().height);
        g.setColor(Color.WHITE);
        g.fillOval(ballX, ballY, radius, radius);
        g.drawImage(paddles[0].drawPaddle(), paddles[0].getX(), paddles[0].getY(), this);
        g.drawImage(paddles[1].drawPaddle(), paddles[1].getX(), paddles[1].getY(), this);
    }
    
    public void delay(int time) {
        try {
            t.sleep(time);
        } catch (InterruptedException ex) {}
    }
}

// -----------------------------------------------------------

import java.awt.*;
class PongPaddle {
    private int x = 0;
    private int y = 0;
    private int player;
    private Image paddleImg;
    private Graphics g;
    
    public PongPaddle(int _x, int _y, int _player, Image img) {
        x = _x;
        y = _y;
        player = _player; // What player number does this represent?
        paddleImg = img;
    }
    
    public int getX() {return x;}
    public int getY() {return y;}
    
    public Image drawPaddle() {
        // Return an image with the paddle and the player label
        if (g == null) {
            g = paddleImg.getGraphics();
        }
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 15, 50);
        g.setColor(Color.BLACK);
        g.drawString(Integer.toString(player), 2, 22);
        return paddleImg;
    }
}