Advertisement
Guest User

god help me

a guest
Apr 25th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Insets;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.event.MouseWheelEvent;
  10. import java.awt.event.MouseWheelListener;
  11. import java.awt.image.BufferStrategy;
  12.  
  13. import javax.swing.JFrame;
  14.  
  15. public class Pong extends JFrame implements Runnable, MouseMotionListener,
  16.         KeyListener, MouseWheelListener {
  17.    
  18.     private int ballX;
  19.     private int ballY;
  20.     private int ballDX;
  21.     private int ballDY;
  22.     private int leftPaddleY;
  23.     private int rightPaddleY;
  24.     private int leftScore;
  25.     private int rightScore;
  26.     private BufferStrategy bufferStrategy;
  27.     private Thread movePaddle;
  28.    
  29.     private static final int BALL_DIAMETER = 25;
  30.     private static final int PADDLE_HEIGHT = 100;
  31.     private static final int PADDLE_WIDTH = 10;
  32.     private static final int WALL_OFFSET = 10;
  33.     private static final int SCORE_OFFSET = 25;
  34.     private static final int BALL_SPEED = 1;
  35.     private static final int PADDLE_SPEED = 5;
  36.     private static final Color BACKGROUND_COLOUR = Color.BLACK;
  37.     private static final Color BALL_COLOUR = Color.WHITE;
  38.     private static final Color PADDLE_COLOUR = Color.WHITE;
  39.     private static final Color SCORE_COLOUR = Color.WHITE;
  40.    
  41.     public Pong() {
  42.         super("Pong");
  43.         this.setSize(400, 400);
  44.        
  45.         ballX = this.getWidth() / 2;
  46.         ballY = this.getHeight() / 2;
  47.         ballDX = ballDY = BALL_SPEED;
  48.         leftScore = rightScore = 0;
  49.        
  50.         leftPaddleY = rightPaddleY = this.getHeight() / 2;
  51.        
  52.         this.getContentPane().setBackground(BACKGROUND_COLOUR);
  53.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         this.addMouseWheelListener(this);
  55.         this.addMouseMotionListener(this);
  56.         this.addKeyListener(this);
  57.         this.setVisible(true);
  58.        
  59.         new Thread(this).start();
  60.     }
  61.    
  62.     public int verifyPaddleY(int y) {
  63.        
  64.         if (y < this.getInsets().top) {
  65.             y = this.getInsets().top;
  66.         } else if (y > this.getHeight() - this.getInsets().bottom
  67.                 - PADDLE_HEIGHT) {
  68.             y = this.getHeight() - this.getInsets().bottom - PADDLE_HEIGHT;
  69.         }
  70.        
  71.         return y;
  72.     }
  73.    
  74.     @Override
  75.     public void mouseWheelMoved(MouseWheelEvent e) {
  76.         leftPaddleY += e.getWheelRotation() * PADDLE_SPEED;
  77.         leftPaddleY = verifyPaddleY(leftPaddleY);
  78.     }
  79.    
  80.     @Override
  81.     public void keyPressed(KeyEvent e) {
  82.         int dy = 0;
  83.         if (e.getKeyCode() == KeyEvent.VK_UP) {
  84.             dy = -PADDLE_SPEED;
  85.         } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  86.             dy = PADDLE_SPEED;
  87.         }
  88.        
  89.         final int arg = dy;
  90.        
  91.         if (movePaddle != null) {
  92.             movePaddle.stop();
  93.         }
  94.        
  95.         movePaddle = new Thread(new Runnable() {
  96.             public void run() {
  97.                 while (true) {
  98.                     leftPaddleY += arg;
  99.                     leftPaddleY = verifyPaddleY(leftPaddleY);
  100.                     try {
  101.                         Thread.sleep(20);
  102.                     } catch (InterruptedException e1) {
  103.                     }
  104.                 }
  105.             }
  106.         });
  107.         movePaddle.start();
  108.     }
  109.    
  110.     @Override
  111.     public void keyReleased(KeyEvent e) {
  112.         movePaddle.stop();
  113.     }
  114.    
  115.     @Override
  116.     public void keyTyped(KeyEvent e) {
  117.     }
  118.    
  119.     @Override
  120.     public void mouseDragged(MouseEvent e) {
  121.         this.mouseMoved(e);
  122.     }
  123.    
  124.     @Override
  125.     public void mouseMoved(MouseEvent e) {
  126.         rightPaddleY = e.getY() - PADDLE_HEIGHT / 2;
  127.         rightPaddleY = this.verifyPaddleY(rightPaddleY);
  128.     }
  129.    
  130.     @Override
  131.     public void run() {
  132.        
  133.         while (true) {
  134.             Insets insets = this.getInsets();
  135.            
  136.             if (ballX < 0 || ballX > this.getWidth()) {
  137.                 System.exit(0);
  138.             }
  139.            
  140.             if (ballX <= insets.left + WALL_OFFSET + PADDLE_WIDTH
  141.                     && ballY >= leftPaddleY
  142.                     && ballY <= leftPaddleY + PADDLE_HEIGHT && ballDX < 0) {
  143.                 ballDX = Math.abs(ballDX);
  144.             } else if (ballX + BALL_DIAMETER >= this.getWidth() - insets.right
  145.                     - PADDLE_WIDTH - WALL_OFFSET
  146.                     && ballY >= rightPaddleY
  147.                     && ballY <= rightPaddleY + PADDLE_HEIGHT && ballDX > 0) {
  148.                 ballDX = -Math.abs(ballDX);
  149.                 rightScore++;
  150.             }
  151.            
  152.             if (ballY <= insets.top) {
  153.                 ballDY = Math.abs(ballDY);
  154.             } else if (ballY + BALL_DIAMETER >= this.getHeight()
  155.                     - insets.bottom) {
  156.                 ballDY = -Math.abs(ballDY);
  157.             }
  158.            
  159.             repaint();
  160.             ballX += ballDX;
  161.             ballY += ballDY;
  162.             try {
  163.                 Thread.sleep(5);
  164.             } catch (InterruptedException e) {
  165.             }
  166.         }
  167.     }
  168.    
  169.     @Override
  170.     public void paint(Graphics g) {
  171.         Insets insets = this.getInsets();
  172.         super.paint(g);
  173.        
  174.         g.setColor(BALL_COLOUR);
  175.         g.fillOval(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
  176.        
  177.         g.setColor(PADDLE_COLOUR);
  178.         g.fillRect(insets.left + WALL_OFFSET, leftPaddleY, PADDLE_WIDTH,
  179.                 PADDLE_HEIGHT);
  180.         g.fillRect(this.getWidth() - insets.right - PADDLE_WIDTH - WALL_OFFSET,
  181.                 rightPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
  182.        
  183.         g.setColor(SCORE_COLOUR);
  184.         g.setFont(new Font("Arial", Font.BOLD, 20));
  185.         g.drawString(String.valueOf(leftScore), this.getWidth() / 4,
  186.                 SCORE_OFFSET + insets.top);
  187.         g.drawString(String.valueOf(rightScore), this.getWidth() * 3 / 4,
  188.                 SCORE_OFFSET + insets.top);
  189.     }
  190.    
  191.     public static void main(String[] args) {
  192.         new Pong();
  193.        
  194.     }
  195.    
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement