Public paste
Undefined
By: Guest | Date: Jul 6 2010 12:25 | Format: None | Expires: never | Size: 5.3 KB | Hits: 805

  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 appletsize_x = 700;
  19.     int appletsize_y = 400;
  20.     int trefferanzahl = 5;
  21.     int radius = 10;
  22.     int balken_x_pos = 30;
  23.     int balken_y_pos = mouse_y_pos;    
  24.     int balken_width = 20;
  25.     int balken_height = 100;
  26.     int ball_x_speed = 0;
  27.     int ball_y_speed = 0;
  28.     Image background;
  29.     private Image dbImage;
  30.     private Graphics dbg;
  31.     Button start;
  32.     Button Pause;
  33.  
  34.     public void init() {
  35.         this.setLayout(null);
  36.         addKeyListener(this);
  37.         addMouseListener(this);
  38.         addMouseMotionListener(this);
  39.  
  40.         background = getImage(getCodeBase(), "bg.jpg");
  41.         start = new Button("Spiel starten");
  42.         Pause = new Button("Pause");
  43.         start.setBounds(250, 125, 80, 50);
  44.      
  45.         add(start);
  46.         add(Pause);
  47.     }
  48.  
  49.  
  50.     public boolean action(Event e, Object args) {
  51.         if (e.target == start) {
  52.             ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  53.             ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  54.             start.setVisible(false);
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     public void start() {
  60.         Thread th = new Thread(this);
  61.         th.start();
  62.     }
  63.  
  64.     public void stop() {
  65.     }
  66.  
  67.     public void destroy() {
  68.     }
  69.  
  70.    /* public boolean mouseMove(Event e, int x, int y) {
  71.         mouse_y_pos = y;
  72.         return true;
  73.     }*/
  74.    
  75.     public void mouseMoved(MouseEvent arg0) {
  76.                 // TODO Auto-generated method stub
  77.         mouse_y_pos = arg0.getY();
  78.         }
  79.    
  80.         @Override
  81.         public void keyPressed(KeyEvent e) {
  82.                 // TODO Auto-generated method stub
  83.        
  84.                 System.out.println("hallo");
  85.         }
  86.         @Override
  87.         public void keyReleased(KeyEvent e) {
  88.                 // TODO Auto-generated method stub
  89.                 System.out.println("hallo");
  90.         }
  91.         @Override
  92.         public void keyTyped(KeyEvent e) {
  93.                 // TODO Auto-generated method stub
  94.                 System.out.println("hallo");
  95.         }
  96.        
  97.  
  98.     public void run() {
  99.  
  100.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  101.         while (true) {
  102.             if (    ball_y_pos >= mouse_y_pos + radius &&
  103.                     ball_y_pos <= mouse_y_pos + balken_height + radius &&
  104.                     ball_x_pos <= balken_width + balken_x_pos + radius) {
  105.                 System.out.println("Ball getroffen!");
  106.                 trefferanzahl++;
  107.                 if (trefferanzahl >= 10) { trefferanzahl = 5; }
  108.                 ball_y_speed = 1 + rd.nextInt(trefferanzahl);
  109.                 ball_x_speed = 1 + rd.nextInt(trefferanzahl);
  110.             }
  111.  
  112.             if (ball_x_pos > appletsize_x - radius) {
  113.                 ball_x_speed *= -1;
  114.  
  115.             }
  116.             if (ball_y_pos > appletsize_y - radius) {
  117.                 ball_y_speed *= -1;
  118.             }
  119.             if (ball_x_pos < radius) {
  120.                 ball_x_pos = 350;
  121.                 ball_y_pos = 200;
  122.             }
  123.  
  124.             if (ball_y_pos < radius) {
  125.                 ball_y_speed *= -1;
  126.             }
  127.             ball_x_pos += ball_x_speed;
  128.             ball_y_pos += ball_y_speed;
  129.  
  130.             if (ball_x_pos <= radius) {
  131.                 ball_x_pos = radius + 2;
  132.                 ball_x_speed = ball_x_speed * -1;
  133.             }
  134.             if (ball_y_pos <= radius) {
  135.                 ball_y_pos = radius + 2;
  136.                 ball_y_speed = ball_y_speed * -1;
  137.             }
  138.             repaint();
  139.             try {
  140.                 Thread.sleep(20);
  141.             } catch (InterruptedException ex) {
  142.             }
  143.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  144.         }
  145.     }
  146.  
  147.  
  148.  
  149.     public void paint(Graphics g) {
  150.         g.drawImage(background, 0, 0, this);
  151.  
  152.         g.setColor(Color.black);
  153.         g.fillOval(ball_x_pos - radius, ball_y_pos - radius, 2 * radius, 2 * radius);
  154.  
  155.         g.setColor(Color.black);
  156.         g.fillRect(balken_x_pos, mouse_y_pos, balken_width, balken_height);
  157.     }
  158.  
  159.  
  160.  
  161.         public void mouseEntered(MouseEvent arg0) {
  162.                 // TODO Auto-generated method stub
  163.                
  164.                
  165.                
  166.         }
  167.  
  168.  
  169.         public void mouseExited(MouseEvent arg0) {
  170.                 // TODO Auto-generated method stub
  171.                
  172.         }
  173.  
  174.  
  175.         public void mousePressed(MouseEvent arg0) {
  176.                 // TODO Auto-generated method stub
  177.        
  178.         }
  179.  
  180.  
  181.         public void mouseReleased(MouseEvent arg0) {
  182.                 // TODO Auto-generated method stub
  183.                
  184.         }
  185.  
  186.  
  187.         public void mouseDragged(MouseEvent e) {
  188.                 // TODO Auto-generated method stub
  189.                
  190.         }
  191.  
  192.            public void mouseClicked(MouseEvent arg0) {
  193.                         // TODO Auto-generated method stub
  194.                
  195.                 }
  196.  
  197.  
  198.     /*public void update(Graphics g) {
  199.         if (dbImage == null) {
  200.             dbImage = createImage(this.getSize().width, this.getSize().height);
  201.             dbg = dbImage.getGraphics();
  202.         }
  203.         dbg.setColor(getBackground());
  204.         dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  205.  
  206.         dbg.setColor(getForeground());
  207.         paint(dbg);
  208.  
  209.         g.drawImage(dbImage, 0, 0, this);
  210.     }*/
  211. }