Public paste
Undefined
By: Guest | Date: Jul 6 2010 06:31 | Format: None | Expires: never | Size: 4.05 KB | Hits: 757

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.Random;
  4. import java.awt.event.*;
  5.  
  6.  
  7. public class Game extends Applet implements Runnable {
  8.    
  9.         Random rd = new Random();
  10.     int x1_pos = 250;
  11.     int trefferanzahl = 5;
  12.     int y1_pos=125;
  13.     int radius = 20;
  14.     int peter =1;
  15.     int peter2 = -1;
  16.     int balken_y = 0;
  17.     int xclick = 0;
  18.     int yclick = 0;
  19.     int appletsize_x = 500;
  20.     int appletsize_y = 250;
  21.     int ball_x_speed = 0;
  22.     int ball_y_speed = 0;
  23.     Image background;
  24.     private Image dbImage;
  25.     private Graphics dbg;
  26.     Button start;
  27.     Button Pause;
  28.  
  29.  
  30.     public void init() {
  31.         this.setLayout(null);
  32.         background = getImage(getCodeBase(), "bg.jpg");
  33.         start = new Button("Spiel starten");
  34.         Pause = new Button("Pause");
  35.         start.setBounds(250,125,80,50);
  36.         add(start);
  37.         add(Pause);
  38.        
  39.         }
  40.  
  41.  
  42.  
  43.     public boolean action(Event e, Object args){
  44.         if(e.target == start ) {
  45.                 ball_x_speed = 1+rd.nextInt(trefferanzahl);
  46.                 ball_y_speed = 1+rd.nextInt(trefferanzahl);
  47.                 start.setVisible(false);
  48.         }
  49.        
  50.                 return false;
  51.        
  52.     }
  53.  
  54.     public void start() {
  55.        
  56.         Thread th = new Thread(this);
  57.      
  58.         th.start();
  59.     }
  60.  
  61.     public void stop() {
  62.     }
  63.  
  64.     public void destroy() {
  65.     }
  66.     public boolean mouseMove (Event e, int x, int y)
  67.         {
  68.                
  69.                
  70.                 //System.out.println(""+x+" "+y);
  71.                 xclick = x;
  72.                 yclick = y;
  73.                 return true;
  74.         }
  75.    
  76.  
  77.  
  78.  
  79.     public void run() {
  80.        
  81.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  82.  
  83.        
  84.         while (true) {
  85.                
  86.    
  87.                
  88.                
  89.        
  90.                
  91.                    if(yclick+30 >= y1_pos && yclick-30 <= y1_pos && x1_pos >= 30 - radius && x1_pos <= 30 + radius){
  92.                 System.out.println("Ball getroffen!");
  93.                 trefferanzahl++;
  94.                 if (trefferanzahl >=10){ trefferanzahl = 5; }
  95.                
  96.                 ball_y_speed =1+rd.nextInt(trefferanzahl);
  97.                 ball_x_speed = 1+rd.nextInt(trefferanzahl);
  98.                    }
  99.                    
  100.                    if (x1_pos > appletsize_x - radius) {
  101.                    ball_x_speed = ball_x_speed* -1;
  102.                  
  103.                    }
  104.                    if (y1_pos > appletsize_y - radius) {
  105.                    ball_y_speed = ball_y_speed* -1;
  106.                  
  107.                    }
  108.                    if (x1_pos < radius){
  109.                            x1_pos = 250;
  110.                            y1_pos = 125;
  111.                            
  112.                    }
  113.                    
  114.                    
  115.                    if (y1_pos <radius){
  116.                            ball_y_speed = ball_y_speed* -1; }
  117.                    
  118.                         x1_pos = x1_pos + ball_x_speed;
  119.                         y1_pos = y1_pos + ball_y_speed;
  120.                
  121.        
  122.            
  123.             if (x1_pos <= radius){
  124.                 x1_pos = radius +2;
  125.                 ball_x_speed = ball_x_speed * -1;
  126.             }
  127.             if (y1_pos <= radius){
  128.                 y1_pos = radius +2;
  129.                 ball_y_speed = ball_y_speed * -1;
  130.             }
  131.                
  132.                        
  133.              
  134.            
  135.      
  136.             repaint();
  137.  
  138.             try {
  139.              
  140.                 Thread.sleep(20);
  141.             } catch (InterruptedException ex) {
  142.                
  143.             }
  144.  
  145.          
  146.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  147.         }
  148.     }
  149.        
  150.     public void paint (Graphics g)
  151.     {
  152.         g.drawImage(background,0,0,this);
  153.  
  154.  
  155.     g.setColor (Color.black);
  156.     g.fillOval(x1_pos - radius, y1_pos - radius, 2 * radius, 2 * radius);
  157.    
  158.     g.setColor(Color.black);
  159.     g.fillRect(30,yclick,20,60);
  160.     }
  161.  
  162.     public void update(Graphics g) {
  163.         if (dbImage == null) {
  164.             dbImage = createImage(this.getSize().width, this.getSize().height);
  165.             dbg = dbImage.getGraphics();
  166.         }
  167.         dbg.setColor(getBackground());
  168.         dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  169.  
  170.         dbg.setColor(getForeground());
  171.         paint(dbg);
  172.  
  173.         g.drawImage(dbImage, 0, 0, this);
  174.     }
  175.        
  176.  
  177.  
  178. }