Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.event.MouseWheelEvent;
- import java.awt.event.MouseWheelListener;
- import java.awt.image.BufferStrategy;
- import javax.swing.JFrame;
- public class Pong extends JFrame implements Runnable, MouseMotionListener,
- KeyListener, MouseWheelListener {
- private int ballX;
- private int ballY;
- private int ballDX;
- private int ballDY;
- private int leftPaddleY;
- private int rightPaddleY;
- private int leftScore;
- private int rightScore;
- private BufferStrategy bufferStrategy;
- private Thread movePaddle;
- private static final int BALL_DIAMETER = 25;
- private static final int PADDLE_HEIGHT = 100;
- private static final int PADDLE_WIDTH = 10;
- private static final int WALL_OFFSET = 10;
- private static final int SCORE_OFFSET = 25;
- private static final int BALL_SPEED = 1;
- private static final int PADDLE_SPEED = 5;
- private static final Color BACKGROUND_COLOUR = Color.BLACK;
- private static final Color BALL_COLOUR = Color.WHITE;
- private static final Color PADDLE_COLOUR = Color.WHITE;
- private static final Color SCORE_COLOUR = Color.WHITE;
- public Pong() {
- super("Pong");
- this.setSize(400, 400);
- ballX = this.getWidth() / 2;
- ballY = this.getHeight() / 2;
- ballDX = ballDY = BALL_SPEED;
- leftScore = rightScore = 0;
- leftPaddleY = rightPaddleY = this.getHeight() / 2;
- this.getContentPane().setBackground(BACKGROUND_COLOUR);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.addMouseWheelListener(this);
- this.addMouseMotionListener(this);
- this.addKeyListener(this);
- this.setVisible(true);
- new Thread(this).start();
- }
- public int verifyPaddleY(int y) {
- if (y < this.getInsets().top) {
- y = this.getInsets().top;
- } else if (y > this.getHeight() - this.getInsets().bottom
- - PADDLE_HEIGHT) {
- y = this.getHeight() - this.getInsets().bottom - PADDLE_HEIGHT;
- }
- return y;
- }
- @Override
- public void mouseWheelMoved(MouseWheelEvent e) {
- leftPaddleY += e.getWheelRotation() * PADDLE_SPEED;
- leftPaddleY = verifyPaddleY(leftPaddleY);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- int dy = 0;
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- dy = -PADDLE_SPEED;
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- dy = PADDLE_SPEED;
- }
- final int arg = dy;
- if (movePaddle != null) {
- movePaddle.stop();
- }
- movePaddle = new Thread(new Runnable() {
- public void run() {
- while (true) {
- leftPaddleY += arg;
- leftPaddleY = verifyPaddleY(leftPaddleY);
- try {
- Thread.sleep(20);
- } catch (InterruptedException e1) {
- }
- }
- }
- });
- movePaddle.start();
- }
- @Override
- public void keyReleased(KeyEvent e) {
- movePaddle.stop();
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- this.mouseMoved(e);
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- rightPaddleY = e.getY() - PADDLE_HEIGHT / 2;
- rightPaddleY = this.verifyPaddleY(rightPaddleY);
- }
- @Override
- public void run() {
- while (true) {
- Insets insets = this.getInsets();
- if (ballX < 0 || ballX > this.getWidth()) {
- System.exit(0);
- }
- if (ballX <= insets.left + WALL_OFFSET + PADDLE_WIDTH
- && ballY >= leftPaddleY
- && ballY <= leftPaddleY + PADDLE_HEIGHT && ballDX < 0) {
- ballDX = Math.abs(ballDX);
- } else if (ballX + BALL_DIAMETER >= this.getWidth() - insets.right
- - PADDLE_WIDTH - WALL_OFFSET
- && ballY >= rightPaddleY
- && ballY <= rightPaddleY + PADDLE_HEIGHT && ballDX > 0) {
- ballDX = -Math.abs(ballDX);
- rightScore++;
- }
- if (ballY <= insets.top) {
- ballDY = Math.abs(ballDY);
- } else if (ballY + BALL_DIAMETER >= this.getHeight()
- - insets.bottom) {
- ballDY = -Math.abs(ballDY);
- }
- repaint();
- ballX += ballDX;
- ballY += ballDY;
- try {
- Thread.sleep(5);
- } catch (InterruptedException e) {
- }
- }
- }
- @Override
- public void paint(Graphics g) {
- Insets insets = this.getInsets();
- super.paint(g);
- g.setColor(BALL_COLOUR);
- g.fillOval(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
- g.setColor(PADDLE_COLOUR);
- g.fillRect(insets.left + WALL_OFFSET, leftPaddleY, PADDLE_WIDTH,
- PADDLE_HEIGHT);
- g.fillRect(this.getWidth() - insets.right - PADDLE_WIDTH - WALL_OFFSET,
- rightPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
- g.setColor(SCORE_COLOUR);
- g.setFont(new Font("Arial", Font.BOLD, 20));
- g.drawString(String.valueOf(leftScore), this.getWidth() / 4,
- SCORE_OFFSET + insets.top);
- g.drawString(String.valueOf(rightScore), this.getWidth() * 3 / 4,
- SCORE_OFFSET + insets.top);
- }
- public static void main(String[] args) {
- new Pong();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement