Advertisement
Guest User

Pong

a guest
Mar 12th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.78 KB | None | 0 0
  1. package games.complete;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Toolkit;
  7.  
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.util.Arrays;
  13.  
  14. import javax.swing.JComponent;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18. public class Pong
  19. {
  20.     JFrame frame;
  21.     JPanel mainPanel;
  22.    
  23.     Ball ball;
  24.    
  25.     Paddle player;
  26.     Paddle enemy;
  27.    
  28.     MoveController mc = new MoveController();
  29.     DisplayManager dm = new DisplayManager();
  30.    
  31.     int maxSpeed = 5;
  32.     double movement = 1;
  33.    
  34.     public static void main(String[] args)
  35.     {
  36.         new Pong();
  37.     }
  38.  
  39.     public Pong()
  40.     {
  41.         frame = new JFrame("Pong");
  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.        
  44.         mainPanel = new JPanel(null);
  45.        
  46.         mainPanel.setBackground(Color.black);
  47.         mainPanel.setPreferredSize(new Dimension(500, 500));
  48.        
  49.         player = new Paddle(20, 100);
  50.         enemy = new Paddle(20, 100);
  51.         ball = new Ball(5, Color.GREEN);
  52.        
  53.         mainPanel.add(player);
  54.         mainPanel.add(enemy);
  55.         mainPanel.add(ball);
  56.         enemy.setColor(Color.RED);
  57.        
  58.         frame.setContentPane(mainPanel);
  59.         frame.pack();
  60.        
  61.         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  62.         int x = screen.width/2 - frame.getWidth()/2;
  63.         int y = screen.height/2 - frame.getHeight()/2;
  64.         frame.setLocation(x, y);
  65.                
  66.         mainPanel.addKeyListener(mc);
  67.        
  68.         frame.setVisible(true);
  69.        
  70.         mainPanel.repaint();
  71.        
  72.         x = mainPanel.getWidth()/2 - ball.getWidth()/2;
  73.         y = mainPanel.getHeight()/2 - ball.getHeight()/2;
  74.         System.out.println("X: " + x + ", Y: " + y);
  75.         ball.setLocation(x, y);
  76.        
  77.         x = 0;
  78.         y = mainPanel.getHeight()/2 - player.getHeight()/2;
  79.         System.out.println("X: " + x + ", Y: " + y);
  80.         player.setLocation(x, y);
  81.        
  82.         x = mainPanel.getWidth() - enemy.getWidth();
  83.         y = mainPanel.getHeight()/2 - enemy.getHeight()/2;
  84.         System.out.println("X: " + x + ", Y: " + y);
  85.         enemy.setLocation(x, y);
  86.        
  87.         System.out.println(Arrays.deepToString(mainPanel.getComponents()));
  88.        
  89.         mainPanel.repaint();
  90.        
  91.         frame.addMouseListener(new MouseAdapter()
  92.         {
  93.             @Override
  94.             public void mousePressed(MouseEvent e)
  95.             {
  96.                 mainPanel.requestFocus();
  97.             }
  98.         });
  99.        
  100.         dm.start();
  101.         mainPanel.requestFocus();
  102.     }
  103.    
  104.     private class Ball extends JComponent
  105.     {
  106.         private int radius;
  107.         private Color color;
  108.        
  109.         public Ball(int size, Color c)
  110.         {
  111.             if(size > 0)
  112.                 radius = size;
  113.             else
  114.                 radius = 1;
  115.             this.setPreferredSize(new Dimension(radius, radius));
  116.             color = c;
  117.         }
  118.        
  119.         public Ball(int size)
  120.         {
  121.             this(size, Color.RED);
  122.         }
  123.        
  124.         public Color getColor()
  125.         {
  126.             return color;
  127.         }
  128.        
  129.         public void setColor(Color c)
  130.         {
  131.             color = c;
  132.         }
  133.        
  134.         public int getRadius()
  135.         {
  136.             return radius;
  137.         }
  138.        
  139.         public void setRadius(int r)
  140.         {
  141.             radius = r > 0 ? r : radius;
  142.         }
  143.        
  144.         @Override
  145.         public void paintComponent(Graphics g)
  146.         {
  147.             System.out.println("Radius: " + radius);
  148.             g.setColor(color);
  149.             g.fillOval(0, 0, radius, radius);
  150.         }
  151.        
  152.         @Override
  153.         public String toString()
  154.         {
  155.             return "games.common.Pong.Ball[Radius=" + radius + ",Color="+color.toString()+"]";
  156.         }
  157.     }
  158.    
  159.     private class Paddle extends JComponent
  160.     {
  161.         private int width;
  162.         private int height;
  163.         private Color color;
  164.        
  165.         public double vx;
  166.         public double vy;
  167.        
  168.         public Paddle(int width, int height, Color c)
  169.         {
  170.             this.width = width > 0 ? width : 1;
  171.             this.height = height > 0 ? height : 1;
  172.             this.setPreferredSize(new Dimension(width, height));
  173.             color = c;
  174.         }
  175.        
  176.         public Paddle(int width, int height)
  177.         {
  178.             this(width, height, Color.BLUE);
  179.         }
  180.        
  181.         public int getHeight()
  182.         {
  183.             return height;
  184.         }
  185.        
  186.         public int getWidth()
  187.         {
  188.             return width;
  189.         }
  190.        
  191.         public void setHeight(int h)
  192.         {
  193.             height = h > 0 ? h : height;
  194.         }
  195.        
  196.         public void setWidth(int w)
  197.         {
  198.             width = w > 0 ? w : width;
  199.         }
  200.        
  201.         public Color getColor()
  202.         {
  203.             return color;
  204.         }
  205.        
  206.         public void setColor(Color c)
  207.         {
  208.             color = c;
  209.         }
  210.        
  211.         @Override
  212.         public void paintComponent(Graphics g)
  213.         {
  214.             g.setColor(color);
  215.             g.fillRect(0, 0, width, height);
  216.         }
  217.     }
  218.    
  219.     private class DisplayManager extends Thread
  220.     {        
  221.         public DisplayManager()
  222.         {
  223.             super(new Runnable() {
  224.                
  225.                 boolean paused;
  226.                 boolean run;
  227.                
  228.                 @Override
  229.                 public void run()
  230.                 {
  231.                     while(true)
  232.                     {
  233.                         mainPanel.repaint();
  234.                        
  235.                         player.setLocation(player.getX(), (int)Math.round(player.getY() + player.vy));
  236.                         enemy.setLocation(enemy.getX(), (int)Math.round(enemy.getY() + enemy.vy));
  237.                        
  238.                         if(player.getY() < 0)
  239.                         {
  240.                             player.setLocation(player.getX(), 0);
  241.                             player.vy = 0;
  242.                         }
  243.                        
  244.                         if(player.getY() > mainPanel.getHeight() - player.getHeight())
  245.                         {
  246.                             player.setLocation(player.getX(), mainPanel.getHeight() - player.getHeight());
  247.                             player.vy = 0;
  248.                         }
  249.                        
  250.                         if(enemy.getY() < 0)
  251.                         {
  252.                             enemy.setLocation(enemy.getX(), 0);
  253.                             player.vy = 0;
  254.                         }
  255.                        
  256.                         if(enemy.getY() > mainPanel.getHeight() - enemy.getHeight())
  257.                         {
  258.                             enemy.setLocation(enemy.getX(), mainPanel.getHeight() - enemy.getHeight());
  259.                             player.vy = 0;
  260.                         }
  261.                        
  262.                         if(player.vy > maxSpeed)
  263.                             player.vy = maxSpeed;
  264.                         if(player.vy < -maxSpeed)
  265.                             player.vy = -maxSpeed;
  266.                         if(enemy.vy > maxSpeed)
  267.                             enemy.vy = maxSpeed;
  268.                         if(enemy.vy < -maxSpeed)
  269.                             enemy.vy = -maxSpeed;
  270.                        
  271.                         try
  272.                         {
  273.                             Thread.sleep(1000/60);
  274.                         }
  275.                         catch(InterruptedException ex)
  276.                         {
  277.                            
  278.                         }
  279.                        
  280.                         player.vy *= 0.98;
  281.                     }                        
  282.                 }
  283.             });
  284.         }  
  285.     }
  286.    
  287.     private class MoveController implements KeyListener
  288.     {
  289.         @Override
  290.         public void keyTyped(KeyEvent e) {
  291.         }
  292.  
  293.         @Override
  294.         public void keyPressed(KeyEvent e)
  295.         {
  296.             switch(e.getKeyCode())
  297.             {
  298.                 case KeyEvent.VK_UP: case KeyEvent.VK_W:
  299.                     player.vy -= movement;
  300.                     break;
  301.                 case KeyEvent.VK_DOWN: case KeyEvent.VK_S:
  302.                     player.vy += movement;
  303.                     break;
  304.                 default:
  305.                     System.out.println("You're not doing it right!");
  306.             }
  307.             System.out.println("VX: " + player.vx + ", VY: " + player.vy);
  308.         }
  309.  
  310.         @Override
  311.         public void keyReleased(KeyEvent e) {
  312.         }
  313.        
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement