Public paste
Undefined
By: Guest | Date: Jul 6 2010 13:35 | Format: None | Expires: never | Size: 6.16 KB | Hits: 813

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseMotionListener;
  7. import java.awt.event.MouseListener;
  8. import java.util.Random;
  9.  
  10.  
  11. public class Game extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener {
  12.  
  13.     Random rd = new Random();
  14.     int ball_x_pos = 250;
  15.     int ball_y_pos = 125;
  16.     int mouse_x_pos = 0;
  17.     int mouse_y_pos = 0;
  18.     int tastaturhoch = 38;
  19.     int player2y = 100;
  20.     int tastaturrunter = 40;
  21.     int appletsize_x = 700;
  22.     int appletsize_y = 400;
  23.     int mousepressed = 0;
  24.     int trefferanzahl = 15;
  25.     int radius = 10;
  26.     int balken_x_pos = 30;
  27.     int balken_y_pos = mouse_y_pos;    
  28.     int balken_width = 20;
  29.     int balken_height = 100;
  30.     int ball_x_speed = 0;
  31.     int ball_y_speed = 0;
  32.     int punktespieler1 = 0;
  33.     int punktespieler2 = 0;
  34.     Image background;
  35.     private Image dbImage;
  36.     private Graphics dbg;
  37.  
  38.  
  39.     public void init() {
  40.         this.setLayout(null);
  41.         addKeyListener(this);
  42.         addMouseListener(this);
  43.         addMouseMotionListener(this);
  44.         setFocusable(true);
  45.  
  46.         background = getImage(getCodeBase(), "bg.jpg");
  47.      
  48.        
  49.  
  50.     }
  51.  
  52.  
  53.         public void mousePressed(MouseEvent arg0) {
  54.                        
  55.                         if ( mousepressed == 0){
  56.             ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  57.             ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  58.                         }
  59.                         mousepressed++;
  60.            
  61.        
  62.         }
  63.  
  64.     public void start() {
  65.         Thread th = new Thread(this);
  66.         th.start();
  67.     }
  68.  
  69.     public void stop() {
  70.     }
  71.  
  72.     public void destroy() {
  73.     }
  74.  
  75.    /* public boolean mouseMove(Event e, int x, int y) {
  76.         mouse_y_pos = y;
  77.         return true;
  78.     }*/
  79.    
  80.     public void mouseMoved(MouseEvent arg0) {
  81.                 // TODO Auto-generated method stub
  82.         mouse_y_pos = arg0.getY();
  83.        
  84.         }
  85.    
  86.         public void keyPressed(KeyEvent e) {
  87.                
  88.                 if (e.getKeyCode() == tastaturhoch) {
  89.                         player2y = player2y - 35;      
  90.                        
  91.                 }
  92.                 if(e.getKeyCode() == tastaturrunter){
  93.                         player2y = player2y + 35;
  94.                 }
  95.                
  96.                
  97.                
  98.         }
  99.  
  100.         public void keyReleased(KeyEvent e) {
  101.                 // TODO Auto-generated method stub
  102.                 System.out.println("hallo");
  103.         }
  104.  
  105.         public void keyTyped(KeyEvent e) {
  106.                 // TODO Auto-generated method stub
  107.                 System.out.println("hallo");
  108.         }
  109.        
  110.  
  111.     public void run() {
  112.    
  113.  
  114.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  115.         while (true) {
  116.             if (    ball_y_pos >= mouse_y_pos + radius &&
  117.                     ball_y_pos <= mouse_y_pos + balken_height + radius &&
  118.                     ball_x_pos <= balken_width + balken_x_pos + radius) {
  119.                 //System.out.println("Ball getroffen!");
  120.                 trefferanzahl++;
  121.                 if (trefferanzahl >= 25) { trefferanzahl = 15; }
  122.                 ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  123.                 ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  124.             }
  125.  
  126.             if (    ball_y_pos >= player2y + radius &&
  127.                     ball_y_pos <= player2y + balken_height + radius &&
  128.                     ball_x_pos >= 670) {
  129.                 System.out.println("Ball getroffen!");
  130.                 trefferanzahl++;
  131.                 if (trefferanzahl >= 25) { trefferanzahl = 15; }
  132.                 ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  133.                 ball_x_speed = 1 - rd.nextInt(trefferanzahl);
  134.             }
  135.  
  136.            
  137.            
  138.            
  139.             if (ball_x_pos > appletsize_x - radius) {
  140.                 ball_x_speed *= -1;
  141.  
  142.             }
  143.             if (ball_y_pos > appletsize_y - radius) {
  144.                 ball_y_speed *= -1;
  145.             }
  146.             if (ball_x_pos < radius) {
  147.                 ball_x_pos = 350;
  148.                 ball_y_pos = 200;
  149.             }
  150.  
  151.             if (ball_y_pos < radius) {
  152.                 ball_y_speed *= -1;
  153.             }
  154.             ball_x_pos += ball_x_speed;
  155.             ball_y_pos += ball_y_speed;
  156.  
  157.             if (ball_x_pos <= radius) {
  158.                 ball_x_pos = radius + 2;
  159.                 ball_x_speed = ball_x_speed * -1;
  160.             }
  161.             if (ball_y_pos <= radius) {
  162.                 ball_y_pos = radius + 2;
  163.                 ball_y_speed = ball_y_speed * -1;
  164.             }
  165.             if (ball_x_pos >= 675) { ball_x_speed = 0; ball_y_speed = 0; ball_x_pos = 350; ball_y_pos = 200; punktespieler1++; mousepressed = 0;}
  166.             if (ball_x_pos <= 25) { ball_x_speed = 0; ball_y_speed = 0; ball_x_pos = 350; ball_y_pos = 200; punktespieler2++; mousepressed = 0;}
  167.             repaint();
  168.             try {
  169.                 Thread.sleep(20);
  170.             } catch (InterruptedException ex) {
  171.             }
  172.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  173.         }
  174.     }
  175.  
  176.  
  177.  
  178.     public void paint(Graphics g) {
  179.         g.drawImage(background, 0, 0, this);
  180.  
  181.         g.setColor(Color.black);
  182.         g.fillOval(ball_x_pos - radius, ball_y_pos - radius, 2 * radius, 2 * radius);
  183.  
  184.         g.setColor(Color.black);
  185.         g.fillRect(balken_x_pos, mouse_y_pos, balken_width, balken_height);
  186.        
  187.         g.setColor(Color.black);
  188.         g.fillRect(670,player2y, balken_width, balken_height );
  189.    
  190.     }
  191.  
  192.  
  193.  
  194.         public void mouseEntered(MouseEvent arg0) {
  195.                 // TODO Auto-generated method stub
  196.                
  197.                
  198.                
  199.         }
  200.  
  201.  
  202.         public void mouseExited(MouseEvent arg0) {
  203.                 // TODO Auto-generated method stub
  204.                
  205.         }
  206.  
  207.  
  208.  
  209.  
  210.         public void mouseReleased(MouseEvent arg0) {
  211.                 // TODO Auto-generated method stub
  212.                
  213.         }
  214.  
  215.  
  216.         public void mouseDragged(MouseEvent e) {
  217.                 // TODO Auto-generated method stub
  218.                
  219.         }
  220.  
  221.            public void mouseClicked(MouseEvent arg0) {
  222.                         // TODO Auto-generated method stub
  223.                
  224.                 }
  225.  
  226.  
  227.   /*  public void update(Graphics g) {
  228.         if (dbImage == null) {
  229.             dbImage = createImage(this.getSize().width, this.getSize().height);
  230.             dbg = dbImage.getGraphics();
  231.         }
  232.         dbg.setColor(getBackground());
  233.         dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  234.  
  235.         dbg.setColor(getForeground());
  236.         paint(dbg);
  237.  
  238.         g.drawImage(dbImage, 0, 0, this);
  239.     } */
  240. }