Advertisement
Guest User

Java Pong

a guest
Jan 14th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.geom.*;
  4. import java.awt.event.*;
  5.  
  6. class Vector {
  7.     final double x,y;
  8.     Vector(double x, double y) {
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12.     public Vector setX(double x) {
  13.         return new Vector(x, y);
  14.     }
  15.     public Vector setY(double y) {
  16.         return new Vector(x, y);
  17.     }
  18.     public Vector add(Vector v) {
  19.         return new Vector(x+v.x, y+v.y);
  20.     }
  21.     public Vector add(double x, double y) {
  22.         return new Vector(this.x+x, this.y+y);
  23.     }
  24.     public Vector sub(Vector v) {
  25.         return new Vector(x-v.x, y-v.y);
  26.     }
  27.     public Vector mul(double d) {
  28.         return new Vector(x*d, y*d);
  29.     }
  30.     public Vector div(double d) {
  31.         return new Vector(x/d, y/d);
  32.     }
  33.     public Vector neg() {
  34.         return new Vector(-x, -y);
  35.     }
  36.     public Vector negX() {
  37.         return new Vector(-x, y);
  38.     }
  39.     public Vector negY() {
  40.         return new Vector(x, -y);
  41.     }
  42.     public double angle() {
  43.         return Math.atan2(y, x);
  44.     }
  45.     public double length() {
  46.         return Math.sqrt(x*x+y*y);
  47.     }
  48.     public double distance(Vector v) {
  49.         return sub(v).length();
  50.     }
  51.     public Vector rotate(double angle) {
  52.         double sin = Math.sin(angle);
  53.         double cos = Math.cos(angle);
  54.         return new Vector(x*cos - y*sin,
  55.                           x*sin + y*cos);
  56.     }
  57.     @Override
  58.     public String toString() {
  59.         return "Vector("+x+", "+y+")";
  60.     }
  61. }
  62.  
  63. class Ball {
  64.     static Ellipse2D.Double kreis = new Ellipse2D.Double();
  65.     Vector pos, lastPos;
  66.     Vector speed = new Vector(0,0);
  67.     double radius;
  68.    
  69.     public Ball(Vector pos, double radius) {
  70.         this.pos = pos;
  71.         this.radius = radius;
  72.     }
  73.    
  74.     public boolean contains(Vector v) {
  75.         return v.sub(pos).length() <= radius;
  76.     }
  77.    
  78.     public void update(double time) {
  79.         lastPos = pos;
  80.         pos = pos.add(speed.mul(time));
  81.     }
  82.    
  83.     public void stepBack() {
  84.         pos = lastPos;
  85.     }
  86.    
  87.     public void paint(Graphics2D g) {
  88.         kreis.setFrame(pos.x-radius, pos.y-radius,radius*2,radius*2);
  89.         g.fill(kreis);
  90.     }
  91. }
  92.  
  93. class Padder {
  94.     static Rectangle2D.Double rechteck = new Rectangle2D.Double();
  95.     static double daempfung = 0.000001;
  96.     Vector pos, lastPos;
  97.     Vector speed = new Vector(0,0);
  98.     double width, height;
  99.    
  100.     public Padder(Vector pos, double width, double height) {
  101.         this.pos = pos;
  102.         this.width = width;
  103.         this.height = height;
  104.     }
  105.    
  106.     Vector getCenter() {
  107.         return pos.add(width/2, height/2);
  108.     }
  109.    
  110.     public void update(double time) {
  111.         lastPos = pos;
  112.         speed = speed.mul(Math.pow(daempfung,time));
  113.         if(speed.length() > 200) speed = speed.div(speed.length()).mul(200);
  114.         pos = pos.add(speed.mul(time));
  115.     }
  116.    
  117.     public void stepBack() {
  118.         pos = lastPos;
  119.     }
  120.    
  121.     public Vector[] getCorners() {
  122.         return new Vector[] {pos, pos.add(width,0),
  123.                              pos.add(width, height), pos.add(0,height)};
  124.     }
  125.    
  126.     public boolean collides(Ball b) {
  127.         for(Vector corner: getCorners()) {
  128.             if(b.contains(corner)) {
  129.                 return true;
  130.             }
  131.         }
  132.         return  (b.pos.x >= pos.x &&
  133.                  b.pos.x < pos.x+width &&
  134.                  b.pos.y+b.radius >= pos.y &&
  135.                  b.pos.y-b.radius < pos.y+height) ||
  136.                 (b.pos.x+b.radius >= pos.x &&
  137.                  b.pos.x-b.radius < pos.x+width &&
  138.                  b.pos.y >= pos.y &&
  139.                  b.pos.y < pos.y+height);
  140.     }
  141.    
  142.     public void reflect(Ball b) {
  143.         Vector relativeBallSpeed = b.speed.sub(speed);
  144.         if(b.pos.x >= pos.x && b.pos.x < pos.x+width) {
  145.             relativeBallSpeed = relativeBallSpeed.negY();
  146.         } else if(b.pos.y >= pos.y && b.pos.y < pos.y+height) {
  147.             relativeBallSpeed = relativeBallSpeed.negX();
  148.         } else {
  149.             Vector corner = null;
  150.             for(Vector c: getCorners()) {
  151.                 if(corner == null) corner = c;
  152.                 else if(b.pos.distance(c) < b.pos.distance(corner)) {
  153.                     corner = c;
  154.                 }
  155.             }
  156.             double w1 = b.pos.sub(corner).angle();
  157.             double w2 = relativeBallSpeed.angle();
  158.             double einfallswinkel = w2-w1;
  159.             relativeBallSpeed = relativeBallSpeed.neg().rotate(-2*einfallswinkel);
  160.         }
  161.         b.speed = relativeBallSpeed.add(speed);
  162.     }
  163.    
  164.     public void paint(Graphics2D g) {
  165.         rechteck.setFrame(pos.x, pos.y,width,height);
  166.         g.fill(rechteck);
  167.     }
  168. }
  169.  
  170. public class Pong extends JPanel {
  171.     Padder padder = new Padder(new Vector(20,260), 15, 60);
  172.     Ball ball = new Ball(new Vector(300,300), 10);
  173.     volatile Vector target = padder.getCenter();
  174.    
  175.     MouseAdapter mouseAdapter = new MouseAdapter() {
  176.         public void mouseMoved(MouseEvent e) {
  177.             target = new Vector(e.getX(), e.getY());
  178.         }
  179.     };
  180.    
  181.     public Pong() {
  182.         addMouseMotionListener(mouseAdapter);
  183.     }
  184.    
  185.     public void start() {
  186.         ball.speed = new Vector(-200,20);
  187.         long lastTime = System.nanoTime();
  188.         while(true) {
  189.             long currentTime = System.nanoTime();
  190.             double time = (currentTime - lastTime)/1e9;
  191.             lastTime = currentTime;
  192.             Vector dist = target.sub(padder.getCenter());
  193.             padder.speed = padder.speed.add(dist.mul(time*100));
  194.             ball.update(time);
  195.             padder.update(time);
  196.             if(padder.collides(ball)) {
  197.                 ball.stepBack();
  198.                 padder.stepBack();
  199.                 padder.reflect(ball);
  200.             }
  201.             if(ball.pos.x + ball.radius > getWidth()) {
  202.                 ball.pos = ball.pos.setX(getWidth()-ball.radius);
  203.                 ball.speed = ball.speed.setX(-Math.abs(ball.speed.x));
  204.             }
  205.             if(ball.pos.x - ball.radius < 0) {
  206.                 ball.pos = ball.pos.setX(ball.radius);
  207.                 ball.speed = ball.speed.setX(Math.abs(ball.speed.x));
  208.             }
  209.             if(ball.pos.y + ball.radius > getHeight()) {
  210.                 ball.pos = ball.pos.setY(getHeight()-ball.radius);
  211.                 ball.speed = ball.speed.setY(-Math.abs(ball.speed.y));
  212.             }
  213.             if(ball.pos.y - ball.radius < 0) {
  214.                 ball.pos = ball.pos.setY(ball.radius);
  215.                 ball.speed = ball.speed.setY(Math.abs(ball.speed.y));
  216.             }
  217.             repaint();
  218.             try {
  219.                 Thread.sleep(16);
  220.             } catch(Exception e) {
  221.             }
  222.         }
  223.     }
  224.    
  225.     public static void main(String[] args) {
  226.         JFrame frame = new JFrame();
  227.         Pong pong = new Pong();
  228.        
  229.         pong.setPreferredSize(new Dimension(800,600));
  230.         frame.add(pong);
  231.         frame.pack();
  232.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  233.         frame.setVisible(true);
  234.         pong.start();
  235.     }
  236.    
  237.     @Override
  238.     public void paint(Graphics g) {
  239.         Graphics2D g2 = (Graphics2D)g;
  240.         g2.setColor(Color.WHITE);
  241.         g2.fillRect(0,0,getWidth(),getHeight());
  242.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  243.         g2.setColor(Color.BLACK);
  244.         ball.paint(g2);
  245.         padder.paint(g2);
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement